Skip to content

Commit 9c6c432

Browse files
fix: Avoid error when removing read only spec in region config that does not define electable specs (#3162)
* add additional step for ensuring empty plans are encountered when blocks are removed * adjust test step check making it different from one for migration tests * add logic for copying values from state for read only spec if electable is not defined * refactor removeBlocksFromConfig into regular acceptance test * adding changelog * fix replicationSpecsAutoScaling avoiding misalignment of instance_size and disk_size_gb values with state after ignore block is removed * fix removeBlocksFromConfig by using node_count instead of instance_size for triggering a change * fix for handling change of instance size in separate region config * Revert "fix removeBlocksFromConfig by using node_count instead of instance_size for triggering a change" This reverts commit 9709558. * favour electable specs of same region config over one defined in general replication spec * adjust check to consider change in read only spec of different region config * adding additional docs clarification comments
1 parent 9ccac8f commit 9c6c432

9 files changed

+91
-540
lines changed

.changelog/3162.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/mongodbatlas_advanced_cluster (preview provider 2.0.0): Avoids error when removing `read_only_specs` in `region_configs` that does not define `electable_specs`
3+
```

internal/service/advancedcluster/resource_advanced_cluster_test.go

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -212,14 +212,23 @@ func replicaSetAWSProviderTestCase(t *testing.T, isAcc bool) resource.TestCase {
212212
}),
213213
Check: checkReplicaSetAWSProvider(isAcc, projectID, clusterName, 60, 3, true, true),
214214
},
215+
// empty plan when analytics block is removed
216+
acc.TestStepCheckEmptyPlan(configAWSProvider(t, isAcc, ReplicaSetAWSConfig{
217+
ProjectID: projectID,
218+
ClusterName: clusterName,
219+
ClusterType: "REPLICASET",
220+
DiskSizeGB: 60,
221+
NodeCountElectable: 3,
222+
WithAnalyticsSpecs: false,
223+
})),
215224
{
216225
Config: configAWSProvider(t, isAcc, ReplicaSetAWSConfig{
217226
ProjectID: projectID,
218227
ClusterName: clusterName,
219228
ClusterType: "REPLICASET",
220229
DiskSizeGB: 50,
221230
NodeCountElectable: 5,
222-
WithAnalyticsSpecs: false, // removed as part of other updates, computed value is expected to be the same
231+
WithAnalyticsSpecs: false, // other update made after removed analytics block, computed value is expected to be the same
223232
}),
224233
Check: checkReplicaSetAWSProvider(isAcc, projectID, clusterName, 50, 5, true, true),
225234
},
@@ -556,8 +565,10 @@ func TestAccClusterAdvancedClusterConfig_replicationSpecsAutoScaling(t *testing.
556565
acc.TestCheckResourceAttrPreviewProviderV2(true, resourceName, "replication_specs.0.region_configs.0.electable_specs.0.disk_size_gb", "10"), // modified disk size gb in config is ignored
557566
),
558567
},
568+
// empty plan when auto_scaling block is removed (also aligns instance_size/disk_size_gb to values in state)
569+
acc.TestStepCheckEmptyPlan(configReplicationSpecsAutoScaling(t, true, projectID, clusterName, nil, "M10", 10, 1)),
559570
{
560-
Config: configReplicationSpecsAutoScaling(t, true, projectID, clusterName, nil, "M10", 10, 2), // auto_scaling block removed together with other changes, preserves previous state
571+
Config: configReplicationSpecsAutoScaling(t, true, projectID, clusterName, nil, "M10", 10, 2), // other change after autoscaling block removed, preserves previous state
561572
Check: resource.ComposeAggregateTestCheckFunc(
562573
acc.CheckExistsCluster(resourceName),
563574
acc.TestCheckResourceAttrPreviewProviderV2(true, resourceName, "name", clusterName),
@@ -610,8 +621,10 @@ func TestAccClusterAdvancedClusterConfig_replicationSpecsAnalyticsAutoScaling(t
610621
acc.TestCheckResourceAttrPreviewProviderV2(true, resourceName, "replication_specs.0.region_configs.0.analytics_auto_scaling.0.compute_enabled", "true"),
611622
),
612623
},
624+
// empty plan when analytics_auto_scaling block is removed
625+
acc.TestStepCheckEmptyPlan(configReplicationSpecsAnalyticsAutoScaling(t, true, projectID, clusterNameUpdated, nil, 1)),
613626
{
614-
Config: configReplicationSpecsAnalyticsAutoScaling(t, true, projectID, clusterNameUpdated, nil, 2), // analytics_auto_scaling block removed together with other changes, preserves previous state
627+
Config: configReplicationSpecsAnalyticsAutoScaling(t, true, projectID, clusterNameUpdated, nil, 2), // other changes after analytics_auto_scaling block removed, preserves previous state
615628
Check: resource.ComposeAggregateTestCheckFunc(
616629
acc.CheckExistsCluster(resourceName),
617630
acc.TestCheckResourceAttrPreviewProviderV2(true, resourceName, "name", clusterNameUpdated),
@@ -1374,23 +1387,25 @@ func TestAccMockableAdvancedCluster_shardedAddAnalyticsAndAutoScaling(t *testing
13741387
})
13751388
}
13761389

1377-
func TestAccMockableAdvancedCluster_removeBlocksFromConfig(t *testing.T) {
1390+
func TestAccAdvancedCluster_removeBlocksFromConfig(t *testing.T) {
13781391
if !config.PreviewProviderV2AdvancedCluster() { // SDKv2 don't set "computed" specs in the state
13791392
t.Skip("This test is not applicable for SDKv2")
13801393
}
13811394
var (
13821395
projectID, clusterName = acc.ProjectIDExecutionWithCluster(t, 15)
13831396
)
1384-
unit.CaptureOrMockTestCaseAndRun(t, mockConfig, &resource.TestCase{
1397+
resource.ParallelTest(t, resource.TestCase{
13851398
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
13861399
Steps: []resource.TestStep{
13871400
{
1388-
Config: configBlocks(t, projectID, clusterName, true),
1389-
Check: checkBlocks(true),
1401+
Config: configBlocks(t, projectID, clusterName, "M10", true),
1402+
Check: checkBlocks("M10"),
13901403
},
1404+
// removing blocks generates an empty plan
1405+
acc.TestStepCheckEmptyPlan(configBlocks(t, projectID, clusterName, "M10", false)),
13911406
{
1392-
Config: configBlocks(t, projectID, clusterName, false),
1393-
Check: checkBlocks(false),
1407+
Config: configBlocks(t, projectID, clusterName, "M20", false), // applying a change after removing blocks preserves previous state
1408+
Check: checkBlocks("M20"),
13941409
},
13951410
acc.TestStepImportCluster(resourceName),
13961411
},
@@ -1490,10 +1505,10 @@ func configSharded(t *testing.T, projectID, clusterName string, withUpdate bool)
14901505
`, projectID, clusterName, autoScaling, analyticsSpecs, analyticsSpecsForSpec2)) + dataSourcesTFNewSchema
14911506
}
14921507

1493-
func configBlocks(t *testing.T, projectID, clusterName string, firstStep bool) string {
1508+
func configBlocks(t *testing.T, projectID, clusterName, instanceSize string, defineBlocks bool) string {
14941509
t.Helper()
14951510
var extraConfig0, extraConfig1 string
1496-
autoStr := `
1511+
autoScalingBlocks := `
14971512
auto_scaling {
14981513
disk_gb_enabled = true
14991514
compute_enabled = true
@@ -1509,15 +1524,15 @@ func configBlocks(t *testing.T, projectID, clusterName string, firstStep bool) s
15091524
compute_scale_down_enabled = true
15101525
}
15111526
`
1512-
instanceSize1 := "M20"
1513-
if firstStep {
1514-
instanceSize1 = "M10"
1527+
if defineBlocks {
1528+
// read only + autoscaling blocks
15151529
extraConfig0 = `
15161530
read_only_specs {
15171531
instance_size = "M10"
15181532
node_count = 2
15191533
}
1520-
` + autoStr
1534+
` + autoScalingBlocks
1535+
// read only + analytics + autoscaling blocks
15211536
extraConfig1 = `
15221537
read_only_specs {
15231538
instance_size = "M10"
@@ -1527,7 +1542,7 @@ func configBlocks(t *testing.T, projectID, clusterName string, firstStep bool) s
15271542
instance_size = "M10"
15281543
node_count = 4
15291544
}
1530-
` + autoStr
1545+
` + autoScalingBlocks
15311546
}
15321547
return acc.ConvertAdvancedClusterToPreviewProviderV2(t, true, fmt.Sprintf(`
15331548
resource "mongodbatlas_advanced_cluster" "test" {
@@ -1561,29 +1576,34 @@ func configBlocks(t *testing.T, projectID, clusterName string, firstStep bool) s
15611576
}
15621577
%[5]s
15631578
}
1579+
region_configs { // region with no electable specs
1580+
provider_name = "AWS"
1581+
priority = 0
1582+
region_name = "US_EAST_1"
1583+
%[4]s
1584+
}
15641585
}
15651586
}
1566-
`, projectID, clusterName, instanceSize1, extraConfig0, extraConfig1))
1587+
`, projectID, clusterName, instanceSize, extraConfig0, extraConfig1))
15671588
}
15681589

1569-
func checkBlocks(firstStep bool) resource.TestCheckFunc {
1590+
func checkBlocks(instanceSize string) resource.TestCheckFunc {
15701591
checksMap := map[string]string{
15711592
"replication_specs.0.region_configs.0.electable_specs.0.instance_size": "M10",
15721593
"replication_specs.0.region_configs.0.electable_specs.0.node_count": "5",
15731594
"replication_specs.0.region_configs.0.read_only_specs.0.instance_size": "M10",
15741595
"replication_specs.0.region_configs.0.read_only_specs.0.node_count": "2",
15751596
"replication_specs.0.region_configs.0.analytics_specs.0.node_count": "0",
15761597

1577-
"replication_specs.1.region_configs.0.electable_specs.0.instance_size": "M10",
1598+
"replication_specs.1.region_configs.0.electable_specs.0.instance_size": instanceSize,
15781599
"replication_specs.1.region_configs.0.electable_specs.0.node_count": "3",
1579-
"replication_specs.1.region_configs.0.read_only_specs.0.instance_size": "M10",
1600+
"replication_specs.1.region_configs.0.read_only_specs.0.instance_size": instanceSize,
15801601
"replication_specs.1.region_configs.0.read_only_specs.0.node_count": "1",
15811602
"replication_specs.1.region_configs.0.analytics_specs.0.instance_size": "M10",
15821603
"replication_specs.1.region_configs.0.analytics_specs.0.node_count": "4",
1583-
}
1584-
if !firstStep {
1585-
checksMap["replication_specs.1.region_configs.0.electable_specs.0.instance_size"] = "M20"
1586-
checksMap["replication_specs.1.region_configs.0.read_only_specs.0.instance_size"] = "M20"
1604+
1605+
"replication_specs.1.region_configs.1.read_only_specs.0.instance_size": instanceSize,
1606+
"replication_specs.1.region_configs.1.read_only_specs.0.node_count": "2",
15871607
}
15881608
for repSpecsIdx := range 2 {
15891609
for _, block := range []string{"auto_scaling", "analytics_auto_scaling"} {

internal/service/advancedcluster/testdata/TestAccMockableAdvancedCluster_removeBlocksFromConfig.yaml

Lines changed: 0 additions & 312 deletions
This file was deleted.

internal/service/advancedcluster/testdata/TestAccMockableAdvancedCluster_removeBlocksFromConfig/01_01_POST__api_atlas_v2_groups_{groupId}_clusters_2024-10-23.json

Lines changed: 0 additions & 92 deletions
This file was deleted.

internal/service/advancedcluster/testdata/TestAccMockableAdvancedCluster_removeBlocksFromConfig/02_01_PATCH__api_atlas_v2_groups_{groupId}_clusters_{clusterName}_2024-10-23.json

Lines changed: 0 additions & 95 deletions
This file was deleted.

internal/service/advancedcluster/testdata/TestAccMockableAdvancedCluster_removeBlocksFromConfig/03_01_DELETE__api_atlas_v2_groups_{groupId}_clusters_{clusterName}_2023-02-01.json

Whitespace-only changes.

0 commit comments

Comments
 (0)