diff --git a/google-beta/resource_compute_disk_resource_policy_attachment.go b/google-beta/resource_compute_disk_resource_policy_attachment.go index 0cbb81862f..e3a13f5a92 100644 --- a/google-beta/resource_compute_disk_resource_policy_attachment.go +++ b/google-beta/resource_compute_disk_resource_policy_attachment.go @@ -192,8 +192,21 @@ func resourceComputeDiskResourcePolicyAttachmentDelete(d *schema.ResourceData, m var obj map[string]interface{} obj = make(map[string]interface{}) - // projects/{project}/regions/{region}/resourcePolicies/{resourceId} - region := getRegionFromZone(d.Get("zone").(string)) + zone, err := getZone(d, config) + if err != nil { + return err + } + if zone == "" { + return fmt.Errorf("zone must be non-empty - set in resource or at provider-level") + } + + // resourcePolicies are referred to by region but affixed to zonal disks. + // We construct the regional name from the zone: + // projects/{project}/regions/{region}/resourcePolicies/{resourceId} + region := getRegionFromZone(zone) + if region == "" { + return fmt.Errorf("invalid zone %q, unable to infer region from zone", zone) + } name, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config) if err != nil { @@ -256,7 +269,21 @@ func resourceComputeDiskResourcePolicyAttachmentEncoder(d *schema.ResourceData, return nil, err } - region := getRegionFromZone(d.Get("zone").(string)) + zone, err := getZone(d, config) + if err != nil { + return nil, err + } + if zone == "" { + return nil, fmt.Errorf("zone must be non-empty - set in resource or at provider-level") + } + + // resourcePolicies are referred to by region but affixed to zonal disks. + // We construct the regional name from the zone: + // projects/{project}/regions/{region}/resourcePolicies/{resourceId} + region := getRegionFromZone(zone) + if region == "" { + return nil, fmt.Errorf("invalid zone %q, unable to infer region from zone", zone) + } obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, obj["name"])} delete(obj, "name") diff --git a/google-beta/resource_compute_disk_resource_policy_attachment_generated_test.go b/google-beta/resource_compute_disk_resource_policy_attachment_generated_test.go new file mode 100644 index 0000000000..ccf501e9a3 --- /dev/null +++ b/google-beta/resource_compute_disk_resource_policy_attachment_generated_test.go @@ -0,0 +1,111 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccComputeDiskResourcePolicyAttachment_diskResourcePolicyAttachmentBasicExample(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": acctest.RandString(10), + } + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeDiskResourcePolicyAttachmentDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeDiskResourcePolicyAttachment_diskResourcePolicyAttachmentBasicExample(context), + }, + { + ResourceName: "google_compute_disk_resource_policy_attachment.attachment", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"disk", "zone"}, + }, + }, + }) +} + +func testAccComputeDiskResourcePolicyAttachment_diskResourcePolicyAttachmentBasicExample(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk_resource_policy_attachment" "attachment" { + name = google_compute_resource_policy.policy.name + disk = google_compute_disk.ssd.name + zone = "us-central1-a" +} + +resource "google_compute_disk" "ssd" { + name = "my-disk%{random_suffix}" + image = data.google_compute_image.my_image.self_link + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_resource_policy" "policy" { + name = "my-resource-policy%{random_suffix}" + region = "us-central1" + snapshot_schedule_policy { + schedule { + daily_schedule { + days_in_cycle = 1 + start_time = "04:00" + } + } + } +} + +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} +`, context) +} + +func testAccCheckComputeDiskResourcePolicyAttachmentDestroy(s *terraform.State) error { + for name, rs := range s.RootModule().Resources { + if rs.Type != "google_compute_disk_resource_policy_attachment" { + continue + } + if strings.HasPrefix(name, "data.") { + continue + } + + config := testAccProvider.Meta().(*Config) + + url, err := replaceVarsForTest(config, rs, "{{ComputeBasePath}}projects/{{project}}/zones/{{zone}}/disks/{{disk}}") + if err != nil { + return err + } + + _, err = sendRequest(config, "GET", "", url, nil) + if err == nil { + return fmt.Errorf("ComputeDiskResourcePolicyAttachment still exists at %s", url) + } + } + + return nil +} diff --git a/google-beta/resource_compute_disk_resource_policy_attachment_test.go b/google-beta/resource_compute_disk_resource_policy_attachment_test.go index cb1ddda195..8ab3a2c896 100644 --- a/google-beta/resource_compute_disk_resource_policy_attachment_test.go +++ b/google-beta/resource_compute_disk_resource_policy_attachment_test.go @@ -8,7 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" ) -func TestAccComputeDiskResourcePolicyAttachment_basic(t *testing.T) { +func TestAccComputeDiskResourcePolicyAttachment_update(t *testing.T) { t.Parallel() diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) diff --git a/website/docs/r/compute_disk_resource_policy_attachment.html.markdown b/website/docs/r/compute_disk_resource_policy_attachment.html.markdown index ea29b68a9f..a22eeea31d 100644 --- a/website/docs/r/compute_disk_resource_policy_attachment.html.markdown +++ b/website/docs/r/compute_disk_resource_policy_attachment.html.markdown @@ -28,6 +28,48 @@ retention period for these snapshots. +
+## Example Usage - Disk Resource Policy Attachment Basic + + +```hcl +resource "google_compute_disk_resource_policy_attachment" "attachment" { + name = google_compute_resource_policy.policy.name + disk = google_compute_disk.ssd.name + zone = "us-central1-a" +} + +resource "google_compute_disk" "ssd" { + name = "my-disk" + image = data.google_compute_image.my_image.self_link + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_resource_policy" "policy" { + name = "my-resource-policy" + region = "us-central1" + snapshot_schedule_policy { + schedule { + daily_schedule { + days_in_cycle = 1 + start_time = "04:00" + } + } + } +} + +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} +``` + ## Argument Reference The following arguments are supported: