Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTMDB-503: Configurable cluster timeouts #951

Merged
merged 3 commits into from
Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions mongodbatlas/resource_mongodbatlas_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func resourceMongoDBAtlasAdvancedCluster() *schema.Resource {
},
"advanced_configuration": clusterAdvancedConfigurationSchema(),
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(3 * time.Hour),
Update: schema.DefaultTimeout(3 * time.Hour),
Delete: schema.DefaultTimeout(3 * time.Hour),
},
}
}

Expand Down Expand Up @@ -359,11 +364,12 @@ func resourceMongoDBAtlasAdvancedClusterCreate(ctx context.Context, d *schema.Re
return diag.FromErr(fmt.Errorf(errorClusterAdvancedCreate, err))
}

timeout := d.Timeout(schema.TimeoutCreate)
stateConf := &resource.StateChangeConf{
Pending: []string{"CREATING", "UPDATING", "REPAIRING", "REPEATING", "PENDING"},
Target: []string{"IDLE"},
Refresh: resourceClusterAdvancedRefreshFunc(ctx, d.Get("name").(string), projectID, conn),
Timeout: 3 * time.Hour,
Timeout: timeout,
MinTimeout: 1 * time.Minute,
Delay: 3 * time.Minute,
}
Expand Down Expand Up @@ -396,7 +402,7 @@ func resourceMongoDBAtlasAdvancedClusterCreate(ctx context.Context, d *schema.Re
Paused: pointy.Bool(v),
}

_, _, err = updateAdvancedCluster(ctx, conn, request, projectID, d.Get("name").(string))
_, _, err = updateAdvancedCluster(ctx, conn, request, projectID, d.Get("name").(string), timeout)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, d.Get("name").(string), err))
}
Expand Down Expand Up @@ -547,7 +553,7 @@ func resourceMongoDBAtlasAdvancedClusterUpgrade(ctx context.Context, d *schema.R
return diag.FromErr(fmt.Errorf("upgrade called without %s in ctx", string(upgradeRequestCtxKey)))
}

upgradeResponse, _, err := upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName)
upgradeResponse, _, err := upgradeCluster(ctx, conn, upgradeRequest, projectID, clusterName, d.Timeout(schema.TimeoutUpdate))

if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
Expand Down Expand Up @@ -623,17 +629,19 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
cluster.VersionReleaseSystem = d.Get("version_release_system").(string)
}

timeout := d.Timeout(schema.TimeoutUpdate)

// Has changes
if !reflect.DeepEqual(cluster, matlas.Cluster{}) {
err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError {
_, _, err := updateAdvancedCluster(ctx, conn, cluster, projectID, clusterName)
err := resource.RetryContext(ctx, timeout, func() *resource.RetryError {
_, _, err := updateAdvancedCluster(ctx, conn, cluster, projectID, clusterName, timeout)
if err != nil {
var target *matlas.ErrorResponse
if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" {
clusterRequest := &matlas.AdvancedCluster{
Paused: pointy.Bool(false),
}
_, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName)
_, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName, timeout)
if err != nil {
return resource.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
}
Expand Down Expand Up @@ -670,7 +678,7 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
Paused: pointy.Bool(true),
}

_, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName)
_, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName, timeout)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
}
Expand Down Expand Up @@ -698,7 +706,7 @@ func resourceMongoDBAtlasAdvancedClusterDelete(ctx context.Context, d *schema.Re
Pending: []string{"IDLE", "CREATING", "UPDATING", "REPAIRING", "DELETING"},
Target: []string{"DELETED"},
Refresh: resourceClusterAdvancedRefreshFunc(ctx, clusterName, projectID, conn),
Timeout: 3 * time.Hour,
Timeout: d.Timeout(schema.TimeoutDelete),
MinTimeout: 30 * time.Second,
Delay: 1 * time.Minute, // Wait 30 secs before starting
}
Expand Down Expand Up @@ -1198,7 +1206,13 @@ func getUpgradeRequest(d *schema.ResourceData) *matlas.Cluster {
}
}

func updateAdvancedCluster(ctx context.Context, conn *matlas.Client, request *matlas.AdvancedCluster, projectID, name string) (*matlas.AdvancedCluster, *matlas.Response, error) {
func updateAdvancedCluster(
ctx context.Context,
conn *matlas.Client,
request *matlas.AdvancedCluster,
projectID, name string,
timeout time.Duration,
) (*matlas.AdvancedCluster, *matlas.Response, error) {
cluster, resp, err := conn.AdvancedClusters.Update(ctx, projectID, name, request)
if err != nil {
return nil, nil, err
Expand All @@ -1208,7 +1222,7 @@ func updateAdvancedCluster(ctx context.Context, conn *matlas.Client, request *ma
Pending: []string{"CREATING", "UPDATING", "REPAIRING"},
Target: []string{"IDLE"},
Refresh: resourceClusterAdvancedRefreshFunc(ctx, name, projectID, conn),
Timeout: 3 * time.Hour,
Timeout: timeout,
MinTimeout: 30 * time.Second,
Delay: 1 * time.Minute,
}
Expand Down
32 changes: 20 additions & 12 deletions mongodbatlas/resource_mongodbatlas_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ func resourceMongoDBAtlasCluster() *schema.Resource {
},
},
CustomizeDiff: resourceClusterCustomizeDiff,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(3 * time.Hour),
Update: schema.DefaultTimeout(3 * time.Hour),
Delete: schema.DefaultTimeout(3 * time.Hour),
},
}
}

Expand Down Expand Up @@ -547,11 +552,12 @@ func resourceMongoDBAtlasClusterCreate(ctx context.Context, d *schema.ResourceDa
return diag.FromErr(fmt.Errorf(errorClusterCreate, err))
}

timeout := d.Timeout(schema.TimeoutCreate)
stateConf := &resource.StateChangeConf{
Pending: []string{"CREATING", "UPDATING", "REPAIRING", "REPEATING", "PENDING"},
Target: []string{"IDLE"},
Refresh: resourceClusterRefreshFunc(ctx, d.Get("name").(string), projectID, conn),
Timeout: 3 * time.Hour,
Timeout: timeout,
MinTimeout: 1 * time.Minute,
Delay: 3 * time.Minute,
}
Expand Down Expand Up @@ -584,7 +590,7 @@ func resourceMongoDBAtlasClusterCreate(ctx context.Context, d *schema.ResourceDa
Paused: pointy.Bool(v),
}

_, _, err = updateCluster(ctx, conn, clusterRequest, projectID, d.Get("name").(string))
_, _, err = updateCluster(ctx, conn, clusterRequest, projectID, d.Get("name").(string), timeout)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterUpdate, d.Get("name").(string), err))
}
Expand Down Expand Up @@ -918,8 +924,10 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa
}
}

timeout := d.Timeout(schema.TimeoutUpdate)

if isUpgradeRequired(d) {
updatedCluster, _, err := upgradeCluster(ctx, conn, cluster, projectID, clusterName)
updatedCluster, _, err := upgradeCluster(ctx, conn, cluster, projectID, clusterName, timeout)

if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err))
Expand All @@ -932,15 +940,15 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa
"provider_name": updatedCluster.ProviderSettings.ProviderName,
}))
} else if !reflect.DeepEqual(cluster, matlas.Cluster{}) {
err := resource.RetryContext(ctx, 3*time.Hour, func() *resource.RetryError {
_, _, err := updateCluster(ctx, conn, cluster, projectID, clusterName)
err := resource.RetryContext(ctx, timeout, func() *resource.RetryError {
_, _, err := updateCluster(ctx, conn, cluster, projectID, clusterName, timeout)

if didErrOnPausedCluster(err) {
clusterRequest := &matlas.Cluster{
Paused: pointy.Bool(false),
}

_, _, err = updateCluster(ctx, conn, clusterRequest, projectID, clusterName)
_, _, err = updateCluster(ctx, conn, clusterRequest, projectID, clusterName, timeout)
}

if err != nil {
Expand Down Expand Up @@ -976,7 +984,7 @@ func resourceMongoDBAtlasClusterUpdate(ctx context.Context, d *schema.ResourceDa
Paused: pointy.Bool(true),
}

_, _, err := updateCluster(ctx, conn, clusterRequest, projectID, clusterName)
_, _, err := updateCluster(ctx, conn, clusterRequest, projectID, clusterName, timeout)
if err != nil {
return diag.FromErr(fmt.Errorf(errorClusterUpdate, clusterName, err))
}
Expand Down Expand Up @@ -1014,7 +1022,7 @@ func resourceMongoDBAtlasClusterDelete(ctx context.Context, d *schema.ResourceDa
Pending: []string{"IDLE", "CREATING", "UPDATING", "REPAIRING", "DELETING"},
Target: []string{"DELETED"},
Refresh: resourceClusterRefreshFunc(ctx, clusterName, projectID, conn),
Timeout: 3 * time.Hour,
Timeout: d.Timeout(schema.TimeoutDelete),
MinTimeout: 30 * time.Second,
Delay: 1 * time.Minute, // Wait 30 secs before starting
}
Expand Down Expand Up @@ -1716,7 +1724,7 @@ func clusterAdvancedConfigurationSchema() *schema.Schema {
}
}

func updateCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cluster, projectID, name string) (*matlas.Cluster, *matlas.Response, error) {
func updateCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cluster, projectID, name string, timeout time.Duration) (*matlas.Cluster, *matlas.Response, error) {
cluster, resp, err := conn.Clusters.Update(ctx, projectID, name, request)
if err != nil {
return nil, nil, err
Expand All @@ -1726,7 +1734,7 @@ func updateCluster(ctx context.Context, conn *matlas.Client, request *matlas.Clu
Pending: []string{"CREATING", "UPDATING", "REPAIRING"},
Target: []string{"IDLE"},
Refresh: resourceClusterRefreshFunc(ctx, name, projectID, conn),
Timeout: 3 * time.Hour,
Timeout: timeout,
MinTimeout: 30 * time.Second,
Delay: 1 * time.Minute,
}
Expand All @@ -1740,7 +1748,7 @@ func updateCluster(ctx context.Context, conn *matlas.Client, request *matlas.Clu
return cluster, resp, nil
}

func upgradeCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cluster, projectID, name string) (*matlas.Cluster, *matlas.Response, error) {
func upgradeCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cluster, projectID, name string, timeout time.Duration) (*matlas.Cluster, *matlas.Response, error) {
request.Name = name

cluster, resp, err := conn.Clusters.Upgrade(ctx, projectID, request)
Expand All @@ -1752,7 +1760,7 @@ func upgradeCluster(ctx context.Context, conn *matlas.Client, request *matlas.Cl
Pending: []string{"CREATING", "UPDATING", "REPAIRING"},
Target: []string{"IDLE"},
Refresh: resourceClusterRefreshFunc(ctx, name, projectID, conn),
Timeout: 3 * time.Hour,
Timeout: timeout,
MinTimeout: 30 * time.Second,
Delay: 1 * time.Minute,
}
Expand Down