From e538b0110084e638c72d00926bd3347637f36b5f Mon Sep 17 00:00:00 2001 From: Ray Ward Date: Mon, 18 Jan 2021 10:52:18 +1100 Subject: [PATCH] Add support for configuring disk resource policies for google_compute_instance_template. Fixes #6719 --- google/resource_compute_instance_template.go | 19 ++++ ...resource_compute_instance_template_test.go | 87 ++++++++++++++++++- 2 files changed, 104 insertions(+), 2 deletions(-) diff --git a/google/resource_compute_instance_template.go b/google/resource_compute_instance_template.go index 1c39c14358c..753a21b2a90 100644 --- a/google/resource_compute_instance_template.go +++ b/google/resource_compute_instance_template.go @@ -208,6 +208,18 @@ func resourceComputeInstanceTemplate() *schema.Resource { }, }, }, + + "resource_policies": { + Type: schema.TypeList, + Optional: true, + ForceNew: true, + MaxItems: 1, + Description: `A list of short names or self_links of resource policies to attach to this disk. Currently a max of 1 resource policy is supported.`, + Elem: &schema.Schema{ + Type: schema.TypeString, + DiffSuppressFunc: compareResourceNames, + }, + }, }, }, }, @@ -757,6 +769,11 @@ func buildDisks(d *schema.ResourceData, config *Config) ([]*computeBeta.Attached } disk.InitializeParams.Labels = expandStringMap(d, prefix+".labels") + + if _, ok := d.GetOk(prefix + ".resource_policies"); ok { + // instance template only supports a resource name here (not uri) + disk.InitializeParams.ResourcePolicies = convertAndMapStringArr(d.Get(prefix+".resource_policies").([]interface{}), GetResourceNameFromSelfLink) + } } if v, ok := d.GetOk(prefix + ".interface"); ok { @@ -951,6 +968,8 @@ func flattenDisk(disk *computeBeta.AttachedDisk, defaultProject string) (map[str } else { diskMap["disk_size_gb"] = disk.InitializeParams.DiskSizeGb } + + diskMap["resource_policies"] = disk.InitializeParams.ResourcePolicies } if disk.DiskEncryptionKey != nil { diff --git a/google/resource_compute_instance_template_test.go b/google/resource_compute_instance_template_test.go index df28d1cd89e..389a741e361 100644 --- a/google/resource_compute_instance_template_test.go +++ b/google/resource_compute_instance_template_test.go @@ -898,6 +898,33 @@ func TestAccComputeInstanceTemplate_imageResourceTest(t *testing.T) { }) } +func TestAccComputeInstanceTemplate_resourcePolicies(t *testing.T) { + t.Parallel() + + var instanceTemplate computeBeta.InstanceTemplate + policyName := "tf-test-policy-" + randString(t, 10) + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceTemplate_resourcePolicies(randString(t, 10), policyName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceTemplateExists(t, "google_compute_instance_template.foobar", &instanceTemplate), + testAccCheckComputeInstanceTemplateHasDiskResourcePolicy(&instanceTemplate, policyName), + ), + }, + { + ResourceName: "google_compute_instance_template.foobar", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func testAccCheckComputeInstanceTemplateDestroyProducer(t *testing.T) func(s *terraform.State) error { return func(s *terraform.State) error { config := googleProviderConfig(t) @@ -1247,6 +1274,17 @@ func testAccCheckComputeInstanceTemplateLacksShieldedVmConfig(instanceTemplate * } } +func testAccCheckComputeInstanceTemplateHasDiskResourcePolicy(instanceTemplate *computeBeta.InstanceTemplate, resourcePolicy string) resource.TestCheckFunc { + return func(s *terraform.State) error { + resourcePolicyActual := instanceTemplate.Properties.Disks[0].InitializeParams.ResourcePolicies[0] + if resourcePolicyActual != resourcePolicy { + return fmt.Errorf("Wrong disk resource policy: expected %s, got %s", resourcePolicy, resourcePolicyActual) + } + + return nil + } +} + func testAccComputeInstanceTemplate_basic(suffix string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { @@ -2200,7 +2238,7 @@ data "google_compute_image" "my_image" { family = "debian-9" project = "debian-cloud" } - + resource "google_compute_disk" "my_disk" { name = "%s" zone = "us-central1-a" @@ -2222,6 +2260,51 @@ resource "google_compute_instance_template" "foobar" { access_config {} } } - + `, diskName, imageName, imageDescription) } + +func testAccComputeInstanceTemplate_resourcePolicies(suffix string, policyName string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_instance_template" "foobar" { + name = "instance-test-%s" + machine_type = "e2-medium" + can_ip_forward = false + + disk { + source_image = data.google_compute_image.my_image.self_link + resource_policies = [google_compute_resource_policy.foo.self_link] + } + + network_interface { + network = "default" + } + + service_account { + scopes = ["userinfo-email", "compute-ro", "storage-ro"] + } + + labels = { + my_label = "foobar" + } +} + +resource "google_compute_resource_policy" "foo" { + name = "%s" + region = "us-central1" + snapshot_schedule_policy { + schedule { + daily_schedule { + days_in_cycle = 1 + start_time = "04:00" + } + } + } +} +`, suffix, policyName) +}