From a2701ad5418346e497ac855ed8a8f44f1ec95382 Mon Sep 17 00:00:00 2001 From: Marcin Maciaszczyk Date: Tue, 23 Apr 2024 14:54:37 +0200 Subject: [PATCH] allow stack detach --- internal/resource/infrastructure_stack.go | 41 +++++++++++-------- .../resource/infrastructure_stack_model.go | 3 +- .../resource/infrastructure_stack_schema.go | 7 ++++ 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/internal/resource/infrastructure_stack.go b/internal/resource/infrastructure_stack.go index 88e41bb..5bee9ae 100644 --- a/internal/resource/infrastructure_stack.go +++ b/internal/resource/infrastructure_stack.go @@ -5,13 +5,12 @@ import ( "fmt" "time" + "terraform-provider-plural/internal/client" "terraform-provider-plural/internal/common" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "k8s.io/apimachinery/pkg/util/wait" - - "terraform-provider-plural/internal/client" ) var _ resource.Resource = &InfrastructureStackResource{} @@ -109,23 +108,31 @@ func (r *InfrastructureStackResource) Delete(ctx context.Context, req resource.D return } - _, err := r.client.DeleteStack(ctx, data.Id.ValueString()) - if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete infrastructure stack, got error: %s", err)) - return - } - - err = wait.WaitForWithContext(ctx, client.Ticker(5*time.Second), func(ctx context.Context) (bool, error) { - _, err := r.client.GetInfrastructureStack(ctx, data.Id.ValueString()) - if client.IsNotFound(err) { - return true, nil + if data.Detach.ValueBool() { + _, err := r.client.DetachStack(ctx, data.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to detach infrastructure stack, got error: %s", err)) + return + } + } else { + _, err := r.client.DeleteStack(ctx, data.Id.ValueString()) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete infrastructure stack, got error: %s", err)) + return } - return false, err - }) - if err != nil { - resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error during watiting for infrastructure stack to be deleted, got error: %s", err)) - return + err = wait.WaitForWithContext(ctx, client.Ticker(5*time.Second), func(ctx context.Context) (bool, error) { + _, err := r.client.GetInfrastructureStack(ctx, data.Id.ValueString()) + if client.IsNotFound(err) { + return true, nil + } + + return false, err + }) + if err != nil { + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Error during watiting for infrastructure stack to be deleted, got error: %s", err)) + return + } } } diff --git a/internal/resource/infrastructure_stack_model.go b/internal/resource/infrastructure_stack_model.go index eb1823f..ba9646a 100644 --- a/internal/resource/infrastructure_stack_model.go +++ b/internal/resource/infrastructure_stack_model.go @@ -18,6 +18,7 @@ type infrastructureStack struct { Name types.String `tfsdk:"name"` Type types.String `tfsdk:"type"` Approval types.Bool `tfsdk:"approval"` + Detach types.Bool `tfsdk:"detach"` ClusterId types.String `tfsdk:"cluster_id"` Repository *InfrastructureStackRepository `tfsdk:"repository"` Configuration *InfrastructureStackConfiguration `tfsdk:"configuration"` @@ -40,7 +41,7 @@ func (is *infrastructureStack) Attributes(ctx context.Context, d diag.Diagnostic ReadBindings: is.Bindings.ReadAttributes(ctx, d), WriteBindings: is.Bindings.WriteAttributes(ctx, d), Files: is.FilesAttributes(ctx, d), - Environemnt: is.EnvironmentAttributes(ctx, d), + Environment: is.EnvironmentAttributes(ctx, d), } } diff --git a/internal/resource/infrastructure_stack_schema.go b/internal/resource/infrastructure_stack_schema.go index 1c8f8d0..c9c9a8c 100644 --- a/internal/resource/infrastructure_stack_schema.go +++ b/internal/resource/infrastructure_stack_schema.go @@ -8,6 +8,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" @@ -43,6 +44,12 @@ func (r *InfrastructureStackResource) schema() schema.Schema { MarkdownDescription: "Determines whether to require approval.", Optional: true, }, + "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, + Default: booldefault.StaticBool(false), + }, "cluster_id": schema.StringAttribute{ Description: "The cluster on which the stack will be applied.", MarkdownDescription: "The cluster on which the stack will be applied.",