Skip to content

Commit

Permalink
Disable delete for bare metal admin cluster resource (#8823)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician committed Sep 26, 2023
1 parent 1aa3b3a commit e419bcf
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 235 deletions.
3 changes: 3 additions & 0 deletions .changelog/8823.txt
Original file line number Diff line number Diff line change
@@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit e419bcf

Please sign in to comment.