Skip to content

Commit

Permalink
allow stack detach
Browse files Browse the repository at this point in the history
  • Loading branch information
maciaszczykm committed Apr 23, 2024
1 parent 369c2bd commit a2701ad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
41 changes: 24 additions & 17 deletions internal/resource/infrastructure_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/resource/infrastructure_stack_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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),
}
}

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

0 comments on commit a2701ad

Please sign in to comment.