-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow updating of resource policies in compute_disk (#2228)
* Allow updating of resource policies in compute_disk Signed-off-by: Chris Sng <[email protected]> * Address add and removeResourcePolicies API via an attachment resource instead - custom pre_delete code to include resourcePolicies in POST body * Apply suggestions from code review Co-Authored-By: Riley Karson <[email protected]> * Fixes based on suggestions from PR review * exclude inspec and ansible. change resource name to reference singular * Remove unused code * Add resource policy attachment test * Use custom checker * Exclude resource policy for ansible and inspec * Update products/compute/ansible.yaml Co-Authored-By: Riley Karson <[email protected]> * make imports work * exclude resource policy in ansible overrides also * fix importing of tf resource * resource policy example use google provider, default zone * fix async operation timeout
- Loading branch information
1 parent
c84606e
commit d8cc4e5
Showing
10 changed files
with
190 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
res["name"] = GetResourceNameFromSelfLink(res["name"].(string)) | ||
return res, nil |
11 changes: 11 additions & 0 deletions
11
templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
config := meta.(*Config) | ||
project, err := getProject(d, config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
region := getRegionFromZone(d.Get("zone").(string)) | ||
|
||
obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, obj["name"])} | ||
delete(obj, "name") | ||
return obj, nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
obj = make(map[string]interface{}) | ||
|
||
// projects/{project}/regions/{region}/resourcePolicies/{resourceId} | ||
region := getRegionFromZone(d.Get("zone").(string)) | ||
|
||
name, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config) | ||
if err != nil { | ||
return err | ||
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(name)) && (ok || !reflect.DeepEqual(v, name)) { | ||
obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, name)} | ||
} |
80 changes: 80 additions & 0 deletions
80
third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package google | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccComputeDiskResourcePolicyAttachment_basic(t *testing.T) { | ||
t.Parallel() | ||
|
||
diskName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) | ||
policyName := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(10)) | ||
policyName2 := fmt.Sprintf("tf-test-policy-%s", acctest.RandString(10)) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName), | ||
}, | ||
{ | ||
ResourceName: "google_compute_disk_resource_policy_attachment.foobar", | ||
// ImportStateId: fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", getTestProjectFromEnv(), "us-central1", policyName), | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Config: testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName2), | ||
}, | ||
{ | ||
ResourceName: "google_compute_disk_resource_policy_attachment.foobar", | ||
// ImportStateId: fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", getTestProjectFromEnv(), "us-central1", policyName), | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName string) string { | ||
return fmt.Sprintf(` | ||
data "google_compute_image" "my_image" { | ||
family = "debian-9" | ||
project = "debian-cloud" | ||
} | ||
resource "google_compute_disk" "foobar" { | ||
name = "%s" | ||
image = "${data.google_compute_image.my_image.self_link}" | ||
size = 50 | ||
type = "pd-ssd" | ||
zone = "us-central1-a" | ||
labels = { | ||
my-label = "my-label-value" | ||
} | ||
} | ||
resource "google_compute_resource_policy" "foobar" { | ||
name = "%s" | ||
region = "us-central1" | ||
snapshot_schedule_policy { | ||
schedule { | ||
daily_schedule { | ||
days_in_cycle = 1 | ||
start_time = "04:00" | ||
} | ||
} | ||
} | ||
} | ||
resource "google_compute_disk_resource_policy_attachment" "foobar" { | ||
name = google_compute_resource_policy.foobar.name | ||
disk = google_compute_disk.foobar.name | ||
zone = "us-central1-a" | ||
}`, diskName, policyName) | ||
} |