Skip to content

Commit

Permalink
workbench: Fix a bug with instance labels being removed not working a…
Browse files Browse the repository at this point in the history
…s expected (#11667) (#19620)

[upstream:cc2d986b82253ab8d7db13958d56b3c6def2cd71]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 24, 2024
1 parent e60fd97 commit 46a9478
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/11667.txt
Original file line number Diff line number Diff line change
@@ -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.
```
27 changes: 27 additions & 0 deletions google/services/workbench/resource_workbench_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 66 additions & 0 deletions google/services/workbench/resource_workbench_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 46a9478

Please sign in to comment.