From 9a502e951070512b5de481615f88a736ced00678 Mon Sep 17 00:00:00 2001 From: Swamita Gupta Date: Wed, 22 May 2024 06:53:14 +0000 Subject: [PATCH] Add Delayed Deletion Hours parameter to Vmwareengine Private Cloud --- mmv1/products/vmwareengine/PrivateCloud.yaml | 9 +- .../vmware_engine_private_cloud_full.tf.erb | 1 + .../post_delete/private_cloud.go.erb | 7 ++ ...esource_vmwareengine_private_cloud_test.go | 106 ++++++++++-------- 4 files changed, 75 insertions(+), 48 deletions(-) diff --git a/mmv1/products/vmwareengine/PrivateCloud.yaml b/mmv1/products/vmwareengine/PrivateCloud.yaml index 3d7e3db1381f..f740e27dac3b 100644 --- a/mmv1/products/vmwareengine/PrivateCloud.yaml +++ b/mmv1/products/vmwareengine/PrivateCloud.yaml @@ -15,7 +15,7 @@ name: 'PrivateCloud' base_url: 'projects/{{project}}/locations/{{location}}/privateClouds' self_link: 'projects/{{project}}/locations/{{location}}/privateClouds/{{name}}' -delete_url: 'projects/{{project}}/locations/{{location}}/privateClouds/{{name}}?delay_hours=0' +delete_url: 'projects/{{project}}/locations/{{location}}/privateClouds/{{name}}?delay_hours={{deletion_delay_hours}}' create_url: 'projects/{{project}}/locations/{{location}}/privateClouds?privateCloudId={{name}}' update_verb: :PATCH references: !ruby/object:Api::Resource::ReferenceLinks @@ -89,6 +89,13 @@ parameters: url_param_only: true description: | The ID of the PrivateCloud. + - !ruby/object:Api::Type::Integer + name: "deletionDelayHours" + url_param_only: true + ignore_read: true + default_value: 3 + description: | + The number of hours to delay this request; the default is 3. You can set an hour between 0 to 8. Setting this value to 0 starts the deletion request immediately. properties: - !ruby/object:Api::Type::String diff --git a/mmv1/templates/terraform/examples/vmware_engine_private_cloud_full.tf.erb b/mmv1/templates/terraform/examples/vmware_engine_private_cloud_full.tf.erb index ee12d6bfdd24..773d2896aea5 100644 --- a/mmv1/templates/terraform/examples/vmware_engine_private_cloud_full.tf.erb +++ b/mmv1/templates/terraform/examples/vmware_engine_private_cloud_full.tf.erb @@ -16,6 +16,7 @@ resource "google_vmwareengine_private_cloud" "<%= ctx[:primary_resource_id] %>" custom_core_count = 32 } } + deletion_delay_hours = 4 } resource "google_vmwareengine_network" "pc-nw" { diff --git a/mmv1/templates/terraform/post_delete/private_cloud.go.erb b/mmv1/templates/terraform/post_delete/private_cloud.go.erb index 669377a2f9ab..1dd7c3133039 100644 --- a/mmv1/templates/terraform/post_delete/private_cloud.go.erb +++ b/mmv1/templates/terraform/post_delete/private_cloud.go.erb @@ -26,9 +26,16 @@ privateCloudPollRead := func(d *schema.ResourceData, meta interface{}) transport RawURL: url, UserAgent: userAgent, }) + // if resource does not exist if err != nil { return res, err } + // if resource exists but is deleted + v, ok := res["state"] + if ok && v.(string) == "DELETED" { + return res, errors.New("PrivateCloud is marked for deletion") + } + // if resource exists return res, nil } } diff --git a/mmv1/third_party/terraform/services/vmwareengine/resource_vmwareengine_private_cloud_test.go b/mmv1/third_party/terraform/services/vmwareengine/resource_vmwareengine_private_cloud_test.go index 162cad8d35c5..306df6112116 100644 --- a/mmv1/third_party/terraform/services/vmwareengine/resource_vmwareengine_private_cloud_test.go +++ b/mmv1/third_party/terraform/services/vmwareengine/resource_vmwareengine_private_cloud_test.go @@ -18,7 +18,7 @@ func TestAccVmwareenginePrivateCloud_vmwareEnginePrivateCloudUpdate(t *testing.T t.Parallel() context := map[string]interface{}{ - "region": "southamerica-west1", + "region": "me-west1", "random_suffix": acctest.RandString(t, 10), "org_id": envvar.GetTestOrgFromEnv(t), "billing_account": envvar.GetTestBillingAccountFromEnv(t), @@ -33,7 +33,7 @@ func TestAccVmwareenginePrivateCloud_vmwareEnginePrivateCloudUpdate(t *testing.T CheckDestroy: testAccCheckVmwareenginePrivateCloudDestroyProducer(t), Steps: []resource.TestStep{ { - Config: testPrivateCloudUpdateConfig(context, "description1", 1), + Config: testPrivateCloudCreateConfig(context), Check: resource.ComposeTestCheckFunc( acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_vmwareengine_private_cloud.ds", "google_vmwareengine_private_cloud.vmw-engine-pc", map[string]struct{}{"type": {}}), testAccCheckGoogleVmwareengineNsxCredentialsMeta("data.google_vmwareengine_nsx_credentials.nsx-ds"), @@ -47,16 +47,10 @@ func TestAccVmwareenginePrivateCloud_vmwareEnginePrivateCloudUpdate(t *testing.T ImportStateVerifyIgnore: []string{"location", "name", "update_time", "type"}, }, { - Config: testPrivateCloudUpdateConfig(context, "description2", 4), // Expand PC - }, - { - ResourceName: "google_vmwareengine_private_cloud.vmw-engine-pc", - ImportState: true, - ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"location", "name", "update_time", "type"}, - }, - { - Config: testPrivateCloudUpdateConfig(context, "description2", 3), // Shrink PC + Config: testPrivateCloudUpdateConfig(context), + Check: resource.ComposeTestCheckFunc( + acctest.CheckDataSourceStateMatchesResourceStateWithIgnores("data.google_vmwareengine_private_cloud.ds", "google_vmwareengine_private_cloud.vmw-engine-pc", map[string]struct{}{"type": {}}), + ), }, { ResourceName: "google_vmwareengine_private_cloud.vmw-engine-pc", @@ -68,46 +62,19 @@ func TestAccVmwareenginePrivateCloud_vmwareEnginePrivateCloudUpdate(t *testing.T }) } -func testPrivateCloudUpdateConfig(context map[string]interface{}, description string, nodeCount int) string { - context["node_count"] = nodeCount - context["description"] = description - +func testPrivateCloudCreateConfig(context map[string]interface{}) string { return acctest.Nprintf(` -resource "google_project" "project" { - project_id = "tf-test%{random_suffix}" - name = "tf-test%{random_suffix}" - org_id = "%{org_id}" - billing_account = "%{billing_account}" -} - -resource "google_project_service" "vmwareengine" { - project = google_project.project.project_id - service = "vmwareengine.googleapis.com" -} - -resource "time_sleep" "sleep" { - create_duration = "1m" - depends_on = [ - google_project_service.vmwareengine, - ] -} - resource "google_vmwareengine_network" "default-nw" { - project = google_project.project.project_id name = "tf-test-pc-nw-%{random_suffix}" location = "global" type = "STANDARD" description = "PC network description." - depends_on = [ - time_sleep.sleep # Sleep allows permissions in the new project to propagate - ] } resource "google_vmwareengine_private_cloud" "vmw-engine-pc" { - project = google_project.project.project_id - location = "%{region}-a" + location = "%{region}-b" name = "tf-test-sample-pc%{random_suffix}" - description = "%{description}" + description = "test description" type = "TIME_LIMITED" network_config { management_cidr = "192.168.30.0/24" @@ -117,15 +84,14 @@ resource "google_vmwareengine_private_cloud" "vmw-engine-pc" { cluster_id = "tf-test-sample-mgmt-cluster-custom-core-count%{random_suffix}" node_type_configs { node_type_id = "standard-72" - node_count = "%{node_count}" + node_count = "1" custom_core_count = 32 } } } data "google_vmwareengine_private_cloud" "ds" { - project = google_project.project.project_id - location = "%{region}-a" + location = "%{region}-b" name = "tf-test-sample-pc%{random_suffix}" depends_on = [ google_vmwareengine_private_cloud.vmw-engine-pc, @@ -144,6 +110,45 @@ data "google_vmwareengine_vcenter_credentials" "vcenter-ds" { `, context) } +func testPrivateCloudUpdateConfig(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_vmwareengine_network" "default-nw" { + name = "tf-test-pc-nw-%{random_suffix}" + location = "global" + type = "STANDARD" + description = "PC network description." +} + +resource "google_vmwareengine_private_cloud" "vmw-engine-pc" { + location = "%{region}-b" + name = "tf-test-sample-pc%{random_suffix}" + description = "updated description" + deletion_delay_hours = 8 + type = "TIME_LIMITED" + network_config { + management_cidr = "192.168.30.0/24" + vmware_engine_network = google_vmwareengine_network.default-nw.id + } + management_cluster { + cluster_id = "tf-test-sample-mgmt-cluster-custom-core-count%{random_suffix}" + node_type_configs { + node_type_id = "standard-72" + node_count = "3" + custom_core_count = 32 + } + } +} + +data "google_vmwareengine_private_cloud" "ds" { + location = "%{region}-b" + name = "tf-test-sample-pc%{random_suffix}" + depends_on = [ + google_vmwareengine_private_cloud.vmw-engine-pc, + ] +} +`, context) +} + func testAccCheckGoogleVmwareengineNsxCredentialsMeta(n string) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -198,14 +203,21 @@ func testAccCheckVmwareenginePrivateCloudDestroyProducer(t *testing.T) func(s *t if config.BillingProject != "" { billingProject = config.BillingProject } - _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ Config: config, Method: "GET", Project: billingProject, RawURL: url, UserAgent: config.UserAgent, }) - if err == nil { + if err != nil { + return nil + } + v, ok := res["state"] + if !ok { + return nil + } + if err == nil || v.(string) != "DELETED" { return fmt.Errorf("VmwareenginePrivateCloud still exists at %s", url) } }