From a7d3c437e48094dd5965e3415b55cc0c9268b225 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 30 Apr 2019 14:46:58 -0700 Subject: [PATCH] Make Subnetwork secondary_ranges Computed again, apply fixup, fix patch (#636) Signed-off-by: Modular Magician --- google-beta/resource_compute_subnetwork.go | 10 ++-- .../resource_compute_subnetwork_test.go | 57 +++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/google-beta/resource_compute_subnetwork.go b/google-beta/resource_compute_subnetwork.go index aed6af5a9e2..1b1dc1b5746 100644 --- a/google-beta/resource_compute_subnetwork.go +++ b/google-beta/resource_compute_subnetwork.go @@ -148,8 +148,10 @@ func resourceComputeSubnetwork() *schema.Resource { DiffSuppressFunc: compareSelfLinkOrResourceName, }, "secondary_ip_range": { - Type: schema.TypeList, - Optional: true, + Type: schema.TypeList, + Computed: true, + Optional: true, + ConfigMode: schema.SchemaConfigModeAttr, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "ip_cidr_range": { @@ -276,7 +278,7 @@ func resourceComputeSubnetworkCreate(d *schema.ResourceData, meta interface{}) e secondaryIpRangesProp, err := expandComputeSubnetworkSecondaryIpRange(d.Get("secondary_ip_range"), d, config) if err != nil { return err - } else if v, ok := d.GetOkExists("secondary_ip_range"); !isEmptyValue(reflect.ValueOf(secondaryIpRangesProp)) && (ok || !reflect.DeepEqual(v, secondaryIpRangesProp)) { + } else if v, ok := d.GetOkExists("secondary_ip_range"); ok || !reflect.DeepEqual(v, secondaryIpRangesProp) { obj["secondaryIpRanges"] = secondaryIpRangesProp } privateIpGoogleAccessProp, err := expandComputeSubnetworkPrivateIpGoogleAccess(d.Get("private_ip_google_access"), d, config) @@ -465,7 +467,7 @@ func resourceComputeSubnetworkUpdate(d *schema.ResourceData, meta interface{}) e secondaryIpRangesProp, err := expandComputeSubnetworkSecondaryIpRange(d.Get("secondary_ip_range"), d, config) if err != nil { return err - } else if v, ok := d.GetOkExists("secondary_ip_range"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, secondaryIpRangesProp)) { + } else if v, ok := d.GetOkExists("secondary_ip_range"); ok || !reflect.DeepEqual(v, secondaryIpRangesProp) { obj["secondaryIpRanges"] = secondaryIpRangesProp } diff --git a/google-beta/resource_compute_subnetwork_test.go b/google-beta/resource_compute_subnetwork_test.go index f97decbb13b..c8220a92ba0 100644 --- a/google-beta/resource_compute_subnetwork_test.go +++ b/google-beta/resource_compute_subnetwork_test.go @@ -159,6 +159,22 @@ func TestAccComputeSubnetwork_secondaryIpRanges(t *testing.T) { testAccCheckComputeSubnetworkHasSecondaryIpRange(&subnetwork, "tf-test-secondary-range-update2", "192.168.11.0/24"), ), }, + { + Config: testAccComputeSubnetwork_secondaryIpRanges_update3(cnName, subnetworkName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeSubnetworkExists("google_compute_subnetwork.network-with-private-secondary-ip-ranges", &subnetwork), + testAccCheckComputeSubnetworkHasSecondaryIpRange(&subnetwork, "tf-test-secondary-range-update1", "192.168.10.0/24"), + testAccCheckComputeSubnetworkHasSecondaryIpRange(&subnetwork, "tf-test-secondary-range-update2", "192.168.11.0/24"), + ), + }, + { + Config: testAccComputeSubnetwork_secondaryIpRanges_update4(cnName, subnetworkName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeSubnetworkExists("google_compute_subnetwork.network-with-private-secondary-ip-ranges", &subnetwork), + testAccCheckComputeSubnetworkHasNotSecondaryIpRange(&subnetwork, "tf-test-secondary-range-update1", "192.168.10.0/24"), + testAccCheckComputeSubnetworkHasNotSecondaryIpRange(&subnetwork, "tf-test-secondary-range-update2", "192.168.11.0/24"), + ), + }, { Config: testAccComputeSubnetwork_secondaryIpRanges_update1(cnName, subnetworkName), Check: resource.ComposeTestCheckFunc( @@ -384,6 +400,47 @@ resource "google_compute_subnetwork" "network-with-private-secondary-ip-ranges" `, cnName, subnetworkName) } +func testAccComputeSubnetwork_secondaryIpRanges_update3(cnName, subnetworkName string) string { + return fmt.Sprintf(` +resource "google_compute_network" "custom-test" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "network-with-private-secondary-ip-ranges" { + name = "%s" + ip_cidr_range = "10.2.0.0/16" + region = "us-central1" + network = "${google_compute_network.custom-test.self_link}" + secondary_ip_range { + range_name = "tf-test-secondary-range-update2" + ip_cidr_range = "192.168.11.0/24" + } + secondary_ip_range { + range_name = "tf-test-secondary-range-update1" + ip_cidr_range = "192.168.10.0/24" + } +} +`, cnName, subnetworkName) +} + +func testAccComputeSubnetwork_secondaryIpRanges_update4(cnName, subnetworkName string) string { + return fmt.Sprintf(` +resource "google_compute_network" "custom-test" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "network-with-private-secondary-ip-ranges" { + name = "%s" + ip_cidr_range = "10.2.0.0/16" + region = "us-central1" + network = "${google_compute_network.custom-test.self_link}" + secondary_ip_range = [] +} +`, cnName, subnetworkName) +} + func testAccComputeSubnetwork_flowLogs(cnName, subnetworkName string, enableLogs bool) string { return fmt.Sprintf(` resource "google_compute_network" "custom-test" {