Skip to content

Commit 5354d0b

Browse files
workbench: Fix a bug with instance labels being removed not working as expected (#11667)
[upstream:cc2d986b82253ab8d7db13958d56b3c6def2cd71] Signed-off-by: Modular Magician <[email protected]>
1 parent 305a621 commit 5354d0b

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

.changelog/11667.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
workbench: fixed a bug in the `google_workbench_instance` resource where the removal of `labels` was not functioning as expected.
3+
```

google-beta/services/workbench/resource_workbench_instance.go

+27
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,26 @@ func resizeWorkbenchInstanceDisk(config *transport_tpg.Config, d *schema.Resourc
274274
return nil
275275
}
276276

277+
// mergeLabels takes two maps of labels and returns a new map with the labels merged.
278+
// If a key exists in old_labels but not in new_labels, it is added to the new map with an empty value.
279+
func mergeLabels(oldLabels, newLabels map[string]interface{}) map[string]string {
280+
modifiedLabels := make(map[string]string)
281+
282+
// Add all labels from newLabels to modifiedLabels
283+
for k, v := range newLabels {
284+
modifiedLabels[k] = v.(string)
285+
}
286+
287+
// Add any keys from oldLabels that are not in newLabels with an empty value
288+
for k := range oldLabels {
289+
if _, ok := newLabels[k]; !ok {
290+
modifiedLabels[k] = ""
291+
}
292+
}
293+
294+
return modifiedLabels
295+
}
296+
277297
func ResourceWorkbenchInstance() *schema.Resource {
278298
return &schema.Resource{
279299
Create: resourceWorkbenchInstanceCreate,
@@ -1104,6 +1124,13 @@ func resourceWorkbenchInstanceUpdate(d *schema.ResourceData, meta interface{}) e
11041124
return err
11051125
}
11061126

1127+
if d.HasChange("effective_labels") {
1128+
old_labels_interface, new_labels_interface := d.GetChange("effective_labels")
1129+
old_labels := old_labels_interface.(map[string]interface{})
1130+
new_labels := new_labels_interface.(map[string]interface{})
1131+
obj["labels"] = mergeLabels(old_labels, new_labels)
1132+
}
1133+
11071134
name := d.Get("name").(string)
11081135
if stopInstance {
11091136
state := d.Get("state").(string)

google-beta/services/workbench/resource_workbench_instance_test.go

+66
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,69 @@ resource "google_workbench_instance" "instance" {
626626
}
627627
`, context)
628628
}
629+
630+
func TestAccWorkbenchInstance_updatelabels(t *testing.T) {
631+
t.Parallel()
632+
633+
context := map[string]interface{}{
634+
"random_suffix": acctest.RandString(t, 10),
635+
}
636+
637+
acctest.VcrTest(t, resource.TestCase{
638+
PreCheck: func() { acctest.AccTestPreCheck(t) },
639+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
640+
Steps: []resource.TestStep{
641+
{
642+
Config: testAccWorkbenchInstance_label(context),
643+
Check: resource.ComposeTestCheckFunc(
644+
resource.TestCheckResourceAttr(
645+
"google_workbench_instance.instance", "state", "ACTIVE"),
646+
),
647+
},
648+
{
649+
ResourceName: "google_workbench_instance.instance",
650+
ImportState: true,
651+
ImportStateVerify: true,
652+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
653+
},
654+
{
655+
Config: testAccWorkbenchInstance_basic(context),
656+
Check: resource.ComposeTestCheckFunc(
657+
resource.TestCheckResourceAttr(
658+
"google_workbench_instance.instance", "state", "ACTIVE"),
659+
),
660+
},
661+
{
662+
ResourceName: "google_workbench_instance.instance",
663+
ImportState: true,
664+
ImportStateVerify: true,
665+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
666+
},
667+
{
668+
Config: testAccWorkbenchInstance_label(context),
669+
Check: resource.ComposeTestCheckFunc(
670+
resource.TestCheckResourceAttr(
671+
"google_workbench_instance.instance", "state", "ACTIVE"),
672+
),
673+
},
674+
{
675+
ResourceName: "google_workbench_instance.instance",
676+
ImportState: true,
677+
ImportStateVerify: true,
678+
ImportStateVerifyIgnore: []string{"name", "instance_owners", "location", "instance_id", "request_id", "labels", "terraform_labels", "desired_state"},
679+
},
680+
},
681+
})
682+
}
683+
684+
func testAccWorkbenchInstance_label(context map[string]interface{}) string {
685+
return acctest.Nprintf(`
686+
resource "google_workbench_instance" "instance" {
687+
name = "tf-test-workbench-instance%{random_suffix}"
688+
location = "us-central1-a"
689+
labels = {
690+
k = "val"
691+
}
692+
}
693+
`, context)
694+
}

0 commit comments

Comments
 (0)