Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into marcin/prod-2233-add-…
Browse files Browse the repository at this point in the history
…support-for-projects-to-terraform-provider
  • Loading branch information
maciaszczykm committed Jun 14, 2024
2 parents 18e5dc9 + 6b54ee0 commit 0f5a607
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions example/cluster/byok/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
plural = {
source = "pluralsh/plural"
version = "0.0.1"
version = "0.2.1"
}
}
}
Expand All @@ -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
Expand Down
42 changes: 27 additions & 15 deletions internal/resource/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions internal/resource/cluster_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
7 changes: 7 additions & 0 deletions internal/resource/cluster_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down

0 comments on commit 0f5a607

Please sign in to comment.