From e419bcf63f1c518855ba7dd618dbfcfec5112e5d Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Tue, 26 Sep 2023 20:24:08 +0000 Subject: [PATCH] Disable delete for bare metal admin cluster resource (#8823) Signed-off-by: Modular Magician --- .changelog/8823.txt | 3 + ...urce_gkeonprem_bare_metal_admin_cluster.go | 54 +------ ...bare_metal_admin_cluster_generated_test.go | 46 ------ ...onprem_bare_metal_admin_cluster_sweeper.go | 139 ------------------ ...rem_bare_metal_admin_cluster.html.markdown | 2 +- 5 files changed, 9 insertions(+), 235 deletions(-) create mode 100644 .changelog/8823.txt delete mode 100644 google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_sweeper.go diff --git a/.changelog/8823.txt b/.changelog/8823.txt new file mode 100644 index 0000000000..2bc339c5d9 --- /dev/null +++ b/.changelog/8823.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +baremetal: make delete a noop for the resource `google_bare_metal_admin_cluster` to better align with actual behavior +``` diff --git a/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster.go b/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster.go index f5fc487875..c0b8bde239 100644 --- a/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster.go +++ b/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster.go @@ -46,7 +46,7 @@ func ResourceGkeonpremBareMetalAdminCluster() *schema.Resource { Timeouts: &schema.ResourceTimeout{ Create: schema.DefaultTimeout(60 * time.Minute), Update: schema.DefaultTimeout(60 * time.Minute), - Delete: schema.DefaultTimeout(60 * time.Minute), + Delete: schema.DefaultTimeout(20 * time.Minute), }, CustomizeDiff: customdiff.All( @@ -1182,55 +1182,11 @@ func resourceGkeonpremBareMetalAdminClusterUpdate(d *schema.ResourceData, meta i } func resourceGkeonpremBareMetalAdminClusterDelete(d *schema.ResourceData, meta interface{}) error { - config := meta.(*transport_tpg.Config) - userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) - if err != nil { - return err - } - - billingProject := "" - - project, err := tpgresource.GetProject(d, config) - if err != nil { - return fmt.Errorf("Error fetching project for BareMetalAdminCluster: %s", err) - } - billingProject = project - - url, err := tpgresource.ReplaceVars(d, config, "{{GkeonpremBasePath}}projects/{{project}}/locations/{{location}}/bareMetalAdminClusters/{{name}}:unenroll?ignore_errors=true") - if err != nil { - return err - } - - var obj map[string]interface{} - log.Printf("[DEBUG] Deleting BareMetalAdminCluster %q", d.Id()) - - // err == nil indicates that the billing_project value was found - if bp, err := tpgresource.GetBillingProject(d, config); err == nil { - billingProject = bp - } - - res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "DELETE", - Project: billingProject, - RawURL: url, - UserAgent: userAgent, - Body: obj, - Timeout: d.Timeout(schema.TimeoutDelete), - }) - if err != nil { - return transport_tpg.HandleNotFoundError(err, d, "BareMetalAdminCluster") - } - - err = GkeonpremOperationWaitTime( - config, res, project, "Deleting BareMetalAdminCluster", userAgent, - d.Timeout(schema.TimeoutDelete)) - - if err != nil { - return err - } + log.Printf("[WARNING] Gkeonprem BareMetalAdminCluster resources"+ + " cannot be deleted from Google Cloud. The resource %s will be removed from Terraform"+ + " state, but will still be present on Google Cloud.", d.Id()) + d.SetId("") - log.Printf("[DEBUG] Finished deleting BareMetalAdminCluster %q: %#v", d.Id(), res) return nil } diff --git a/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_generated_test.go b/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_generated_test.go index 28233fc95e..8230b41ecc 100644 --- a/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_generated_test.go +++ b/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_generated_test.go @@ -18,16 +18,11 @@ package gkeonprem_test import ( - "fmt" - "strings" "testing" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" "github.com/hashicorp/terraform-provider-google-beta/google-beta/acctest" - "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" - transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" ) func TestAccGkeonpremBareMetalAdminCluster_gkeonpremBareMetalAdminClusterBasicExample(t *testing.T) { @@ -40,7 +35,6 @@ func TestAccGkeonpremBareMetalAdminCluster_gkeonpremBareMetalAdminClusterBasicEx acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), - CheckDestroy: testAccCheckGkeonpremBareMetalAdminClusterDestroyProducer(t), Steps: []resource.TestStep{ { Config: testAccGkeonpremBareMetalAdminCluster_gkeonpremBareMetalAdminClusterBasicExample(context), @@ -129,7 +123,6 @@ func TestAccGkeonpremBareMetalAdminCluster_gkeonpremBareMetalAdminClusterFullExa acctest.VcrTest(t, resource.TestCase{ PreCheck: func() { acctest.AccTestPreCheck(t) }, ProtoV5ProviderFactories: acctest.ProtoV5ProviderBetaFactories(t), - CheckDestroy: testAccCheckGkeonpremBareMetalAdminClusterDestroyProducer(t), Steps: []resource.TestStep{ { Config: testAccGkeonpremBareMetalAdminCluster_gkeonpremBareMetalAdminClusterFullExample(context), @@ -240,42 +233,3 @@ resource "google_gkeonprem_bare_metal_admin_cluster" "admin-cluster-basic" { } `, context) } - -func testAccCheckGkeonpremBareMetalAdminClusterDestroyProducer(t *testing.T) func(s *terraform.State) error { - return func(s *terraform.State) error { - for name, rs := range s.RootModule().Resources { - if rs.Type != "google_gkeonprem_bare_metal_admin_cluster" { - continue - } - if strings.HasPrefix(name, "data.") { - continue - } - - config := acctest.GoogleProviderConfig(t) - - url, err := tpgresource.ReplaceVarsForTest(config, rs, "{{GkeonpremBasePath}}projects/{{project}}/locations/{{location}}/bareMetalAdminClusters/{{name}}") - if err != nil { - return err - } - - billingProject := "" - - if config.BillingProject != "" { - billingProject = config.BillingProject - } - - _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "GET", - Project: billingProject, - RawURL: url, - UserAgent: config.UserAgent, - }) - if err == nil { - return fmt.Errorf("GkeonpremBareMetalAdminCluster still exists at %s", url) - } - } - - return nil - } -} diff --git a/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_sweeper.go b/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_sweeper.go deleted file mode 100644 index 96664dc526..0000000000 --- a/google-beta/services/gkeonprem/resource_gkeonprem_bare_metal_admin_cluster_sweeper.go +++ /dev/null @@ -1,139 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -// ---------------------------------------------------------------------------- -// -// *** AUTO GENERATED CODE *** Type: MMv1 *** -// -// ---------------------------------------------------------------------------- -// -// This file is automatically generated by Magic Modules and manual -// changes will be clobbered when the file is regenerated. -// -// Please read more about how to change this file in -// .github/CONTRIBUTING.md. -// -// ---------------------------------------------------------------------------- - -package gkeonprem - -import ( - "context" - "log" - "strings" - "testing" - - "github.com/hashicorp/terraform-provider-google-beta/google-beta/envvar" - "github.com/hashicorp/terraform-provider-google-beta/google-beta/sweeper" - "github.com/hashicorp/terraform-provider-google-beta/google-beta/tpgresource" - transport_tpg "github.com/hashicorp/terraform-provider-google-beta/google-beta/transport" -) - -func init() { - sweeper.AddTestSweepers("GkeonpremBareMetalAdminCluster", testSweepGkeonpremBareMetalAdminCluster) -} - -// At the time of writing, the CI only passes us-central1 as the region -func testSweepGkeonpremBareMetalAdminCluster(region string) error { - resourceName := "GkeonpremBareMetalAdminCluster" - log.Printf("[INFO][SWEEPER_LOG] Starting sweeper for %s", resourceName) - - config, err := sweeper.SharedConfigForRegion(region) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error getting shared config for region: %s", err) - return err - } - - err = config.LoadAndValidate(context.Background()) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error loading: %s", err) - return err - } - - t := &testing.T{} - billingId := envvar.GetTestBillingAccountFromEnv(t) - - // Setup variables to replace in list template - d := &tpgresource.ResourceDataMock{ - FieldsInSchema: map[string]interface{}{ - "project": config.Project, - "region": region, - "location": region, - "zone": "-", - "billing_account": billingId, - }, - } - - listTemplate := strings.Split("https://gkeonprem.googleapis.com/v1/projects/{{project}}/locations/{{location}}/bareMetalAdminClusters", "?")[0] - listUrl, err := tpgresource.ReplaceVars(d, config, listTemplate) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error preparing sweeper list url: %s", err) - return nil - } - - res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "GET", - Project: config.Project, - RawURL: listUrl, - UserAgent: config.UserAgent, - }) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] Error in response from request %s: %s", listUrl, err) - return nil - } - - resourceList, ok := res["bareMetalAdminClusters"] - if !ok { - log.Printf("[INFO][SWEEPER_LOG] Nothing found in response.") - return nil - } - - rl := resourceList.([]interface{}) - - log.Printf("[INFO][SWEEPER_LOG] Found %d items in %s list response.", len(rl), resourceName) - // Keep count of items that aren't sweepable for logging. - nonPrefixCount := 0 - for _, ri := range rl { - obj := ri.(map[string]interface{}) - if obj["name"] == nil { - log.Printf("[INFO][SWEEPER_LOG] %s resource name was nil", resourceName) - return nil - } - - name := tpgresource.GetResourceNameFromSelfLink(obj["name"].(string)) - // Skip resources that shouldn't be sweeped - if !sweeper.IsSweepableTestResource(name) { - nonPrefixCount++ - continue - } - - deleteTemplate := "https://gkeonprem.googleapis.com/v1/projects/{{project}}/locations/{{location}}/bareMetalAdminClusters/{{name}}:unenroll?ignore_errors=true" - deleteUrl, err := tpgresource.ReplaceVars(d, config, deleteTemplate) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] error preparing delete url: %s", err) - return nil - } - deleteUrl = deleteUrl + name - - // Don't wait on operations as we may have a lot to delete - _, err = transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ - Config: config, - Method: "DELETE", - Project: config.Project, - RawURL: deleteUrl, - UserAgent: config.UserAgent, - }) - if err != nil { - log.Printf("[INFO][SWEEPER_LOG] Error deleting for url %s : %s", deleteUrl, err) - } else { - log.Printf("[INFO][SWEEPER_LOG] Sent delete request for %s resource: %s", resourceName, name) - } - } - - if nonPrefixCount > 0 { - log.Printf("[INFO][SWEEPER_LOG] %d items were non-sweepable and skipped.", nonPrefixCount) - } - - return nil -} diff --git a/website/docs/r/gkeonprem_bare_metal_admin_cluster.html.markdown b/website/docs/r/gkeonprem_bare_metal_admin_cluster.html.markdown index 29eaf2071a..206893d011 100644 --- a/website/docs/r/gkeonprem_bare_metal_admin_cluster.html.markdown +++ b/website/docs/r/gkeonprem_bare_metal_admin_cluster.html.markdown @@ -711,7 +711,7 @@ This resource provides the following - `create` - Default is 60 minutes. - `update` - Default is 60 minutes. -- `delete` - Default is 60 minutes. +- `delete` - Default is 20 minutes. ## Import