Skip to content

Commit a10a1e8

Browse files
coderGo93Edgar López
andauthored
INTMDB-199: Fixes the error when updating an replication specs after removed one zone (#434)
* fix: changed typelist to typeset in replication specs schema, and added validation and set the id for update to avoid an error about updating an existing replication specs * fixes linter error * refactor: changed TypeSet to TypeList for replication specs Co-authored-by: Edgar López <[email protected]>
1 parent 1bda102 commit a10a1e8

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

mongodbatlas/resource_mongodbatlas_cluster.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,14 +1165,23 @@ func expandReplicationSpecs(d *schema.ResourceData) ([]matlas.ReplicationSpec, e
11651165
if v, ok := d.GetOk("replication_specs"); ok {
11661166
for _, s := range v.([]interface{}) {
11671167
spec := s.(map[string]interface{})
1168-
1168+
id := cast.ToString(spec["id"])
1169+
// Check if has changes
1170+
if d.HasChange("replication_specs") && cast.ToString(d.Get("cluster_type")) == "GEOSHARDED" {
1171+
// Get original and new object
1172+
original, _ := d.GetChange("replication_specs")
1173+
isSame, oldID := compareReplicationSpecs(original, spec)
1174+
if isSame {
1175+
id = oldID
1176+
}
1177+
}
11691178
regionsConfig, err := expandRegionsConfig(spec["regions_config"].(*schema.Set).List())
11701179
if err != nil {
11711180
return rSpecs, err
11721181
}
11731182

11741183
rSpec := matlas.ReplicationSpec{
1175-
ID: cast.ToString(spec["id"]),
1184+
ID: id,
11761185
NumShards: pointy.Int64(cast.ToInt64(spec["num_shards"])),
11771186
ZoneName: cast.ToString(spec["zone_name"]),
11781187
RegionsConfig: regionsConfig,
@@ -1464,3 +1473,13 @@ func clusterConnectionStringsSchema() *schema.Schema {
14641473
},
14651474
}
14661475
}
1476+
1477+
func compareReplicationSpecs(oldObject interface{}, newValues map[string]interface{}) (flag bool, idSpec string) {
1478+
for _, s := range oldObject.([]interface{}) {
1479+
spec := s.(map[string]interface{})
1480+
if cast.ToString(spec["zone_name"]) == cast.ToString(newValues["zone_name"]) {
1481+
return true, cast.ToString(spec["id"])
1482+
}
1483+
}
1484+
return false, ""
1485+
}

0 commit comments

Comments
 (0)