Skip to content

Commit 1a744ca

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

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

.changelog/9740.txt

Lines changed: 3 additions & 0 deletions
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-beta/services/compute/resource_compute_instance.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ func ResourceComputeInstance() *schema.Resource {
235235
"resource_manager_tags": {
236236
Type: schema.TypeMap,
237237
Optional: true,
238-
AtLeastOneOf: initializeParamsKeys,
239238
ForceNew: true,
239+
AtLeastOneOf: initializeParamsKeys,
240240
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.`,
241241
},
242242

@@ -649,10 +649,8 @@ func ResourceComputeInstance() *schema.Resource {
649649
Elem: &schema.Resource{
650650
Schema: map[string]*schema.Schema{
651651
"resource_manager_tags": {
652-
Type: schema.TypeMap,
653-
Optional: true,
654-
// 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.
655-
ForceNew: true,
652+
Type: schema.TypeMap,
653+
Optional: true,
656654
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.`,
657655
},
658656
},
@@ -1804,6 +1802,40 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
18041802
}
18051803
}
18061804

1805+
if d.HasChange("params.0.resource_manager_tags") {
1806+
err = transport_tpg.Retry(transport_tpg.RetryOptions{
1807+
RetryFunc: func() error {
1808+
instance, err := config.NewComputeClient(userAgent).Instances.Get(project, zone, instance.Name).Do()
1809+
if err != nil {
1810+
return fmt.Errorf("Error retrieving instance: %s", err)
1811+
}
1812+
1813+
params, err := expandParams(d)
1814+
if err != nil {
1815+
return fmt.Errorf("Error updating params: %s", err)
1816+
}
1817+
1818+
instance.Params = params
1819+
1820+
op, err := config.NewComputeClient(userAgent).Instances.Update(project, zone, instance.Name, instance).Do()
1821+
if err != nil {
1822+
return fmt.Errorf("Error updating instance: %s", err)
1823+
}
1824+
1825+
opErr := ComputeOperationWaitTime(config, op, project, "resource_manager_tags, updating", userAgent, d.Timeout(schema.TimeoutUpdate))
1826+
if opErr != nil {
1827+
return opErr
1828+
}
1829+
1830+
return nil
1831+
},
1832+
})
1833+
1834+
if err != nil {
1835+
return err
1836+
}
1837+
}
1838+
18071839
if d.HasChange("resource_policies") {
18081840
if len(instance.ResourcePolicies) > 0 {
18091841
req := compute.InstancesRemoveResourcePoliciesRequest{ResourcePolicies: instance.ResourcePolicies}

google-beta/services/compute/resource_compute_instance_test.go

Lines changed: 63 additions & 4 deletions
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
}
@@ -4136,8 +4142,6 @@ resource "google_compute_instance" "foobar" {
41364142
name = "%{instance_name}"
41374143
machine_type = "e2-medium"
41384144
zone = "us-central1-a"
4139-
can_ip_forward = false
4140-
tags = ["tag-key", "tag-value"]
41414145
41424146
boot_disk {
41434147
initialize_params {
@@ -4157,9 +4161,64 @@ resource "google_compute_instance" "foobar" {
41574161
network_interface {
41584162
network = "default"
41594163
}
4164+
}
4165+
`, context)
4166+
}
41604167

4161-
metadata = {
4162-
foo = "bar"
4168+
func testAccComputeInstance_resourceManagerTagsUpdate(context map[string]interface{}) string {
4169+
return acctest.Nprintf(`
4170+
resource "google_tags_tag_key" "key" {
4171+
parent = "projects/%{project}"
4172+
short_name = "foobarbaz%{random_suffix}"
4173+
description = "For foo/bar resources."
4174+
}
4175+
4176+
resource "google_tags_tag_value" "value" {
4177+
parent = "tagKeys/${google_tags_tag_key.key.name}"
4178+
short_name = "foo%{random_suffix}"
4179+
description = "For foo resources."
4180+
}
4181+
4182+
resource "google_tags_tag_key" "key_new" {
4183+
parent = "projects/%{project}"
4184+
short_name = "foobarbaznew%{random_suffix}"
4185+
description = "New key for foo/bar resources."
4186+
}
4187+
4188+
resource "google_tags_tag_value" "value_new" {
4189+
parent = "tagKeys/${google_tags_tag_key.key_new.name}"
4190+
short_name = "foonew%{random_suffix}"
4191+
description = "New value for foo resources."
4192+
}
4193+
4194+
data "google_compute_image" "my_image" {
4195+
family = "debian-11"
4196+
project = "debian-cloud"
4197+
}
4198+
4199+
resource "google_compute_instance" "foobar" {
4200+
name = "%{instance_name}"
4201+
machine_type = "e2-medium"
4202+
zone = "us-central1-a"
4203+
4204+
boot_disk {
4205+
initialize_params {
4206+
image = data.google_compute_image.my_image.self_link
4207+
resource_manager_tags = {
4208+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
4209+
}
4210+
}
4211+
}
4212+
4213+
params {
4214+
resource_manager_tags = {
4215+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
4216+
"tagKeys/${google_tags_tag_key.key_new.name}" = "tagValues/${google_tags_tag_value.value_new.name}"
4217+
}
4218+
}
4219+
4220+
network_interface {
4221+
network = "default"
41634222
}
41644223
}
41654224
`, context)

0 commit comments

Comments
 (0)