Skip to content

Commit

Permalink
Add example, fix inferred zone for disk resource policies attachment
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
emilymye authored and modular-magician committed Dec 27, 2019
1 parent cf09a0c commit cafb1d0
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 4 deletions.
33 changes: 30 additions & 3 deletions google/resource_compute_disk_resource_policy_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,48 @@ retention period for these snapshots.



<div class = "oics-button" style="float: right; margin: 0 0 -15px">
<a href="https://console.cloud.google.com/cloudshell/open?cloudshell_git_repo=https%3A%2F%2Fgithub.com%2Fterraform-google-modules%2Fdocs-examples.git&cloudshell_working_dir=disk_resource_policy_attachment_basic&cloudshell_image=gcr.io%2Fgraphite-cloud-shell-images%2Fterraform%3Alatest&open_in_editor=main.tf&cloudshell_print=.%2Fmotd&cloudshell_tutorial=.%2Ftutorial.md" target="_blank">
<img alt="Open in Cloud Shell" src="//gstatic.com/cloudssh/images/open-btn.svg" style="max-height: 44px; margin: 32px auto; max-width: 100%;">
</a>
</div>
## 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:
Expand Down

0 comments on commit cafb1d0

Please sign in to comment.