Fail if both name and name_prefix are set for node_pool in google_container_cluster (#296)

This commit is contained in:
Vincent Roseberry 2017-08-04 15:34:02 -07:00 committed by GitHub
parent 1480539c82
commit 018e01887b
2 changed files with 57 additions and 13 deletions

View File

@ -243,11 +243,10 @@ func resourceContainerCluster() *schema.Resource {
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"node_pool.name_prefix"},
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name_prefix": {
@ -431,16 +430,11 @@ func resourceContainerClusterCreate(d *schema.ResourceData, meta interface{}) er
nodePools := make([]*container.NodePool, 0, nodePoolsCount)
for i := 0; i < nodePoolsCount; i++ {
prefix := fmt.Sprintf("node_pool.%d", i)
nodeCount := d.Get(prefix + ".initial_node_count").(int)
var name string
if v, ok := d.GetOk(prefix + ".name"); ok {
name = v.(string)
} else if v, ok := d.GetOk(prefix + ".name_prefix"); ok {
name = resource.PrefixedUniqueId(v.(string))
} else {
name = resource.UniqueId()
name, err := generateNodePoolName(prefix, d)
if err != nil {
return err
}
nodePool := &container.NodePool{
@ -731,3 +725,20 @@ func flattenClusterNodePools(d *schema.ResourceData, c []*container.NodePool) []
return nodePools
}
func generateNodePoolName(prefix string, d *schema.ResourceData) (string, error) {
name, okName := d.GetOk(prefix + ".name")
namePrefix, okPrefix := d.GetOk(prefix + ".name_prefix")
if okName && okPrefix {
return "", fmt.Errorf("Cannot specify both name and name_prefix for a node_pool")
}
if okName {
return name.(string), nil
} else if okPrefix {
return resource.PrefixedUniqueId(namePrefix.(string)), nil
} else {
return resource.UniqueId(), nil
}
}

View File

@ -12,6 +12,7 @@ import (
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"regexp"
)
func TestAccContainerCluster_basic(t *testing.T) {
@ -257,6 +258,20 @@ func TestAccContainerCluster_withNodePoolMultiple(t *testing.T) {
})
}
func TestAccContainerCluster_withNodePoolConflictingNameFields(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withNodePoolConflictingNameFields,
ExpectError: regexp.MustCompile("Cannot specify both name and name_prefix for a node_pool"),
},
},
})
}
func testAccCheckContainerClusterDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -769,3 +784,21 @@ resource "google_container_cluster" "with_node_pool_multiple" {
initial_node_count = 3
}
}`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10))
var testAccContainerCluster_withNodePoolConflictingNameFields = fmt.Sprintf(`
resource "google_container_cluster" "with_node_pool_multiple" {
name = "tf-cluster-nodepool-test-%s"
zone = "us-central1-a"
master_auth {
username = "mr.yoda"
password = "adoy.rm"
}
node_pool {
# ERROR: name and name_prefix cannot be both specified
name = "tf-cluster-nodepool-test-%s"
name_prefix = "tf-cluster-nodepool-test-"
initial_node_count = 1
}
}`, acctest.RandString(10), acctest.RandString(10))