diff --git a/google/resource_container_cluster_test.go b/google/resource_container_cluster_test.go index ecde6dbc..79fc0756 100644 --- a/google/resource_container_cluster_test.go +++ b/google/resource_container_cluster_test.go @@ -3,6 +3,7 @@ package google import ( "bytes" "fmt" + "regexp" "testing" "strconv" @@ -933,6 +934,28 @@ func TestAccContainerCluster_withNodePoolAutoscaling(t *testing.T) { }) } +func TestAccContainerCluster_withNodePoolNamePrefix(t *testing.T) { + t.Parallel() + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerClusterDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContainerCluster_withNodePoolNamePrefix(), + }, + { + ResourceName: "google_container_cluster.with_node_pool_name_prefix", + ImportStateIdPrefix: "us-central1-a/", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"node_pool.0.name_prefix"}, + }, + }, + }) +} + func TestAccContainerCluster_withNodePoolMultiple(t *testing.T) { t.Parallel() @@ -954,6 +977,22 @@ func TestAccContainerCluster_withNodePoolMultiple(t *testing.T) { }) } +func TestAccContainerCluster_withNodePoolConflictingNameFields(t *testing.T) { + t.Parallel() + + 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 TestAccContainerCluster_withNodePoolNodeConfig(t *testing.T) { t.Parallel() @@ -1966,6 +2005,19 @@ resource "google_container_cluster" "with_node_pool" { }`, cluster, np) } +func testAccContainerCluster_withNodePoolNamePrefix() string { + return fmt.Sprintf(` +resource "google_container_cluster" "with_node_pool_name_prefix" { + name = "tf-cluster-nodepool-test-%s" + zone = "us-central1-a" + + node_pool { + name_prefix = "tf-np-test" + node_count = 2 + } +}`, acctest.RandString(10)) +} + func testAccContainerCluster_withNodePoolMultiple() string { return fmt.Sprintf(` resource "google_container_cluster" "with_node_pool_multiple" { @@ -1984,6 +2036,21 @@ resource "google_container_cluster" "with_node_pool_multiple" { }`, acctest.RandString(10), acctest.RandString(10), acctest.RandString(10)) } +func testAccContainerCluster_withNodePoolConflictingNameFields() string { + return fmt.Sprintf(` +resource "google_container_cluster" "with_node_pool_multiple" { + name = "tf-cluster-nodepool-test-%s" + zone = "us-central1-a" + + node_pool { + # ERROR: name and name_prefix cannot be both specified + name = "tf-cluster-nodepool-test-%s" + name_prefix = "tf-cluster-nodepool-test-" + node_count = 1 + } +}`, acctest.RandString(10), acctest.RandString(10)) +} + func testAccContainerCluster_withNodePoolNodeConfig() string { testId := acctest.RandString(10) return fmt.Sprintf(` diff --git a/google/resource_container_node_pool.go b/google/resource_container_node_pool.go index 3886ced7..2c4e83e5 100644 --- a/google/resource_container_node_pool.go +++ b/google/resource_container_node_pool.go @@ -139,8 +139,6 @@ var schemaNodePool = map[string]*schema.Schema{ Optional: true, Computed: true, ForceNew: true, - Removed: "Use the random provider instead. See migration instructions at " + - "https://github.com/terraform-providers/terraform-provider-google/issues/1054#issuecomment-377390209", }, "node_config": schemaNodeConfig, @@ -441,7 +439,12 @@ func resourceContainerNodePoolStateImporter(d *schema.ResourceData, meta interfa func expandNodePool(d *schema.ResourceData, prefix string) (*containerBeta.NodePool, error) { var name string if v, ok := d.GetOk(prefix + "name"); ok { + if _, ok := d.GetOk(prefix + "name_prefix"); ok { + return nil, fmt.Errorf("Cannot specify both name and name_prefix for a node_pool") + } name = v.(string) + } else if v, ok := d.GetOk(prefix + "name_prefix"); ok { + name = resource.PrefixedUniqueId(v.(string)) } else { name = resource.UniqueId() } @@ -509,6 +512,7 @@ func flattenNodePool(d *schema.ResourceData, config *Config, np *containerBeta.N } nodePool := map[string]interface{}{ "name": np.Name, + "name_prefix": d.Get(prefix + "name_prefix"), "initial_node_count": np.InitialNodeCount, "node_count": size / len(np.InstanceGroupUrls), "node_config": flattenNodeConfig(np.Config), @@ -728,5 +732,6 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, nodePoolInfo *Node } func getNodePoolName(id string) string { + // name can be specified with name, name_prefix, or neither, so read it from the id. return strings.Split(id, "/")[2] } diff --git a/google/resource_container_node_pool_test.go b/google/resource_container_node_pool_test.go index 4a258f1c..a5c49f26 100644 --- a/google/resource_container_node_pool_test.go +++ b/google/resource_container_node_pool_test.go @@ -32,6 +32,29 @@ func TestAccContainerNodePool_basic(t *testing.T) { }) } +func TestAccContainerNodePool_namePrefix(t *testing.T) { + t.Parallel() + + cluster := fmt.Sprintf("tf-nodepool-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckContainerNodePoolDestroy, + Steps: []resource.TestStep{ + { + Config: testAccContainerNodePool_namePrefix(cluster, "tf-np-"), + }, + { + ResourceName: "google_container_node_pool.np", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name_prefix"}, + }, + }, + }) +} + func TestAccContainerNodePool_noName(t *testing.T) { t.Parallel() @@ -457,6 +480,21 @@ resource "google_container_node_pool" "np" { }`, cluster, np) } +func testAccContainerNodePool_namePrefix(cluster, np string) string { + return fmt.Sprintf(` +resource "google_container_cluster" "cluster" { + name = "%s" + zone = "us-central1-a" + initial_node_count = 3 +} +resource "google_container_node_pool" "np" { + name_prefix = "%s" + zone = "us-central1-a" + cluster = "${google_container_cluster.cluster.name}" + initial_node_count = 2 +}`, cluster, np) +} + func testAccContainerNodePool_noName(cluster string) string { return fmt.Sprintf(` resource "google_container_cluster" "cluster" {