Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow updating of resource policies in compute_disk #2228

Merged
merged 29 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fea1285
Allow updating of resource policies in compute_disk
chrissng Aug 22, 2019
1fd406a
Address add and removeResourcePolicies API via an attachment resource…
chrissng Aug 23, 2019
983ca8a
Apply suggestions from code review
chrissng Aug 24, 2019
c6dc881
Fixes based on suggestions from PR review
chrissng Aug 24, 2019
f1a42a6
exclude inspec and ansible. change resource name to reference singular
chrissng Aug 24, 2019
b23d829
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Aug 24, 2019
dcbd5ae
Remove unused code
chrissng Aug 24, 2019
b610d42
Merge branch 'compute-disk-update-resource-policies' of github.com:ch…
chrissng Aug 24, 2019
14aa274
Add resource policy attachment test
chrissng Aug 24, 2019
e416437
Use custom checker
chrissng Aug 24, 2019
460c842
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Aug 27, 2019
2c948c4
Exclude resource policy for ansible and inspec
chrissng Aug 28, 2019
964d489
Update products/compute/ansible.yaml
chrissng Aug 28, 2019
c9f861b
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Aug 30, 2019
6cd2d18
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 2, 2019
14e714f
make imports work
chrissng Sep 4, 2019
fe1b1c0
exclude resource policy in ansible overrides also
chrissng Sep 4, 2019
f49df1b
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 4, 2019
573b783
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 5, 2019
6d4da33
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 6, 2019
60eb12f
fix importing of tf resource
chrissng Sep 6, 2019
d2995ab
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 7, 2019
b41b21e
resource policy example use google provider, default zone
chrissng Sep 7, 2019
d09db5e
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 10, 2019
8d6fb75
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 12, 2019
b7de2e6
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 16, 2019
0594529
Merge branch 'master' into compute-disk-update-resource-policies
chrissng Sep 16, 2019
b001d14
fix async operation timeout
chrissng Sep 17, 2019
819ad06
Merge branch 'master' into compute-disk-update-resource-policies
rileykarson Sep 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions products/compute/ansible.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ datasources: !ruby/object:Overrides::ResourceOverrides
exclude: true
RegionAutoscaler: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
DiskResourcePolicyAttachment: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
# Readonly resources.
DiskType: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
Expand All @@ -43,6 +45,8 @@ datasources: !ruby/object:Overrides::ResourceOverrides
exclude: true
RegionDiskType: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
ResourcePolicy: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
chrissng marked this conversation as resolved.
Show resolved Hide resolved
TargetInstance: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
ExternalVpnGateway: !ruby/object:Overrides::Ansible::ResourceOverride
Expand Down Expand Up @@ -77,6 +81,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
- timeout_seconds
RegionBackendService: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
DiskResourcePolicyAttachment: !ruby/object:Overrides::Ansible::ResourceOverride
exclude: true
Disk: !ruby/object:Overrides::Ansible::ResourceOverride
properties:
sourceSnapshot: !ruby/object:Overrides::Ansible::PropertyOverride
Expand Down
62 changes: 61 additions & 1 deletion products/compute/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,67 @@ objects:
An optional textual description of the valid disk size, such as
"10GB-10TB".
output: true
- !ruby/object:Api::Resource
name: 'DiskResourcePolicyAttachment'
input: true
base_url: projects/{{project}}/zones/{{zone}}/disks
chrissng marked this conversation as resolved.
Show resolved Hide resolved
create_verb: :POST
create_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies
delete_verb: :POST
delete_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/removeResourcePolicies
self_link: projects/{{project}}/zones/{{zone}}/disks/{{disk}}
nested_query: !ruby/object:Api::Resource::NestedQuery
keys:
- resourcePolicies
is_list_of_ids: true
identity:
- name
description: |
Disk resource policies define a schedule for taking snapshots and a
retention period for these snapshots.
async: !ruby/object:Api::Async
operation: !ruby/object:Api::Async::Operation
kind: 'compute#operation'
path: 'name'
base_url: 'projects/{{project}}/zones/{{zone}}/operations/{{op_id}}'
wait_ms: 1000
timeouts: !ruby/object:Api::Timeouts
chrissng marked this conversation as resolved.
Show resolved Hide resolved
insert_minutes: 3
result: !ruby/object:Api::Async::Result
path: 'targetLink'
status: !ruby/object:Api::Async::Status
path: 'status'
complete: 'DONE'
allowed:
- 'PENDING'
- 'RUNNING'
- 'DONE'
error: !ruby/object:Api::Async::Error
path: 'error/errors'
message: 'message'
parameters:
- !ruby/object:Api::Type::ResourceRef
name: 'disk'
resource: 'Disk'
imports: 'name'
description: |
The name of the disk in which the resource policies are attached to.
required: true
url_param_only: true
- !ruby/object:Api::Type::ResourceRef
name: 'zone'
chrissng marked this conversation as resolved.
Show resolved Hide resolved
resource: 'Zone'
imports: 'name'
description: 'A reference to the zone where the disk resides.'
required: true
url_param_only: true
properties:
- !ruby/object:Api::Type::String
name: 'name'
description: |
The resource policy to be attached to the disk for scheduling snapshot
creation. Do not specify the self link.
required: true
- !ruby/object:Api::Resource
name: 'Disk'
# TODO(nelsonjr): Implement disk special actions as defined in the API:
Expand Down Expand Up @@ -6105,7 +6166,6 @@ objects:
create the disk. Provide this when creating the disk.
- !ruby/object:Api::Resource
name: 'ResourcePolicy'
rileykarson marked this conversation as resolved.
Show resolved Hide resolved
min_version: beta
kind: 'compute#resourcePolicy'
base_url: projects/{{project}}/regions/{{region}}/resourcePolicies
input: true
Expand Down
4 changes: 4 additions & 0 deletions products/compute/inspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
exclude: true
BackendServiceSignedUrlKey: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
DiskResourcePolicyAttachment: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
DiskType: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
ExternalVpnGateway: !ruby/object:Overrides::Inspec::ResourceOverride
Expand Down Expand Up @@ -70,6 +72,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
exclude: true
RegionDiskType: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
ResourcePolicy: !ruby/object:Overrides::Inspec::ResourceOverride
exclude: true
Subnetwork: !ruby/object:Overrides::Inspec::ResourceOverride
exclude_resource: true
iam_policy: !ruby/object:Api::Resource::IamPolicy
Expand Down
8 changes: 8 additions & 0 deletions products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,12 @@ overrides: !ruby/object:Overrides::ResourceOverrides
state as plain-text. [Read more about sensitive data in state](/docs/state/sensitive-data.html).
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
chrissng marked this conversation as resolved.
Show resolved Hide resolved
id_format: "{{project}}/{{zone}}/{{disk}}/{{name}}"
custom_code: !ruby/object:Provider::Terraform::CustomCode
encoder: templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb
decoder: templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb
pre_delete: templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb
chrissng marked this conversation as resolved.
Show resolved Hide resolved
Disk: !ruby/object:Overrides::Terraform::ResourceOverride
# Larger disks were timing out at creation. Bumping up to 5 minutes.
# https://github.com/terraform-providers/terraform-provider-google/issues/703
Expand Down Expand Up @@ -378,6 +384,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
https://cloud.google.com/compute/docs/disks/customer-managed-encryption#encrypt_a_new_persistent_disk_with_your_own_keys
physicalBlockSizeBytes: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
resourcePolicies: !ruby/object:Overrides::Terraform::PropertyOverride
default_from_api: true
custom_code: !ruby/object:Provider::Terraform::CustomCode
pre_delete: templates/terraform/pre_delete/detach_disk.erb
constants: templates/terraform/constants/disk.erb
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
config := meta.(*Config)
chrissng marked this conversation as resolved.
Show resolved Hide resolved
project, err := getProject(d, config)
if err != nil {
return nil, err
}

zone := d.Get("zone").(string)
region := zone[:len(zone)-2]

name := d.Get("name")

if v, ok := res["name"]; ok {
selfLink := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/regions/%s/resourcePolicies/%s", project, region, name)
if selfLink == v {
res["name"] = name
}
}

return res, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
config := meta.(*Config)
project, err := getProject(d, config)
if err != nil {
return nil, err
}

zone := d.Get("zone").(string)
chrissng marked this conversation as resolved.
Show resolved Hide resolved
region := zone[:len(zone)-2]

obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, obj["name"])}
delete(obj, "name")
return obj, nil
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
obj = make(map[string]interface{})

// projects/{project}/regions/{region}/resourcePolicies/{resourceId}
zone := d.Get("zone").(string)
region := zone[:len(zone)-2]

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)}
}
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)
}