From 3efe2c942c5408ec4632cf3563ec0482b1514ca2 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Thu, 2 Feb 2017 17:37:03 -0800 Subject: [PATCH 1/3] provider/google: only set additional zones on read if it had been set in the config --- builtin/providers/google/resource_container_cluster.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index 19ab48a9c..07e679c1a 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -444,7 +444,10 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro d.Set("name", cluster.Name) d.Set("zone", cluster.Zone) - d.Set("additional_zones", cluster.Locations) + + if _, ok := d.GetOk("additional_zones"); ok { + d.Set("additional_zones", cluster.Locations) + } d.Set("endpoint", cluster.Endpoint) masterAuth := []map[string]interface{}{ From 260e8038804e002ef3e2849610f670992ec4ce3a Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Mon, 6 Feb 2017 17:21:34 -0800 Subject: [PATCH 2/3] providers/google: disallow specifying the original zone in additional_zones, change field to computed --- .../google/resource_container_cluster.go | 20 +++++++++++-------- .../google/resource_container_cluster_test.go | 16 ++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index 07e679c1a..a61149d09 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -95,6 +95,7 @@ func resourceContainerCluster() *schema.Resource { "additional_zones": &schema.Schema{ Type: schema.TypeList, Optional: true, + Computed: true, ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, }, @@ -292,18 +293,14 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er if v, ok := d.GetOk("additional_zones"); ok { locationsList := v.([]interface{}) locations := []string{} - zoneInLocations := false for _, v := range locationsList { location := v.(string) locations = append(locations, location) if location == zoneName { - zoneInLocations = true + return fmt.Errorf("additional_zones should not contain the original 'zone'.") } } - if !zoneInLocations { - // zone must be in locations if specified separately - locations = append(locations, zoneName) - } + locations = append(locations, zoneName) cluster.Locations = locations } @@ -445,9 +442,16 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro d.Set("name", cluster.Name) d.Set("zone", cluster.Zone) - if _, ok := d.GetOk("additional_zones"); ok { - d.Set("additional_zones", cluster.Locations) + if len(cluster.Locations) > 1 { + locations := []string{} + for _, location := range cluster.Locations { + if location != cluster.Zone { + locations = append(locations, location) + } + } + d.Set("additional_zones", locations) } + d.Set("endpoint", cluster.Endpoint) masterAuth := []map[string]interface{}{ diff --git a/builtin/providers/google/resource_container_cluster_test.go b/builtin/providers/google/resource_container_cluster_test.go index 364de87e9..6359ab426 100644 --- a/builtin/providers/google/resource_container_cluster_test.go +++ b/builtin/providers/google/resource_container_cluster_test.go @@ -39,7 +39,7 @@ func TestAccContainerCluster_withAdditionalZones(t *testing.T) { testAccCheckContainerClusterExists( "google_container_cluster.with_additional_zones"), testAccCheckContainerClusterAdditionalZonesExist( - "google_container_cluster.with_additional_zones"), + "google_container_cluster.with_additional_zones", 2), ), }, }, @@ -163,23 +163,19 @@ func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc { } } -func testAccCheckContainerClusterAdditionalZonesExist(n string) resource.TestCheckFunc { +func testAccCheckContainerClusterAdditionalZonesExist(n string, num int) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Not found: %s", n) } - var ( - additionalZonesSize int - err error - ) - - if additionalZonesSize, err = strconv.Atoi(rs.Primary.Attributes["additional_zones.#"]); err != nil { + additionalZonesSize, err := strconv.Atoi(rs.Primary.Attributes["additional_zones.#"]) + if err != nil { return err } - if additionalZonesSize != 2 { - return fmt.Errorf("number of additional zones did not match 2") + if additionalZonesSize != num { + return fmt.Errorf("number of additional zones did not match %d, was %d", num, additionalZonesSize) } return nil From a3ca05a3c9bba9cc949b192b5233fc361b448c18 Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Tue, 7 Feb 2017 19:21:00 -0800 Subject: [PATCH 3/3] provider/google: always set additional_zones on read --- builtin/providers/google/resource_container_cluster.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index a61149d09..fd9aa43a9 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -442,15 +442,15 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro d.Set("name", cluster.Name) d.Set("zone", cluster.Zone) + locations := []string{} if len(cluster.Locations) > 1 { - locations := []string{} for _, location := range cluster.Locations { if location != cluster.Zone { locations = append(locations, location) } } - d.Set("additional_zones", locations) } + d.Set("additional_zones", locations) d.Set("endpoint", cluster.Endpoint)