Skip to content

Commit

Permalink
Add support for configuring disk resource policies for google_compute…
Browse files Browse the repository at this point in the history
…_instance_template.

Fixes hashicorp#6719
  • Loading branch information
rayward committed Jan 17, 2021
1 parent d937ce4 commit e538b01
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 2 deletions.
19 changes: 19 additions & 0 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
},
},
},
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
87 changes: 85 additions & 2 deletions google/resource_compute_instance_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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" {
Expand Down Expand Up @@ -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"
Expand All @@ -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)
}

0 comments on commit e538b01

Please sign in to comment.