Skip to content

Commit 77d12d0

Browse files
committed
implementation
1 parent a80db24 commit 77d12d0

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

internal/convert/clu2adv.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,32 @@ func fillMovedBlocks(body *hclwrite.Body, moveLabels []string) {
107107
}
108108
}
109109

110+
// createDefaultReplicationSpec creates a default replication_specs for clusters without any
111+
// (e.g. upgraded from free tier).
112+
func createDefaultReplicationSpec(resourceb *hclwrite.Body, root attrVals) error {
113+
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType))
114+
configb := hclwrite.NewEmptyFile().Body()
115+
hcl.SetAttrInt(configb, nPriority, valMaxPriority)
116+
if err := hcl.MoveAttr(resourceb, configb, nRegionNameSrc, nRegionName, errRoot); err != nil {
117+
return err
118+
}
119+
if providerNameTokens, found := root.req[nProviderName]; found {
120+
configb.SetAttributeRaw(nProviderName, providerNameTokens)
121+
}
122+
123+
electableSpecb := hclwrite.NewEmptyFile().Body()
124+
if instanceSizeTokens, found := root.req[nInstanceSizeSrc]; found {
125+
electableSpecb.SetAttributeRaw(nInstanceSize, instanceSizeTokens)
126+
}
127+
electableSpecb.SetAttributeValue(nNodeCount, cty.NumberIntVal(valDefaultNodeCount))
128+
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpecb))
129+
130+
repSpecsb := hclwrite.NewEmptyFile().Body()
131+
repSpecsb.SetAttributeRaw(nConfig, hcl.TokensArraySingle(configb))
132+
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecsb))
133+
return nil
134+
}
135+
110136
// fillFreeTierCluster is the entry point to convert clusters in free tier
111137
func processFreeTierCluster(resourceb *hclwrite.Body) error {
112138
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType))
@@ -160,7 +186,7 @@ func processRepSpecsCluster(resourceb *hclwrite.Body, root attrVals) error {
160186
}
161187
repSpecBlocks := collectBlocks(resourceb, nRepSpecs)
162188
if len(repSpecBlocks) == 0 {
163-
return fmt.Errorf("must have at least one replication_specs")
189+
return createDefaultReplicationSpec(resourceb, root)
164190
}
165191
dConfig, err := processConfigsWithDynamicRegion(repSpecBlocks[0].Body(), root, false)
166192
if err != nil {

internal/convert/const.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
package convert
22

33
const (
4-
resourceType = "resource"
5-
dataSourceType = "data"
6-
cluster = "mongodbatlas_cluster"
7-
advCluster = "mongodbatlas_advanced_cluster"
8-
clusterPlural = "mongodbatlas_clusters"
9-
advClusterPlural = "mongodbatlas_advanced_clusters"
10-
valClusterType = "REPLICASET"
11-
valMaxPriority = 7
12-
valMinPriority = 0
13-
errFreeCluster = "free cluster (because no " + nRepSpecs + ")"
14-
errRepSpecs = "setting " + nRepSpecs
15-
errPriority = "setting " + nPriority
16-
errNumShards = "setting " + nNumShards
17-
errRoot = "setting root attributes"
4+
resourceType = "resource"
5+
dataSourceType = "data"
6+
cluster = "mongodbatlas_cluster"
7+
advCluster = "mongodbatlas_advanced_cluster"
8+
clusterPlural = "mongodbatlas_clusters"
9+
advClusterPlural = "mongodbatlas_advanced_clusters"
10+
valClusterType = "REPLICASET"
11+
valMaxPriority = 7
12+
valMinPriority = 0
13+
valDefaultNodeCount = 3
14+
errFreeCluster = "free cluster (because no " + nRepSpecs + ")"
15+
errRepSpecs = "setting " + nRepSpecs
16+
errPriority = "setting " + nPriority
17+
errNumShards = "setting " + nNumShards
18+
errRoot = "setting root attributes"
1819

1920
commentGeneratedBy = "Generated by atlas-cli-plugin-terraform."
2021
commentConfirmReferences = "Please review the changes and confirm that references to this resource are updated."

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ resource "mongodbatlas_advanced_cluster" "count" {
4747
# Please review the changes and confirm that references to this resource are updated.
4848
}
4949

50+
5051
resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster" {
52+
# upgraded free cluster to dedicated
5153
project_id = var.project_id
5254
name = var.cluster_name
5355
cluster_type = "REPLICASET"
@@ -60,6 +62,7 @@ resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster" {
6062
provider_name = "AWS"
6163
electable_specs = {
6264
instance_size = "M10"
65+
node_count = 3
6366
}
6467
}
6568
]
@@ -71,6 +74,7 @@ resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster" {
7174
}
7275

7376
resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster_with_variables" {
77+
# upgraded free cluster to dedicated, using variables in all attributes
7478
project_id = var.project_id
7579
name = var.cluster_name
7680
cluster_type = "REPLICASET"
@@ -83,6 +87,7 @@ resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster_with_variabl
8387
provider_name = var.provider_name
8488
electable_specs = {
8589
instance_size = var.instance_size
90+
node_count = 3
8691
}
8792
}
8893
]

0 commit comments

Comments
 (0)