Merge pull request #11018 from unguiculus/feature/additional_zones

provider/google: [GKE] Allow additional zones to be configured
This commit is contained in:
Dana Hoffman 2017-01-18 10:10:13 -08:00 committed by GitHub
commit 00fad24a96
2 changed files with 86 additions and 0 deletions

View File

@ -92,6 +92,13 @@ func resourceContainerCluster() *schema.Resource {
ForceNew: true,
},
"additional_zones": &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"cluster_ipv4_cidr": &schema.Schema{
Type: schema.TypeString,
Optional: true,
@ -282,6 +289,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 +444,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{}{

View File

@ -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) {
@ -26,6 +27,25 @@ 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"),
testAccCheckContainerClusterAdditionalZonesExist(
"google_container_cluster.with_additional_zones"),
),
},
},
})
}
func TestAccContainerCluster_withVersion(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
@ -143,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"
@ -155,6 +198,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"