Skip to content

Commit cbeec93

Browse files
authored
fix: Sets replication_specs IDs when updating them. (#1876)
* fix: sets replication_specs IDs when updating them. * change test value type. * fix resource name for tests. * fix resource_cluster test expectations.
1 parent 8d173b1 commit cbeec93

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

internal/service/advancedcluster/resource_advanced_cluster.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,10 @@ func expandAdvancedReplicationSpec(tfMap map[string]any) *matlas.AdvancedReplica
850850
RegionConfigs: expandRegionConfigs(tfMap["region_configs"].([]any)),
851851
}
852852

853+
if tfMap["id"].(string) != "" {
854+
apiObject.ID = tfMap["id"].(string)
855+
}
856+
853857
return apiObject
854858
}
855859

internal/service/advancedcluster/resource_advanced_cluster_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,44 @@ func TestAccClusterAdvancedClusterConfig_ReplicationSpecsAnalyticsAutoScaling(t
599599
})
600600
}
601601

602+
func TestAccClusterAdvancedClusterConfig_ReplicationSpecsAndShardUpdating(t *testing.T) {
603+
var (
604+
cluster matlas.AdvancedCluster
605+
resourceName = "mongodbatlas_advanced_cluster.test"
606+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
607+
projectName = acctest.RandomWithPrefix("test-acc")
608+
rName = acctest.RandomWithPrefix("test-acc")
609+
numShards = "1"
610+
numShardsUpdated = "2"
611+
)
612+
613+
resource.ParallelTest(t, resource.TestCase{
614+
PreCheck: func() { acc.PreCheckBasic(t) },
615+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
616+
CheckDestroy: acc.CheckDestroyTeamAdvancedCluster,
617+
Steps: []resource.TestStep{
618+
{
619+
Config: testAccMongoDBAtlasAdvancedClusterConfigMultiZoneWithShards(orgID, projectName, rName, numShards, numShards),
620+
Check: resource.ComposeTestCheckFunc(
621+
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
622+
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
623+
resource.TestCheckResourceAttr(resourceName, "replication_specs.0.num_shards", "1"),
624+
resource.TestCheckResourceAttr(resourceName, "replication_specs.1.num_shards", "1"),
625+
),
626+
},
627+
{
628+
Config: testAccMongoDBAtlasAdvancedClusterConfigMultiZoneWithShards(orgID, projectName, rName, numShardsUpdated, numShards),
629+
Check: resource.ComposeTestCheckFunc(
630+
testAccCheckMongoDBAtlasAdvancedClusterExists(resourceName, &cluster),
631+
testAccCheckMongoDBAtlasAdvancedClusterAttributes(&cluster, rName),
632+
resource.TestCheckResourceAttr(resourceName, "replication_specs.0.num_shards", "2"),
633+
resource.TestCheckResourceAttr(resourceName, "replication_specs.1.num_shards", "1"),
634+
),
635+
},
636+
},
637+
})
638+
}
639+
602640
func TestAccClusterAdvancedCluster_WithTags(t *testing.T) {
603641
var (
604642
cluster matlas.AdvancedCluster
@@ -1142,3 +1180,59 @@ resource "mongodbatlas_advanced_cluster" "test" {
11421180
11431181
`, orgID, projectName, name, *p.Compute.Enabled, *p.DiskGBEnabled, p.Compute.MaxInstanceSize)
11441182
}
1183+
1184+
func testAccMongoDBAtlasAdvancedClusterConfigMultiZoneWithShards(orgID, projectName, name, numShardsFirstZone, numShardsSecondZone string) string {
1185+
return fmt.Sprintf(`
1186+
1187+
resource "mongodbatlas_project" "cluster_project" {
1188+
name = %[2]q
1189+
org_id = %[1]q
1190+
}
1191+
1192+
resource "mongodbatlas_advanced_cluster" "test" {
1193+
project_id = mongodbatlas_project.cluster_project.id
1194+
name = %[3]q
1195+
backup_enabled = false
1196+
mongo_db_major_version = "6.0"
1197+
cluster_type = "GEOSHARDED"
1198+
1199+
replication_specs {
1200+
zone_name = "zone n1"
1201+
num_shards = %[4]q
1202+
1203+
region_configs {
1204+
electable_specs {
1205+
instance_size = "M10"
1206+
node_count = 3
1207+
}
1208+
analytics_specs {
1209+
instance_size = "M10"
1210+
node_count = 0
1211+
}
1212+
provider_name = "AWS"
1213+
priority = 7
1214+
region_name = "US_EAST_1"
1215+
}
1216+
}
1217+
1218+
replication_specs {
1219+
zone_name = "zone n2"
1220+
num_shards = %[5]q
1221+
1222+
region_configs {
1223+
electable_specs {
1224+
instance_size = "M10"
1225+
node_count = 3
1226+
}
1227+
analytics_specs {
1228+
instance_size = "M10"
1229+
node_count = 0
1230+
}
1231+
provider_name = "AWS"
1232+
priority = 7
1233+
region_name = "EU_WEST_1"
1234+
}
1235+
}
1236+
}
1237+
`, orgID, projectName, name, numShardsFirstZone, numShardsSecondZone)
1238+
}

internal/service/cluster/resource_cluster_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,28 @@ func TestAccClusterRSCluster_RegionsConfig(t *testing.T) {
13801380
}
13811381
}`
13821382

1383+
replicationsShardsUpdate := `replication_specs {
1384+
num_shards = 2
1385+
zone_name = "us2"
1386+
regions_config{
1387+
region_name = "US_WEST_2"
1388+
electable_nodes = 3
1389+
priority = 7
1390+
read_only_nodes = 0
1391+
}
1392+
}
1393+
1394+
replication_specs {
1395+
num_shards = 1
1396+
zone_name = "us1"
1397+
regions_config{
1398+
region_name = "US_WEST_1"
1399+
electable_nodes = 3
1400+
priority = 7
1401+
read_only_nodes = 0
1402+
}
1403+
}`
1404+
13831405
resource.ParallelTest(t, resource.TestCase{
13841406
PreCheck: func() { acc.PreCheckBasic(t) },
13851407
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
@@ -1401,6 +1423,19 @@ func TestAccClusterRSCluster_RegionsConfig(t *testing.T) {
14011423
resource.TestCheckResourceAttr(resourceName, "replication_specs.#", "2"),
14021424
),
14031425
},
1426+
{
1427+
Config: testAccMongoDBAtlasClusterConfigRegions(orgID, projectName, clusterName, replicationsShardsUpdate),
1428+
Check: resource.ComposeTestCheckFunc(
1429+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
1430+
resource.TestCheckResourceAttr(resourceName, "name", clusterName),
1431+
// Note: replication_specs is a set for the cluster resource, therefore the order does not
1432+
// necessarily match the one used to insert the configuration in the .tf file.
1433+
// In fact, the num_shards field is used in the custom hash algorithm hence it affects the ordering.
1434+
// https://github.com/mongodb/terraform-provider-mongodbatlas/blob/059cd565e7aafd59eb8be30bbc9372b56ce2ffa4/internal/service/cluster/resource_cluster.go#L274
1435+
resource.TestCheckResourceAttr(resourceName, "replication_specs.0.num_shards", "1"),
1436+
resource.TestCheckResourceAttr(resourceName, "replication_specs.1.num_shards", "2"),
1437+
),
1438+
},
14041439
},
14051440
})
14061441
}

0 commit comments

Comments
 (0)