diff --git a/docs/resources/cluster.md b/docs/resources/cluster.md index 6aa39f9b..8d3d9f5e 100644 --- a/docs/resources/cluster.md +++ b/docs/resources/cluster.md @@ -22,6 +22,7 @@ A representation of a cluster you can deploy to. ### Optional - `bindings` (Attributes) Read and write policies of this cluster. (see [below for nested schema](#nestedatt--bindings)) +- `detach` (Boolean) Determines behavior during resource destruction, if true it will detach resource instead of deleting it. - `handle` (String) A short, unique human-readable name used to identify this cluster. Does not necessarily map to the cloud resource name. - `helm_repo_url` (String) Helm repository URL you'd like to use in deployment agent Helm install. - `helm_values` (String) Additional Helm values you'd like to use in deployment agent Helm installs. This is useful for BYOK clusters that need to use custom images or other constructs. diff --git a/example/cluster/byok/main.tf b/example/cluster/byok/main.tf index cffceb5c..0dfff9c7 100644 --- a/example/cluster/byok/main.tf +++ b/example/cluster/byok/main.tf @@ -2,7 +2,7 @@ terraform { required_providers { plural = { source = "pluralsh/plural" - version = "0.0.1" + version = "0.2.1" } } } @@ -12,8 +12,9 @@ provider "plural" { } resource "plural_cluster" "byok" { - name = "byok" - protect = "false" + name = "byok" + protect = "false" + detach = true kubeconfig = { # Required, can be sourced from environment variables # export PLURAL_KUBE_CONFIG_PATH to read from local file diff --git a/internal/resource/cluster.go b/internal/resource/cluster.go index d1a640ff..3c3b9b27 100644 --- a/internal/resource/cluster.go +++ b/internal/resource/cluster.go @@ -132,23 +132,35 @@ func (r *clusterResource) Delete(ctx context.Context, req resource.DeleteRequest return } - _, err := r.client.DeleteCluster(ctx, data.Id.ValueString()) - if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete cluster, got error: %s", err)) - return - } - - err = wait.WaitForWithContext(ctx, client.Ticker(5*time.Second), func(ctx context.Context) (bool, error) { - response, err := r.client.GetCluster(ctx, data.Id.ValueStringPointer()) - if client.IsNotFound(err) || response.Cluster == nil { - return true, nil + if data.Detach.ValueBool() { + _, err := r.client.DetachCluster(ctx, data.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to detach cluster, got error: %s", err)) + return + } + } else { + _, err := r.client.DeleteCluster(ctx, data.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete cluster, got error: %s", err)) + return } - return false, err - }) - if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error while watiting for cluster to be deleted, got error: %s", err)) - return + if err = wait.PollWithContext(ctx, 10*time.Second, 10*time.Minute, func(ctx context.Context) (bool, error) { + response, err := r.client.GetCluster(ctx, data.Id.ValueStringPointer()) + if client.IsNotFound(err) || response.Cluster == nil { + return true, nil + } + + return false, err + }); err != nil { + resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf("Error while watiting for cluster to be deleted, got error: %s", err)) + + _, err = r.client.DetachCluster(ctx, data.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to detach cluster, got error: %s", err)) + return + } + } } } diff --git a/internal/resource/cluster_model.go b/internal/resource/cluster_model.go index 2b1a87e2..355aaf53 100644 --- a/internal/resource/cluster_model.go +++ b/internal/resource/cluster_model.go @@ -18,6 +18,7 @@ type cluster struct { Name types.String `tfsdk:"name"` Handle types.String `tfsdk:"handle"` ProjectId types.String `tfsdk:"project_id"` + Detach types.Bool `tfsdk:"detach"` // Version types.String `tfsdk:"version"` // DesiredVersion types.String `tfsdk:"desired_version"` // ProviderId types.String `tfsdk:"provider_id"` diff --git a/internal/resource/cluster_schema.go b/internal/resource/cluster_schema.go index 1585e901..05b5048e 100644 --- a/internal/resource/cluster_schema.go +++ b/internal/resource/cluster_schema.go @@ -53,6 +53,13 @@ func (r *clusterResource) schema() schema.Schema { Optional: true, PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, }, + "detach": schema.BoolAttribute{ + Description: "Determines behavior during resource destruction, if true it will detach resource instead of deleting it.", + MarkdownDescription: "Determines behavior during resource destruction, if true it will detach resource instead of deleting it.", + Optional: true, + Computed: true, + Default: booldefault.StaticBool(false), + }, // "version": schema.StringAttribute{ // Description: "Kubernetes version to use for this cluster. Leave empty for bring your own cluster. Supported version ranges can be found at https://github.com/pluralsh/console/tree/master/static/k8s-versions.", // MarkdownDescription: "Kubernetes version to use for this cluster. Leave empty for bring your own cluster. Supported version ranges can be found at https://github.com/pluralsh/console/tree/master/static/k8s-versions.",