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 metadata being removed not working as expected #12884

Merged
merged 26 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
a966f6c
update documentation for instance_owners field
bcreddy-gcp Oct 31, 2024
71e2553
update documentation for instance_owners field
bcreddy-gcp Oct 31, 2024
6a7ddbb
lint fix
bcreddy-gcp Oct 31, 2024
85b523e
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Nov 6, 2024
b4f50e3
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Dec 5, 2024
268e2ae
gcs-data-bucket is now modifiable by the customer
bcreddy-gcp Dec 5, 2024
c9e07e6
Add suport for Third Party Identity
bcreddy-gcp Dec 18, 2024
b5377ef
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Dec 18, 2024
609f8ad
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Dec 23, 2024
90e2d72
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Jan 6, 2025
505400c
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Jan 10, 2025
52ed31f
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Jan 13, 2025
6b4d5e9
Merge branch 'GoogleCloudPlatform:main' into main
bcreddy-gcp Jan 22, 2025
b9bef97
Update metadata to remove metadata
bcreddy-gcp Jan 22, 2025
9eb23ce
metadata removal now adds a key with empty value
bcreddy-gcp Jan 28, 2025
6b3fc2d
remove other changes
bcreddy-gcp Jan 28, 2025
4dc258f
lint
bcreddy-gcp Jan 28, 2025
a972803
lint
bcreddy-gcp Jan 28, 2025
1f47c68
lint
bcreddy-gcp Jan 28, 2025
4b4d65e
Update mmv1/templates/terraform/constants/workbench_instance.go.tmpl
bcreddy-gcp Jan 30, 2025
125e4ac
Update mmv1/third_party/terraform/services/workbench/resource_workben…
bcreddy-gcp Jan 30, 2025
5aa9e88
Update mmv1/third_party/terraform/services/workbench/resource_workben…
bcreddy-gcp Jan 30, 2025
2bd7133
Merge branch 'GoogleCloudPlatform:main' into wbi-idle-fix
bcreddy-gcp Jan 30, 2025
b839b99
check for empty gceSetup
bcreddy-gcp Jan 30, 2025
f584e3a
fix build
bcreddy-gcp Jan 30, 2025
bb96706
fix comment
bcreddy-gcp Jan 30, 2025
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
26 changes: 13 additions & 13 deletions mmv1/templates/terraform/constants/workbench_instance.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,23 @@ 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)
// mergeMaps takes two maps and returns a new map with the merged results.
// If a key exists in old_maps but not in new_maps, it is added to the new map with an empty value.
func mergeMaps(oldMaps, newMaps map[string]interface{}) map[string]string {
modifiedMaps := make(map[string]string)

// Add all labels from newLabels to modifiedLabels
for k, v := range newLabels {
modifiedLabels[k] = v.(string)
// Add all maps from newMaps to modifiedMaps
for k, v := range newMaps {
modifiedMaps[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] = ""
// Add any keys from oldMaps that are not in newMaps with an empty value
for k := range oldMaps {
if _, ok := newMaps[k]; !ok {
modifiedMaps[k] = ""
}
}

return modifiedLabels
return modifiedMaps
}
{{- end }}
{{- end }}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ 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)
obj["labels"] = mergeMaps(old_labels,new_labels)
}

if d.HasChange("gce_setup.0.metadata") {
old_metadata_interface, new_metadata_interface := d.GetChange("gce_setup.0.metadata")
old_metadata := old_metadata_interface.(map[string]interface{})
new_metadata := new_metadata_interface.(map[string]interface{})
obj["gceSetup"].(map[string]interface{})["metadata"] = mergeMaps(old_metadata,new_metadata)
}

name := d.Get("name").(string)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,73 @@ func TestAccWorkbenchInstance_updateMetadata(t *testing.T) {
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"},
},
{
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"},
},
},
})
}

func TestAccWorkbenchInstance_updateMetadataKey(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_updateMetadata(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"},
},
{
Config: testAccWorkbenchInstance_updateMetadataKey(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"},
},
{
Config: testAccWorkbenchInstance_updateMetadata(context),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"google_workbench_instance.instance", "state", "ACTIVE"),
),
},
{
Expand Down Expand Up @@ -322,6 +389,27 @@ resource "google_workbench_instance" "instance" {
`, context)
}

func testAccWorkbenchInstance_updateMetadataKey(context map[string]interface{}) string {
return acctest.Nprintf(`
resource "google_workbench_instance" "instance" {
name = "tf-test-workbench-instance%{random_suffix}"
location = "us-central1-a"

gce_setup {
metadata = {
terraform = "true",
"idle-timeout-seconds" = "10800",
}
}

labels = {
k = "val"
}

}
`, context)
}

func TestAccWorkbenchInstance_updateState(t *testing.T) {
t.Parallel()

Expand Down
Loading