Skip to content

Commit

Permalink
Add WaitForStateto the network delete (#179)
Browse files Browse the repository at this point in the history
* Add WaitForStateto the network delete
* Fix fmt in the project
  • Loading branch information
alejandrojnm committed Jun 12, 2023
1 parent 0c24127 commit 237bae0
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions civo/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package civo
import (
"context"
"log"
"time"

"github.com/civo/civogo"
"github.com/civo/terraform-provider-civo/internal/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -133,10 +135,35 @@ func resourceNetworkDelete(_ context.Context, d *schema.ResourceData, m interfac
apiClient.Region = region.(string)
}

log.Printf("[INFO] deleting the network %s", d.Id())
_, err := apiClient.DeleteNetwork(d.Id())
netowrkID := d.Id()
log.Printf("[INFO] Checking if firewall %s exists", netowrkID)
_, err := apiClient.FindNetwork(netowrkID)
if err != nil {
return diag.Errorf("[ERR] an error occurred while trying to delete the network %s", d.Id())
log.Printf("[INFO] Unable to find network %s - probably it's been deleted", netowrkID)
return nil
}

log.Printf("[INFO] deleting the network %s", netowrkID)

deleteStateConf := &resource.StateChangeConf{
Pending: []string{"failed"},
Target: []string{"success"},
Refresh: func() (interface{}, string, error) {
resp, err := apiClient.DeleteNetwork(netowrkID)
if err != nil {
return 0, "", err
}
return resp, string(resp.Result), nil
},
Timeout: 60 * time.Minute,
Delay: 3 * time.Second,
MinTimeout: 3 * time.Second,
NotFoundChecks: 10,
}
_, err = deleteStateConf.WaitForStateContext(context.Background())
if err != nil {
return diag.Errorf("error waiting for network (%s) to be deleted: %s", netowrkID, err)
}

return nil
}

0 comments on commit 237bae0

Please sign in to comment.