Skip to content

Commit 2e9de57

Browse files
committed
pass tests
1 parent ea632ba commit 2e9de57

File tree

5 files changed

+121
-41
lines changed

5 files changed

+121
-41
lines changed

internal/hcl/hcl.go

Lines changed: 100 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,29 @@ package hcl
22

33
import (
44
"fmt"
5+
"strconv"
56

67
"github.com/hashicorp/hcl/v2"
78
"github.com/hashicorp/hcl/v2/hclsyntax"
89
"github.com/hashicorp/hcl/v2/hclwrite"
10+
"github.com/zclconf/go-cty/cty"
911
)
1012

1113
const (
12-
resourceType = "resource"
13-
cluster = "mongodbatlas_cluster"
14-
advCluster = "mongodbatlas_advanced_cluster"
14+
resourceType = "resource"
15+
cluster = "mongodbatlas_cluster"
16+
advCluster = "mongodbatlas_advanced_cluster"
17+
strReplicationSpecs = "replication_specs"
18+
strRegionConfigs = "region_configs"
19+
strElectableSpecs = "electable_specs"
20+
strProviderRegionName = "provider_region_name"
21+
strRegionName = "region_name"
22+
strProviderName = "provider_name"
23+
strBackingProviderName = "backing_provider_name"
24+
strProviderInstanceSizeName = "provider_instance_size_name"
25+
strInstanceSize = "instance_size"
26+
27+
errFreeCluster = "free cluster"
1528
)
1629

1730
// ClusterToAdvancedCluster transforms all mongodbatlas_cluster definitions in a
@@ -34,13 +47,97 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
3447
// TODO: Do the full transformation
3548
labels[0] = advCluster
3649
resource.SetLabels(labels)
50+
51+
if resourceBody.FirstMatchingBlock(strReplicationSpecs, nil) == nil {
52+
if err := fillFreeTier(resourceBody); err != nil {
53+
return nil, err
54+
}
55+
}
56+
3757
resourceBody.AppendNewline()
3858
appendComment(resourceBody, "Generated by atlas-cli-plugin-terraform.")
3959
appendComment(resourceBody, "Please confirm that all references to this resource are updated.")
4060
}
4161
return parser.Bytes(), nil
4262
}
4363

64+
func fillFreeTier(resourceBody *hclwrite.Body) error {
65+
resourceBody.SetAttributeValue("cluster_type", cty.StringVal("REPLICASET"))
66+
67+
regionConfig := hclwrite.NewEmptyFile()
68+
regionConfigBody := regionConfig.Body()
69+
setAttrInt(regionConfig.Body(), "priority", 7)
70+
if err := moveAttribute(strProviderRegionName, strRegionName, resourceBody, regionConfigBody, errFreeCluster); err != nil {
71+
return err
72+
}
73+
if err := moveAttribute(strProviderName, strProviderName, resourceBody, regionConfigBody, errFreeCluster); err != nil {
74+
return err
75+
}
76+
if err := moveAttribute(strBackingProviderName, strBackingProviderName, resourceBody, regionConfigBody, errFreeCluster); err != nil {
77+
return err
78+
}
79+
80+
electableSpec := hclwrite.NewEmptyFile()
81+
if err := moveAttribute(strProviderInstanceSizeName, strInstanceSize, resourceBody, electableSpec.Body(), errFreeCluster); err != nil {
82+
return err
83+
}
84+
regionConfig.Body().SetAttributeRaw(strElectableSpecs, objectTokens(electableSpec))
85+
86+
replicationSpec := hclwrite.NewEmptyFile()
87+
replicationSpec.Body().SetAttributeRaw(strRegionConfigs, singleArrayTokens(regionConfig))
88+
89+
resourceBody.SetAttributeRaw(strReplicationSpecs, singleArrayTokens(replicationSpec))
90+
return nil
91+
}
92+
93+
func moveAttribute(fromAttrName, toAttrName string, fromBody, toBody *hclwrite.Body, errPrefix string) error {
94+
attr := fromBody.GetAttribute(fromAttrName)
95+
if attr == nil {
96+
return fmt.Errorf("%s: attribute %s not found", errPrefix, fromAttrName)
97+
}
98+
fromBody.RemoveAttribute(fromAttrName)
99+
toBody.SetAttributeRaw(toAttrName, attr.Expr().BuildTokens(nil))
100+
return nil
101+
}
102+
103+
func setAttrExpr(body *hclwrite.Body, attrName, expression string) {
104+
tokens := hclwrite.Tokens{
105+
{Type: hclsyntax.TokenIdent, Bytes: []byte(expression)},
106+
}
107+
body.SetAttributeRaw(attrName, tokens)
108+
}
109+
110+
func setAttrInt(body *hclwrite.Body, attrName string, number int) {
111+
tokens := hclwrite.Tokens{
112+
{Type: hclsyntax.TokenNumberLit, Bytes: []byte(strconv.Itoa(number))},
113+
}
114+
body.SetAttributeRaw(attrName, tokens)
115+
}
116+
117+
func singleArrayTokens(file *hclwrite.File) hclwrite.Tokens {
118+
ret := hclwrite.Tokens{
119+
{Type: hclsyntax.TokenOBrack, Bytes: []byte("[")},
120+
{Type: hclsyntax.TokenOBrack, Bytes: []byte("{")},
121+
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")},
122+
}
123+
ret = append(ret, file.BuildTokens(nil)...)
124+
ret = append(ret,
125+
&hclwrite.Token{Type: hclsyntax.TokenCBrack, Bytes: []byte("}")},
126+
&hclwrite.Token{Type: hclsyntax.TokenCBrack, Bytes: []byte("]")})
127+
return ret
128+
}
129+
130+
func objectTokens(file *hclwrite.File) hclwrite.Tokens {
131+
ret := hclwrite.Tokens{
132+
{Type: hclsyntax.TokenOBrack, Bytes: []byte("{")},
133+
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")},
134+
}
135+
ret = append(ret, file.BuildTokens(nil)...)
136+
ret = append(ret,
137+
&hclwrite.Token{Type: hclsyntax.TokenCBrack, Bytes: []byte("}")})
138+
return ret
139+
}
140+
44141
func getParser(config []byte) (*hclwrite.File, error) {
45142
parser, diags := hclwrite.ParseConfig(config, "", hcl.Pos{Line: 1, Column: 1})
46143
if diags.HasErrors() {

internal/hcl/testdata/clu2adv/basic.in.tf

Lines changed: 0 additions & 15 deletions
This file was deleted.

internal/hcl/testdata/clu2adv/basic.out.tf

Lines changed: 0 additions & 18 deletions
This file was deleted.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
resource "mongodbatlas_cluster" "project_cluster_free" {
1+
resource "resource1" "res1" {
2+
name = "name1"
3+
}
4+
5+
resource "mongodbatlas_cluster" "free_cluster" {
26
count = local.use_free_cluster ? 1 : 0
37
project_id = var.project_id
48
name = var.cluster_name
@@ -7,3 +11,7 @@ resource "mongodbatlas_cluster" "project_cluster_free" {
711
provider_region_name = var.region
812
provider_instance_size_name = "M0"
913
}
14+
15+
data "mongodbatlas_cluster" "cluster2" {
16+
name = "name4"
17+
}

internal/hcl/testdata/clu2adv/free_cluster_with_count.out.tf

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
resource "mongodbatlas_advanced_cluster" "project_cluster_free" {
2-
count = local.use_free_cluster ? 1 : 0
3-
project_id = var.project_id
4-
name = var.cluster_name
1+
resource "resource1" "res1" {
2+
name = "name1"
3+
}
4+
5+
resource "mongodbatlas_advanced_cluster" "free_cluster" {
6+
count = local.use_free_cluster ? 1 : 0
7+
project_id = var.project_id
8+
name = var.cluster_name
59
cluster_type = "REPLICASET"
610
replication_specs = [{
711
region_configs = [{
@@ -18,3 +22,7 @@ resource "mongodbatlas_advanced_cluster" "project_cluster_free" {
1822
# Generated by atlas-cli-plugin-terraform.
1923
# Please confirm that all references to this resource are updated.
2024
}
25+
26+
data "mongodbatlas_cluster" "cluster2" {
27+
name = "name4"
28+
}

0 commit comments

Comments
 (0)