From fea128574f65ff72de4da0acbafd51622d941534 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Thu, 22 Aug 2019 11:23:47 +0800 Subject: [PATCH 01/15] Allow updating of resource policies in compute_disk Signed-off-by: Chris Sng --- products/compute/api.yaml | 2 + .../tests/resource_compute_disk_test.go.erb | 88 ++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/products/compute/api.yaml b/products/compute/api.yaml index c32654467044..9d19b4840b19 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1575,6 +1575,8 @@ objects: resource: 'ResourcePolicy' imports: 'selfLink' description: 'A resource policy applied to this disk for automatic snapshot creations.' + update_url: 'projects/{{project}}/zones/{{zone}}/disks/{{name}}/addResourcePolicies' + update_verb: :POST - !ruby/object:Api::Resource name: 'Firewall' kind: 'compute#firewall' diff --git a/third_party/terraform/tests/resource_compute_disk_test.go.erb b/third_party/terraform/tests/resource_compute_disk_test.go.erb index dfd1e3ac829f..32ad8eac18ca 100644 --- a/third_party/terraform/tests/resource_compute_disk_test.go.erb +++ b/third_party/terraform/tests/resource_compute_disk_test.go.erb @@ -11,7 +11,8 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "google.golang.org/api/compute/v1" + computeBeta "google.golang.org/api/compute/v0.beta" + compute "google.golang.org/api/compute/v1" ) func TestDiskImageDiffSuppress(t *testing.T) { @@ -444,6 +445,8 @@ func TestAccComputeDisk_resourcePolicies(t *testing.T) { 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-bar", acctest.RandString(10)) + var disk computeBeta.Disk resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -457,9 +460,64 @@ func TestAccComputeDisk_resourcePolicies(t *testing.T) { ImportState: true, ImportStateVerify: true, }, + { + Config: testAccComputeDisk_resourcePolicies_updated(diskName, policyName2), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeBetaDiskExists( + "google_compute_disk.foobar", getTestProjectFromEnv(), &disk), + testAccCheckComputeDiskHasResourcePolicies( + &disk, policyName2), + ), + }, }, }) } + +func testAccCheckComputeBetaDiskExists(n, p string, disk *computeBeta.Disk) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + config := testAccProvider.Meta().(*Config) + + found, err := config.clientComputeBeta.Disks.Get( + p, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() + if err != nil { + return err + } + + if found.Name != rs.Primary.ID { + return fmt.Errorf("Disk not found") + } + + *disk = *found + + return nil + } +} + +func testAccCheckComputeDiskHasResourcePolicies(disk *computeBeta.Disk, policyName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + found := false + for _, v := range disk.ResourcePolicies { + if v == policyName { + found = true + } + } + + if !found { + return fmt.Errorf("Resource policy is wrong. Found: %s", disk.ResourcePolicies) + } + + return nil + } +} <% end -%> func testAccCheckComputeDiskExists(n, p string, disk *compute.Disk) resource.TestCheckFunc { @@ -828,4 +886,32 @@ resource "google_compute_disk" "foobar" { resource_policies = [google_compute_resource_policy.foo.self_link] }`, policyName, diskName) } + +func testAccComputeDisk_resourcePolicies_updated(diskName, policyName string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} +resource "google_compute_resource_policy" "bar" { + name = "%s" + region = "us-central1" + snapshot_schedule_policy { + schedule { + daily_schedule { + days_in_cycle = 3 + start_time = "06:00" + } + } + } +} +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" + resource_policies = [google_compute_resource_policy.bar.self_link] +}`, policyName, diskName) +} <% end -%> From 1fd406a4bb33835d86de3af9b1d2fcde36ec6544 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Fri, 23 Aug 2019 18:36:07 +0800 Subject: [PATCH 02/15] Address add and removeResourcePolicies API via an attachment resource instead - custom pre_delete code to include resourcePolicies in POST body --- products/compute/api.yaml | 72 ++++++++++++++++++- products/compute/terraform.yaml | 11 +++ ...e_disk_resource_policies_attachment.go.erb | 7 ++ 3 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb diff --git a/products/compute/api.yaml b/products/compute/api.yaml index 9d19b4840b19..dfbde18cffb2 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1275,6 +1275,76 @@ objects: An optional textual description of the valid disk size, such as "10GB-10TB". output: true + - !ruby/object:Api::Resource + name: 'DiskResourcePoliciesAttachment' + kind: 'compute#diskResourcePoliciesAttachment' + input: true + min_version: beta + base_url: projects/{{project}}/zones/{{zone}}/disks + create_verb: :POST + create_url: projects/{{project}}/zones/{{zone}}/disks/{{name}}/addResourcePolicies + delete_verb: :POST + delete_url: projects/{{project}}/zones/{{zone}}/disks/{{name}}/removeResourcePolicies + self_link: projects/{{project}}/zones/{{zone}}/disks/{{name}} + # nested_query: !ruby/object:Api::Resource::NestedQuery + # keys: + # - disk + # - resourcePolicies + # - zone + # is_list_of_ids: true + # identity: + # - resourcePolicies + 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 + 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' + properties: + - !ruby/object:Api::Type::ResourceRef + name: 'name' + resource: 'Disk' + imports: 'name' + description: | + The name of the disk in which the resource policies are attached to. + required: true + - !ruby/object:Api::Type::Array + name: 'resourcePolicies' + description: | + Resource policies to be added to this disk. You can only add one + policy which will be applied to the disk for scheduling snapshot + creation. + item_type: !ruby/object:Api::Type::ResourceRef + name: 'resourcePolicy' + resource: 'ResourcePolicy' + imports: 'selfLink' + description: | + A resource policy applied to this disk for automatic snapshot + creations. + required: true + - !ruby/object:Api::Type::ResourceRef + name: 'zone' + resource: 'Zone' + imports: 'name' + description: 'A reference to the zone where the disk resides.' + required: true - !ruby/object:Api::Resource name: 'Disk' # TODO(nelsonjr): Implement disk special actions as defined in the API: @@ -1575,8 +1645,6 @@ objects: resource: 'ResourcePolicy' imports: 'selfLink' description: 'A resource policy applied to this disk for automatic snapshot creations.' - update_url: 'projects/{{project}}/zones/{{zone}}/disks/{{name}}/addResourcePolicies' - update_verb: :POST - !ruby/object:Api::Resource name: 'Firewall' kind: 'compute#firewall' diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index 6464b089f81a..134d1cacdead 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -302,6 +302,15 @@ 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. + DiskResourcePoliciesAttachment: !ruby/object:Overrides::Terraform::ResourceOverride + custom_code: !ruby/object:Provider::Terraform::CustomCode + pre_delete: templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb + # properties: + # name: !ruby/object:Overrides::Terraform::PropertyOverride + # name: disk + # create_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies + # delete_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/removeResourcePolicies + # self_link: projects/{{project}}/zones/{{zone}}/disks/{{disk}} 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 @@ -369,6 +378,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 diff --git a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb new file mode 100644 index 000000000000..aa5eb5a1f5ee --- /dev/null +++ b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb @@ -0,0 +1,7 @@ +obj = make(map[string]interface{}) +resourcePoliciesProp, err := expandComputeDiskResourcePoliciesAttachmentResourcePolicies(d.Get("resource_policies"), d, config) +if err != nil { + return err +} else if v, ok := d.GetOkExists("resource_policies"); !isEmptyValue(reflect.ValueOf(resourcePoliciesProp)) && (ok || !reflect.DeepEqual(v, resourcePoliciesProp)) { + obj["resourcePolicies"] = resourcePoliciesProp +} From 983ca8a0d6fc920277bfa0c007a2a92e24132855 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sat, 24 Aug 2019 22:13:42 +0800 Subject: [PATCH 03/15] Apply suggestions from code review Co-Authored-By: Riley Karson --- products/compute/api.yaml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/products/compute/api.yaml b/products/compute/api.yaml index dfbde18cffb2..c70bf3da743b 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1277,12 +1277,11 @@ objects: output: true - !ruby/object:Api::Resource name: 'DiskResourcePoliciesAttachment' - kind: 'compute#diskResourcePoliciesAttachment' input: true min_version: beta base_url: projects/{{project}}/zones/{{zone}}/disks create_verb: :POST - create_url: projects/{{project}}/zones/{{zone}}/disks/{{name}}/addResourcePolicies + create_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies delete_verb: :POST delete_url: projects/{{project}}/zones/{{zone}}/disks/{{name}}/removeResourcePolicies self_link: projects/{{project}}/zones/{{zone}}/disks/{{name}} From c6dc881c11e14d2fb5c10dc0ad7088f10f71be7c Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sun, 25 Aug 2019 00:18:49 +0800 Subject: [PATCH 04/15] Fixes based on suggestions from PR review --- products/compute/api.yaml | 46 ++++++++----------- products/compute/terraform.yaml | 1 + ...e_disk_resource_policies_attachment.go.erb | 3 ++ ...e_disk_resource_policies_attachment.go.erb | 6 +-- 4 files changed, 26 insertions(+), 30 deletions(-) create mode 100644 templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb diff --git a/products/compute/api.yaml b/products/compute/api.yaml index c70bf3da743b..2743c084557f 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1278,21 +1278,18 @@ objects: - !ruby/object:Api::Resource name: 'DiskResourcePoliciesAttachment' input: true - min_version: beta base_url: projects/{{project}}/zones/{{zone}}/disks create_verb: :POST create_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies delete_verb: :POST - delete_url: projects/{{project}}/zones/{{zone}}/disks/{{name}}/removeResourcePolicies - self_link: projects/{{project}}/zones/{{zone}}/disks/{{name}} - # nested_query: !ruby/object:Api::Resource::NestedQuery - # keys: - # - disk - # - resourcePolicies - # - zone - # is_list_of_ids: true - # identity: - # - resourcePolicies + 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. @@ -1316,34 +1313,29 @@ objects: error: !ruby/object:Api::Async::Error path: 'error/errors' message: 'message' - properties: + parameters: - !ruby/object:Api::Type::ResourceRef - name: 'name' + name: 'disk' resource: 'Disk' imports: 'name' description: | The name of the disk in which the resource policies are attached to. required: true - - !ruby/object:Api::Type::Array - name: 'resourcePolicies' - description: | - Resource policies to be added to this disk. You can only add one - policy which will be applied to the disk for scheduling snapshot - creation. - item_type: !ruby/object:Api::Type::ResourceRef - name: 'resourcePolicy' - resource: 'ResourcePolicy' - imports: 'selfLink' - description: | - A resource policy applied to this disk for automatic snapshot - creations. - required: true + url_param_only: true - !ruby/object:Api::Type::ResourceRef name: 'zone' 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: | + A resource policy to be attached to the disk for scheduling snapshot + creation. + required: true - !ruby/object:Api::Resource name: 'Disk' # TODO(nelsonjr): Implement disk special actions as defined in the API: diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index 134d1cacdead..066c172f1c3f 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -304,6 +304,7 @@ overrides: !ruby/object:Overrides::ResourceOverrides we cannot confirm or reverse changes to a key outside of Terraform. DiskResourcePoliciesAttachment: !ruby/object:Overrides::Terraform::ResourceOverride custom_code: !ruby/object:Provider::Terraform::CustomCode + encoder: templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb pre_delete: templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb # properties: # name: !ruby/object:Overrides::Terraform::PropertyOverride diff --git a/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb new file mode 100644 index 000000000000..a80ad3060121 --- /dev/null +++ b/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb @@ -0,0 +1,3 @@ +obj["resourcePolicies"] = []interface{}{obj["name"]} +delete(obj, "name") +return obj, nil diff --git a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb index aa5eb5a1f5ee..3ce0f3980a90 100644 --- a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb @@ -1,7 +1,7 @@ obj = make(map[string]interface{}) -resourcePoliciesProp, err := expandComputeDiskResourcePoliciesAttachmentResourcePolicies(d.Get("resource_policies"), d, config) +nameProp, err := expandComputeDiskResourcePoliciesAttachmentName(d.Get("name"), d, config) if err != nil { return err -} else if v, ok := d.GetOkExists("resource_policies"); !isEmptyValue(reflect.ValueOf(resourcePoliciesProp)) && (ok || !reflect.DeepEqual(v, resourcePoliciesProp)) { - obj["resourcePolicies"] = resourcePoliciesProp +} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { + obj["resourcePolicies"] = []interface{}{nameProp} } From f1a42a6650f76d34b2eb30a8babe608511af00e0 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sun, 25 Aug 2019 00:36:29 +0800 Subject: [PATCH 05/15] exclude inspec and ansible. change resource name to reference singular --- products/compute/ansible.yaml | 2 ++ products/compute/api.yaml | 4 ++-- products/compute/inspec.yaml | 2 ++ products/compute/terraform.yaml | 2 +- .../compute_disk_resource_policies_attachment.go.erb | 2 +- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index cbc7359692c9..ca1bbc97f8e9 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -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 diff --git a/products/compute/api.yaml b/products/compute/api.yaml index 2743c084557f..ecd8a856839b 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1276,7 +1276,7 @@ objects: "10GB-10TB". output: true - !ruby/object:Api::Resource - name: 'DiskResourcePoliciesAttachment' + name: 'DiskResourcePolicyAttachment' input: true base_url: projects/{{project}}/zones/{{zone}}/disks create_verb: :POST @@ -1333,7 +1333,7 @@ objects: - !ruby/object:Api::Type::String name: 'name' description: | - A resource policy to be attached to the disk for scheduling snapshot + The resource policy to be attached to the disk for scheduling snapshot creation. required: true - !ruby/object:Api::Resource diff --git a/products/compute/inspec.yaml b/products/compute/inspec.yaml index 1989940bb88a..b87b1e3aa7e4 100644 --- a/products/compute/inspec.yaml +++ b/products/compute/inspec.yaml @@ -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 diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index 066c172f1c3f..c04fd91444a2 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -302,7 +302,7 @@ 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. - DiskResourcePoliciesAttachment: !ruby/object:Overrides::Terraform::ResourceOverride + DiskResourcePolicyAttachment: !ruby/object:Overrides::Terraform::ResourceOverride custom_code: !ruby/object:Provider::Terraform::CustomCode encoder: templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb pre_delete: templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb diff --git a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb index 3ce0f3980a90..f44bd56a1792 100644 --- a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb @@ -1,5 +1,5 @@ obj = make(map[string]interface{}) -nameProp, err := expandComputeDiskResourcePoliciesAttachmentName(d.Get("name"), d, config) +nameProp, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config) if err != nil { return err } else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { From dcbd5ae4035bbcb2f448e6b035167f78281a7e57 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sun, 25 Aug 2019 00:50:56 +0800 Subject: [PATCH 06/15] Remove unused code --- products/compute/terraform.yaml | 6 -- .../tests/resource_compute_disk_test.go.erb | 88 +------------------ 2 files changed, 1 insertion(+), 93 deletions(-) diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index c04fd91444a2..4f46a0e149d2 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -306,12 +306,6 @@ overrides: !ruby/object:Overrides::ResourceOverrides custom_code: !ruby/object:Provider::Terraform::CustomCode encoder: templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb pre_delete: templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb - # properties: - # name: !ruby/object:Overrides::Terraform::PropertyOverride - # name: disk - # create_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies - # delete_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/removeResourcePolicies - # self_link: projects/{{project}}/zones/{{zone}}/disks/{{disk}} 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 diff --git a/third_party/terraform/tests/resource_compute_disk_test.go.erb b/third_party/terraform/tests/resource_compute_disk_test.go.erb index 32ad8eac18ca..dfd1e3ac829f 100644 --- a/third_party/terraform/tests/resource_compute_disk_test.go.erb +++ b/third_party/terraform/tests/resource_compute_disk_test.go.erb @@ -11,8 +11,7 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - computeBeta "google.golang.org/api/compute/v0.beta" - compute "google.golang.org/api/compute/v1" + "google.golang.org/api/compute/v1" ) func TestDiskImageDiffSuppress(t *testing.T) { @@ -445,8 +444,6 @@ func TestAccComputeDisk_resourcePolicies(t *testing.T) { 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-bar", acctest.RandString(10)) - var disk computeBeta.Disk resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -460,64 +457,9 @@ func TestAccComputeDisk_resourcePolicies(t *testing.T) { ImportState: true, ImportStateVerify: true, }, - { - Config: testAccComputeDisk_resourcePolicies_updated(diskName, policyName2), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeBetaDiskExists( - "google_compute_disk.foobar", getTestProjectFromEnv(), &disk), - testAccCheckComputeDiskHasResourcePolicies( - &disk, policyName2), - ), - }, }, }) } - -func testAccCheckComputeBetaDiskExists(n, p string, disk *computeBeta.Disk) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - config := testAccProvider.Meta().(*Config) - - found, err := config.clientComputeBeta.Disks.Get( - p, rs.Primary.Attributes["zone"], rs.Primary.ID).Do() - if err != nil { - return err - } - - if found.Name != rs.Primary.ID { - return fmt.Errorf("Disk not found") - } - - *disk = *found - - return nil - } -} - -func testAccCheckComputeDiskHasResourcePolicies(disk *computeBeta.Disk, policyName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - found := false - for _, v := range disk.ResourcePolicies { - if v == policyName { - found = true - } - } - - if !found { - return fmt.Errorf("Resource policy is wrong. Found: %s", disk.ResourcePolicies) - } - - return nil - } -} <% end -%> func testAccCheckComputeDiskExists(n, p string, disk *compute.Disk) resource.TestCheckFunc { @@ -886,32 +828,4 @@ resource "google_compute_disk" "foobar" { resource_policies = [google_compute_resource_policy.foo.self_link] }`, policyName, diskName) } - -func testAccComputeDisk_resourcePolicies_updated(diskName, policyName string) string { - return fmt.Sprintf(` -data "google_compute_image" "my_image" { - family = "debian-9" - project = "debian-cloud" -} -resource "google_compute_resource_policy" "bar" { - name = "%s" - region = "us-central1" - snapshot_schedule_policy { - schedule { - daily_schedule { - days_in_cycle = 3 - start_time = "06:00" - } - } - } -} -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" - resource_policies = [google_compute_resource_policy.bar.self_link] -}`, policyName, diskName) -} <% end -%> From 14aa2743f48f14ff209515819f16c59fd527f6a5 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sun, 25 Aug 2019 01:28:02 +0800 Subject: [PATCH 07/15] Add resource policy attachment test --- products/compute/api.yaml | 1 - ...te_disk_resource_policy_attachment_test.go | 78 +++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go diff --git a/products/compute/api.yaml b/products/compute/api.yaml index ecd8a856839b..cbcbc0e61855 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -6166,7 +6166,6 @@ objects: create the disk. Provide this when creating the disk. - !ruby/object:Api::Resource name: 'ResourcePolicy' - min_version: beta kind: 'compute#resourcePolicy' base_url: projects/{{project}}/regions/{{region}}/resourcePolicies input: true diff --git a/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go b/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go new file mode 100644 index 000000000000..3f96760bcbdd --- /dev/null +++ b/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go @@ -0,0 +1,78 @@ +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", + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName2), + }, + { + ResourceName: "google_compute_disk_resource_policy_attachment.foobar", + 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.self_link + disk = google_compute_disk.foobar.name + zone = "us-central1-a" +}`, diskName, policyName) +} From e416437cec77d92d0a1a0d2c24c88cc5779a36b6 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sun, 25 Aug 2019 01:28:02 +0800 Subject: [PATCH 08/15] Use custom checker --- ...te_disk_resource_policy_attachment_test.go | 46 +++++++++++++++---- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go b/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go index 3f96760bcbdd..56dc8974c0e0 100644 --- a/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go +++ b/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" ) func TestAccComputeDiskResourcePolicyAttachment_basic(t *testing.T) { @@ -21,24 +22,49 @@ func TestAccComputeDiskResourcePolicyAttachment_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName), - }, - { - ResourceName: "google_compute_disk_resource_policy_attachment.foobar", - ImportState: true, - ImportStateVerify: true, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeDiskResourcePolicyAttachmentExists( + "google_compute_disk_resource_policy_attachment.foobar", "google_compute_resource_policy.foobar", policyName), + ), }, { Config: testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName2), - }, - { - ResourceName: "google_compute_disk_resource_policy_attachment.foobar", - ImportState: true, - ImportStateVerify: true, + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeDiskResourcePolicyAttachmentExists( + "google_compute_disk_resource_policy_attachment.foobar", "google_compute_resource_policy.foobar", policyName2), + ), }, }, }) } +func testAccCheckComputeDiskResourcePolicyAttachmentExists(attachResName, policyResName, policyName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + attachRes, ok := s.RootModule().Resources[attachResName] + if !ok { + return fmt.Errorf("Not found: %s", attachResName) + } + + if attachRes.Primary.ID == "" { + return fmt.Errorf("No ID is set") + } + + policyRes, ok := s.RootModule().Resources[policyResName] + if !ok { + return fmt.Errorf("Not found: %s", policyResName) + } + + if policyRes.Primary.Attributes["name"] != policyName { + return fmt.Errorf("Resource Policy is incorrect") + } + if attachRes.Primary.Attributes["name"] != policyRes.Primary.Attributes["self_link"] { + return fmt.Errorf("Resource Policy Attachment is incorrect") + } + + return nil + } +} + func testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { From 2c948c4efa44a5a6e26af334a993e0a1a2e0c432 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Wed, 28 Aug 2019 09:55:55 +0800 Subject: [PATCH 09/15] Exclude resource policy for ansible and inspec --- products/compute/ansible.yaml | 2 ++ products/compute/inspec.yaml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index ca1bbc97f8e9..24fd3335e8f2 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -45,6 +45,8 @@ datasources: !ruby/object:Overrides::ResourceOverrides exclude: true RegionDiskType: !ruby/object:Overrides::Ansible::ResourceOverride exclude: true + ResourcePolicy: !ruby/object:Overrides::Inspec::ResourceOverride + exclude: true TargetInstance: !ruby/object:Overrides::Ansible::ResourceOverride exclude: true ExternalVpnGateway: !ruby/object:Overrides::Ansible::ResourceOverride diff --git a/products/compute/inspec.yaml b/products/compute/inspec.yaml index b87b1e3aa7e4..60436855c30e 100644 --- a/products/compute/inspec.yaml +++ b/products/compute/inspec.yaml @@ -72,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 From 964d4893f7b052c4855c5ebadd2dab74dde1ae47 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Thu, 29 Aug 2019 06:33:55 +0800 Subject: [PATCH 10/15] Update products/compute/ansible.yaml Co-Authored-By: Riley Karson --- products/compute/ansible.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index 24fd3335e8f2..0ece9182d9e1 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -45,7 +45,7 @@ datasources: !ruby/object:Overrides::ResourceOverrides exclude: true RegionDiskType: !ruby/object:Overrides::Ansible::ResourceOverride exclude: true - ResourcePolicy: !ruby/object:Overrides::Inspec::ResourceOverride + ResourcePolicy: !ruby/object:Overrides::Ansible::ResourceOverride exclude: true TargetInstance: !ruby/object:Overrides::Ansible::ResourceOverride exclude: true From 14e714faef1fab74a9903f47b70a27575200a43b Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Wed, 4 Sep 2019 11:49:11 +0800 Subject: [PATCH 11/15] make imports work --- products/compute/ansible.yaml | 2 + products/compute/api.yaml | 2 +- products/compute/terraform.yaml | 2 + ...e_disk_resource_policies_attachment.go.erb | 19 +++++++ ...e_disk_resource_policies_attachment.go.erb | 11 +++- ...e_disk_resource_policies_attachment.go.erb | 11 ++-- ...te_disk_resource_policy_attachment_test.go | 50 +++++-------------- 7 files changed, 55 insertions(+), 42 deletions(-) create mode 100644 templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index 0ece9182d9e1..212ec34de2e7 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -81,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 diff --git a/products/compute/api.yaml b/products/compute/api.yaml index 9d8d36789bbf..f6af193a5928 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1334,7 +1334,7 @@ objects: name: 'name' description: | The resource policy to be attached to the disk for scheduling snapshot - creation. + creation. Do not specify the self link. required: true - !ruby/object:Api::Resource name: 'Disk' diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index beaf666db2d4..fddf687bfca9 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -303,8 +303,10 @@ 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 + 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 Disk: !ruby/object:Overrides::Terraform::ResourceOverride # Larger disks were timing out at creation. Bumping up to 5 minutes. diff --git a/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb new file mode 100644 index 000000000000..5bd75d5ab8fe --- /dev/null +++ b/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb @@ -0,0 +1,19 @@ +config := meta.(*Config) +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 diff --git a/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb index a80ad3060121..fb728eac385c 100644 --- a/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb @@ -1,3 +1,12 @@ -obj["resourcePolicies"] = []interface{}{obj["name"]} +config := meta.(*Config) +project, err := getProject(d, config) +if err != nil { + return nil, err +} + +zone := d.Get("zone").(string) +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 diff --git a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb index f44bd56a1792..0ebc33936f29 100644 --- a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb @@ -1,7 +1,12 @@ obj = make(map[string]interface{}) -nameProp, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config) + +// 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(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) { - obj["resourcePolicies"] = []interface{}{nameProp} +} 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)} } diff --git a/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go b/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go index 56dc8974c0e0..fbfbeab07149 100644 --- a/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go +++ b/third_party/terraform/tests/resource_compute_disk_resource_policy_attachment_test.go @@ -6,7 +6,6 @@ import ( "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" - "github.com/hashicorp/terraform/terraform" ) func TestAccComputeDiskResourcePolicyAttachment_basic(t *testing.T) { @@ -22,49 +21,26 @@ func TestAccComputeDiskResourcePolicyAttachment_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeDiskResourcePolicyAttachmentExists( - "google_compute_disk_resource_policy_attachment.foobar", "google_compute_resource_policy.foobar", 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), - Check: resource.ComposeTestCheckFunc( - testAccCheckComputeDiskResourcePolicyAttachmentExists( - "google_compute_disk_resource_policy_attachment.foobar", "google_compute_resource_policy.foobar", 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 testAccCheckComputeDiskResourcePolicyAttachmentExists(attachResName, policyResName, policyName string) resource.TestCheckFunc { - return func(s *terraform.State) error { - attachRes, ok := s.RootModule().Resources[attachResName] - if !ok { - return fmt.Errorf("Not found: %s", attachResName) - } - - if attachRes.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - policyRes, ok := s.RootModule().Resources[policyResName] - if !ok { - return fmt.Errorf("Not found: %s", policyResName) - } - - if policyRes.Primary.Attributes["name"] != policyName { - return fmt.Errorf("Resource Policy is incorrect") - } - if attachRes.Primary.Attributes["name"] != policyRes.Primary.Attributes["self_link"] { - return fmt.Errorf("Resource Policy Attachment is incorrect") - } - - return nil - } -} - func testAccComputeDiskResourcePolicyAttachment_basic(diskName, policyName string) string { return fmt.Sprintf(` data "google_compute_image" "my_image" { @@ -97,7 +73,7 @@ resource "google_compute_resource_policy" "foobar" { } resource "google_compute_disk_resource_policy_attachment" "foobar" { - name = google_compute_resource_policy.foobar.self_link + name = google_compute_resource_policy.foobar.name disk = google_compute_disk.foobar.name zone = "us-central1-a" }`, diskName, policyName) From fe1b1c08a185fa79bce852d71ae585c72a954a71 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Wed, 4 Sep 2019 13:23:10 +0800 Subject: [PATCH 12/15] exclude resource policy in ansible overrides also --- products/compute/ansible.yaml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/products/compute/ansible.yaml b/products/compute/ansible.yaml index 212ec34de2e7..a3abe4db7d5e 100644 --- a/products/compute/ansible.yaml +++ b/products/compute/ansible.yaml @@ -246,6 +246,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides description: | The source snapshot used to create this disk. You can provide this as a partial or full URL to the resource. + ResourcePolicy: !ruby/object:Overrides::Ansible::ResourceOverride + exclude: true Route: !ruby/object:Overrides::Ansible::ResourceOverride properties: nextHopGateway: !ruby/object:Overrides::Ansible::PropertyOverride From 60eb12faf6e79e5548877e8569d2b6fd7fa7f2a7 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Fri, 6 Sep 2019 18:07:38 +0800 Subject: [PATCH 13/15] fix importing of tf resource --- products/compute/api.yaml | 2 +- ...e_disk_resource_policies_attachment.go.erb | 19 +------------------ ...e_disk_resource_policies_attachment.go.erb | 3 +-- ...e_disk_resource_policies_attachment.go.erb | 3 +-- 4 files changed, 4 insertions(+), 23 deletions(-) diff --git a/products/compute/api.yaml b/products/compute/api.yaml index 4b7d56508be0..1e26544da255 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1288,7 +1288,7 @@ objects: - !ruby/object:Api::Resource name: 'DiskResourcePolicyAttachment' input: true - base_url: projects/{{project}}/zones/{{zone}}/disks + base_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}} create_verb: :POST create_url: projects/{{project}}/zones/{{zone}}/disks/{{disk}}/addResourcePolicies delete_verb: :POST diff --git a/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb index 5bd75d5ab8fe..c981b8c3702f 100644 --- a/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/decoders/compute_disk_resource_policies_attachment.go.erb @@ -1,19 +1,2 @@ -config := meta.(*Config) -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 - } -} - +res["name"] = GetResourceNameFromSelfLink(res["name"].(string)) return res, nil diff --git a/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb index fb728eac385c..584c6462f9dd 100644 --- a/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/encoders/compute_disk_resource_policies_attachment.go.erb @@ -4,8 +4,7 @@ if err != nil { return nil, err } -zone := d.Get("zone").(string) -region := zone[:len(zone)-2] +region := getRegionFromZone(d.Get("zone").(string)) obj["resourcePolicies"] = []interface{}{fmt.Sprintf("projects/%s/regions/%s/resourcePolicies/%s", project, region, obj["name"])} delete(obj, "name") diff --git a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb index 0ebc33936f29..82bee6607463 100644 --- a/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb +++ b/templates/terraform/pre_delete/compute_disk_resource_policies_attachment.go.erb @@ -1,8 +1,7 @@ obj = make(map[string]interface{}) // projects/{project}/regions/{region}/resourcePolicies/{resourceId} -zone := d.Get("zone").(string) -region := zone[:len(zone)-2] +region := getRegionFromZone(d.Get("zone").(string)) name, err := expandComputeDiskResourcePolicyAttachmentName(d.Get("name"), d, config) if err != nil { From b41b21e9ef42ef9ee770b5bfbfbac0da2dcfd27e Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Sat, 7 Sep 2019 11:56:00 +0800 Subject: [PATCH 14/15] resource policy example use google provider, default zone --- products/compute/terraform.yaml | 4 ++++ templates/terraform/examples/resource_policy_basic.tf.erb | 6 ------ templates/terraform/examples/resource_policy_full.tf.erb | 7 ------- 3 files changed, 4 insertions(+), 13 deletions(-) diff --git a/products/compute/terraform.yaml b/products/compute/terraform.yaml index d8e3f13efb33..75ab7cd072d8 100644 --- a/products/compute/terraform.yaml +++ b/products/compute/terraform.yaml @@ -315,6 +315,10 @@ overrides: !ruby/object:Overrides::ResourceOverrides 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 + properties: + zone: !ruby/object:Overrides::Terraform::PropertyOverride + required: false + default_from_api: true 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 diff --git a/templates/terraform/examples/resource_policy_basic.tf.erb b/templates/terraform/examples/resource_policy_basic.tf.erb index ff18bc672f42..e3b2fa0b5038 100644 --- a/templates/terraform/examples/resource_policy_basic.tf.erb +++ b/templates/terraform/examples/resource_policy_basic.tf.erb @@ -1,10 +1,4 @@ -provider "google-beta" { - region = "us-central1" - zone = "us-central1-a" -} - resource "google_compute_resource_policy" "foo" { - provider = "google-beta" name = "<%= ctx[:vars]['name'] %>" region = "us-central1" snapshot_schedule_policy { diff --git a/templates/terraform/examples/resource_policy_full.tf.erb b/templates/terraform/examples/resource_policy_full.tf.erb index 1bf9b44000b7..b7de3477b841 100644 --- a/templates/terraform/examples/resource_policy_full.tf.erb +++ b/templates/terraform/examples/resource_policy_full.tf.erb @@ -1,10 +1,4 @@ -provider "google-beta" { - region = "us-central1" - zone = "us-central1-a" -} - resource "google_compute_resource_policy" "bar" { - provider = "google-beta" name = "<%= ctx[:vars]['name'] %>" region = "us-central1" snapshot_schedule_policy { @@ -27,4 +21,3 @@ resource "google_compute_resource_policy" "bar" { } } } - From b001d149764ff8ea86a540040cf10ac7fad10922 Mon Sep 17 00:00:00 2001 From: Chris Sng Date: Tue, 17 Sep 2019 08:10:24 +0800 Subject: [PATCH 15/15] fix async operation timeout --- products/compute/api.yaml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/products/compute/api.yaml b/products/compute/api.yaml index faad6a6c605d..bb63b2bd5c1a 100644 --- a/products/compute/api.yaml +++ b/products/compute/api.yaml @@ -1328,7 +1328,8 @@ objects: base_url: 'projects/{{project}}/zones/{{zone}}/operations/{{op_id}}' wait_ms: 1000 timeouts: !ruby/object:Api::Timeouts - insert_minutes: 3 + insert_minutes: 4 + delete_minutes: 4 result: !ruby/object:Api::Async::Result path: 'targetLink' status: !ruby/object:Api::Async::Status