Skip to content

Commit cfb5307

Browse files
[#16356] Add support for resource manager tags for google_compute_instance_template (#9612) (#16889)
* [#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 23172e5 commit cfb5307

7 files changed

+209
-3
lines changed

.changelog/9612.txt

+6
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/services/compute/resource_compute_instance_template.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ func ResourceComputeInstanceTemplate() *schema.Resource {
172172
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).`,
173173
},
174174

175+
"resource_manager_tags": {
176+
Type: schema.TypeMap,
177+
Optional: true,
178+
ForceNew: true,
179+
Elem: &schema.Schema{Type: schema.TypeString},
180+
Set: schema.HashString,
181+
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.`,
182+
},
183+
175184
"source_image": {
176185
Type: schema.TypeString,
177186
Optional: true,
@@ -588,6 +597,18 @@ Google Cloud KMS.`,
588597
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.`,
589598
},
590599

600+
"resource_manager_tags": {
601+
Type: schema.TypeMap,
602+
Optional: true,
603+
ForceNew: false,
604+
Elem: &schema.Schema{Type: schema.TypeString},
605+
Set: schema.HashString,
606+
Description: `A map of resource manager tags.
607+
Resource manager tag keys and values have the same definition as resource manager tags.
608+
Keys must be in the format tagKeys/{tag_key_id}, and values are in the format tagValues/456.
609+
The field is ignored (both PUT & PATCH) when empty.`,
610+
},
611+
591612
"scheduling": {
592613
Type: schema.TypeList,
593614
Optional: true,
@@ -1124,7 +1145,9 @@ func buildDisks(d *schema.ResourceData, config *transport_tpg.Config) ([]*comput
11241145
if v, ok := d.GetOk(prefix + ".provisioned_iops"); ok {
11251146
disk.InitializeParams.ProvisionedIops = int64(v.(int))
11261147
}
1127-
1148+
if _, ok := d.GetOk(prefix + ".resource_manager_tags"); ok {
1149+
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, prefix+".resource_manager_tags")
1150+
}
11281151
disk.InitializeParams.Labels = tpgresource.ExpandStringMap(d, prefix+".labels")
11291152

11301153
if v, ok := d.GetOk(prefix + ".source_image"); ok {
@@ -1287,6 +1310,10 @@ func resourceComputeInstanceTemplateCreate(d *schema.ResourceData, meta interfac
12871310
instanceProperties.Labels = tpgresource.ExpandEffectiveLabels(d)
12881311
}
12891312

1313+
if _, ok := d.GetOk("resource_manager_tags"); ok {
1314+
instanceProperties.ResourceManagerTags = tpgresource.ExpandStringMap(d, "resource_manager_tags")
1315+
}
1316+
12901317
var itName string
12911318
if v, ok := d.GetOk("name"); ok {
12921319
itName = v.(string)
@@ -1401,8 +1428,8 @@ func flattenDisk(disk *compute.AttachedDisk, configDisk map[string]any, defaultP
14011428
} else {
14021429
diskMap["disk_size_gb"] = disk.InitializeParams.DiskSizeGb
14031430
}
1404-
14051431
diskMap["resource_policies"] = disk.InitializeParams.ResourcePolicies
1432+
diskMap["resource_manager_tags"] = disk.InitializeParams.ResourceManagerTags
14061433
}
14071434

14081435
if disk.DiskEncryptionKey != nil {

google/services/compute/resource_compute_instance_template_test.go

+71-1
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,32 @@ func TestAccComputeInstanceTemplate_withLabels(t *testing.T) {
11941194
})
11951195
}
11961196

1197+
func TestAccComputeInstanceTemplate_resourceManagerTags(t *testing.T) {
1198+
t.Parallel()
1199+
1200+
var instanceTemplate compute.InstanceTemplate
1201+
var instanceTemplateName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
1202+
context := map[string]interface{}{
1203+
"project": envvar.GetTestProjectFromEnv(),
1204+
"random_suffix": acctest.RandString(t, 10),
1205+
"instance_name": instanceTemplateName,
1206+
}
1207+
1208+
acctest.VcrTest(t, resource.TestCase{
1209+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1210+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1211+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
1212+
Steps: []resource.TestStep{
1213+
{
1214+
Config: testAccComputeInstanceTemplate_resourceManagerTags(context),
1215+
Check: resource.ComposeTestCheckFunc(
1216+
testAccCheckComputeInstanceTemplateExists(
1217+
t, "google_compute_instance_template.foobar", &instanceTemplate)),
1218+
},
1219+
},
1220+
})
1221+
}
1222+
11971223
func testAccCheckComputeInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
11981224
return func(s *terraform.State) error {
11991225
config := acctest.GoogleProviderConfig(t)
@@ -3404,7 +3430,6 @@ resource "google_compute_image" "image" {
34043430
]
34053431
}
34063432
3407-
34083433
resource "google_compute_instance_template" "template" {
34093434
name = "tf-test-instance-template-%{random_suffix}"
34103435
machine_type = "e2-medium"
@@ -3429,3 +3454,48 @@ resource "google_compute_instance_template" "template" {
34293454
}
34303455
`, context)
34313456
}
3457+
3458+
func testAccComputeInstanceTemplate_resourceManagerTags(context map[string]interface{}) string {
3459+
return acctest.Nprintf(`
3460+
resource "google_tags_tag_key" "key" {
3461+
parent = "projects/%{project}"
3462+
short_name = "foobarbaz%{random_suffix}"
3463+
description = "For foo/bar resources."
3464+
}
3465+
3466+
resource "google_tags_tag_value" "value" {
3467+
parent = "tagKeys/${google_tags_tag_key.key.name}"
3468+
short_name = "foo%{random_suffix}"
3469+
description = "For foo resources."
3470+
}
3471+
3472+
data "google_compute_image" "my_image" {
3473+
family = "debian-11"
3474+
project = "debian-cloud"
3475+
}
3476+
3477+
resource "google_compute_instance_template" "foobar" {
3478+
name = "%{instance_name}"
3479+
machine_type = "e2-medium"
3480+
3481+
disk {
3482+
source_image = data.google_compute_image.my_image.self_link
3483+
auto_delete = true
3484+
disk_size_gb = 10
3485+
boot = true
3486+
3487+
resource_manager_tags = {
3488+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3489+
}
3490+
}
3491+
3492+
resource_manager_tags = {
3493+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3494+
}
3495+
3496+
network_interface {
3497+
network = "default"
3498+
}
3499+
}
3500+
`, context)
3501+
}

google/services/compute/resource_compute_region_instance_template.go

+23
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,
@@ -565,6 +574,16 @@ Google Cloud KMS.`,
565574
Description: `The ID of the project in which the resource belongs. If it is not provided, the provider project is used.`,
566575
},
567576

577+
"resource_manager_tags": {
578+
Type: schema.TypeMap,
579+
Optional: true,
580+
ForceNew: false,
581+
Elem: &schema.Schema{Type: schema.TypeString},
582+
Set: schema.HashString,
583+
Description: `A map of resource manager tags.
584+
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.`,
585+
},
586+
568587
"scheduling": {
569588
Type: schema.TypeList,
570589
Optional: true,
@@ -1002,6 +1021,10 @@ func resourceComputeRegionInstanceTemplateCreate(d *schema.ResourceData, meta in
10021021
instanceProperties.Labels = tpgresource.ExpandEffectiveLabels(d)
10031022
}
10041023

1024+
if _, ok := d.GetOk("resource_manager_tags"); ok {
1025+
instanceProperties.ResourceManagerTags = tpgresource.ExpandStringMap(d, "resource_manager_tags")
1026+
}
1027+
10051028
var itName string
10061029
if v, ok := d.GetOk("name"); ok {
10071030
itName = v.(string)

google/services/compute/resource_compute_region_instance_template_test.go

+72
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,32 @@ func TestAccComputeRegionInstanceTemplate_sourceImageEncryptionKey(t *testing.T)
10431043
})
10441044
}
10451045

1046+
func TestAccComputeRegionInstanceTemplate_resourceManagerTags(t *testing.T) {
1047+
t.Parallel()
1048+
1049+
var instanceTemplate compute.InstanceTemplate
1050+
var instanceTemplateName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
1051+
context := map[string]interface{}{
1052+
"project": envvar.GetTestProjectFromEnv(),
1053+
"random_suffix": acctest.RandString(t, 10),
1054+
"instance_name": instanceTemplateName,
1055+
}
1056+
1057+
acctest.VcrTest(t, resource.TestCase{
1058+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1059+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1060+
CheckDestroy: testAccCheckComputeRegionInstanceTemplateDestroyProducer(t),
1061+
Steps: []resource.TestStep{
1062+
{
1063+
Config: testAccComputeRegionInstanceTemplate_resourceManagerTags(context),
1064+
Check: resource.ComposeTestCheckFunc(
1065+
testAccCheckComputeRegionInstanceTemplateExists(
1066+
t, "google_compute_region_instance_template.foobar", &instanceTemplate)),
1067+
},
1068+
},
1069+
})
1070+
}
1071+
10461072
func testAccCheckComputeRegionInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error {
10471073
return func(s *terraform.State) error {
10481074
config := acctest.GoogleProviderConfig(t)
@@ -3146,3 +3172,49 @@ resource "google_compute_region_instance_template" "template" {
31463172
}
31473173
`, context)
31483174
}
3175+
3176+
func testAccComputeRegionInstanceTemplate_resourceManagerTags(context map[string]interface{}) string {
3177+
return acctest.Nprintf(`
3178+
resource "google_tags_tag_key" "key" {
3179+
parent = "projects/%{project}"
3180+
short_name = "foobarbaz%{random_suffix}"
3181+
description = "For foo/bar resources."
3182+
}
3183+
3184+
resource "google_tags_tag_value" "value" {
3185+
parent = "tagKeys/${google_tags_tag_key.key.name}"
3186+
short_name = "foo%{random_suffix}"
3187+
description = "For foo resources."
3188+
}
3189+
3190+
data "google_compute_image" "my_image" {
3191+
family = "debian-11"
3192+
project = "debian-cloud"
3193+
}
3194+
3195+
resource "google_compute_region_instance_template" "foobar" {
3196+
name = "%{instance_name}"
3197+
machine_type = "e2-medium"
3198+
region = "us-central1"
3199+
3200+
disk {
3201+
source_image = data.google_compute_image.my_image.self_link
3202+
auto_delete = true
3203+
disk_size_gb = 10
3204+
boot = true
3205+
3206+
resource_manager_tags = {
3207+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3208+
}
3209+
}
3210+
3211+
resource_manager_tags = {
3212+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3213+
}
3214+
3215+
network_interface {
3216+
network = "default"
3217+
}
3218+
}
3219+
`, context)
3220+
}

website/docs/r/compute_instance_template.html.markdown

+4
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

+4
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)