Skip to content

Commit 3e19559

Browse files
Allow users to update google_compute_instance resource_manager_tags in place (#9740) (#16942)
* Initial commit * Fixes --------- [upstream:603220f9331feef044d8b673c522fc9cf0d29ea4] Signed-off-by: Modular Magician <[email protected]>
1 parent 7c41487 commit 3e19559

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

.changelog/9740.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added update support to `params.resource_manager_tags` in `google_compute_instance`
3+
```

google/services/compute/resource_compute_instance.go

+37-5
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ func ResourceComputeInstance() *schema.Resource {
232232
"resource_manager_tags": {
233233
Type: schema.TypeMap,
234234
Optional: true,
235-
AtLeastOneOf: initializeParamsKeys,
236235
ForceNew: true,
236+
AtLeastOneOf: initializeParamsKeys,
237237
Description: `A map of resource manager tags. Resource manager tag keys and values have the same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.`,
238238
},
239239

@@ -613,10 +613,8 @@ func ResourceComputeInstance() *schema.Resource {
613613
Elem: &schema.Resource{
614614
Schema: map[string]*schema.Schema{
615615
"resource_manager_tags": {
616-
Type: schema.TypeMap,
617-
Optional: true,
618-
// This field is intentionally not updatable. The API overrides all existing tags on the field when updated. See go/gce-tags-terraform-support for details.
619-
ForceNew: true,
616+
Type: schema.TypeMap,
617+
Optional: true,
620618
Description: `A map of resource manager tags. Resource manager tag keys and values have the same definition as resource manager tags. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456. The field is ignored (both PUT & PATCH) when empty.`,
621619
},
622620
},
@@ -1717,6 +1715,40 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
17171715
}
17181716
}
17191717

1718+
if d.HasChange("params.0.resource_manager_tags") {
1719+
err = transport_tpg.Retry(transport_tpg.RetryOptions{
1720+
RetryFunc: func() error {
1721+
instance, err := config.NewComputeClient(userAgent).Instances.Get(project, zone, instance.Name).Do()
1722+
if err != nil {
1723+
return fmt.Errorf("Error retrieving instance: %s", err)
1724+
}
1725+
1726+
params, err := expandParams(d)
1727+
if err != nil {
1728+
return fmt.Errorf("Error updating params: %s", err)
1729+
}
1730+
1731+
instance.Params = params
1732+
1733+
op, err := config.NewComputeClient(userAgent).Instances.Update(project, zone, instance.Name, instance).Do()
1734+
if err != nil {
1735+
return fmt.Errorf("Error updating instance: %s", err)
1736+
}
1737+
1738+
opErr := ComputeOperationWaitTime(config, op, project, "resource_manager_tags, updating", userAgent, d.Timeout(schema.TimeoutUpdate))
1739+
if opErr != nil {
1740+
return opErr
1741+
}
1742+
1743+
return nil
1744+
},
1745+
})
1746+
1747+
if err != nil {
1748+
return err
1749+
}
1750+
}
1751+
17201752
if d.HasChange("resource_policies") {
17211753
if len(instance.ResourcePolicies) > 0 {
17221754
req := compute.InstancesRemoveResourcePoliciesRequest{ResourcePolicies: instance.ResourcePolicies}

google/services/compute/resource_compute_instance_test.go

+63-4
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,12 @@ func TestAccComputeInstance_resourceManagerTags(t *testing.T) {
263263
testAccCheckComputeInstanceExists(
264264
t, "google_compute_instance.foobar", &instance)),
265265
},
266+
{
267+
Config: testAccComputeInstance_resourceManagerTagsUpdate(context),
268+
Check: resource.ComposeTestCheckFunc(
269+
testAccCheckComputeInstanceExists(
270+
t, "google_compute_instance.foobar", &instance)),
271+
},
266272
},
267273
})
268274
}
@@ -3572,8 +3578,6 @@ resource "google_compute_instance" "foobar" {
35723578
name = "%{instance_name}"
35733579
machine_type = "e2-medium"
35743580
zone = "us-central1-a"
3575-
can_ip_forward = false
3576-
tags = ["tag-key", "tag-value"]
35773581
35783582
boot_disk {
35793583
initialize_params {
@@ -3593,9 +3597,64 @@ resource "google_compute_instance" "foobar" {
35933597
network_interface {
35943598
network = "default"
35953599
}
3600+
}
3601+
`, context)
3602+
}
35963603

3597-
metadata = {
3598-
foo = "bar"
3604+
func testAccComputeInstance_resourceManagerTagsUpdate(context map[string]interface{}) string {
3605+
return acctest.Nprintf(`
3606+
resource "google_tags_tag_key" "key" {
3607+
parent = "projects/%{project}"
3608+
short_name = "foobarbaz%{random_suffix}"
3609+
description = "For foo/bar resources."
3610+
}
3611+
3612+
resource "google_tags_tag_value" "value" {
3613+
parent = "tagKeys/${google_tags_tag_key.key.name}"
3614+
short_name = "foo%{random_suffix}"
3615+
description = "For foo resources."
3616+
}
3617+
3618+
resource "google_tags_tag_key" "key_new" {
3619+
parent = "projects/%{project}"
3620+
short_name = "foobarbaznew%{random_suffix}"
3621+
description = "New key for foo/bar resources."
3622+
}
3623+
3624+
resource "google_tags_tag_value" "value_new" {
3625+
parent = "tagKeys/${google_tags_tag_key.key_new.name}"
3626+
short_name = "foonew%{random_suffix}"
3627+
description = "New value for foo resources."
3628+
}
3629+
3630+
data "google_compute_image" "my_image" {
3631+
family = "debian-11"
3632+
project = "debian-cloud"
3633+
}
3634+
3635+
resource "google_compute_instance" "foobar" {
3636+
name = "%{instance_name}"
3637+
machine_type = "e2-medium"
3638+
zone = "us-central1-a"
3639+
3640+
boot_disk {
3641+
initialize_params {
3642+
image = data.google_compute_image.my_image.self_link
3643+
resource_manager_tags = {
3644+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3645+
}
3646+
}
3647+
}
3648+
3649+
params {
3650+
resource_manager_tags = {
3651+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3652+
"tagKeys/${google_tags_tag_key.key_new.name}" = "tagValues/${google_tags_tag_value.value_new.name}"
3653+
}
3654+
}
3655+
3656+
network_interface {
3657+
network = "default"
35993658
}
36003659
}
36013660
`, context)

0 commit comments

Comments
 (0)