Fix crash when updating container cluster resource labels.

We had a bad type coercion in the code to update container cluster
resource labels. This fixes the coercion.

We didn't notice this because there was no test exercising the update
code path. I've added the test, which reproduced the panic before this
PR was applied and passes successfully now that the coercion is fixed.
This commit is contained in:
Paddy Carver 2018-07-13 02:27:40 -07:00
parent 687f8d0857
commit 0435b1f387
2 changed files with 41 additions and 1 deletions

View File

@ -1145,7 +1145,11 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er
}
if d.HasChange("resource_labels") {
resourceLabels := d.Get("resource_labels").(map[string]string)
resourceLabelsI := d.Get("resource_labels").(map[string]interface{})
resourceLabels := make(map[string]string, len(resourceLabelsI))
for k, v := range resourceLabelsI {
resourceLabels[k] = v.(string)
}
req := &containerBeta.SetLabelsRequest{
ResourceLabels: resourceLabels,

View File

@ -1183,6 +1183,32 @@ func TestAccContainerCluster_withResourceLabels(t *testing.T) {
})
}
func TestAccContainerCluster_withResourceLabelsUpdate(t *testing.T) {
t.Parallel()
clusterName := fmt.Sprintf("cluster-test-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckContainerClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccContainerCluster_withoutResourceLabels(clusterName),
},
{
Config: testAccContainerCluster_withResourceLabels(clusterName),
},
{
ResourceName: "google_container_cluster.with_resource_labels",
ImportStateIdPrefix: "us-central1-a/",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckContainerClusterDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
@ -2254,6 +2280,16 @@ resource "google_container_cluster" "shared_vpc_cluster" {
}`, projectName, org, billingId, projectName, org, billingId, acctest.RandString(10), acctest.RandString(10), name)
}
func testAccContainerCluster_withoutResourceLabels(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_resource_labels" {
name = "%s"
zone = "us-central1-a"
initial_node_count = 1
}
`, clusterName)
}
func testAccContainerCluster_withResourceLabels(clusterName string) string {
return fmt.Sprintf(`
resource "google_container_cluster" "with_resource_labels" {