From fc5e5a8264f33d3b20a17280d0c8e923e0dcbb5d Mon Sep 17 00:00:00 2001 From: Paul Tyng Date: Sun, 21 Nov 2021 11:08:06 -0500 Subject: [PATCH] Validate that certain WAN attributes aren't set --- internal/provider/resource_network.go | 22 +++++++-- internal/provider/resource_network_test.go | 54 ++++++++++++++++++++++ 2 files changed, 71 insertions(+), 5 deletions(-) diff --git a/internal/provider/resource_network.go b/internal/provider/resource_network.go index 9b1948cad..03c8c7bf2 100644 --- a/internal/provider/resource_network.go +++ b/internal/provider/resource_network.go @@ -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), @@ -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), @@ -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{ diff --git a/internal/provider/resource_network_test.go b/internal/provider/resource_network_test.go index 4c9779ca4..794e049ac 100644 --- a/internal/provider/resource_network_test.go +++ b/internal/provider/resource_network_test.go @@ -342,6 +342,44 @@ 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, `""`), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("unifi_network.test", "wan_gateway", ""), + ), + }, + { + ResourceName: "unifi_network.test", + ImportState: true, + ImportStateIdFunc: siteAndIDImportStateIDFunc("unifi_network.test"), + ImportStateVerify: true, + }, + }, + }) +} + // TODO: ipv6 prefix delegation test func quoteStrings(src []string) []string { @@ -541,3 +579,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) +}