Skip to content

Commit 971e21b

Browse files
authored
chore: Invoke Delete operation if timeout in autogen (#3820)
1 parent 78d1a9a commit 971e21b

37 files changed

+931
-495
lines changed

internal/common/autogen/handle_operations.go

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/hashicorp/terraform-plugin-framework/diag"
1313
"github.com/hashicorp/terraform-plugin-framework/resource"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
15+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/cleanup"
1516
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/retrystrategy"
1617
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/validate"
1718
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
@@ -40,11 +41,13 @@ type WaitReq struct {
4041
}
4142

4243
type HandleCreateReq struct {
43-
Resp *resource.CreateResponse
44-
Client *config.MongoDBClient
45-
Plan any
46-
CallParams *config.APICallParams
47-
Wait *WaitReq
44+
Resp *resource.CreateResponse
45+
Client *config.MongoDBClient
46+
Plan any
47+
CallParams *config.APICallParams
48+
DeleteReq *HandleDeleteReq
49+
Wait *WaitReq
50+
DeleteOnCreateTimeout bool
4851
}
4952

5053
func HandleCreate(ctx context.Context, req HandleCreateReq) {
@@ -54,7 +57,7 @@ func HandleCreate(ctx context.Context, req HandleCreateReq) {
5457
addError(d, opCreate, errBuildingAPIRequest, err)
5558
return
5659
}
57-
bodyResp, err := callAPIWithBody(ctx, req.Client, req.CallParams, bodyReq)
60+
bodyResp, _, err := callAPIWithBody(ctx, req.Client, req.CallParams, bodyReq)
5861
if err != nil {
5962
addError(d, opCreate, errCallingAPI, err)
6063
return
@@ -69,8 +72,15 @@ func HandleCreate(ctx context.Context, req HandleCreateReq) {
6972
addError(d, opCreate, errResolvingResponse, err)
7073
return
7174
}
72-
if err := handleWaitCreateUpdate(ctx, req.Wait, req.Client, req.Plan); err != nil {
73-
addError(d, opCreate, errWaitingForChanges, err)
75+
errWait := handleWaitCreateUpdate(ctx, req.Wait, req.Client, req.Plan)
76+
if req.DeleteReq != nil {
77+
// Handle timeout with cleanup if delete_on_create_timeout is enabled.
78+
errWait = cleanup.HandleCreateTimeout(req.DeleteOnCreateTimeout, errWait, func(ctxCleanup context.Context) error {
79+
return callDelete(ctxCleanup, req.DeleteReq)
80+
})
81+
}
82+
if errWait != nil {
83+
addError(d, opCreate, errWaitingForChanges, errWait)
7484
return
7585
}
7686
req.Resp.Diagnostics.Append(req.Resp.State.Set(ctx, req.Plan)...)
@@ -122,7 +132,7 @@ func HandleUpdate(ctx context.Context, req HandleUpdateReq) {
122132
addError(d, opUpdate, errBuildingAPIRequest, err)
123133
return
124134
}
125-
bodyResp, err := callAPIWithBody(ctx, req.Client, req.CallParams, bodyReq)
135+
bodyResp, _, err := callAPIWithBody(ctx, req.Client, req.CallParams, bodyReq)
126136
if err != nil {
127137
addError(d, opUpdate, errCallingAPI, err)
128138
return
@@ -145,7 +155,7 @@ func HandleUpdate(ctx context.Context, req HandleUpdateReq) {
145155
}
146156

147157
type HandleDeleteReq struct {
148-
Resp *resource.DeleteResponse
158+
Diags *diag.Diagnostics
149159
Client *config.MongoDBClient
150160
State any
151161
CallParams *config.APICallParams
@@ -154,19 +164,12 @@ type HandleDeleteReq struct {
154164
}
155165

156166
func HandleDelete(ctx context.Context, req HandleDeleteReq) {
157-
d := &req.Resp.Diagnostics
158-
var err error
159-
if req.StaticRequestBody == "" {
160-
_, _, err = callAPIWithoutBody(ctx, req.Client, req.CallParams)
161-
} else {
162-
_, err = callAPIWithBody(ctx, req.Client, req.CallParams, []byte(req.StaticRequestBody))
163-
}
164-
if err != nil {
165-
addError(d, opDelete, errCallingAPI, err)
167+
if err := callDelete(ctx, &req); err != nil {
168+
addError(req.Diags, opDelete, errCallingAPI, err)
166169
return
167170
}
168171
if errWait := handleWaitDelete(ctx, req.Wait, req.Client); errWait != nil {
169-
addError(d, opDelete, errWaitingForChanges, errWait)
172+
addError(req.Diags, opDelete, errWaitingForChanges, errWait)
170173
}
171174
}
172175

@@ -202,18 +205,18 @@ func addError(d *diag.Diagnostics, opName, errSummary string, err error) {
202205
}
203206

204207
// callAPIWithBody makes a request to the API with the given request body and returns the response body.
205-
// It is used for POST, PUT, and PATCH requests where a request body is required.
206-
func callAPIWithBody(ctx context.Context, client *config.MongoDBClient, callParams *config.APICallParams, bodyReq []byte) ([]byte, error) {
208+
// It is used for POST, PUT, PATCH and DELETE with static content.
209+
func callAPIWithBody(ctx context.Context, client *config.MongoDBClient, callParams *config.APICallParams, bodyReq []byte) ([]byte, *http.Response, error) {
207210
apiResp, err := client.UntypedAPICall(ctx, callParams, bodyReq)
208211
if err != nil {
209-
return nil, err
212+
return nil, apiResp, err
210213
}
211214
bodyResp, err := io.ReadAll(apiResp.Body)
212215
apiResp.Body.Close()
213216
if err != nil {
214-
return nil, err
217+
return nil, apiResp, err
215218
}
216-
return bodyResp, nil
219+
return bodyResp, apiResp, nil
217220
}
218221

219222
// callAPIWithoutBody makes a request to the API without a request body and returns the response body.
@@ -231,6 +234,23 @@ func callAPIWithoutBody(ctx context.Context, client *config.MongoDBClient, callP
231234
return bodyResp, apiResp, nil
232235
}
233236

237+
// callDelete makes a DELETE request to the API, supporting both requests with and without a body.
238+
// Returns nil if the resource is not found (already deleted).
239+
func callDelete(ctx context.Context, req *HandleDeleteReq) error {
240+
var err error
241+
var bodyResp []byte
242+
var apiResp *http.Response
243+
if req.StaticRequestBody == "" {
244+
bodyResp, apiResp, err = callAPIWithoutBody(ctx, req.Client, req.CallParams)
245+
} else {
246+
bodyResp, apiResp, err = callAPIWithBody(ctx, req.Client, req.CallParams, []byte(req.StaticRequestBody))
247+
}
248+
if notFound(bodyResp, apiResp) { // Resource is already deleted, don't fail.
249+
return nil
250+
}
251+
return err
252+
}
253+
234254
// waitForChanges waits until a long-running operation is done.
235255
// It returns the latest JSON response from the API so it can be used to update the response state.
236256
func waitForChanges(ctx context.Context, wait *WaitReq, client *config.MongoDBClient) ([]byte, error) {

internal/serviceapi/auditingapi/resource.go

Lines changed: 23 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/serviceapi/clusterapi/resource.go

Lines changed: 40 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/serviceapi/clusterapi/resource_schema.go

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)