Skip to content

Commit

Permalink
Fix crash when creating node pools with name_prefix (hashicorp#531)
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow authored and Nic Cope committed Oct 16, 2017
1 parent 24c6f89 commit e86d17f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
11 changes: 8 additions & 3 deletions google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ func resourceContainerNodePoolRead(d *schema.ResourceData, meta interface{}) err
}

zone := d.Get("zone").(string)
name := d.Get("name").(string)
cluster := d.Get("cluster").(string)
name := getNodePoolName(d.Id())

nodePool, err := config.clientContainer.Projects.Zones.Clusters.NodePools.Get(
project, zone, cluster, name).Do()
Expand Down Expand Up @@ -233,13 +233,13 @@ func resourceContainerNodePoolExists(d *schema.ResourceData, meta interface{}) (
}

zone := d.Get("zone").(string)
name := d.Get("name").(string)
cluster := d.Get("cluster").(string)
name := getNodePoolName(d.Id())

_, err = config.clientContainer.Projects.Zones.Clusters.NodePools.Get(
project, zone, cluster, name).Do()
if err != nil {
if err = handleNotFoundError(err, d, fmt.Sprintf("Container NodePool %s", d.Get("name").(string))); err == nil {
if err = handleNotFoundError(err, d, fmt.Sprintf("Container NodePool %s", name)); err == nil {
return false, nil
}
// There was some other error in reading the resource
Expand Down Expand Up @@ -417,3 +417,8 @@ func nodePoolUpdate(d *schema.ResourceData, meta interface{}, clusterName, prefi

return nil
}

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]
}
67 changes: 67 additions & 0 deletions google/resource_container_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@ func TestAccContainerNodePool_basic(t *testing.T) {
})
}

func TestAccContainerNodePool_namePrefix(t *testing.T) {
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{
resource.TestStep{
Config: testAccContainerNodePool_namePrefix(cluster, "tf-np-"),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerNodePoolMatches("google_container_node_pool.np"),
),
},
},
})
}

func TestAccContainerNodePool_noName(t *testing.T) {
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{
resource.TestStep{
Config: testAccContainerNodePool_noName(cluster),
Check: resource.ComposeTestCheckFunc(
testAccCheckContainerNodePoolMatches("google_container_node_pool.np"),
),
},
},
})
}

func TestAccContainerNodePool_withNodeConfig(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -238,6 +274,37 @@ 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" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 3
}
resource "google_container_node_pool" "np" {
zone = "us-central1-a"
cluster = "${google_container_cluster.cluster.name}"
initial_node_count = 2
}`, cluster)
}

func testAccContainerNodePool_autoscaling(cluster, np string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "cluster" {
Expand Down

0 comments on commit e86d17f

Please sign in to comment.