Skip to content

Commit

Permalink
Validate that certain WAN attributes aren't set
Browse files Browse the repository at this point in the history
  • Loading branch information
paultyng committed Nov 22, 2021
1 parent 6959a34 commit 5b61451
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 5 deletions.
22 changes: 17 additions & 5 deletions internal/provider/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,17 @@ func resourceNetworkGetResourceData(d *schema.ResourceData, meta interface{}) (*
if err != nil {
return nil, fmt.Errorf("unable to convert wan_dns to string slice: %w", err)
}
purpose := d.Get("purpose").(string)

wanType := d.Get("wan_type").(string)
wanIP := d.Get("wan_ip").(string)
wanNetmask := d.Get("wan_netmask").(string)
wanGateway := d.Get("wan_gateway").(string)
if purpose != "wan" {
if wanNetmask != "" || wanGateway != "" || wanIP != "" || wanType != "" || len(wanDNS) > 0 {
return nil, fmt.Errorf(`wan_dns, wan_type, wan_ip, wan_netmask, and wan_gateway can only be used when the purpose is "wan"`)
}
}

return &unifi.Network{
Name: d.Get("name").(string),
Expand Down Expand Up @@ -308,10 +319,10 @@ func resourceNetworkGetResourceData(d *schema.ResourceData, meta interface{}) (*
IPV6PDPrefixid: d.Get("ipv6_pd_prefixid").(string),
IPV6RaEnabled: d.Get("ipv6_ra_enable").(bool),

WANIP: d.Get("wan_ip").(string),
WANType: d.Get("wan_type").(string),
WANNetmask: d.Get("wan_netmask").(string),
WANGateway: d.Get("wan_gateway").(string),
WANIP: wanIP,
WANType: wanType,
WANNetmask: wanNetmask,
WANGateway: wanGateway,
WANNetworkGroup: d.Get("wan_networkgroup").(string),
WANEgressQOS: d.Get("wan_egress_qos").(int),
WANUsername: d.Get("wan_username").(string),
Expand All @@ -332,7 +343,8 @@ func resourceNetworkSetResourceData(resp *unifi.Network, d *schema.ResourceData,
wanNetmask := ""
wanGateway := ""

if resp.Purpose == "wan" {
switch resp.Purpose {
case "wan":
wanType = resp.WANType

for _, dns := range []string{
Expand Down
46 changes: 46 additions & 0 deletions internal/provider/resource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,36 @@ func TestAccNetwork_vlanOnly(t *testing.T) {
})
}

func TestAccNetwork_wanGateway(t *testing.T) {
name := acctest.RandomWithPrefix("tfacc")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
preCheck(t)
},
ProviderFactories: providerFactories,
// TODO: CheckDestroy: ,
Steps: []resource.TestStep{
{
Config: testAccNetworkWanGateway(name, "null"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("unifi_network.test", "wan_gateway", ""),
),
},
{
ResourceName: "unifi_network.test",
ImportState: true,
ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"),
ImportStateVerify: true,
},
{
Config: testAccNetworkWanGateway(name, `""`),
ExpectError: regexp.MustCompile("expected wan_gateway to contain a valid IPv4 address"),
},
},
})
}

// TODO: ipv6 prefix delegation test

func quoteStrings(src []string) []string {
Expand Down Expand Up @@ -541,3 +571,19 @@ resource "unifi_network" "test" {
}
`, name)
}

func testAccNetworkWanGateway(site string, wanGatewayHCL string) string {
return fmt.Sprintf(`
resource "unifi_site" "test" {
description = "%[1]s"
}
resource "unifi_network" "test" {
site = unifi_site.test.name
name = "test"
purpose = "vlan-only"
vlan_id = 107
wan_gateway = %[2]s
}
`, site, wanGatewayHCL)
}

0 comments on commit 5b61451

Please sign in to comment.