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 disk resource policies field to the compute instance #8134

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/11527.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: added support for `boot_disk.initialize_params.resource_policies` in `google_compute_instance` and `google_instance_template`
```
17 changes: 17 additions & 0 deletions google-beta/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var (
"boot_disk.0.initialize_params.0.provisioned_throughput",
"boot_disk.0.initialize_params.0.enable_confidential_compute",
"boot_disk.0.initialize_params.0.storage_pool",
"boot_disk.0.initialize_params.0.resource_policies",
}

schedulingKeys = []string{
Expand Down Expand Up @@ -305,6 +306,17 @@ func ResourceComputeInstance() *schema.Resource {
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.`,
},

"resource_policies": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
ForceNew: true,
AtLeastOneOf: initializeParamsKeys,
DiffSuppressFunc: tpgresource.CompareSelfLinkRelativePaths,
MaxItems: 1,
Description: `A list of self_links of resource policies to attach to the instance's boot disk. Modifying this list will cause the instance to recreate. Currently a max of 1 resource policy is supported.`,
},

"provisioned_iops": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -3010,6 +3022,10 @@ func expandBootDisk(d *schema.ResourceData, config *transport_tpg.Config, projec
disk.InitializeParams.ResourceManagerTags = tpgresource.ExpandStringMap(d, "boot_disk.0.initialize_params.0.resource_manager_tags")
}

if _, ok := d.GetOk("boot_disk.0.initialize_params.0.resource_policies"); ok {
disk.InitializeParams.ResourcePolicies = tpgresource.ConvertStringArr(d.Get("boot_disk.0.initialize_params.0.resource_policies").([]interface{}))
}

if v, ok := d.GetOk("boot_disk.0.initialize_params.0.storage_pool"); ok {
disk.InitializeParams.StoragePool = v.(string)
}
Expand Down Expand Up @@ -3055,6 +3071,7 @@ func flattenBootDisk(d *schema.ResourceData, disk *compute.AttachedDisk, config
"size": diskDetails.SizeGb,
"labels": diskDetails.Labels,
"resource_manager_tags": d.Get("boot_disk.0.initialize_params.0.resource_manager_tags"),
"resource_policies": diskDetails.ResourcePolicies,
"provisioned_iops": diskDetails.ProvisionedIops,
"provisioned_throughput": diskDetails.ProvisionedThroughput,
"enable_confidential_compute": diskDetails.EnableConfidentialCompute,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ func adjustInstanceFromTemplateDisks(d *schema.ResourceData, config *transport_t
// only have the name (since they're global).
disk.InitializeParams.DiskType = fmt.Sprintf("zones/%s/diskTypes/%s", zone.Name, dt)
}
if rp := disk.InitializeParams.ResourcePolicies; len(rp) > 0 {
// Instances need a URL for the resource policy, but instance templates
// only have the name (since they're global).
for i := range rp {
rp[i], _ = parseUniqueId(rp[i]) // in some cases the API translation doesn't work and returns entire url when only name is provided. And allows for id to be passed as well
rp[i] = fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, regionFromUrl(zone.Region), rp[i])
}
disk.InitializeParams.ResourcePolicies = rp
}
}
disks = append(disks, disk)
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,38 @@ func TestAccComputeInstanceFromTemplateWithOverride_localSsdRecoveryTimeout(t *t
})
}

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

var instance compute.Instance
templateName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
suffix := acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceFromTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceFromTemplate_diskResourcePoliciesCreate(suffix, templateName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(t, "google_compute_instance_from_template.foobar", &instance),
),
},
{
Config: testAccComputeInstanceFromTemplate_diskResourcePoliciesUpdate(suffix, templateName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(t, "google_compute_instance_from_template.foobar", &instance),
),
},
{
Config: testAccComputeInstanceFromTemplate_diskResourcePoliciesTwoPolicies(suffix, templateName),
ExpectError: regexp.MustCompile("Too many list items"),
},
},
})
}

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

Expand Down Expand Up @@ -1834,3 +1866,159 @@ resource "google_compute_instance_from_template" "foobar" {
}
`, template, instance, template, instance)
}

func testAccComputeInstanceFromTemplate_diskResourcePoliciesCreate(suffix, template string) string {
return fmt.Sprintf(`
resource "google_compute_resource_policy" "test-snapshot-policy" {
name = "test-policy-%s"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 1
start_time = "11:00"
}
}
}
}

resource "google_compute_resource_policy" "test-snapshot-policy2" {
name = "test-policy2-%s"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 1
start_time = "22:00"
}
}
}
}

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

resource "google_compute_region_instance_template" "foobar" {
name = "%s"
region = "us-central1"
machine_type = "n1-standard-1"
disk {
resource_policies = [ google_compute_resource_policy.test-snapshot-policy.name ]
source_image = data.google_compute_image.my_image.self_link
}
network_interface {
network = "default"
}
}

resource "google_compute_instance_from_template" "foobar" {
name = "%s"
zone = "us-central1-a"
source_instance_template = google_compute_region_instance_template.foobar.id
}
`, suffix, suffix, template, template)
}

func testAccComputeInstanceFromTemplate_diskResourcePoliciesUpdate(suffix, template string) string {
return fmt.Sprintf(`
resource "google_compute_resource_policy" "test-snapshot-policy" {
name = "test-policy-%s"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 1
start_time = "11:00"
}
}
}
}

resource "google_compute_resource_policy" "test-snapshot-policy2" {
name = "test-policy2-%s"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 1
start_time = "22:00"
}
}
}
}

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

resource "google_compute_region_instance_template" "foobar" {
name = "%s"
region = "us-central1"
machine_type = "n1-standard-1"
disk {
resource_policies = [ google_compute_resource_policy.test-snapshot-policy2.name ]
source_image = data.google_compute_image.my_image.self_link
}
network_interface {
network = "default"
}
}

resource "google_compute_instance_from_template" "foobar" {
name = "%s"
zone = "us-central1-a"
source_instance_template = google_compute_region_instance_template.foobar.id
}
`, suffix, suffix, template, template)
}

func testAccComputeInstanceFromTemplate_diskResourcePoliciesTwoPolicies(suffix, template string) string {
return fmt.Sprintf(`
resource "google_compute_resource_policy" "test-snapshot-policy" {
name = "test-policy-%s"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 1
start_time = "11:00"
}
}
}
}

resource "google_compute_resource_policy" "test-snapshot-policy2" {
name = "test-policy2-%s"
snapshot_schedule_policy {
schedule {
hourly_schedule {
hours_in_cycle = 1
start_time = "22:00"
}
}
}
}

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

resource "google_compute_region_instance_template" "foobar" {
name = "%s"
region = "us-central1"
machine_type = "n1-standard-1"
disk {
resource_policies = [ google_compute_resource_policy.test-snapshot-policy.name, google_compute_resource_policy.test-snapshot-policy2.name ]
source_image = data.google_compute_image.my_image.self_link
}
network_interface {
network = "default"
}
}

resource "google_compute_instance_from_template" "foobar" {
name = "%s"
zone = "us-central1-a"
source_instance_template = google_compute_region_instance_template.foobar.id
}
`, suffix, suffix, template, template)
}
Loading