diff --git a/.changelog/11667.txt b/.changelog/11667.txt new file mode 100644 index 00000000000..37ea1c9b23d --- /dev/null +++ b/.changelog/11667.txt @@ -0,0 +1,3 @@ +```release-note:bug +workbench: fixed a bug in the `google_workbench_instance` resource where the removal of `labels` was not functioning as expected. +``` \ No newline at end of file diff --git a/google/services/workbench/resource_workbench_instance.go b/google/services/workbench/resource_workbench_instance.go index 2526248a312..92be5292626 100644 --- a/google/services/workbench/resource_workbench_instance.go +++ b/google/services/workbench/resource_workbench_instance.go @@ -274,6 +274,26 @@ func resizeWorkbenchInstanceDisk(config *transport_tpg.Config, d *schema.Resourc return nil } +// mergeLabels takes two maps of labels and returns a new map with the labels merged. +// If a key exists in old_labels but not in new_labels, it is added to the new map with an empty value. +func mergeLabels(oldLabels, newLabels map[string]interface{}) map[string]string { + modifiedLabels := make(map[string]string) + + // Add all labels from newLabels to modifiedLabels + for k, v := range newLabels { + modifiedLabels[k] = v.(string) + } + + // Add any keys from oldLabels that are not in newLabels with an empty value + for k := range oldLabels { + if _, ok := newLabels[k]; !ok { + modifiedLabels[k] = "" + } + } + + return modifiedLabels +} + func ResourceWorkbenchInstance() *schema.Resource { return &schema.Resource{ Create: resourceWorkbenchInstanceCreate, @@ -1104,6 +1124,13 @@ func resourceWorkbenchInstanceUpdate(d *schema.ResourceData, meta interface{}) e return err } + if d.HasChange("effective_labels") { + old_labels_interface, new_labels_interface := d.GetChange("effective_labels") + old_labels := old_labels_interface.(map[string]interface{}) + new_labels := new_labels_interface.(map[string]interface{}) + obj["labels"] = mergeLabels(old_labels, new_labels) + } + name := d.Get("name").(string) if stopInstance { state := d.Get("state").(string) diff --git a/google/services/workbench/resource_workbench_instance_test.go b/google/services/workbench/resource_workbench_instance_test.go index e1cf2d5b776..ba39529ad53 100644 --- a/google/services/workbench/resource_workbench_instance_test.go +++ b/google/services/workbench/resource_workbench_instance_test.go @@ -626,3 +626,69 @@ resource "google_workbench_instance" "instance" { } `, context) } + +func TestAccWorkbenchInstance_updatelabels(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + Steps: []resource.TestStep{ + { + Config: testAccWorkbenchInstance_label(context), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "google_workbench_instance.instance", "state", "ACTIVE"), + ), + }, + { + ResourceName: "google_workbench_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"}, + }, + { + Config: testAccWorkbenchInstance_basic(context), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "google_workbench_instance.instance", "state", "ACTIVE"), + ), + }, + { + ResourceName: "google_workbench_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"}, + }, + { + Config: testAccWorkbenchInstance_label(context), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr( + "google_workbench_instance.instance", "state", "ACTIVE"), + ), + }, + { + ResourceName: "google_workbench_instance.instance", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"}, + }, + }, + }) +} + +func testAccWorkbenchInstance_label(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_workbench_instance" "instance" { + name = "tf-test-workbench-instance%{random_suffix}" + location = "us-central1-a" + labels = { + k = "val" + } +} +`, context) +}