Skip to content

Commit

Permalink
Add Pooling to delete cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
uzaxirr committed Aug 15, 2024
1 parent e9b5377 commit 9f7af0e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
28 changes: 27 additions & 1 deletion civo/kubernetes/resource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kubernetes

import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"log"
"strings"
"time"
Expand Down Expand Up @@ -460,7 +462,7 @@ func resourceKubernetesClusterUpdate(ctx context.Context, d *schema.ResourceData
}

// function to delete the kubernetes cluster
func resourceKubernetesClusterDelete(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func resourceKubernetesClusterDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if it is defined in the datasource
Expand All @@ -474,6 +476,30 @@ func resourceKubernetesClusterDelete(_ context.Context, d *schema.ResourceData,
return diag.Errorf("[INFO] an error occurred while trying to delete the kubernetes cluster %s", err)
}

// Wait for the cluster to be completely deleted
deleteStateConf := &retry.StateChangeConf{
Pending: []string{"DELETING"},
Target: []string{"DELETED"},
Refresh: func() (interface{}, string, error) {
resp, err := apiClient.GetKubernetesCluster(d.Id())
if err != nil {
if errors.Is(err, civogo.DatabaseKubernetesClusterNotFoundError) {
return 0, "DELETED", nil
}
return 0, "", err
}
return resp, resp.Status, nil
},
Timeout: 60 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 5 * time.Second,
NotFoundChecks: 10,
}
_, err = deleteStateConf.WaitForStateContext(ctx)
if err != nil {
return diag.Errorf("error waiting for instance (%s) to be deleted: %s", d.Id(), err)
}

return nil
}

Expand Down
5 changes: 2 additions & 3 deletions civo/network/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"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 @@ -239,7 +239,7 @@ func resourceNetworkDelete(_ context.Context, d *schema.ResourceData, m interfac
networkID := d.Id()
log.Printf("[INFO] Deleting the network %s", networkID)

deleteStateConf := &resource.StateChangeConf{
deleteStateConf := &retry.StateChangeConf{
Pending: []string{"deleting", "exists"},
Target: []string{"deleted"},
Refresh: func() (interface{}, string, error) {
Expand All @@ -248,7 +248,6 @@ func resourceNetworkDelete(_ context.Context, d *schema.ResourceData, m interfac
if err != nil {
return 0, "", err
}

// If delete was successful, start polling
if resp.Result == "success" {
// Check if the network still exists
Expand Down

0 comments on commit 9f7af0e

Please sign in to comment.