Skip to content

Commit

Permalink
upstreaming 8237 from tpg (#4463)
Browse files Browse the repository at this point in the history
Co-authored-by: Ray Ward <[email protected]>
  • Loading branch information
ScottSuarez and rayward authored Feb 4, 2021
1 parent 1193c69 commit 47026d4
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,19 @@ func resourceComputeInstanceTemplate() *schema.Resource {
},
},
},


"resource_policies": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Description: `A list (short name or id) 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 @@ -769,6 +782,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 @@ -963,6 +981,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 Expand Up @@ -1121,7 +1141,7 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
if err != nil {
return err
}

project, err := getProject(d, config)
if err != nil {
return err
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ func TestAccComputeInstanceTemplate_ConfidentialInstanceConfigMain(t *testing.T)
testAccCheckComputeInstanceTemplateHasConfidentialInstanceConfig(&instanceTemplate, true),
),
},
},
},
})
}

Expand Down Expand Up @@ -899,6 +899,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 @@ -1248,6 +1275,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 @@ -1617,7 +1655,6 @@ resource "google_compute_instance_template" "foobar" {
`, suffix)
}


func testAccComputeInstanceTemplate_regionDisks(suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down Expand Up @@ -2205,7 +2242,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 @@ -2227,6 +2264,44 @@ 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.id]
}
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ The following arguments are supported:

- `name` - (Optional) The name of the instance template. One of `name` or `filter` must be provided.

- `filter` - (Optional) A filter to retrieve the instance templates.
- `filter` - (Optional) A filter to retrieve the instance templates.
See [gcloud topic filters](https://cloud.google.com/sdk/gcloud/reference/topic/filters) for reference.
If multiple instance templates match, either adjust the filter or specify `most_recent`. One of `name` or `filter` must be provided.

Expand Down Expand Up @@ -137,9 +137,9 @@ The `disk` block supports:
`{project}/{image}`, `{family}`, or `{image}`.
~> **Note:** Either `source` or `source_image` is **required** in a disk block unless the disk type is `local-ssd`. Check the API [docs](https://cloud.google.com/compute/docs/reference/rest/v1/instanceTemplates/insert) for details.

* `interface` - Specifies the disk interface to use for attaching this disk,
which is either SCSI or NVME. The default is SCSI. Persistent disks must always use SCSI
and the request will fail if you attempt to attach a persistent disk in any other format
* `interface` - Specifies the disk interface to use for attaching this disk,
which is either SCSI or NVME. The default is SCSI. Persistent disks must always use SCSI
and the request will fail if you attempt to attach a persistent disk in any other format
than SCSI. Local SSDs can use either NVME or SCSI.

* `mode` - The mode in which to attach this disk, either READ_WRITE
Expand Down Expand Up @@ -170,6 +170,8 @@ The `disk` block supports:

Instance templates do not store customer-supplied encryption keys, so you cannot use your own keys to encrypt disks in a managed instance group.

* `resource_policies` (Optional) -- A list of short names of resource policies to attach to this disk for automatic snapshot creations. Currently a max of 1 resource policy is supported.

The `disk_encryption_key` block supports:

* `kms_key_self_link` - The self link of the encryption key that is stored in Google Cloud KMS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ resource "google_compute_instance_template" "default" {
// Create a new boot disk from an image
disk {
source_image = "debian-cloud/debian-9"
auto_delete = true
boot = true
source_image = "debian-cloud/debian-9"
auto_delete = true
boot = true
// backup the disk every day
resource_policies = [google_compute_resource_policy.daily_backup.id]
}
// Use an existing disk resource
Expand Down Expand Up @@ -84,6 +86,19 @@ resource "google_compute_disk" "foobar" {
type = "pd-ssd"
zone = "us-central1-a"
}
resource "google_compute_resource_policy" "daily_backup" {
name = "every-day-4am"
region = "us-central1"
snapshot_schedule_policy {
schedule {
daily_schedule {
days_in_cycle = 1
start_time = "04:00"
}
}
}
}
```

## Using with Instance Group Manager
Expand Down Expand Up @@ -278,9 +293,9 @@ The `disk` block supports:
`{project}/{image}`, `{family}`, or `{image}`.
~> **Note:** Either `source` or `source_image` is **required** in a disk block unless the disk type is `local-ssd`. Check the API [docs](https://cloud.google.com/compute/docs/reference/rest/v1/instanceTemplates/insert) for details.

* `interface` - (Optional) Specifies the disk interface to use for attaching this disk,
which is either SCSI or NVME. The default is SCSI. Persistent disks must always use SCSI
and the request will fail if you attempt to attach a persistent disk in any other format
* `interface` - (Optional) Specifies the disk interface to use for attaching this disk,
which is either SCSI or NVME. The default is SCSI. Persistent disks must always use SCSI
and the request will fail if you attempt to attach a persistent disk in any other format
than SCSI. Local SSDs can use either NVME or SCSI.

* `mode` - (Optional) The mode in which to attach this disk, either READ_WRITE
Expand Down Expand Up @@ -311,6 +326,8 @@ The `disk` block supports:

Instance templates do not store customer-supplied encryption keys, so you cannot use your own keys to encrypt disks in a managed instance group.

* `resource_policies` (Optional) -- A list (short name or id) of resource policies to attach to this disk for automatic snapshot creations. Currently a max of 1 resource policy is supported.

The `disk_encryption_key` block supports:

* `kms_key_self_link` - (Required) The self link of the encryption key that is stored in Google Cloud KMS
Expand Down Expand Up @@ -458,4 +475,4 @@ $ terraform import google_compute_instance_template.default {{name}}
```

[custom-vm-types]: https://cloud.google.com/dataproc/docs/concepts/compute/custom-machine-types
[network-tier]: https://cloud.google.com/network-tiers/docs/overview
[network-tier]: https://cloud.google.com/network-tiers/docs/overview

0 comments on commit 47026d4

Please sign in to comment.