Skip to content

Commit acaf94a

Browse files
modular-magicianrileykarsonzqc764487
authored
Add Params and resourceManagerTags for instance and disks (#8107) (#14924)
Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Riley Karson <[email protected]> Co-authored-by: QichangZ <[email protected]>
1 parent 7ed1e63 commit acaf94a

File tree

4 files changed

+142
-4
lines changed

4 files changed

+142
-4
lines changed

.changelog/8107.txt

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

google/resource_compute_instance_test.go

+79-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func computeInstanceImportStep(zone, instanceName string, additionalImportIgnore
8484
// metadata is only read into state if set in the config
8585
// importing doesn't know whether metadata.startup_script vs metadata_startup_script is set in the config,
8686
// it always takes metadata.startup-script
87-
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script"}
87+
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script", "boot_disk.0.initialize_params.0.resource_manager_tags.%", "params.0.resource_manager_tags.%"}
8888

8989
return resource.TestStep{
9090
ResourceName: "google_compute_instance.foobar",
@@ -230,6 +230,32 @@ func TestAccComputeInstance_basic5(t *testing.T) {
230230
})
231231
}
232232

233+
func TestAccComputeInstance_resourceManagerTags(t *testing.T) {
234+
t.Parallel()
235+
236+
var instance compute.Instance
237+
var instanceName = fmt.Sprintf("tf-test-%s", RandString(t, 10))
238+
context := map[string]interface{}{
239+
"project": acctest.GetTestProjectFromEnv(),
240+
"random_suffix": RandString(t, 10),
241+
"instance_name": instanceName,
242+
}
243+
244+
VcrTest(t, resource.TestCase{
245+
PreCheck: func() { acctest.AccTestPreCheck(t) },
246+
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
247+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
248+
Steps: []resource.TestStep{
249+
{
250+
Config: testAccComputeInstance_resourceManagerTags(context),
251+
Check: resource.ComposeTestCheckFunc(
252+
testAccCheckComputeInstanceExists(
253+
t, "google_compute_instance.foobar", &instance)),
254+
},
255+
},
256+
})
257+
}
258+
233259
func TestAccComputeInstance_IP(t *testing.T) {
234260
t.Parallel()
235261

@@ -3192,6 +3218,58 @@ resource "google_compute_instance" "foobar" {
31923218
`, instance)
31933219
}
31943220

3221+
func testAccComputeInstance_resourceManagerTags(context map[string]interface{}) string {
3222+
return Nprintf(`
3223+
resource "google_tags_tag_key" "key" {
3224+
parent = "projects/%{project}"
3225+
short_name = "foobarbaz%{random_suffix}"
3226+
description = "For foo/bar resources."
3227+
}
3228+
3229+
resource "google_tags_tag_value" "value" {
3230+
parent = "tagKeys/${google_tags_tag_key.key.name}"
3231+
short_name = "foo%{random_suffix}"
3232+
description = "For foo resources."
3233+
}
3234+
3235+
data "google_compute_image" "my_image" {
3236+
family = "debian-11"
3237+
project = "debian-cloud"
3238+
}
3239+
3240+
resource "google_compute_instance" "foobar" {
3241+
name = "%{instance_name}"
3242+
machine_type = "e2-medium"
3243+
zone = "us-central1-a"
3244+
can_ip_forward = false
3245+
tags = ["tag-key", "tag-value"]
3246+
3247+
boot_disk {
3248+
initialize_params {
3249+
image = data.google_compute_image.my_image.self_link
3250+
resource_manager_tags = {
3251+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3252+
}
3253+
}
3254+
}
3255+
3256+
params {
3257+
resource_manager_tags = {
3258+
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
3259+
}
3260+
}
3261+
3262+
network_interface {
3263+
network = "default"
3264+
}
3265+
3266+
metadata = {
3267+
foo = "bar"
3268+
}
3269+
}
3270+
`, context)
3271+
}
3272+
31953273
func testAccComputeInstance_basic_deletionProtectionFalse(instance string) string {
31963274
return fmt.Sprintf(`
31973275
data "google_compute_image" "my_image" {

google/services/compute/resource_compute_instance.go

+52-3
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ var (
4242
"boot_disk.0.initialize_params.0.type",
4343
"boot_disk.0.initialize_params.0.image",
4444
"boot_disk.0.initialize_params.0.labels",
45+
"boot_disk.0.initialize_params.0.resource_manager_tags",
4546
}
4647

4748
schedulingKeys = []string{
@@ -216,6 +217,14 @@ func ResourceComputeInstance() *schema.Resource {
216217
ForceNew: true,
217218
Description: `A set of key/value label pairs assigned to the disk.`,
218219
},
220+
221+
"resource_manager_tags": {
222+
Type: schema.TypeMap,
223+
Optional: true,
224+
AtLeastOneOf: initializeParamsKeys,
225+
ForceNew: true,
226+
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.`,
227+
},
219228
},
220229
},
221230
},
@@ -538,6 +547,25 @@ func ResourceComputeInstance() *schema.Resource {
538547
},
539548
},
540549

550+
"params": {
551+
Type: schema.TypeList,
552+
MaxItems: 1,
553+
Optional: true,
554+
ForceNew: true,
555+
Description: `Stores additional params passed with the request, but not persisted as part of resource payload.`,
556+
Elem: &schema.Resource{
557+
Schema: map[string]*schema.Schema{
558+
"resource_manager_tags": {
559+
Type: schema.TypeMap,
560+
Optional: true,
561+
// 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.
562+
ForceNew: true,
563+
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.`,
564+
},
565+
},
566+
},
567+
},
568+
541569
"labels": {
542570
Type: schema.TypeMap,
543571
Optional: true,
@@ -1013,6 +1041,11 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *trans
10131041
return nil, fmt.Errorf("Error creating scheduling: %s", err)
10141042
}
10151043

1044+
params, err := expandParams(d)
1045+
if err != nil {
1046+
return nil, fmt.Errorf("Error creating params: %s", err)
1047+
}
1048+
10161049
metadata, err := resourceInstanceMetadata(d)
10171050
if err != nil {
10181051
return nil, fmt.Errorf("Error creating metadata: %s", err)
@@ -1047,6 +1080,7 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *trans
10471080
NetworkInterfaces: networkInterfaces,
10481081
NetworkPerformanceConfig: networkPerformanceConfig,
10491082
Tags: resourceInstanceTags(d),
1083+
Params: params,
10501084
Labels: tpgresource.ExpandLabels(d),
10511085
ServiceAccounts: expandServiceAccounts(d.Get("service_account").([]interface{})),
10521086
GuestAccelerators: accels,
@@ -2361,6 +2395,16 @@ func resourceComputeInstanceImportState(d *schema.ResourceData, meta interface{}
23612395
return []*schema.ResourceData{d}, nil
23622396
}
23632397

2398+
func expandParams(d *schema.ResourceData) (*compute.InstanceParams, error) {
2399+
params := &compute.InstanceParams{}
2400+
2401+
if _, ok := d.GetOk("params.0.resource_manager_tags"); ok {
2402+
params.ResourceManagerTags = tpgresource.ExpandStringMap(d, "params.0.resource_manager_tags")
2403+
}
2404+
2405+
return params, nil
2406+
}
2407+
23642408
func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, project string) (*compute.AttachedDisk, error) {
23652409
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
23662410
if err != nil {
@@ -2429,6 +2473,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
24292473
if _, ok := d.GetOk("boot_disk.0.initialize_params.0.labels"); ok {
24302474
disk.InitializeParams.Labels = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.labels")
24312475
}
2476+
2477+
if _, ok := d.GetOk("boot_disk.0.initialize_params.0.resource_manager_tags"); ok {
2478+
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.resource_manager_tags")
2479+
}
24322480
}
24332481

24342482
if v, ok := d.GetOk("boot_disk.0.mode"); ok {
@@ -2464,9 +2512,10 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
24642512
"type": tpgresource.GetResourceNameFromSelfLink(diskDetails.Type),
24652513
// If the config specifies a family name that doesn't match the image name, then
24662514
// the diff won't be properly suppressed. See DiffSuppressFunc for this field.
2467-
"image": diskDetails.SourceImage,
2468-
"size": diskDetails.SizeGb,
2469-
"labels": diskDetails.Labels,
2515+
"image": diskDetails.SourceImage,
2516+
"size": diskDetails.SizeGb,
2517+
"labels": diskDetails.Labels,
2518+
"resource_manager_tags": d.Get("boot_disk.0.initialize_params.0.resource_manager_tags"),
24702519
}}
24712520
}
24722521

website/docs/r/compute_instance.html.markdown

+8
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ is desired, you will need to modify your state file manually using
148148
`Intel Haswell` or `Intel Skylake`. See the complete list [here](https://cloud.google.com/compute/docs/instances/specify-min-cpu-platform).
149149
**Note**: [`allow_stopping_for_update`](#allow_stopping_for_update) must be set to true or your instance must have a `desired_status` of `TERMINATED` in order to update this field.
150150

151+
* `params` - (Optional) Additional instance parameters.
152+
.
151153
* `project` - (Optional) The ID of the project in which the resource belongs. If it
152154
is not provided, the provider project is used.
153155

@@ -237,6 +239,8 @@ is desired, you will need to modify your state file manually using
237239
* `labels` - (Optional) A set of key/value label pairs assigned to the disk. This
238240
field is only applicable for persistent disks.
239241

242+
* `resource_manager_tags` - (Optional) A tag is a key-value pair that can be attached to a Google Cloud resource. You can use tags to conditionally allow or deny policies based on whether a resource has a specific tag.
243+
240244
<a name="nested_scratch_disk"></a>The `scratch_disk` block supports:
241245

242246
* `interface` - (Required) The disk interface to use for attaching this disk; either SCSI or NVME.
@@ -411,6 +415,10 @@ specified, then this instance will have no external IPv6 Internet access. Struct
411415

412416
* `values` (Required) - The values for the node affinity label.
413417

418+
<a name="nested_params"></a>The `params` block supports:
419+
420+
* `resource_manager_tags` (Optional) - A tag is a key-value pair that can be attached to a Google Cloud resource. You can use tags to conditionally allow or deny policies based on whether a resource has a specific tag.
421+
414422
<a name="nested_shielded_instance_config"></a>The `shielded_instance_config` block supports:
415423

416424
* `enable_secure_boot` (Optional) -- Verify the digital signature of all boot components, and halt the boot process if signature verification fails. Defaults to false.

0 commit comments

Comments
 (0)