Skip to content

Commit 4a24fdc

Browse files
authored
fix!: handles paused clusters with errors when updating. (#1640)
* fix: handles paused clusters with errors when updating. * test. * changes logic. * changes logic. * changes logic. * updates documentation. * fixes same thing in cluster. * test. * fix cluster pause. * Revert "fix cluster pause." This reverts commit 122111d. * Revert "test." This reverts commit 3542488. * Revert "fixes same thing in cluster." This reverts commit 5b101f5. * address comments.
1 parent 3efba0a commit 4a24fdc

3 files changed

+31
-25
lines changed

mongodbatlas/resource_mongodbatlas_advanced_cluster.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -750,21 +750,12 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
750750
// Has changes
751751
if !reflect.DeepEqual(cluster, clusterChangeDetect) {
752752
err := retry.RetryContext(ctx, timeout, func() *retry.RetryError {
753-
_, _, err := updateAdvancedCluster(ctx, conn, cluster, projectID, clusterName, timeout)
753+
_, resp, err := updateAdvancedCluster(ctx, conn, cluster, projectID, clusterName, timeout)
754754
if err != nil {
755-
var target *matlas.ErrorResponse
756-
if errors.As(err, &target) && target.ErrorCode == "CANNOT_UPDATE_PAUSED_CLUSTER" {
757-
clusterRequest := &matlas.AdvancedCluster{
758-
Paused: pointy.Bool(false),
759-
}
760-
_, _, err := updateAdvancedCluster(ctx, conn, clusterRequest, projectID, clusterName, timeout)
761-
if err != nil {
762-
return retry.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
763-
}
764-
}
765-
if errors.As(err, &target) && target.HTTPCode == 400 {
755+
if resp == nil || resp.StatusCode == 400 {
766756
return retry.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
767757
}
758+
return retry.RetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
768759
}
769760
return nil
770761
})

mongodbatlas/resource_mongodbatlas_advanced_cluster_test.go

+27-12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"log"
77
"os"
8+
"regexp"
89
"testing"
910

1011
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
@@ -241,11 +242,13 @@ func TestAccClusterAdvancedCluster_multicloudSharded(t *testing.T) {
241242
func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
242243
SkipTest(t)
243244
var (
244-
cluster matlas.AdvancedCluster
245-
resourceName = "mongodbatlas_advanced_cluster.test"
246-
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
247-
projectName = acctest.RandomWithPrefix("test-acc")
248-
rName = acctest.RandomWithPrefix("test-acc")
245+
cluster matlas.AdvancedCluster
246+
resourceName = "mongodbatlas_advanced_cluster.test"
247+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
248+
projectName = acctest.RandomWithPrefix("test-acc")
249+
rName = acctest.RandomWithPrefix("test-acc")
250+
instanceSize = "M10"
251+
anotherInstanceSize = "M20"
249252
)
250253

251254
resource.ParallelTest(t, resource.TestCase{
@@ -254,7 +257,7 @@ func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
254257
CheckDestroy: testAccCheckMongoDBAtlasAdvancedClusterDestroy,
255258
Steps: []resource.TestStep{
256259
{
257-
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false),
260+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false, instanceSize),
258261
Check: resource.ComposeTestCheckFunc(
259262
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
260263
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
@@ -266,7 +269,7 @@ func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
266269
),
267270
},
268271
{
269-
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true),
272+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, instanceSize),
270273
Check: resource.ComposeTestCheckFunc(
271274
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
272275
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
@@ -277,6 +280,10 @@ func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
277280
resource.TestCheckResourceAttr(resourceName, "paused", "true"),
278281
),
279282
},
283+
{
284+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, anotherInstanceSize),
285+
ExpectError: regexp.MustCompile("CANNOT_UPDATE_PAUSED_CLUSTER"),
286+
},
280287
{
281288
ResourceName: resourceName,
282289
ImportStateIdFunc: testAccCheckMongoDBAtlasClusterImportStateIDFunc(resourceName),
@@ -296,6 +303,7 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
296303
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
297304
projectName = acctest.RandomWithPrefix("test-acc")
298305
rName = acctest.RandomWithPrefix("test-acc")
306+
instanceSize = "M10"
299307
)
300308

301309
resource.ParallelTest(t, resource.TestCase{
@@ -304,7 +312,7 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
304312
CheckDestroy: testAccCheckMongoDBAtlasAdvancedClusterDestroy,
305313
Steps: []resource.TestStep{
306314
{
307-
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true),
315+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, instanceSize),
308316
Check: resource.ComposeTestCheckFunc(
309317
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
310318
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
@@ -316,7 +324,7 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
316324
),
317325
},
318326
{
319-
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false),
327+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false, instanceSize),
320328
Check: resource.ComposeTestCheckFunc(
321329
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
322330
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
@@ -327,6 +335,13 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
327335
resource.TestCheckResourceAttr(resourceName, "paused", "false"),
328336
),
329337
},
338+
{
339+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, instanceSize),
340+
ExpectError: regexp.MustCompile("CANNOT_PAUSE_RECENTLY_RESUMED_CLUSTER"),
341+
},
342+
{
343+
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false, instanceSize),
344+
},
330345
{
331346
ResourceName: resourceName,
332347
ImportStateIdFunc: testAccCheckMongoDBAtlasClusterImportStateIDFunc(resourceName),
@@ -964,7 +979,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
964979
`, orgID, projectName, name)
965980
}
966981

967-
func testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, name string, paused bool) string {
982+
func testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, name string, paused bool, instanceSize string) string {
968983
return fmt.Sprintf(`
969984
resource "mongodbatlas_project" "cluster_project" {
970985
name = %[2]q
@@ -979,7 +994,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
979994
replication_specs {
980995
region_configs {
981996
electable_specs {
982-
instance_size = "M10"
997+
instance_size = %[5]q
983998
node_count = 3
984999
}
9851000
analytics_specs {
@@ -992,7 +1007,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
9921007
}
9931008
}
9941009
}
995-
`, orgID, projectName, name, paused)
1010+
`, orgID, projectName, name, paused, instanceSize)
9961011
}
9971012

9981013
func testAccMongoDBAtlasAdvancedClusterConfigAdvancedConf(orgID, projectName, name string, p *matlas.ProcessArgs) string {

website/docs/r/advanced_cluster.html.markdown

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ This parameter defaults to false.
398398
* `version_release_system` - (Optional) - Release cadence that Atlas uses for this cluster. This parameter defaults to `LTS`. If you set this field to `CONTINUOUS`, you must omit the `mongo_db_major_version` field. Atlas accepts:
399399
- `CONTINUOUS`: Atlas creates your cluster using the most recent MongoDB release. Atlas automatically updates your cluster to the latest major and rapid MongoDB releases as they become available.
400400
- `LTS`: Atlas creates your cluster using the latest patch release of the MongoDB version that you specify in the mongoDBMajorVersion field. Atlas automatically updates your cluster to subsequent patch releases of this MongoDB version. Atlas doesn't update your cluster to newer rapid or major MongoDB releases as they become available.
401-
* `paused` (Optional) - Flag that indicates whether the cluster is paused or not. You can pause M10 or larger clusters. You cannot initiate pausing for a shared/tenant tier cluster. See [Considerations for Paused Clusters](https://docs.atlas.mongodb.com/pause-terminate-cluster/#considerations-for-paused-clusters)
401+
* `paused` (Optional) - Flag that indicates whether the cluster is paused or not. You can pause M10 or larger clusters. You cannot initiate pausing for a shared/tenant tier cluster. If you try to update a `paused` cluster you will get a `CANNOT_UPDATE_PAUSED_CLUSTER` error. See [Considerations for Paused Clusters](https://docs.atlas.mongodb.com/pause-terminate-cluster/#considerations-for-paused-clusters).
402402
**NOTE** Pause lasts for up to 30 days. If you don't resume the cluster within 30 days, Atlas resumes the cluster. When the cluster resumption happens Terraform will flag the changed state. If you wish to keep the cluster paused, reapply your Terraform configuration. If you prefer to allow the automated change of state to unpaused use:
403403
`lifecycle {
404404
ignore_changes = [paused]

0 commit comments

Comments
 (0)