Skip to content

Commit 125d068

Browse files
[#16356] Add support for resource manager tags for google_compute_instance_template (#9612) (#6798)
* [#16356] Add support for resource manager tags for google_compute_instance_template * Add tags for regional instance template * Add docs --------- [upstream:9fbebe719bf8cee84dc3b664e67a1548dcf4d91a] Signed-off-by: Modular Magician <[email protected]>
1 parent e340001 commit 125d068

7 files changed

+209
-3
lines changed

.changelog/9612.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
```release-note:enhancement
2+
compute: added `resource_manager_tags` and `disk.resource_manager_tags` for `google_compute_instance_template`
3+
```
4+
```release-note:enhancement
5+
compute: added `resource_manager_tags` and `disk.resource_manager_tags` for `google_compute_region_instance_template`
6+
```

google-beta/services/compute/resource_compute_instance_template.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ func ResourceComputeInstanceTemplate() *schema.Resource {
174174
Description: `Indicates how many IOPS to provision for the disk. This sets the number of I/O operations per second that the disk can handle. Values must be between 10,000 and 120,000. For more details, see the [Extreme persistent disk documentation](https://cloud.google.com/compute/docs/disks/extreme-persistent-disk).`,
175175
},
176176

177+
"resource_manager_tags": {
178+
Type: schema.TypeMap,
179+
Optional: true,
180+
ForceNew: true,
181+
Elem: &schema.Schema{Type: schema.TypeString},
182+
Set: schema.HashString,
183+
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.`,
184+
},
185+
177186
"source_image": {
178187
Type: schema.TypeString,
179188
Optional: true,
@@ -606,6 +615,18 @@ Google Cloud KMS.`,
606615
Description: `An instance template is a global resource that is not bound to a zone or a region. However, you can still specify some regional resources in an instance template, which restricts the template to the region where that resource resides. For example, a custom subnetwork resource is tied to a specific region. Defaults to the region of the Provider if no value is given.`,
607616
},
608617

618+
"resource_manager_tags": {
619+
Type: schema.TypeMap,
620+
Optional: true,
621+
ForceNew: false,
622+
Elem: &schema.Schema{Type: schema.TypeString},
623+
Set: schema.HashString,
624+
Description: `A map of resource manager tags.
625+
Resource manager tag keys and values have the same definition as resource manager tags.
626+
Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456.
627+
The field is ignored (both PUT & PATCH) when empty.`,
628+
},
629+
609630
"scheduling": {
610631
Type: schema.TypeList,
611632
Optional: true,
@@ -1175,7 +1196,9 @@ func buildDisks(d *schema.ResourceData, config *transport_tpg.Config) ([]*comput
11751196
if v, ok := d.GetOk(prefix + ".provisioned_iops"); ok {
11761197
disk.InitializeParams.ProvisionedIops = int64(v.(int))
11771198
}
1178-
1199+
if _, ok := d.GetOk(prefix + ".resource_manager_tags"); ok {
1200+
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, prefix+".resource_manager_tags")
1201+
}
11791202
disk.InitializeParams.Labels = tpgresource.ExpandStringMap(d, prefix+".labels")
11801203

11811204
if v, ok := d.GetOk(prefix + ".source_image"); ok {
@@ -1339,6 +1362,10 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
13391362
instanceProperties.Labels = tpgresource.ExpandEffectiveLabels(d)
13401363
}
13411364

1365+
if _, ok := d.GetOk("resource_manager_tags"); ok {
1366+
instanceProperties.ResourceManagerTags = tpgresource.ExpandStringMap(d, "resource_manager_tags")
1367+
}
1368+
13421369
var itName string
13431370
if v, ok := d.GetOk("name"); ok {
13441371
itName = v.(string)
@@ -1453,8 +1480,8 @@ func flattenDisk(disk *compute.AttachedDisk, configDisk map[string]any, defaultP
14531480
} else {
14541481
diskMap["disk_size_gb"] = disk.InitializeParams.DiskSizeGb
14551482
}
1456-
14571483
diskMap["resource_policies"] = disk.InitializeParams.ResourcePolicies
1484+
diskMap["resource_manager_tags"] = disk.InitializeParams.ResourceManagerTags
14581485
}
14591486

14601487
if disk.DiskEncryptionKey != nil {

google-beta/services/compute/resource_compute_instance_template_test.go

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,32 @@ func TestAccComputeInstanceTemplate_withLabels(t *testing.T) {
13251325
})
13261326
}
13271327

1328+
func TestAccComputeInstanceTemplate_resourceManagerTags(t *testing.T) {
1329+
t.Parallel()
1330+
1331+
var instanceTemplate compute.InstanceTemplate
1332+
var instanceTemplateName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
1333+
context := map[string]interface{}{
1334+
"project": envvar.GetTestProjectFromEnv(),
1335+
"random_suffix": acctest.RandString(t, 10),
1336+
"instance_name": instanceTemplateName,
1337+
}
1338+
1339+
acctest.VcrTest(t, resource.TestCase{
1340+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1341+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1342+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
1343+
Steps: []resource.TestStep{
1344+
{
1345+
Config: testAccComputeInstanceTemplate_resourceManagerTags(context),
1346+
Check: resource.ComposeTestCheckFunc(
1347+
testAccCheckComputeInstanceTemplateExists(
1348+
t, "google_compute_instance_template.foobar", &instanceTemplate)),
1349+
},
1350+
},
1351+
})
1352+
}
1353+
13281354
func testAccCheckComputeInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
13291355
return func(s *terraform.State) error {
13301356
config := acctest.GoogleProviderConfig(t)
@@ -3637,7 +3663,6 @@ resource "google_compute_image" "image" {
36373663
]
36383664
}
36393665
3640-
36413666
resource "google_compute_instance_template" "template" {
36423667
name = "tf-test-instance-template-%{random_suffix}"
36433668
machine_type = "e2-medium"
@@ -3663,6 +3688,51 @@ resource "google_compute_instance_template" "template" {
36633688
`, context)
36643689
}
36653690

3691+
func testAccComputeInstanceTemplate_resourceManagerTags(context map[string]interface{}) string {
3692+
return acctest.Nprintf(`
3693+
resource "google_tags_tag_key" "key" {
3694+
parent = "projects/%{project}"
3695+
short_name = "foobarbaz%{random_suffix}"
3696+
description = "For foo/bar resources."
3697+
}
3698+
3699+
resource "google_tags_tag_value" "value" {
3700+
parent = "tagKeys/${google_tags_tag_key.key.name}"
3701+
short_name = "foo%{random_suffix}"
3702+
description = "For foo resources."
3703+
}
3704+
3705+
data "google_compute_image" "my_image" {
3706+
family = "debian-11"
3707+
project = "debian-cloud"
3708+
}
3709+
3710+
resource "google_compute_instance_template" "foobar" {
3711+
name = "%{instance_name}"
3712+
machine_type = "e2-medium"
3713+
3714+
disk {
3715+
source_image = data.google_compute_image.my_image.self_link
3716+
auto_delete = true
3717+
disk_size_gb = 10
3718+
boot = true
3719+
3720+
resource_manager_tags = {
3721+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3722+
}
3723+
}
3724+
3725+
resource_manager_tags = {
3726+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3727+
}
3728+
3729+
network_interface {
3730+
network = "default"
3731+
}
3732+
}
3733+
`, context)
3734+
}
3735+
36663736
func testAccComputeInstanceTemplate_network_attachment(context map[string]interface{}) string {
36673737
return acctest.Nprintf(`
36683738
data "google_compute_image" "my_image" {

google-beta/services/compute/resource_compute_region_instance_template.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ func ResourceComputeRegionInstanceTemplate() *schema.Resource {
155155
Description: `Indicates how many IOPS to provision for the disk. This sets the number of I/O operations per second that the disk can handle. Values must be between 10,000 and 120,000. For more details, see the [Extreme persistent disk documentation](https://cloud.google.com/compute/docs/disks/extreme-persistent-disk).`,
156156
},
157157

158+
"resource_manager_tags": {
159+
Type: schema.TypeMap,
160+
Optional: true,
161+
ForceNew: true,
162+
Elem: &schema.Schema{Type: schema.TypeString},
163+
Set: schema.HashString,
164+
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.`,
165+
},
166+
158167
"source_image": {
159168
Type: schema.TypeString,
160169
Optional: true,
@@ -572,6 +581,16 @@ Google Cloud KMS.`,
572581
Description: `The ID of the project in which the resource belongs. If it is not provided, the provider project is used.`,
573582
},
574583

584+
"resource_manager_tags": {
585+
Type: schema.TypeMap,
586+
Optional: true,
587+
ForceNew: false,
588+
Elem: &schema.Schema{Type: schema.TypeString},
589+
Set: schema.HashString,
590+
Description: `A map of resource manager tags.
591+
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.`,
592+
},
593+
575594
"scheduling": {
576595
Type: schema.TypeList,
577596
Optional: true,
@@ -1043,6 +1062,10 @@ func resourceComputeRegionInstanceTemplateCreate(d *schema.ResourceData, meta in
10431062
instanceProperties.Labels = tpgresource.ExpandEffectiveLabels(d)
10441063
}
10451064

1065+
if _, ok := d.GetOk("resource_manager_tags"); ok {
1066+
instanceProperties.ResourceManagerTags = tpgresource.ExpandStringMap(d, "resource_manager_tags")
1067+
}
1068+
10461069
var itName string
10471070
if v, ok := d.GetOk("name"); ok {
10481071
itName = v.(string)

google-beta/services/compute/resource_compute_region_instance_template_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,32 @@ func TestAccComputeRegionInstanceTemplate_sourceImageEncryptionKey(t *testing.T)
11341134
})
11351135
}
11361136

1137+
func TestAccComputeRegionInstanceTemplate_resourceManagerTags(t *testing.T) {
1138+
t.Parallel()
1139+
1140+
var instanceTemplate compute.InstanceTemplate
1141+
var instanceTemplateName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
1142+
context := map[string]interface{}{
1143+
"project": envvar.GetTestProjectFromEnv(),
1144+
"random_suffix": acctest.RandString(t, 10),
1145+
"instance_name": instanceTemplateName,
1146+
}
1147+
1148+
acctest.VcrTest(t, resource.TestCase{
1149+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1150+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1151+
CheckDestroy: testAccCheckComputeRegionInstanceTemplateDestroyProducer(t),
1152+
Steps: []resource.TestStep{
1153+
{
1154+
Config: testAccComputeRegionInstanceTemplate_resourceManagerTags(context),
1155+
Check: resource.ComposeTestCheckFunc(
1156+
testAccCheckComputeRegionInstanceTemplateExists(
1157+
t, "google_compute_region_instance_template.foobar", &instanceTemplate)),
1158+
},
1159+
},
1160+
})
1161+
}
1162+
11371163
func testAccCheckComputeRegionInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
11381164
return func(s *terraform.State) error {
11391165
config := acctest.GoogleProviderConfig(t)
@@ -3330,3 +3356,49 @@ resource "google_compute_region_instance_template" "template" {
33303356
}
33313357
`, context)
33323358
}
3359+
3360+
func testAccComputeRegionInstanceTemplate_resourceManagerTags(context map[string]interface{}) string {
3361+
return acctest.Nprintf(`
3362+
resource "google_tags_tag_key" "key" {
3363+
parent = "projects/%{project}"
3364+
short_name = "foobarbaz%{random_suffix}"
3365+
description = "For foo/bar resources."
3366+
}
3367+
3368+
resource "google_tags_tag_value" "value" {
3369+
parent = "tagKeys/${google_tags_tag_key.key.name}"
3370+
short_name = "foo%{random_suffix}"
3371+
description = "For foo resources."
3372+
}
3373+
3374+
data "google_compute_image" "my_image" {
3375+
family = "debian-11"
3376+
project = "debian-cloud"
3377+
}
3378+
3379+
resource "google_compute_region_instance_template" "foobar" {
3380+
name = "%{instance_name}"
3381+
machine_type = "e2-medium"
3382+
region = "us-central1"
3383+
3384+
disk {
3385+
source_image = data.google_compute_image.my_image.self_link
3386+
auto_delete = true
3387+
disk_size_gb = 10
3388+
boot = true
3389+
3390+
resource_manager_tags = {
3391+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3392+
}
3393+
}
3394+
3395+
resource_manager_tags = {
3396+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3397+
}
3398+
3399+
network_interface {
3400+
network = "default"
3401+
}
3402+
}
3403+
`, context)
3404+
}

website/docs/r/compute_instance_template.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ The following arguments are supported:
351351
* `reservation_affinity` - (Optional) Specifies the reservations that this instance can consume from.
352352
Structure is [documented below](#nested_reservation_affinity).
353353

354+
* `resource_manager_tags` - (Optional) A set of key/value resource manager tag pairs to bind to the instances. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456.
355+
354356
* `scheduling` - (Optional) The scheduling strategy to use. More details about
355357
this configuration option are [detailed below](#nested_scheduling).
356358

@@ -392,6 +394,8 @@ The following arguments are supported:
392394
Values must be between 10,000 and 120,000. For more details, see the
393395
[Extreme persistent disk documentation](https://cloud.google.com/compute/docs/disks/extreme-persistent-disk).
394396

397+
* `resource_manager_tags` - (Optional) A set of key/value resource manager tag pairs to bind to this disk. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456.
398+
395399
* `source_image` - (Optional) The image from which to
396400
initialize this disk. This can be one of: the image's `self_link`,
397401
`projects/{project}/global/images/{image}`,

website/docs/r/compute_region_instance_template.html.markdown

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,8 @@ The following arguments are supported:
355355
* `region` - (Optional) The Region in which the resource belongs.
356356
If region is not provided, the provider region is used.
357357

358+
* `resource_manager_tags` - (Optional) A set of key/value resource manager tag pairs to bind to the instance. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456.
359+
358360
* `resource_policies` (Optional) -- A list of self_links of resource policies to attach to the instance. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported.
359361

360362
* `reservation_affinity` - (Optional) Specifies the reservations that this instance can consume from.
@@ -401,6 +403,8 @@ The following arguments are supported:
401403
Values must be between 10,000 and 120,000. For more details, see the
402404
[Extreme persistent disk documentation](https://cloud.google.com/compute/docs/disks/extreme-persistent-disk).
403405

406+
* `resource_manager_tags` - (Optional) A set of key/value resource manager tag pairs to bind to this disk. Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456.
407+
404408
* `source_image` - (Optional) The image from which to
405409
initialize this disk. This can be one of: the image's `self_link`,
406410
`projects/{project}/global/images/{image}`,

0 commit comments

Comments
 (0)