@@ -54,6 +54,7 @@ resource "unifi_network" "wan" {
|
||||
- **dhcp_dns** (List of String) Specifies the IPv4 addresses for the DNS server to be returned from the DHCP server. Leave blank to disable this feature.
|
||||
- **dhcp_enabled** (Boolean) Specifies whether DHCP is enabled or not on this network.
|
||||
- **dhcp_lease** (Number) Specifies the lease time for DHCP addresses. Defaults to `86400`.
|
||||
- **dhcp_relay_enabled** (Boolean) Specifies whether DHCP relay is enabled or not on this network.
|
||||
- **dhcp_start** (String) The IPv4 address where the DHCP range of addresses starts.
|
||||
- **dhcp_stop** (String) The IPv4 address where the DHCP range of addresses stops.
|
||||
- **dhcpd_boot_enabled** (Boolean) Toggles on the DHCP boot options. Should be set to true when you want to have dhcpd_boot_filename, and dhcpd_boot_server to take effect.
|
||||
|
||||
@@ -3,10 +3,10 @@ package provider
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
|
||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
|
||||
"github.com/paultyng/go-unifi/unifi"
|
||||
@@ -135,6 +135,11 @@ func resourceNetwork() *schema.Resource {
|
||||
Type: schema.TypeString,
|
||||
Optional: true,
|
||||
},
|
||||
"dhcp_relay_enabled": {
|
||||
Description: "Specifies whether DHCP relay is enabled or not on this network.",
|
||||
Type: schema.TypeBool,
|
||||
Optional: true,
|
||||
},
|
||||
"domain_name": {
|
||||
Description: "The domain name of this network.",
|
||||
Type: schema.TypeString,
|
||||
@@ -236,7 +241,7 @@ func resourceNetwork() *schema.Resource {
|
||||
func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*client)
|
||||
|
||||
req, err := resourceNetworkGetResourceData(d)
|
||||
req, err := resourceNetworkGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
@@ -256,7 +261,9 @@ func resourceNetworkCreate(ctx context.Context, d *schema.ResourceData, meta int
|
||||
return resourceNetworkSetResourceData(resp, d, site)
|
||||
}
|
||||
|
||||
func resourceNetworkGetResourceData(d *schema.ResourceData) (*unifi.Network, error) {
|
||||
func resourceNetworkGetResourceData(d *schema.ResourceData, meta interface{}) (*unifi.Network, error) {
|
||||
// c := meta.(*client)
|
||||
|
||||
vlan := d.Get("vlan_id").(int)
|
||||
dhcpDNS, err := listToStringSlice(d.Get("dhcp_dns").([]interface{}))
|
||||
if err != nil {
|
||||
@@ -280,6 +287,7 @@ func resourceNetworkGetResourceData(d *schema.ResourceData) (*unifi.Network, err
|
||||
DHCPDBootEnabled: d.Get("dhcpd_boot_enabled").(bool),
|
||||
DHCPDBootServer: d.Get("dhcpd_boot_server").(string),
|
||||
DHCPDBootFilename: d.Get("dhcpd_boot_filename").(string),
|
||||
DHCPRelayEnabled: d.Get("dhcp_relay_enabled").(bool),
|
||||
DomainName: d.Get("domain_name").(string),
|
||||
IGMPSnooping: d.Get("igmp_snooping").(bool),
|
||||
|
||||
@@ -386,6 +394,7 @@ func resourceNetworkSetResourceData(resp *unifi.Network, d *schema.ResourceData,
|
||||
d.Set("dhcpd_boot_enabled", resp.DHCPDBootEnabled)
|
||||
d.Set("dhcpd_boot_server", resp.DHCPDBootServer)
|
||||
d.Set("dhcpd_boot_filename", resp.DHCPDBootFilename)
|
||||
d.Set("dhcp_relay_enabled", resp.DHCPRelayEnabled)
|
||||
d.Set("domain_name", resp.DomainName)
|
||||
d.Set("igmp_snooping", resp.IGMPSnooping)
|
||||
d.Set("dhcp_dns", dhcpDNS)
|
||||
@@ -432,7 +441,7 @@ func resourceNetworkRead(ctx context.Context, d *schema.ResourceData, meta inter
|
||||
func resourceNetworkUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
|
||||
c := meta.(*client)
|
||||
|
||||
req, err := resourceNetworkGetResourceData(d)
|
||||
req, err := resourceNetworkGetResourceData(d, meta)
|
||||
if err != nil {
|
||||
return diag.FromErr(err)
|
||||
}
|
||||
|
||||
@@ -287,6 +287,36 @@ func TestAccNetwork_importByName(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAccNetwork_dhcpRelay(t *testing.T) {
|
||||
name := acctest.RandomWithPrefix("tfacc")
|
||||
vlanID := getTestVLAN(t)
|
||||
|
||||
resource.ParallelTest(t, resource.TestCase{
|
||||
PreCheck: func() {
|
||||
preCheck(t)
|
||||
preCheckV6Only(t)
|
||||
},
|
||||
ProviderFactories: providerFactories,
|
||||
// TODO: CheckDestroy: ,
|
||||
Steps: []resource.TestStep{
|
||||
{
|
||||
Config: testAccNetworkConfigDHCPRelay(name, vlanID, true),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_relay_enabled", "true"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
{
|
||||
Config: testAccNetworkConfigDHCPRelay(name, vlanID, false),
|
||||
Check: resource.ComposeTestCheckFunc(
|
||||
resource.TestCheckResourceAttr("unifi_network.test", "dhcp_relay_enabled", "false"),
|
||||
),
|
||||
},
|
||||
importStep("unifi_network.test"),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// TODO: ipv6 prefix delegation test
|
||||
|
||||
func quoteStrings(src []string) []string {
|
||||
@@ -451,3 +481,23 @@ resource "unifi_network" "test2" {
|
||||
}
|
||||
`, vlan1, vlan2, networkName)
|
||||
}
|
||||
|
||||
func testAccNetworkConfigDHCPRelay(name string, vlan int, dhcpRelay bool) string {
|
||||
return fmt.Sprintf(`
|
||||
locals {
|
||||
subnet = cidrsubnet("10.0.0.0/8", 6, %[2]d)
|
||||
vlan_id = %[2]d
|
||||
}
|
||||
|
||||
resource "unifi_network" "test" {
|
||||
name = "%[1]s"
|
||||
purpose = "corporate"
|
||||
|
||||
subnet = local.subnet
|
||||
vlan_id = local.vlan_id
|
||||
domain_name = "foo.local"
|
||||
|
||||
dhcp_relay_enabled = %[3]t
|
||||
}
|
||||
`, name, vlan, dhcpRelay)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user