Skip to content

Commit bb76598

Browse files
committed
refactor to improve readability
1 parent 9657eb2 commit bb76598

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

internal/hcl/hcl.go

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,17 @@ const (
2323
strBackingProviderName = "backing_provider_name"
2424
strProviderInstanceSizeName = "provider_instance_size_name"
2525
strInstanceSize = "instance_size"
26+
strClusterType = "cluster_type"
27+
strPriority = "priority"
2628

27-
errFreeCluster = "free cluster (as doesn't contain " + strReplicationSpecs + ")"
29+
errFreeCluster = "free cluster (because no " + strReplicationSpecs + ")"
2830
)
2931

3032
// ClusterToAdvancedCluster transforms all mongodbatlas_cluster definitions in a
3133
// Terraform configuration file into mongodbatlas_advanced_cluster schema v2 definitions.
3234
// All other resources and data sources are left untouched.
33-
// TODO: at the moment it just changes the resource type.
35+
// Note: hclwrite.Tokens are used instead of cty.Value so expressions like var.region can be preserved.
36+
// cty.Value only supports resolved values.
3437
func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
3538
parser, err := getParser(config)
3639
if err != nil {
@@ -43,12 +46,10 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
4346
continue
4447
}
4548
resourceBody := resource.Body()
46-
47-
// TODO: Do the full transformation
4849
labels[0] = advCluster
4950
resource.SetLabels(labels)
5051

51-
if resourceBody.FirstMatchingBlock(strReplicationSpecs, nil) == nil {
52+
if isFreeTier(resourceBody) {
5253
if err := fillFreeTier(resourceBody); err != nil {
5354
return nil, err
5455
}
@@ -61,32 +62,37 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
6162
return parser.Bytes(), nil
6263
}
6364

64-
func fillFreeTier(resourceBody *hclwrite.Body) error {
65-
resourceBody.SetAttributeValue("cluster_type", cty.StringVal("REPLICASET"))
65+
func isFreeTier(resourceBody *hclwrite.Body) bool {
66+
return resourceBody.FirstMatchingBlock(strReplicationSpecs, nil) == nil
67+
}
6668

69+
func fillFreeTier(body *hclwrite.Body) error {
70+
const (
71+
valClusterType = "REPLICASET"
72+
valPriority = 7
73+
)
74+
body.SetAttributeValue(strClusterType, cty.StringVal(valClusterType))
6775
regionConfig := hclwrite.NewEmptyFile()
6876
regionConfigBody := regionConfig.Body()
69-
setAttrInt(regionConfig.Body(), "priority", 7)
70-
if err := moveAttribute(strProviderRegionName, strRegionName, resourceBody, regionConfigBody, errFreeCluster); err != nil {
77+
setAttrInt(regionConfigBody, "priority", valPriority)
78+
if err := moveAttribute(strProviderRegionName, strRegionName, body, regionConfigBody, errFreeCluster); err != nil {
7179
return err
7280
}
73-
if err := moveAttribute(strProviderName, strProviderName, resourceBody, regionConfigBody, errFreeCluster); err != nil {
81+
if err := moveAttribute(strProviderName, strProviderName, body, regionConfigBody, errFreeCluster); err != nil {
7482
return err
7583
}
76-
if err := moveAttribute(strBackingProviderName, strBackingProviderName, resourceBody, regionConfigBody, errFreeCluster); err != nil {
84+
if err := moveAttribute(strBackingProviderName, strBackingProviderName, body, regionConfigBody, errFreeCluster); err != nil {
7785
return err
7886
}
79-
8087
electableSpec := hclwrite.NewEmptyFile()
81-
if err := moveAttribute(strProviderInstanceSizeName, strInstanceSize, resourceBody, electableSpec.Body(), errFreeCluster); err != nil {
88+
if err := moveAttribute(strProviderInstanceSizeName, strInstanceSize, body, electableSpec.Body(), errFreeCluster); err != nil {
8289
return err
8390
}
84-
regionConfig.Body().SetAttributeRaw(strElectableSpecs, objectTokens(electableSpec))
91+
regionConfigBody.SetAttributeRaw(strElectableSpecs, tokensObject(electableSpec))
8592

8693
replicationSpec := hclwrite.NewEmptyFile()
87-
replicationSpec.Body().SetAttributeRaw(strRegionConfigs, singleArrayTokens(regionConfig))
88-
89-
resourceBody.SetAttributeRaw(strReplicationSpecs, singleArrayTokens(replicationSpec))
94+
replicationSpec.Body().SetAttributeRaw(strRegionConfigs, tokenArrayObject(regionConfig))
95+
body.SetAttributeRaw(strReplicationSpecs, tokenArrayObject(replicationSpec))
9096
return nil
9197
}
9298

@@ -107,20 +113,17 @@ func setAttrInt(body *hclwrite.Body, attrName string, number int) {
107113
body.SetAttributeRaw(attrName, tokens)
108114
}
109115

110-
func singleArrayTokens(file *hclwrite.File) hclwrite.Tokens {
116+
func tokenArrayObject(file *hclwrite.File) hclwrite.Tokens {
111117
ret := hclwrite.Tokens{
112118
{Type: hclsyntax.TokenOBrack, Bytes: []byte("[")},
113-
{Type: hclsyntax.TokenOBrack, Bytes: []byte("{")},
114-
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")},
115119
}
116-
ret = append(ret, file.BuildTokens(nil)...)
120+
ret = append(ret, tokensObject(file)...)
117121
ret = append(ret,
118-
&hclwrite.Token{Type: hclsyntax.TokenCBrack, Bytes: []byte("}")},
119122
&hclwrite.Token{Type: hclsyntax.TokenCBrack, Bytes: []byte("]")})
120123
return ret
121124
}
122125

123-
func objectTokens(file *hclwrite.File) hclwrite.Tokens {
126+
func tokensObject(file *hclwrite.File) hclwrite.Tokens {
124127
ret := hclwrite.Tokens{
125128
{Type: hclsyntax.TokenOBrack, Bytes: []byte("{")},
126129
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")},
@@ -131,20 +134,17 @@ func objectTokens(file *hclwrite.File) hclwrite.Tokens {
131134
return ret
132135
}
133136

137+
func appendComment(body *hclwrite.Body, comment string) {
138+
tokens := hclwrite.Tokens{
139+
&hclwrite.Token{Type: hclsyntax.TokenComment, Bytes: []byte("# " + comment + "\n")},
140+
}
141+
body.AppendUnstructuredTokens(tokens)
142+
}
143+
134144
func getParser(config []byte) (*hclwrite.File, error) {
135145
parser, diags := hclwrite.ParseConfig(config, "", hcl.Pos{Line: 1, Column: 1})
136146
if diags.HasErrors() {
137147
return nil, fmt.Errorf("failed to parse Terraform config file: %s", diags.Error())
138148
}
139149
return parser, nil
140150
}
141-
142-
func appendComment(body *hclwrite.Body, comment string) {
143-
tokens := hclwrite.Tokens{
144-
&hclwrite.Token{
145-
Type: hclsyntax.TokenComment,
146-
Bytes: []byte("# " + comment + "\n"),
147-
},
148-
}
149-
body.AppendUnstructuredTokens(tokens)
150-
}

0 commit comments

Comments
 (0)