Skip to content

Commit

Permalink
Add delete retries (#358)
Browse files Browse the repository at this point in the history
* Add initial attempt at using rety on VPC

* Add VPC detach update when deleting LB

* Use RetryContext instead of deprecated Retry

* Fix comments
  • Loading branch information
optik-aper authored May 10, 2023
1 parent 25fdcb4 commit 3f03eae
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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

0 comments on commit 3f03eae

Please sign in to comment.