Skip to content

Commit 8eef23c

Browse files
authored
feat: Allow no-free clusters without replication_specs in clu2adv command (#83)
1 parent f23bb1d commit 8eef23c

File tree

7 files changed

+205
-41
lines changed

7 files changed

+205
-41
lines changed

internal/convert/clu2adv.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ func convertResource(block *hclwrite.Block) (bool, error) {
6969
}
7070

7171
func isFreeTierCluster(resourceb *hclwrite.Body) bool {
72-
d, _ := getDynamicBlock(resourceb, nRepSpecs, true)
73-
return resourceb.FirstMatchingBlock(nRepSpecs, nil) == nil && !d.IsPresent()
72+
providerName, _ := hcl.GetAttrString(resourceb.GetAttribute(nProviderName))
73+
return providerName == nTenant
7474
}
7575

7676
func convertDataSource(block *hclwrite.Block) bool {
@@ -107,6 +107,32 @@ func fillMovedBlocks(body *hclwrite.Body, moveLabels []string) {
107107
}
108108
}
109109

110+
// createDefaultRepSpec creates a default replication_specs for clusters without any
111+
// (e.g. upgraded from free tier).
112+
func createDefaultRepSpec(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 createDefaultRepSpec(resourceb, root)
164190
}
165191
dConfig, err := processConfigsWithDynamicRegion(repSpecBlocks[0].Body(), root, false)
166192
if err != nil {

internal/convert/const.go

Lines changed: 16 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."
@@ -80,4 +81,5 @@ const (
8081
nSpec = "spec"
8182
nFailIndexKeyTooLong = "fail_index_key_too_long"
8283
nDefaultReadConcern = "default_read_concern"
84+
nTenant = "TENANT"
8385
)

internal/convert/testdata/clu2adv/free_cluster_with_count.in.tf renamed to internal/convert/testdata/clu2adv/comments.in.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
resource "resource1" "res1" {
2-
name = "name1"
3-
}
4-
51
resource "mongodbatlas_cluster" "free_cluster" { # comment in the resource
62
# comment in own line in the beginning
73
count = local.use_free_cluster ? 1 : 0
@@ -14,3 +10,8 @@ resource "mongodbatlas_cluster" "free_cluster" { # comment in the resource
1410
provider_instance_size_name = "M0"
1511
# comment in own line at the end happens before replication_specs
1612
}
13+
14+
resource "another_resource" "res1" {
15+
# comment in own line in the middle is not deleted in unprocessed resource
16+
name = "name1"
17+
}

internal/convert/testdata/clu2adv/free_cluster_with_count.out.tf renamed to internal/convert/testdata/clu2adv/comments.out.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
resource "resource1" "res1" {
2-
name = "name1"
3-
}
4-
51
resource "mongodbatlas_advanced_cluster" "free_cluster" { # comment in the resource
62
# comment in own line in the beginning
73
count = local.use_free_cluster ? 1 : 0
@@ -28,3 +24,8 @@ resource "mongodbatlas_advanced_cluster" "free_cluster" { # comment in the resou
2824
# Generated by atlas-cli-plugin-terraform.
2925
# Please review the changes and confirm that references to this resource are updated.
3026
}
27+
28+
resource "another_resource" "res1" {
29+
# comment in own line in the middle is not deleted in unprocessed resource
30+
name = "name1"
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
resource "mongodbatlas_cluster" "free_cluster" {
2+
project_id = var.project_id
3+
name = var.cluster_name
4+
provider_name = "TENANT"
5+
backing_provider_name = "AWS"
6+
provider_region_name = var.region
7+
provider_instance_size_name = "M0"
8+
}
9+
10+
resource "mongodbatlas_cluster" "count" {
11+
count = local.use_free_cluster ? 1 : 0
12+
project_id = var.project_id
13+
name = var.cluster_name
14+
provider_name = "TENANT"
15+
backing_provider_name = "AWS"
16+
provider_region_name = var.region
17+
provider_instance_size_name = "M0"
18+
}
19+
20+
21+
resource "mongodbatlas_cluster" "upgrade_from_free_cluster" {
22+
# upgraded free cluster to dedicated
23+
project_id = var.project_id
24+
name = var.cluster_name
25+
provider_name = "AWS" # changed from TENANT to AWS
26+
provider_instance_size_name = "M10" # changed from M0 to M10
27+
# removed backing_provider_name = AWS"
28+
provider_region_name = var.region
29+
}
30+
31+
resource "mongodbatlas_cluster" "upgrade_from_free_cluster_with_variables" {
32+
# upgraded free cluster to dedicated, using variables in all attributes
33+
project_id = var.project_id
34+
name = var.cluster_name
35+
provider_name = var.provider_name
36+
provider_instance_size_name = var.instance_size
37+
provider_region_name = var.region
38+
}
39+
40+
resource "another_resource" "res1" {
41+
name = "name1"
42+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
resource "mongodbatlas_advanced_cluster" "free_cluster" {
2+
project_id = var.project_id
3+
name = var.cluster_name
4+
cluster_type = "REPLICASET"
5+
replication_specs = [
6+
{
7+
region_configs = [
8+
{
9+
priority = 7
10+
region_name = var.region
11+
provider_name = "TENANT"
12+
backing_provider_name = "AWS"
13+
electable_specs = {
14+
instance_size = "M0"
15+
}
16+
}
17+
]
18+
}
19+
]
20+
21+
# Generated by atlas-cli-plugin-terraform.
22+
# Please review the changes and confirm that references to this resource are updated.
23+
}
24+
25+
resource "mongodbatlas_advanced_cluster" "count" {
26+
count = local.use_free_cluster ? 1 : 0
27+
project_id = var.project_id
28+
name = var.cluster_name
29+
cluster_type = "REPLICASET"
30+
replication_specs = [
31+
{
32+
region_configs = [
33+
{
34+
priority = 7
35+
region_name = var.region
36+
provider_name = "TENANT"
37+
backing_provider_name = "AWS"
38+
electable_specs = {
39+
instance_size = "M0"
40+
}
41+
}
42+
]
43+
}
44+
]
45+
46+
# Generated by atlas-cli-plugin-terraform.
47+
# Please review the changes and confirm that references to this resource are updated.
48+
}
49+
50+
51+
resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster" {
52+
# upgraded free cluster to dedicated
53+
project_id = var.project_id
54+
name = var.cluster_name
55+
cluster_type = "REPLICASET"
56+
replication_specs = [
57+
{
58+
region_configs = [
59+
{
60+
priority = 7
61+
region_name = var.region
62+
provider_name = "AWS"
63+
electable_specs = {
64+
instance_size = "M10"
65+
node_count = 3
66+
}
67+
}
68+
]
69+
}
70+
]
71+
72+
# Generated by atlas-cli-plugin-terraform.
73+
# Please review the changes and confirm that references to this resource are updated.
74+
}
75+
76+
resource "mongodbatlas_advanced_cluster" "upgrade_from_free_cluster_with_variables" {
77+
# upgraded free cluster to dedicated, using variables in all attributes
78+
project_id = var.project_id
79+
name = var.cluster_name
80+
cluster_type = "REPLICASET"
81+
replication_specs = [
82+
{
83+
region_configs = [
84+
{
85+
priority = 7
86+
region_name = var.region
87+
provider_name = var.provider_name
88+
electable_specs = {
89+
instance_size = var.instance_size
90+
node_count = 3
91+
}
92+
}
93+
]
94+
}
95+
]
96+
97+
# Generated by atlas-cli-plugin-terraform.
98+
# Please review the changes and confirm that references to this resource are updated.
99+
}
100+
101+
resource "another_resource" "res1" {
102+
name = "name1"
103+
}
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,8 @@
1-
resource "resource1" "res1" {
2-
name = "name1"
3-
}
4-
5-
resource "mongodbatlas_cluster" "free_cluster" { # comment in the resource
6-
# comment in own line in the beginning
7-
count = local.use_free_cluster ? 1 : 0
8-
project_id = var.project_id # inline comment kept
9-
name = var.cluster_name
10-
# comment in own line in the middle is deleted
11-
provider_name = "TENANT" # inline comment for attribute moved is not kept
1+
resource "mongodbatlas_cluster" "free_cluster" {
2+
project_id = var.project_id
3+
name = var.cluster_name
4+
provider_name = "TENANT"
125
provider_region_name = var.region
136
provider_instance_size_name = "M0"
14-
# comment in own line at the end happens before replication_specs
15-
}
16-
17-
data "mongodbatlas_cluster" "cluster2" {
18-
name = "name4"
7+
# missing backing_provider_name
198
}

0 commit comments

Comments
 (0)