Skip to content

Commit 5d6e1f9

Browse files
committed
implement delete_on_create_timeout
1 parent cddcf57 commit 5d6e1f9

File tree

4 files changed

+37
-13
lines changed

4 files changed

+37
-13
lines changed

internal/common/cleanup/handle_timeout.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
1010
"github.com/hashicorp/terraform-plugin-framework/diag"
11+
"github.com/hashicorp/terraform-plugin-framework/types"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
1213
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/constant"
1314
)
@@ -99,3 +100,14 @@ func ResolveTimeout(ctx context.Context, t *timeouts.Value, operationName string
99100
}
100101
return timeoutDuration
101102
}
103+
104+
// ResolveDeleteOnCreateTimeout returns true if delete_on_create_timeout should be enabled.
105+
// Default behavior is true when not explicitly set to false.
106+
func ResolveDeleteOnCreateTimeout(deleteOnCreateTimeout types.Bool) bool {
107+
// If null or unknown, default to true
108+
if deleteOnCreateTimeout.IsNull() || deleteOnCreateTimeout.IsUnknown() {
109+
return true
110+
}
111+
// Otherwise use the explicit value
112+
return deleteOnCreateTimeout.ValueBool()
113+
}

internal/service/encryptionatrestprivateendpoint/resource.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,22 @@ func (r *encryptionAtRestPrivateEndpointRS) Create(ctx context.Context, req reso
7070
}
7171

7272
finalResp, err := waitStateTransition(ctx, projectID, cloudProvider, createResp.GetId(), connV2.EncryptionAtRestUsingCustomerKeyManagementApi, createTimeout)
73+
err = cleanup.HandleCreateTimeout(cleanup.ResolveDeleteOnCreateTimeout(earPrivateEndpointPlan.DeleteOnCreateTimeout), err, func(ctxCleanup context.Context) error {
74+
cleanResp, cleanErr := connV2.EncryptionAtRestUsingCustomerKeyManagementApi.RequestEncryptionAtRestPrivateEndpointDeletion(ctxCleanup, projectID, cloudProvider, createResp.GetId()).Execute()
75+
if validate.StatusNotFound(cleanResp) {
76+
return nil
77+
}
78+
return cleanErr
79+
})
80+
7381
if err != nil {
7482
resp.Diagnostics.AddError("error when waiting for status transition in creation", err.Error())
7583
return
7684
}
7785

7886
privateEndpointModel := NewTFEarPrivateEndpoint(*finalResp, projectID)
87+
privateEndpointModel.Timeouts = earPrivateEndpointPlan.Timeouts
88+
privateEndpointModel.DeleteOnCreateTimeout = earPrivateEndpointPlan.DeleteOnCreateTimeout
7989
resp.Diagnostics.Append(resp.State.Set(ctx, privateEndpointModel)...)
8090

8191
diags := CheckErrorMessageAndStatus(finalResp)
@@ -104,7 +114,10 @@ func (r *encryptionAtRestPrivateEndpointRS) Read(ctx context.Context, req resour
104114
return
105115
}
106116

107-
resp.Diagnostics.Append(resp.State.Set(ctx, NewTFEarPrivateEndpoint(*endpointModel, projectID))...)
117+
privateEndpointModel := NewTFEarPrivateEndpoint(*endpointModel, projectID)
118+
privateEndpointModel.Timeouts = earPrivateEndpointState.Timeouts
119+
privateEndpointModel.DeleteOnCreateTimeout = earPrivateEndpointState.DeleteOnCreateTimeout
120+
resp.Diagnostics.Append(resp.State.Set(ctx, privateEndpointModel)...)
108121

109122
diags := CheckErrorMessageAndStatus(endpointModel)
110123
resp.Diagnostics.Append(diags...)

internal/service/encryptionatrestprivateendpoint/resource_schema.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55

66
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
77
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
8+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
89
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/customplanmodifier"
911
)
1012

1113
func ResourceSchema(ctx context.Context) schema.Schema {
@@ -43,6 +45,13 @@ func ResourceSchema(ctx context.Context) schema.Schema {
4345
Create: true,
4446
Delete: true,
4547
}),
48+
"delete_on_create_timeout": schema.BoolAttribute{
49+
Optional: true,
50+
PlanModifiers: []planmodifier.Bool{
51+
customplanmodifier.CreateOnlyBoolPlanModifier(),
52+
},
53+
MarkdownDescription: "Indicates whether to delete the created resource if a timeout is reached when waiting for completion. When set to `true` and timeout occurs, it triggers the cleanup and returns immediately without waiting for deletion to complete. When set to `false`, the timeout will not trigger resource deletion. If you suspect a transient error when the value is `true`, wait before retrying to allow resource deletion to finish. Default is `true`.",
54+
},
4655
},
4756
}
4857
}
@@ -56,4 +65,5 @@ type TFEarPrivateEndpointModel struct {
5665
RegionName types.String `tfsdk:"region_name"`
5766
Status types.String `tfsdk:"status"`
5867
Timeouts timeouts.Value `tfsdk:"timeouts"`
68+
DeleteOnCreateTimeout types.Bool `tfsdk:"delete_on_create_timeout"`
5969
}

internal/service/flexcluster/resource.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou
8080
flexClusterResp, err := CreateFlexCluster(ctx, projectID, clusterName, flexClusterReq, connV2.FlexClustersApi, &createTimeout)
8181

8282
// Handle timeout with cleanup logic
83-
deleteOnCreateTimeout := resolveDeleteOnCreateTimeout(tfModel.DeleteOnCreateTimeout)
83+
deleteOnCreateTimeout := cleanup.ResolveDeleteOnCreateTimeout(tfModel.DeleteOnCreateTimeout)
8484
err = cleanup.HandleCreateTimeout(deleteOnCreateTimeout, err, func(ctxCleanup context.Context) error {
8585
cleanResp, cleanErr := r.Client.AtlasV2.FlexClustersApi.DeleteFlexCluster(ctxCleanup, projectID, clusterName).Execute()
8686
if validate.StatusNotFound(cleanResp) {
@@ -241,17 +241,6 @@ func splitFlexClusterImportID(id string) (projectID, clusterName *string, err er
241241
return
242242
}
243243

244-
// resolveDeleteOnCreateTimeout returns true if delete_on_create_timeout should be enabled.
245-
// Default behavior is true when not explicitly set to false.
246-
func resolveDeleteOnCreateTimeout(deleteOnCreateTimeout types.Bool) bool {
247-
// If null or unknown, default to true
248-
if deleteOnCreateTimeout.IsNull() || deleteOnCreateTimeout.IsUnknown() {
249-
return true
250-
}
251-
// Otherwise use the explicit value
252-
return deleteOnCreateTimeout.ValueBool()
253-
}
254-
255244
func CreateFlexCluster(ctx context.Context, projectID, clusterName string, flexClusterReq *admin.FlexClusterDescriptionCreate20241113, client admin.FlexClustersApi, timeout *time.Duration) (*admin.FlexClusterDescription20241113, error) {
256245
_, _, err := client.CreateFlexCluster(ctx, projectID, flexClusterReq).Execute()
257246
if err != nil {

0 commit comments

Comments
 (0)