Skip to content

Commit cc2d986

Browse files
authored
workbench: Fix a bug with instance labels being removed not working as expected (#11667)
1 parent 897375a commit cc2d986

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

mmv1/templates/terraform/constants/workbench_instance.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,24 @@ func resizeWorkbenchInstanceDisk(config *transport_tpg.Config, d *schema.Resourc
242242

243243
return nil
244244
}
245-
<% end -%>
245+
246+
// mergeLabels takes two maps of labels and returns a new map with the labels merged.
247+
// If a key exists in old_labels but not in new_labels, it is added to the new map with an empty value.
248+
func mergeLabels(oldLabels, newLabels map[string]interface{}) map[string]string {
249+
modifiedLabels := make(map[string]string)
250+
251+
// Add all labels from newLabels to modifiedLabels
252+
for k, v := range newLabels {
253+
modifiedLabels[k] = v.(string)
254+
}
255+
256+
// Add any keys from oldLabels that are not in newLabels with an empty value
257+
for k := range oldLabels {
258+
if _, ok := newLabels[k]; !ok {
259+
modifiedLabels[k] = ""
260+
}
261+
}
262+
263+
return modifiedLabels
264+
}
265+
<% end -%>

mmv1/templates/terraform/pre_update/workbench_instance.go.erb

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ if err != nil {
3434
return err
3535
}
3636

37+
if d.HasChange("effective_labels") {
38+
old_labels_interface, new_labels_interface := d.GetChange("effective_labels")
39+
old_labels := old_labels_interface.(map[string]interface{})
40+
new_labels := new_labels_interface.(map[string]interface{})
41+
obj["labels"] = mergeLabels(old_labels,new_labels)
42+
}
43+
3744
name := d.Get("name").(string)
3845
if stopInstance{
3946
state := d.Get("state").(string)

mmv1/third_party/terraform/services/workbench/resource_workbench_instance_test.go.erb

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

0 commit comments

Comments
 (0)