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

fix!: handles paused clusters with errors when updating. #1640

Merged
merged 13 commits into from
Nov 21, 2023
15 changes: 3 additions & 12 deletions mongodbatlas/resource_mongodbatlas_advanced_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -750,21 +750,12 @@ func resourceMongoDBAtlasAdvancedClusterUpdate(ctx context.Context, d *schema.Re
// Has changes
if !reflect.DeepEqual(cluster, clusterChangeDetect) {
err := retry.RetryContext(ctx, timeout, func() *retry.RetryError {
_, _, err := updateAdvancedCluster(ctx, conn, cluster, projectID, clusterName, timeout)
_, resp, 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, timeout)
if err != nil {
return retry.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
}
}
if errors.As(err, &target) && target.HTTPCode == 400 {
if resp == nil || resp.StatusCode == 400 {
return retry.NonRetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
}
return retry.RetryableError(fmt.Errorf(errorClusterAdvancedUpdate, clusterName, err))
}
return nil
})
Expand Down
39 changes: 27 additions & 12 deletions mongodbatlas/resource_mongodbatlas_advanced_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand Down Expand Up @@ -241,11 +242,13 @@ func TestAccClusterAdvancedCluster_multicloudSharded(t *testing.T) {
func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
SkipTest(t)
var (
cluster matlas.AdvancedCluster
resourceName = "mongodbatlas_advanced_cluster.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
rName = acctest.RandomWithPrefix("test-acc")
cluster matlas.AdvancedCluster
resourceName = "mongodbatlas_advanced_cluster.test"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
rName = acctest.RandomWithPrefix("test-acc")
instanceSize = "M10"
anotherInstanceSize = "M20"
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -254,7 +257,7 @@ func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
CheckDestroy: testAccCheckMongoDBAtlasAdvancedClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false),
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false, instanceSize),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
Expand All @@ -266,7 +269,7 @@ func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
),
},
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true),
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, instanceSize),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
Expand All @@ -277,6 +280,10 @@ func TestAccClusterAdvancedCluster_UnpausedToPaused(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "paused", "true"),
),
},
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, anotherInstanceSize),
ExpectError: regexp.MustCompile("CANNOT_UPDATE_PAUSED_CLUSTER"),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckMongoDBAtlasClusterImportStateIDFunc(resourceName),
Expand All @@ -296,6 +303,7 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
rName = acctest.RandomWithPrefix("test-acc")
instanceSize = "M10"
)

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -304,7 +312,7 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
CheckDestroy: testAccCheckMongoDBAtlasAdvancedClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true),
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, instanceSize),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
Expand All @@ -316,7 +324,7 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
),
},
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false),
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false, instanceSize),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
Expand All @@ -327,6 +335,13 @@ func TestAccClusterAdvancedCluster_PausedToUnpaused(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "paused", "false"),
),
},
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, true, instanceSize),
ExpectError: regexp.MustCompile("CANNOT_PAUSE_RECENTLY_RESUMED_CLUSTER"),
},
{
Config: testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, rName, false, instanceSize),
},
{
ResourceName: resourceName,
ImportStateIdFunc: testAccCheckMongoDBAtlasClusterImportStateIDFunc(resourceName),
Expand Down Expand Up @@ -964,7 +979,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
`, orgID, projectName, name)
}

func testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, name string, paused bool) string {
func testAccMongoDBAtlasAdvancedClusterConfigSingleProviderPaused(orgID, projectName, name string, paused bool, instanceSize string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "cluster_project" {
name = %[2]q
Expand All @@ -979,7 +994,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
replication_specs {
region_configs {
electable_specs {
instance_size = "M10"
instance_size = %[5]q
node_count = 3
}
analytics_specs {
Expand All @@ -992,7 +1007,7 @@ resource "mongodbatlas_advanced_cluster" "test" {
}
}
}
`, orgID, projectName, name, paused)
`, orgID, projectName, name, paused, instanceSize)
}

func testAccMongoDBAtlasAdvancedClusterConfigAdvancedConf(orgID, projectName, name string, p *matlas.ProcessArgs) string {
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/advanced_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ This parameter defaults to false.
* `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:
- `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.
- `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.
* `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)
* `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).
**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:
`lifecycle {
ignore_changes = [paused]
Expand Down
Loading