Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workbench: Fix a bug with instance labels being removed not working as expected #19620

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}