Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Params and resourceManagerTags for instance and disks #14924

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/8107.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added support for `params.resource_manager_tags` and `boot_disk.initialize_params.resource_manager_tags` to `google_compute_instance`
```
80 changes: 79 additions & 1 deletion google/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func computeInstanceImportStep(zone, instanceName string, additionalImportIgnore
// metadata is only read into state if set in the config
// importing doesn't know whether metadata.startup_script vs metadata_startup_script is set in the config,
// it always takes metadata.startup-script
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script"}
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script", "boot_disk.0.initialize_params.0.resource_manager_tags.%", "params.0.resource_manager_tags.%"}

return resource.TestStep{
ResourceName: "google_compute_instance.foobar",
Expand Down Expand Up @@ -230,6 +230,32 @@ func TestAccComputeInstance_basic5(t *testing.T) {
})
}

func TestAccComputeInstance_resourceManagerTags(t *testing.T) {
t.Parallel()

var instance compute.Instance
var instanceName = fmt.Sprintf("tf-test-%s", RandString(t, 10))
context := map[string]interface{}{
"project": acctest.GetTestProjectFromEnv(),
"random_suffix": RandString(t, 10),
"instance_name": instanceName,
}

VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_resourceManagerTags(context),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(
t, "google_compute_instance.foobar", &instance)),
},
},
})
}

func TestAccComputeInstance_IP(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -3192,6 +3218,58 @@ resource "google_compute_instance" "foobar" {
`, instance)
}

func testAccComputeInstance_resourceManagerTags(context map[string]interface{}) string {
return Nprintf(`
resource "google_tags_tag_key" "key" {
parent = "projects/%{project}"
short_name = "foobarbaz%{random_suffix}"
description = "For foo/bar resources."
}

resource "google_tags_tag_value" "value" {
parent = "tagKeys/${google_tags_tag_key.key.name}"
short_name = "foo%{random_suffix}"
description = "For foo resources."
}

data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance" "foobar" {
name = "%{instance_name}"
machine_type = "e2-medium"
zone = "us-central1-a"
can_ip_forward = false
tags = ["tag-key", "tag-value"]

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
resource_manager_tags = {
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
}
}
}

params {
resource_manager_tags = {
"tagKeys/${google_tags_tag_key.key.name}" = "tagValues/${google_tags_tag_value.value.name}"
}
}

network_interface {
network = "default"
}

metadata = {
foo = "bar"
}
}
`, context)
}

func testAccComputeInstance_basic_deletionProtectionFalse(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down
55 changes: 52 additions & 3 deletions google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
"boot_disk.0.initialize_params.0.type",
"boot_disk.0.initialize_params.0.image",
"boot_disk.0.initialize_params.0.labels",
"boot_disk.0.initialize_params.0.resource_manager_tags",
}

schedulingKeys = []string{
Expand Down Expand Up @@ -216,6 +217,14 @@ func ResourceComputeInstance() *schema.Resource {
ForceNew: true,
Description: `A set of key/value label pairs assigned to the disk.`,
},

"resource_manager_tags": {
Type: schema.TypeMap,
Optional: true,
AtLeastOneOf: initializeParamsKeys,
ForceNew: true,
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.`,
},
},
},
},
Expand Down Expand Up @@ -538,6 +547,25 @@ func ResourceComputeInstance() *schema.Resource {
},
},

"params": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
ForceNew: true,
Description: `Stores additional params passed with the request, but not persisted as part of resource payload.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_manager_tags": {
Type: schema.TypeMap,
Optional: true,
// 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.
ForceNew: true,
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.`,
},
},
},
},

"labels": {
Type: schema.TypeMap,
Optional: true,
Expand Down Expand Up @@ -1013,6 +1041,11 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *trans
return nil, fmt.Errorf("Error creating scheduling: %s", err)
}

params, err := expandParams(d)
if err != nil {
return nil, fmt.Errorf("Error creating params: %s", err)
}

metadata, err := resourceInstanceMetadata(d)
if err != nil {
return nil, fmt.Errorf("Error creating metadata: %s", err)
Expand Down Expand Up @@ -1047,6 +1080,7 @@ func expandComputeInstance(project string, d *schema.ResourceData, config *trans
NetworkInterfaces: networkInterfaces,
NetworkPerformanceConfig: networkPerformanceConfig,
Tags: resourceInstanceTags(d),
Params: params,
Labels: tpgresource.ExpandLabels(d),
ServiceAccounts: expandServiceAccounts(d.Get("service_account").([]interface{})),
GuestAccelerators: accels,
Expand Down Expand Up @@ -2361,6 +2395,16 @@ func resourceComputeInstanceImportState(d *schema.ResourceData, meta interface{}
return []*schema.ResourceData{d}, nil
}

func expandParams(d *schema.ResourceData) (*compute.InstanceParams, error) {
params := &compute.InstanceParams{}

if _, ok := d.GetOk("params.0.resource_manager_tags"); ok {
params.ResourceManagerTags = tpgresource.ExpandStringMap(d, "params.0.resource_manager_tags")
}

return params, nil
}

func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, project string) (*compute.AttachedDisk, error) {
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
Expand Down Expand Up @@ -2429,6 +2473,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
if _, ok := d.GetOk("boot_disk.0.initialize_params.0.labels"); ok {
disk.InitializeParams.Labels = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.labels")
}

if _, ok := d.GetOk("boot_disk.0.initialize_params.0.resource_manager_tags"); ok {
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.resource_manager_tags")
}
}

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

Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/compute_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ is desired, you will need to modify your state file manually using
`Intel Haswell` or `Intel Skylake`. See the complete list [here](https://cloud.google.com/compute/docs/instances/specify-min-cpu-platform).
**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.

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

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

* `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.

<a name="nested_scratch_disk"></a>The `scratch_disk` block supports:

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

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

<a name="nested_params"></a>The `params` block supports:

* `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.

<a name="nested_shielded_instance_config"></a>The `shielded_instance_config` block supports:

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