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

Add delete retries #358

Merged
merged 4 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions vultr/resource_vultr_load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,19 @@ func resourceVultrLoadBalancerDelete(ctx context.Context, d *schema.ResourceData

log.Printf("[INFO] Deleting load balancer: %v", d.Id())

// items we should detach before we destroy
// instances and firewall rules are default "null" if not present in LoadBalancerReq
detachConfig := &govultr.LoadBalancerReq{}

if _, vpcOK := d.GetOk("vpc"); vpcOK {
detachConfig.VPC = govultr.StringToStringPtr("")
}

// send update to perform detach
if err := client.LoadBalancer.Update(ctx, d.Id(), detachConfig); err != nil {
return diag.Errorf("error detaching VPC from load balancer before deletion (%v): %v", d.Id(), err)
}

if err := client.LoadBalancer.Delete(ctx, d.Id()); err != nil {
return diag.Errorf("error deleting load balancer %v : %v", d.Id(), err)
}
Expand Down
20 changes: 19 additions & 1 deletion vultr/resource_vultr_vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package vultr

import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/vultr/govultr/v3"
Expand Down Expand Up @@ -121,7 +124,22 @@ func resourceVultrVPCDelete(ctx context.Context, d *schema.ResourceData, meta in
client := meta.(*Client).govultrClient()

log.Printf("[INFO] Deleting VPC: %s", d.Id())
if err := client.VPC.Delete(ctx, d.Id()); err != nil {

err := retry.RetryContext(ctx, d.Timeout(schema.TimeoutCreate)-time.Minute, func() *retry.RetryError {
err := client.VPC.Delete(ctx, d.Id())

if err == nil {
return nil
}

if strings.Contains(err.Error(), "VPC is attached") {
return retry.RetryableError(fmt.Errorf("cannot remove attached VPC: %s", err.Error()))
}

return retry.NonRetryableError(err)
})

if err != nil {
return diag.Errorf("error destroying VPC (%s): %v", d.Id(), err)
}

Expand Down