Skip to content

Commit 6532731

Browse files
lantoliEspenAlbert
andauthored
chore: Takes electable_specs in TPF adv_cluster from state if it was in config (#3203)
* electableSpecs * add test removing electable_specs * Revert "add test removing electable_specs" This reverts commit 296bca2. * test: Enhance PlanChecks with specInstanceSizeNodeCount for read_only_specs and electable_specs --------- Co-authored-by: Espen Albert <espen.albert@mongodb.com>
1 parent ce2ad96 commit 6532731

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

internal/service/advancedcluster/resource_advanced_cluster_test.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,7 @@ func configSharded(t *testing.T, projectID, clusterName string, withUpdate bool)
15001500

15011501
func configBlocks(t *testing.T, projectID, clusterName, instanceSize string, defineBlocks bool) string {
15021502
t.Helper()
1503-
var extraConfig0, extraConfig1 string
1503+
var extraConfig0, extraConfig1, electableSpecs0 string
15041504
autoScalingBlocks := `
15051505
auto_scaling {
15061506
disk_gb_enabled = true
@@ -1518,6 +1518,12 @@ func configBlocks(t *testing.T, projectID, clusterName, instanceSize string, def
15181518
}
15191519
`
15201520
if defineBlocks {
1521+
electableSpecs0 = `
1522+
electable_specs {
1523+
instance_size = "M10"
1524+
node_count = 5
1525+
}
1526+
`
15211527
// read only + autoscaling blocks
15221528
extraConfig0 = `
15231529
read_only_specs {
@@ -1549,10 +1555,7 @@ func configBlocks(t *testing.T, projectID, clusterName, instanceSize string, def
15491555
provider_name = "AWS"
15501556
priority = 7
15511557
region_name = "US_EAST_1"
1552-
electable_specs {
1553-
instance_size = "M10"
1554-
node_count = 5
1555-
}
1558+
%[6]s
15561559
%[4]s
15571560
}
15581561
}
@@ -1577,7 +1580,7 @@ func configBlocks(t *testing.T, projectID, clusterName, instanceSize string, def
15771580
}
15781581
}
15791582
}
1580-
`, projectID, clusterName, instanceSize, extraConfig0, extraConfig1))
1583+
`, projectID, clusterName, instanceSize, extraConfig0, extraConfig1, electableSpecs0))
15811584
}
15821585

15831586
func checkBlocks(instanceSize string) resource.TestCheckFunc {

internal/service/advancedclustertpf/plan_modifier.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,14 @@ func AdjustRegionConfigsChildren(ctx context.Context, diags *diag.Diagnostics, s
119119
return
120120
}
121121
for j := range minLen(planRegionConfigsTF, stateRegionConfigsTF) {
122+
stateElectableSpecs := TFModelObject[TFSpecsModel](ctx, stateRegionConfigsTF[j].ElectableSpecs)
123+
planElectableSpecs := TFModelObject[TFSpecsModel](ctx, planRegionConfigsTF[j].ElectableSpecs)
124+
if planElectableSpecs == nil && stateElectableSpecs != nil && stateElectableSpecs.NodeCount.ValueInt64() > 0 {
125+
planRegionConfigsTF[j].ElectableSpecs = stateRegionConfigsTF[j].ElectableSpecs
126+
planElectableSpecs = stateElectableSpecs
127+
}
122128
stateReadOnlySpecs := TFModelObject[TFSpecsModel](ctx, stateRegionConfigsTF[j].ReadOnlySpecs)
123129
planReadOnlySpecs := TFModelObject[TFSpecsModel](ctx, planRegionConfigsTF[j].ReadOnlySpecs)
124-
planElectableSpecs := TFModelObject[TFSpecsModel](ctx, planRegionConfigsTF[j].ElectableSpecs)
125130
if stateReadOnlySpecs != nil { // read_only_specs is present in state
126131
// logic below ensures that if read only specs is present in state but not in the plan, plan will be populated so that read only spec configuration is not removed on update operations
127132
newPlanReadOnlySpecs := planReadOnlySpecs

internal/service/advancedclustertpf/plan_modifier_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ func autoScalingKnownValue(computeEnabled, diskEnabled, scaleDown bool, minInsta
2727
})
2828
}
2929

30+
func specInstanceSizeNodeCount(instanceSize string, nodeCount int) knownvalue.Check {
31+
return knownvalue.ObjectPartial(map[string]knownvalue.Check{
32+
"instance_size": knownvalue.StringExact(instanceSize),
33+
"node_count": knownvalue.Int64Exact(int64(nodeCount)),
34+
})
35+
}
36+
3037
func TestPlanChecksClusterTwoRepSpecsWithAutoScalingAndSpecs(t *testing.T) {
3138
var (
3239
baseConfig = unit.NewMockPlanChecksConfig(t, &mockConfig, unit.ImportNameClusterTwoRepSpecsWithAutoScalingAndSpecs)
@@ -43,15 +50,20 @@ func TestPlanChecksClusterTwoRepSpecsWithAutoScalingAndSpecs(t *testing.T) {
4350
ConfigFilename: "main_removed_blocks_from_config_and_instance_change.tf",
4451
Checks: []plancheck.PlanCheck{
4552
plancheck.ExpectResourceAction(resourceName, plancheck.ResourceActionUpdate),
46-
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("read_only_specs").AtMapKey("instance_size"), knownvalue.StringExact("M10")),
47-
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("read_only_specs").AtMapKey("instance_size"), knownvalue.StringExact("M20")),
53+
// checks regionConfig0
54+
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M10", 2)),
55+
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M10", 5)),
4856
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("auto_scaling"), autoScalingEnabled),
4957
plancheck.ExpectKnownValue(resourceName, regionConfig0.AtMapKey("analytics_auto_scaling"), autoScalingEnabled),
58+
plancheck.ExpectUnknownValue(resourceName, regionConfig0.AtMapKey("analytics_specs")), // analytics specs was defined in region_configs.0 but not in region_configs.1
59+
plancheck.ExpectUnknownValue(resourceName, repSpec0.AtMapKey("id")),
60+
61+
// checks regionConfig1
62+
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("read_only_specs"), specInstanceSizeNodeCount("M20", 1)),
63+
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("electable_specs"), specInstanceSizeNodeCount("M20", 3)),
5064
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("auto_scaling"), autoScalingEnabled),
5165
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("analytics_auto_scaling"), autoScalingEnabled),
52-
plancheck.ExpectUnknownValue(resourceName, regionConfig0.AtMapKey("analytics_specs")), // analytics specs was defined in region_configs.0 but not in region_configs.1
5366
plancheck.ExpectKnownValue(resourceName, regionConfig1.AtMapKey("analytics_specs"), knownvalue.NotNull()),
54-
plancheck.ExpectUnknownValue(resourceName, repSpec0.AtMapKey("id")),
5567
plancheck.ExpectUnknownValue(resourceName, repSpec1.AtMapKey("id")),
5668
},
5769
},

internal/service/advancedclustertpf/testdata/ClusterTwoRepSpecsWithAutoScalingAndSpecs/main_removed_blocks_from_config_and_instance_change.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ resource "mongodbatlas_advanced_cluster" "test" {
66

77
replication_specs = [{
88
region_configs = [{
9-
electable_specs = {
10-
instance_size = "M10"
11-
node_count = 5
12-
}
139
priority = 7
1410
provider_name = "AWS"
1511
region_name = "US_EAST_1"

0 commit comments

Comments
 (0)