Skip to content

Commit 952b2ec

Browse files
authored
Merge pull request #44406 from hashicorp/f-dsql-cluster-force-destroy
resource/aws_dsql_cluster: Fixes `deletion_protection_enabled`, adds `force_destroy`, and fixes sweeper
2 parents 0201424 + 08d6fcc commit 952b2ec

File tree

12 files changed

+103
-177
lines changed

12 files changed

+103
-177
lines changed

.changelog/44406.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:bug
2+
resource/aws_dsql_cluster: Prevents error when optional attribute `deletion_protection_enabled` not set.
3+
```
4+
5+
```release-note:enhancement
6+
resource/aws_dsql_cluster: Adds attribute `force_destroy`.
7+
```

internal/service/dsql/cluster.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/hashicorp/terraform-plugin-framework/path"
2020
"github.com/hashicorp/terraform-plugin-framework/resource"
2121
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
22+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
2223
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
2324
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
2425
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
@@ -43,6 +44,7 @@ import (
4344
// @Tags(identifierAttribute="arn")
4445
// @Testing(existsType="github.com/aws/aws-sdk-go-v2/service/dsql;dsql.GetClusterOutput")
4546
// @Testing(importStateIdAttribute="identifier")
47+
// @Testing(generator=false)
4648
func newClusterResource(_ context.Context) (resource.ResourceWithConfigure, error) {
4749
r := &clusterResource{}
4850

@@ -64,8 +66,15 @@ func (r *clusterResource) Schema(ctx context.Context, request resource.SchemaReq
6466
names.AttrARN: framework.ARNAttributeComputedOnly(),
6567
"deletion_protection_enabled": schema.BoolAttribute{
6668
Optional: true,
69+
Computed: true,
70+
Default: booldefault.StaticBool(false),
6771
},
6872
"encryption_details": framework.ResourceComputedListOfObjectsAttribute[encryptionDetailsModel](ctx),
73+
names.AttrForceDestroy: schema.BoolAttribute{
74+
Optional: true,
75+
Computed: true,
76+
Default: booldefault.StaticBool(false),
77+
},
6978
names.AttrIdentifier: framework.IDAttribute(),
7079
"kms_encryption_key": schema.StringAttribute{
7180
Optional: true,
@@ -309,6 +318,19 @@ func (r *clusterResource) Delete(ctx context.Context, request resource.DeleteReq
309318

310319
conn := r.Meta().DSQLClient(ctx)
311320

321+
if data.ForceDestroy.ValueBool() {
322+
input := dsql.UpdateClusterInput{
323+
Identifier: data.Identifier.ValueStringPointer(),
324+
DeletionProtectionEnabled: aws.Bool(false),
325+
ClientToken: aws.String(sdkid.UniqueId()),
326+
}
327+
// Changing DeletionProtectionEnabled is instantaneous, no need to wait.
328+
if _, err := conn.UpdateCluster(ctx, &input); err != nil {
329+
response.Diagnostics.AddError(fmt.Sprintf("disabling deletion protection for Aurora DSQL Cluster (%s)", data.Identifier.ValueString()), err.Error())
330+
return
331+
}
332+
}
333+
312334
id := fwflex.StringValueFromFramework(ctx, data.Identifier)
313335
tflog.Debug(ctx, "deleting Aurora DSQL Cluster", map[string]any{
314336
names.AttrIdentifier: id,
@@ -338,6 +360,9 @@ func (r *clusterResource) Delete(ctx context.Context, request resource.DeleteReq
338360

339361
func (r *clusterResource) ImportState(ctx context.Context, request resource.ImportStateRequest, response *resource.ImportStateResponse) {
340362
resource.ImportStatePassthroughID(ctx, path.Root(names.AttrIdentifier), request, response)
363+
364+
// Set force_destroy to false on import to prevent accidental deletion
365+
response.Diagnostics.Append(response.State.SetAttribute(ctx, path.Root(names.AttrForceDestroy), types.BoolValue(false))...)
341366
}
342367

343368
func findClusterByID(ctx context.Context, conn *dsql.Client, id string) (*dsql.GetClusterOutput, error) {
@@ -441,10 +466,12 @@ func waitClusterUpdated(ctx context.Context, conn *dsql.Client, id string, timeo
441466

442467
func waitClusterDeleted(ctx context.Context, conn *dsql.Client, id string, timeout time.Duration) (*dsql.GetClusterOutput, error) {
443468
stateConf := &retry.StateChangeConf{
444-
Pending: enum.Slice(awstypes.ClusterStatusDeleting, awstypes.ClusterStatusPendingDelete),
445-
Target: []string{},
446-
Refresh: statusCluster(ctx, conn, id),
447-
Timeout: timeout,
469+
Pending: enum.Slice(awstypes.ClusterStatusDeleting, awstypes.ClusterStatusPendingDelete),
470+
Target: []string{},
471+
Refresh: statusCluster(ctx, conn, id),
472+
Timeout: timeout,
473+
Delay: 1 * time.Minute,
474+
PollInterval: 10 * time.Second,
448475
}
449476

450477
outputRaw, err := stateConf.WaitForStateContext(ctx)
@@ -522,6 +549,7 @@ type clusterResourceModel struct {
522549
ARN types.String `tfsdk:"arn"`
523550
DeletionProtectionEnabled types.Bool `tfsdk:"deletion_protection_enabled"`
524551
EncryptionDetails fwtypes.ListNestedObjectValueOf[encryptionDetailsModel] `tfsdk:"encryption_details"`
552+
ForceDestroy types.Bool `tfsdk:"force_destroy"`
525553
Identifier types.String `tfsdk:"identifier"`
526554
KMSEncryptionKey types.String `tfsdk:"kms_encryption_key"`
527555
MultiRegionProperties fwtypes.ListNestedObjectValueOf[multiRegionPropertiesModel] `tfsdk:"multi_region_properties"`

0 commit comments

Comments
 (0)