Skip to content

Commit af906f1

Browse files
committed
multi repls
1 parent 57f284b commit af906f1

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

internal/convert/const_names.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
nElectableNodes = "electable_nodes"
4545
nReadOnlyNodes = "read_only_nodes"
4646
nAnalyticsNodes = "analytics_nodes"
47+
nZoneName = "zone_name"
4748
nKey = "key"
4849
nValue = "value"
4950
)

internal/convert/convert.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
5555
resource.SetLabels(labels)
5656

5757
if resourceb.FirstMatchingBlock(nRepSpecs, nil) != nil {
58-
err = fillReplicationSpecs(resourceb)
58+
err = fillCluster(resourceb)
5959
} else {
60-
err = fillFreeTier(resourceb)
60+
err = fillFreeTierCluster(resourceb)
6161
}
6262
if err != nil {
6363
return nil, err
@@ -70,8 +70,8 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
7070
return parser.Bytes(), nil
7171
}
7272

73-
// fillFreeTier is the entry point to convert clusters in free tier
74-
func fillFreeTier(resourceb *hclwrite.Body) error {
73+
// fillFreeTierCluster is the entry point to convert clusters in free tier
74+
func fillFreeTierCluster(resourceb *hclwrite.Body) error {
7575
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType))
7676
config := hclwrite.NewEmptyFile()
7777
configb := config.Body()
@@ -97,28 +97,18 @@ func fillFreeTier(resourceb *hclwrite.Body) error {
9797
return nil
9898
}
9999

100-
// fillReplicationSpecs is the entry point to convert clusters with replications_specs (all but free tier)
101-
func fillReplicationSpecs(resourceb *hclwrite.Body) error {
100+
// fillCluster is the entry point to convert clusters with replications_specs (all but free tier)
101+
func fillCluster(resourceb *hclwrite.Body) error {
102102
root, errRoot := popRootAttrs(resourceb)
103103
if errRoot != nil {
104104
return errRoot
105105
}
106106
resourceb.RemoveAttribute(nNumShards) // num_shards in root is not relevant, only in replication_specs
107107
// ok to fail as cloud_backup is optional
108108
_ = hcl.MoveAttr(resourceb, resourceb, nCloudBackup, nBackupEnabled, errRepSpecs)
109-
110-
// at least one replication_specs exists here, if not it would be a free tier cluster
111-
repSpecsSrc := resourceb.FirstMatchingBlock(nRepSpecs, nil)
112-
if err := checkDynamicBlock(repSpecsSrc.Body()); err != nil {
109+
if err := fillReplicationSpecs(resourceb, root); err != nil {
113110
return err
114111
}
115-
configs, errConfigs := getRegionConfigs(repSpecsSrc, root)
116-
if errConfigs != nil {
117-
return errConfigs
118-
}
119-
repSpecs := hclwrite.NewEmptyFile()
120-
repSpecs.Body().SetAttributeRaw(nConfig, configs)
121-
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs.Body()))
122112
tags, errTags := getTagsLabelsOpt(resourceb, nTags)
123113
if errTags != nil {
124114
return errTags
@@ -137,7 +127,35 @@ func fillReplicationSpecs(resourceb *hclwrite.Body) error {
137127
fillBlockOpt(resourceb, nAdvConf)
138128
fillBlockOpt(resourceb, nBiConnector)
139129
fillBlockOpt(resourceb, nPinnedFCV)
140-
resourceb.RemoveBlock(repSpecsSrc)
130+
return nil
131+
}
132+
133+
func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
134+
// at least one replication_specs exists here, if not it would be a free tier cluster
135+
var specbs []*hclwrite.Body
136+
for {
137+
var (
138+
specSrc = resourceb.FirstMatchingBlock(nRepSpecs, nil)
139+
spec = hclwrite.NewEmptyFile()
140+
specb = spec.Body()
141+
)
142+
if specSrc == nil {
143+
break
144+
}
145+
if err := checkDynamicBlock(specSrc.Body()); err != nil {
146+
return err
147+
}
148+
configs, err := getRegionConfigs(specSrc, root)
149+
if err != nil {
150+
return err
151+
}
152+
// ok to fail as zone_name is optional
153+
_ = hcl.MoveAttr(specSrc.Body(), specb, nZoneName, nZoneName, errRepSpecs)
154+
specb.SetAttributeRaw(nConfig, configs)
155+
specbs = append(specbs, specb)
156+
resourceb.RemoveBlock(specSrc)
157+
}
158+
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArray(specbs))
141159
return nil
142160
}
143161

internal/convert/testdata/clu2adv/multi_replicaction_specs.in.tf

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
resource "mongodbatlas_cluster" "multirep" {
2-
project_id = "66d979971ec97b7de1ef8777"
2+
project_id = var.project_id
33
name = "multirep"
44
disk_size_gb = 80
55
num_shards = 1
66
cloud_backup = false
77
cluster_type = "GEOSHARDED"
88
provider_name = "AWS"
99
provider_instance_size_name = "M10"
10-
1110
replication_specs {
1211
zone_name = "Zone 1"
1312
num_shards = 1

internal/convert/testdata/clu2adv/multi_replicaction_specs.out.tf

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
resource "mongodbatlas_advanced_cluster" "multirep" {
22
project_id = var.project_id
33
name = "multirep"
4-
backup_enabled = false
54
cluster_type = "GEOSHARDED"
5+
backup_enabled = false
66
replication_specs = [
77
{
88
zone_name = "Zone 1"
99
region_configs = [
1010
{
11+
provider_name = "AWS"
1112
region_name = "US_EAST_1"
1213
priority = 7
13-
provider_name = "AWS"
1414
electable_specs = {
1515
node_count = 3
1616
instance_size = "M10"
@@ -20,12 +20,12 @@ resource "mongodbatlas_advanced_cluster" "multirep" {
2020
]
2121
},
2222
{
23-
zone_name = "Zone 2",
23+
zone_name = "Zone 2"
2424
region_configs = [
2525
{
26+
provider_name = "AWS"
2627
region_name = "US_WEST_2"
2728
priority = 7
28-
provider_name = "AWS"
2929
electable_specs = {
3030
node_count = 3
3131
instance_size = "M10"

internal/convert/testdata/clu2adv/replication_specs_unsupported_dynamic.in.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
resource "mongodbatlas_cluster" "geo" {
3-
project_id = "66d979971ec97b7de1ef8777"
3+
project_id = var.project_id
44
name = "geo"
55
cluster_type = "GEOSHARDED"
66
num_shards = 1

0 commit comments

Comments
 (0)