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
…2883)

Merged PR #2883.
  • Loading branch information
emilymye authored and modular-magician committed Dec 27, 2019
1 parent 0f0df8b commit 6447dd0
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build/terraform
2 changes: 1 addition & 1 deletion build/terraform-beta
2 changes: 1 addition & 1 deletion build/terraform-mapper
7 changes: 7 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,13 @@ overrides: !ruby/object:Overrides::ResourceOverrides
Because the API does not return the sensitive key value,
we cannot confirm or reverse changes to a key outside of Terraform.
DiskResourcePolicyAttachment: !ruby/object:Overrides::Terraform::ResourceOverride
examples:
- !ruby/object:Provider::Terraform::Examples
name: "disk_resource_policy_attachment_basic"
primary_resource_id: "attachment"
vars:
disk_name: "my-disk"
policy_name: "my-resource-policy"
id_format: "{{project}}/{{zone}}/{{disk}}/{{name}}"
custom_code: !ruby/object:Provider::Terraform::CustomCode
encoder: templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@ if err != nil {
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,31 @@
resource "google_compute_disk_resource_policy_attachment" "<%= ctx[:primary_resource_id] %>" {
name = google_compute_resource_policy.policy.name
disk = google_compute_disk.ssd.name
zone = "us-central1-a"
}

resource "google_compute_disk" "ssd" {
name = "<%= ctx[:vars]['disk_name'] %>"
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 = "<%= ctx[:vars]['policy_name'] %>"
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"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
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
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

0 comments on commit 6447dd0

Please sign in to comment.