From 07ce12d0f745c775aaefe0bdcb18465dc1a2b37a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Wed, 4 Jan 2017 07:14:39 +0100 Subject: [PATCH 1/4] [GKE] Allow additional zones to be configured --- resource_container_cluster.go | 25 ++++++++++++++++++++++ resource_container_cluster_test.go | 34 ++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/resource_container_cluster.go b/resource_container_cluster.go index 9340d78e..22fd5a40 100644 --- a/resource_container_cluster.go +++ b/resource_container_cluster.go @@ -92,6 +92,12 @@ func resourceContainerCluster() *schema.Resource { ForceNew: true, }, + "additional_zones": &schema.Schema{ + Type: schema.TypeList, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "cluster_ipv4_cidr": &schema.Schema{ Type: schema.TypeString, Optional: true, @@ -282,6 +288,24 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er cluster.InitialClusterVersion = v.(string) } + 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 + } + } + if !zoneInLocations { + // zone must be in locations if specified separately + locations = append(locations, zoneName) + } + cluster.Locations = locations + } + if v, ok := d.GetOk("cluster_ipv4_cidr"); ok { cluster.ClusterIpv4Cidr = v.(string) } @@ -419,6 +443,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro d.Set("name", cluster.Name) d.Set("zone", cluster.Zone) + d.Set("additional_zones", cluster.Locations) d.Set("endpoint", cluster.Endpoint) masterAuth := []map[string]interface{}{ diff --git a/resource_container_cluster_test.go b/resource_container_cluster_test.go index d0dbb48e..3cef09e4 100644 --- a/resource_container_cluster_test.go +++ b/resource_container_cluster_test.go @@ -26,6 +26,23 @@ func TestAccContainerCluster_basic(t *testing.T) { }) } +func TestAccContainerCluster_withAdditionalZones(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerClusterDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccContainerCluster_withAdditionalZones, + Check: resource.ComposeTestCheckFunc( + testAccCheckContainerClusterExists( + "google_container_cluster.with_additional_zones"), + ), + }, + }, + }) +} + func TestAccContainerCluster_withVersion(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -155,6 +172,23 @@ resource "google_container_cluster" "primary" { } }`, acctest.RandString(10)) +var testAccContainerCluster_withAdditionalZones = fmt.Sprintf(` +resource "google_container_cluster" "with_additional_zones" { + name = "cluster-test-%s" + zone = "us-central1-a" + initial_node_count = 1 + + additional_zones = [ + "us-central1-b", + "us-central1-c" + ] + + master_auth { + username = "mr.yoda" + password = "adoy.rm" + } +}`, acctest.RandString(10)) + var testAccContainerCluster_withVersion = fmt.Sprintf(` resource "google_container_cluster" "with_version" { name = "cluster-test-%s" From bf8e00e162c2df62bf83397683bd1e1e6fbe63ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Fri, 13 Jan 2017 17:03:28 +0100 Subject: [PATCH 2/4] Add test for additional zones existance --- resource_container_cluster_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/resource_container_cluster_test.go b/resource_container_cluster_test.go index 3cef09e4..391bd269 100644 --- a/resource_container_cluster_test.go +++ b/resource_container_cluster_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" + "strconv" ) func TestAccContainerCluster_basic(t *testing.T) { @@ -37,6 +38,8 @@ func TestAccContainerCluster_withAdditionalZones(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccCheckContainerClusterExists( "google_container_cluster.with_additional_zones"), + testAccCheckContainerClusterAdditionalZonesExist( + "google_container_cluster.with_additional_zones"), ), }, }, @@ -160,6 +163,29 @@ func testAccCheckContainerClusterExists(n string) resource.TestCheckFunc { } } +func testAccCheckContainerClusterAdditionalZonesExist(n string) 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 { + return err + } + if additionalZonesSize < 2 { + return fmt.Errorf("number of additional zones did not match 2") + } + + return nil + } +} + var testAccContainerCluster_basic = fmt.Sprintf(` resource "google_container_cluster" "primary" { name = "cluster-test-%s" From 6d3f0e5616fae2ca039e25420d0f221bb978e5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Fri, 13 Jan 2017 19:37:59 +0100 Subject: [PATCH 3/4] Fix if condition in test --- resource_container_cluster_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resource_container_cluster_test.go b/resource_container_cluster_test.go index 391bd269..364de87e 100644 --- a/resource_container_cluster_test.go +++ b/resource_container_cluster_test.go @@ -178,7 +178,7 @@ func testAccCheckContainerClusterAdditionalZonesExist(n string) resource.TestChe if additionalZonesSize, err = strconv.Atoi(rs.Primary.Attributes["additional_zones.#"]); err != nil { return err } - if additionalZonesSize < 2 { + if additionalZonesSize != 2 { return fmt.Errorf("number of additional zones did not match 2") } From d1e6adf928ba3109a9398c343c2cf1aeb657b667 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20N=C3=A4gele?= Date: Fri, 13 Jan 2017 19:39:35 +0100 Subject: [PATCH 4/4] Add 'ForceNew: true' to additional_zones --- resource_container_cluster.go | 1 + 1 file changed, 1 insertion(+) diff --git a/resource_container_cluster.go b/resource_container_cluster.go index 22fd5a40..19ab48a9 100644 --- a/resource_container_cluster.go +++ b/resource_container_cluster.go @@ -95,6 +95,7 @@ func resourceContainerCluster() *schema.Resource { "additional_zones": &schema.Schema{ Type: schema.TypeList, Optional: true, + ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, },