Skip to content

Commit 6b54ee0

Browse files
Merge pull request #40 from pluralsh/marcin/prod-2236-add-detach-attribute-to-terraform-provider
feat: Add detach attribute to cluster resource
2 parents 0011626 + 40ed570 commit 6b54ee0

File tree

5 files changed

+40
-18
lines changed

5 files changed

+40
-18
lines changed

docs/resources/cluster.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ A representation of a cluster you can deploy to.
2222
### Optional
2323

2424
- `bindings` (Attributes) Read and write policies of this cluster. (see [below for nested schema](#nestedatt--bindings))
25+
- `detach` (Boolean) Determines behavior during resource destruction, if true it will detach resource instead of deleting it.
2526
- `handle` (String) A short, unique human-readable name used to identify this cluster. Does not necessarily map to the cloud resource name.
2627
- `helm_repo_url` (String) Helm repository URL you'd like to use in deployment agent Helm install.
2728
- `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.

example/cluster/byok/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
plural = {
44
source = "pluralsh/plural"
5-
version = "0.0.1"
5+
version = "0.2.1"
66
}
77
}
88
}
@@ -12,8 +12,9 @@ provider "plural" {
1212
}
1313

1414
resource "plural_cluster" "byok" {
15-
name = "byok"
16-
protect = "false"
15+
name = "byok"
16+
protect = "false"
17+
detach = true
1718
kubeconfig = {
1819
# Required, can be sourced from environment variables
1920
# export PLURAL_KUBE_CONFIG_PATH to read from local file

internal/resource/cluster.go

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,23 +132,35 @@ func (r *clusterResource) Delete(ctx context.Context, req resource.DeleteRequest
132132
return
133133
}
134134

135-
_, err := r.client.DeleteCluster(ctx, data.Id.ValueString())
136-
if err != nil {
137-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete cluster, got error: %s", err))
138-
return
139-
}
140-
141-
err = wait.WaitForWithContext(ctx, client.Ticker(5*time.Second), func(ctx context.Context) (bool, error) {
142-
response, err := r.client.GetCluster(ctx, data.Id.ValueStringPointer())
143-
if client.IsNotFound(err) || response.Cluster == nil {
144-
return true, nil
135+
if data.Detach.ValueBool() {
136+
_, err := r.client.DetachCluster(ctx, data.Id.ValueString())
137+
if err != nil {
138+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to detach cluster, got error: %s", err))
139+
return
140+
}
141+
} else {
142+
_, err := r.client.DeleteCluster(ctx, data.Id.ValueString())
143+
if err != nil {
144+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete cluster, got error: %s", err))
145+
return
145146
}
146147

147-
return false, err
148-
})
149-
if err != nil {
150-
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error while watiting for cluster to be deleted, got error: %s", err))
151-
return
148+
if err = wait.PollWithContext(ctx, 10*time.Second, 10*time.Minute, func(ctx context.Context) (bool, error) {
149+
response, err := r.client.GetCluster(ctx, data.Id.ValueStringPointer())
150+
if client.IsNotFound(err) || response.Cluster == nil {
151+
return true, nil
152+
}
153+
154+
return false, err
155+
}); err != nil {
156+
resp.Diagnostics.AddWarning("Client Error", fmt.Sprintf("Error while watiting for cluster to be deleted, got error: %s", err))
157+
158+
_, err = r.client.DetachCluster(ctx, data.Id.ValueString())
159+
if err != nil {
160+
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to detach cluster, got error: %s", err))
161+
return
162+
}
163+
}
152164
}
153165
}
154166

internal/resource/cluster_model.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type cluster struct {
1717
InsertedAt types.String `tfsdk:"inserted_at"`
1818
Name types.String `tfsdk:"name"`
1919
Handle types.String `tfsdk:"handle"`
20+
Detach types.Bool `tfsdk:"detach"`
2021
// Version types.String `tfsdk:"version"`
2122
// DesiredVersion types.String `tfsdk:"desired_version"`
2223
// ProviderId types.String `tfsdk:"provider_id"`

internal/resource/cluster_schema.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ func (r *clusterResource) schema() schema.Schema {
4646
Computed: true,
4747
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
4848
},
49+
"detach": schema.BoolAttribute{
50+
Description: "Determines behavior during resource destruction, if true it will detach resource instead of deleting it.",
51+
MarkdownDescription: "Determines behavior during resource destruction, if true it will detach resource instead of deleting it.",
52+
Optional: true,
53+
Computed: true,
54+
Default: booldefault.StaticBool(false),
55+
},
4956
// "version": schema.StringAttribute{
5057
// 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.",
5158
// 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.",

0 commit comments

Comments
 (0)