diff --git a/internal/common/autogen/handle_operations.go b/internal/common/autogen/handle_operations.go index 24c477c150..2d9c3022de 100644 --- a/internal/common/autogen/handle_operations.go +++ b/internal/common/autogen/handle_operations.go @@ -34,7 +34,7 @@ type WaitReq struct { StateProperty string PendingStates []string TargetStates []string - TimeoutSeconds int + Timeout time.Duration MinTimeoutSeconds int DelaySeconds int } @@ -240,7 +240,7 @@ func waitForChanges(ctx context.Context, wait *WaitReq, client *config.MongoDBCl stateConf := retry.StateChangeConf{ Target: wait.TargetStates, Pending: wait.PendingStates, - Timeout: time.Duration(wait.TimeoutSeconds) * time.Second, + Timeout: wait.Timeout, MinTimeout: time.Duration(wait.MinTimeoutSeconds) * time.Second, Delay: time.Duration(wait.DelaySeconds) * time.Second, Refresh: refreshFunc(ctx, wait, client), diff --git a/internal/serviceapi/clusterapi/resource.go b/internal/serviceapi/clusterapi/resource.go index bc6ca345f6..1b23d1522a 100755 --- a/internal/serviceapi/clusterapi/resource.go +++ b/internal/serviceapi/clusterapi/resource.go @@ -4,6 +4,7 @@ package clusterapi import ( "context" + "time" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" @@ -48,6 +49,11 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou PathParams: pathParams, Method: "POST", } + timeout, localDiags := plan.Timeouts.Create(ctx, 10800*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleCreateReq{ Resp: resp, Client: r.Client, @@ -57,7 +63,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou StateProperty: "stateName", PendingStates: []string{"CREATING", "UPDATING", "REPAIRING", "REPEATING", "PENDING", "DELETING"}, TargetStates: []string{"IDLE"}, - TimeoutSeconds: 10800, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 30, CallParams: readAPICallParams(&plan), @@ -100,6 +106,11 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou PathParams: pathParams, Method: "PATCH", } + timeout, localDiags := plan.Timeouts.Update(ctx, 10800*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleUpdateReq{ Resp: resp, Client: r.Client, @@ -109,7 +120,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou StateProperty: "stateName", PendingStates: []string{"CREATING", "UPDATING", "REPAIRING", "REPEATING", "PENDING", "DELETING"}, TargetStates: []string{"IDLE"}, - TimeoutSeconds: 10800, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 30, CallParams: readAPICallParams(&state), @@ -134,6 +145,11 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou PathParams: pathParams, Method: "DELETE", } + timeout, localDiags := state.Timeouts.Delete(ctx, 10800*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleDeleteReq{ Resp: resp, Client: r.Client, @@ -143,7 +159,7 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou StateProperty: "stateName", PendingStates: []string{"IDLE", "CREATING", "UPDATING", "REPAIRING", "REPEATING", "PENDING", "DELETING"}, TargetStates: []string{"DELETED"}, - TimeoutSeconds: 10800, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 30, CallParams: readAPICallParams(&state), diff --git a/internal/serviceapi/pushbasedlogexportapi/resource.go b/internal/serviceapi/pushbasedlogexportapi/resource.go index 6c377ab51b..558462cec6 100755 --- a/internal/serviceapi/pushbasedlogexportapi/resource.go +++ b/internal/serviceapi/pushbasedlogexportapi/resource.go @@ -4,6 +4,7 @@ package pushbasedlogexportapi import ( "context" + "time" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" @@ -48,6 +49,11 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou PathParams: pathParams, Method: "POST", } + timeout, localDiags := plan.Timeouts.Create(ctx, 900*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleCreateReq{ Resp: resp, Client: r.Client, @@ -57,7 +63,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou StateProperty: "state", PendingStates: []string{"INITIATING", "BUCKET_VERIFIED"}, TargetStates: []string{"ACTIVE"}, - TimeoutSeconds: 900, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 10, CallParams: readAPICallParams(&plan), @@ -99,6 +105,11 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou PathParams: pathParams, Method: "PATCH", } + timeout, localDiags := plan.Timeouts.Update(ctx, 900*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleUpdateReq{ Resp: resp, Client: r.Client, @@ -108,7 +119,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou StateProperty: "state", PendingStates: []string{"INITIATING", "BUCKET_VERIFIED"}, TargetStates: []string{"ACTIVE"}, - TimeoutSeconds: 900, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 10, CallParams: readAPICallParams(&state), @@ -132,6 +143,11 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou PathParams: pathParams, Method: "DELETE", } + timeout, localDiags := state.Timeouts.Delete(ctx, 900*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleDeleteReq{ Resp: resp, Client: r.Client, @@ -141,7 +157,7 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou StateProperty: "state", PendingStates: []string{"ACTIVE", "INITIATING", "BUCKET_VERIFIED"}, TargetStates: []string{"UNCONFIGURED", "DELETED"}, - TimeoutSeconds: 900, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 10, CallParams: readAPICallParams(&state), diff --git a/internal/serviceapi/searchdeploymentapi/resource.go b/internal/serviceapi/searchdeploymentapi/resource.go index 582f31f221..d5a28f9043 100755 --- a/internal/serviceapi/searchdeploymentapi/resource.go +++ b/internal/serviceapi/searchdeploymentapi/resource.go @@ -4,6 +4,7 @@ package searchdeploymentapi import ( "context" + "time" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" @@ -49,6 +50,11 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou PathParams: pathParams, Method: "POST", } + timeout, localDiags := plan.Timeouts.Create(ctx, 10800*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleCreateReq{ Resp: resp, Client: r.Client, @@ -58,7 +64,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou StateProperty: "stateName", PendingStates: []string{"UPDATING", "PAUSED"}, TargetStates: []string{"IDLE"}, - TimeoutSeconds: 10800, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 60, CallParams: readAPICallParams(&plan), @@ -101,6 +107,11 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou PathParams: pathParams, Method: "PATCH", } + timeout, localDiags := plan.Timeouts.Update(ctx, 10800*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleUpdateReq{ Resp: resp, Client: r.Client, @@ -110,7 +121,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou StateProperty: "stateName", PendingStates: []string{"UPDATING", "PAUSED"}, TargetStates: []string{"IDLE"}, - TimeoutSeconds: 10800, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 60, CallParams: readAPICallParams(&state), @@ -135,6 +146,11 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou PathParams: pathParams, Method: "DELETE", } + timeout, localDiags := state.Timeouts.Delete(ctx, 10800*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleDeleteReq{ Resp: resp, Client: r.Client, @@ -144,7 +160,7 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou StateProperty: "stateName", PendingStates: []string{"IDLE", "UPDATING", "PAUSED"}, TargetStates: []string{"DELETED"}, - TimeoutSeconds: 10800, + Timeout: timeout, MinTimeoutSeconds: 30, DelaySeconds: 60, CallParams: readAPICallParams(&state), diff --git a/internal/serviceapi/streamprocessorapi/resource.go b/internal/serviceapi/streamprocessorapi/resource.go index cd06bcd8d7..4bb0427dde 100755 --- a/internal/serviceapi/streamprocessorapi/resource.go +++ b/internal/serviceapi/streamprocessorapi/resource.go @@ -4,6 +4,7 @@ package streamprocessorapi import ( "context" + "time" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" @@ -49,6 +50,11 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou PathParams: pathParams, Method: "POST", } + timeout, localDiags := plan.Timeouts.Create(ctx, 300*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleCreateReq{ Resp: resp, Client: r.Client, @@ -58,7 +64,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou StateProperty: "state", PendingStates: []string{"INIT", "CREATING"}, TargetStates: []string{"CREATED"}, - TimeoutSeconds: 300, + Timeout: timeout, MinTimeoutSeconds: 10, DelaySeconds: 10, CallParams: readAPICallParams(&plan), @@ -102,6 +108,11 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou PathParams: pathParams, Method: "PATCH", } + timeout, localDiags := plan.Timeouts.Update(ctx, 300*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleUpdateReq{ Resp: resp, Client: r.Client, @@ -111,7 +122,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou StateProperty: "state", PendingStates: []string{"INIT", "CREATING"}, TargetStates: []string{"CREATED"}, - TimeoutSeconds: 300, + Timeout: timeout, MinTimeoutSeconds: 10, DelaySeconds: 10, CallParams: readAPICallParams(&state), @@ -137,6 +148,11 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou PathParams: pathParams, Method: "DELETE", } + timeout, localDiags := state.Timeouts.Delete(ctx, 300*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleDeleteReq{ Resp: resp, Client: r.Client, @@ -146,7 +162,7 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou StateProperty: "state", PendingStates: []string{"INIT", "CREATING", "CREATED", "STARTED", "STOPPED"}, TargetStates: []string{"DELETED"}, - TimeoutSeconds: 300, + Timeout: timeout, MinTimeoutSeconds: 10, DelaySeconds: 10, CallParams: readAPICallParams(&state), diff --git a/tools/codegen/gofilegen/codetemplate/resource-file.go.tmpl b/tools/codegen/gofilegen/codetemplate/resource-file.go.tmpl index 816ca05e19..a5c913028c 100644 --- a/tools/codegen/gofilegen/codetemplate/resource-file.go.tmpl +++ b/tools/codegen/gofilegen/codetemplate/resource-file.go.tmpl @@ -4,7 +4,10 @@ package {{ .PackageName }} import ( "context" - + {{- if or .APIOperations.Create.Wait .APIOperations.Update.Wait (and .APIOperations.Delete .APIOperations.Delete.Wait) }} + "time" + {{ end }} + "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" @@ -49,6 +52,13 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou PathParams: pathParams, Method: "{{ .APIOperations.Create.HTTPMethod }}", } + {{with .APIOperations.Create.Wait -}} + timeout, localDiags := plan.Timeouts.Create(ctx, {{ .TimeoutSeconds }}*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } + {{ end -}} reqHandle := autogen.HandleCreateReq { Resp: resp, Client: r.Client, @@ -59,7 +69,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou StateProperty: "{{ .StateProperty }}", PendingStates: []string{ {{range .PendingStates }}"{{ . }}", {{- end }} }, TargetStates: []string{ {{range .TargetStates }}"{{ . }}", {{- end }} }, - TimeoutSeconds: {{ .TimeoutSeconds }}, + Timeout: timeout, MinTimeoutSeconds: {{ .MinTimeoutSeconds }}, DelaySeconds: {{ .DelaySeconds }}, CallParams: readAPICallParams(&plan), @@ -103,6 +113,13 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou PathParams: pathParams, Method: "{{ .APIOperations.Update.HTTPMethod }}", } + {{with .APIOperations.Update.Wait -}} + timeout, localDiags := plan.Timeouts.Update(ctx, {{ .TimeoutSeconds }}*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } + {{ end -}} reqHandle := autogen.HandleUpdateReq { Resp: resp, Client: r.Client, @@ -113,7 +130,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou StateProperty: "{{ .StateProperty }}", PendingStates: []string{ {{range .PendingStates }}"{{ . }}", {{- end }} }, TargetStates: []string{ {{range .TargetStates }}"{{ . }}", {{- end }} }, - TimeoutSeconds: {{ .TimeoutSeconds }}, + Timeout: timeout, MinTimeoutSeconds: {{ .MinTimeoutSeconds }}, DelaySeconds: {{ .DelaySeconds }}, CallParams: readAPICallParams(&state), @@ -124,7 +141,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou } func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { - {{- if .APIOperations.Delete}} + {{- if .APIOperations.Delete}} var state TFModel resp.Diagnostics.Append(req.State.Get(ctx, &state)...) if resp.Diagnostics.HasError() { @@ -140,6 +157,13 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou PathParams: pathParams, Method: "{{ .APIOperations.Delete.HTTPMethod }}", } + {{with .APIOperations.Delete.Wait -}} + timeout, localDiags := state.Timeouts.Delete(ctx, {{ .TimeoutSeconds }}*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } + {{ end -}} reqHandle := autogen.HandleDeleteReq { Resp: resp, Client: r.Client, @@ -153,7 +177,7 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou StateProperty: "{{ .StateProperty }}", PendingStates: []string{ {{range .PendingStates }}"{{ . }}", {{- end }} }, TargetStates: []string{ {{range .TargetStates }}"{{ . }}", {{- end }} }, - TimeoutSeconds: {{ .TimeoutSeconds }}, + Timeout: timeout, MinTimeoutSeconds: {{ .MinTimeoutSeconds }}, DelaySeconds: {{ .DelaySeconds }}, CallParams: readAPICallParams(&state), diff --git a/tools/codegen/gofilegen/resource/testdata/wait-configuration.golden.go b/tools/codegen/gofilegen/resource/testdata/wait-configuration.golden.go index c8c65cfd0f..984fcbef50 100644 --- a/tools/codegen/gofilegen/resource/testdata/wait-configuration.golden.go +++ b/tools/codegen/gofilegen/resource/testdata/wait-configuration.golden.go @@ -4,6 +4,7 @@ package testname import ( "context" + "time" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/mongodb/terraform-provider-mongodbatlas/internal/common/autogen" @@ -48,6 +49,11 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou PathParams: pathParams, Method: "POST", } + timeout, localDiags := plan.Timeouts.Create(ctx, 300*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleCreateReq{ Resp: resp, Client: r.Client, @@ -57,7 +63,7 @@ func (r *rs) Create(ctx context.Context, req resource.CreateRequest, resp *resou StateProperty: "state", PendingStates: []string{"INITIATING"}, TargetStates: []string{"IDLE"}, - TimeoutSeconds: 300, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 10, CallParams: readAPICallParams(&plan), @@ -99,6 +105,11 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou PathParams: pathParams, Method: "PUT", } + timeout, localDiags := plan.Timeouts.Update(ctx, 300*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleUpdateReq{ Resp: resp, Client: r.Client, @@ -108,7 +119,7 @@ func (r *rs) Update(ctx context.Context, req resource.UpdateRequest, resp *resou StateProperty: "state", PendingStates: []string{"UPDATING"}, TargetStates: []string{"IDLE"}, - TimeoutSeconds: 300, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 10, CallParams: readAPICallParams(&state), @@ -132,6 +143,11 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou PathParams: pathParams, Method: "DELETE", } + timeout, localDiags := state.Timeouts.Delete(ctx, 300*time.Second) + resp.Diagnostics.Append(localDiags...) + if resp.Diagnostics.HasError() { + return + } reqHandle := autogen.HandleDeleteReq{ Resp: resp, Client: r.Client, @@ -141,7 +157,7 @@ func (r *rs) Delete(ctx context.Context, req resource.DeleteRequest, resp *resou StateProperty: "state", PendingStates: []string{"PENDING"}, TargetStates: []string{"UNCONFIGURED", "DELETED"}, - TimeoutSeconds: 300, + Timeout: timeout, MinTimeoutSeconds: 60, DelaySeconds: 10, CallParams: readAPICallParams(&state),