Skip to content

Commit f9ee0d3

Browse files
committed
simple timeouts
1 parent 40d0c5b commit f9ee0d3

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

internal/convert/convert.go

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,11 @@ func fillFreeTier(resourceb *hclwrite.Body) error {
8686
if err := hcl.MoveAttr(resourceb, electableSpec.Body(), nInstanceSizeSrc, nInstanceSize, errFreeCluster); err != nil {
8787
return err
8888
}
89-
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpec))
89+
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpec.Body()))
9090

9191
repSpecs := hclwrite.NewEmptyFile()
92-
repSpecs.Body().SetAttributeRaw(nConfig, hcl.TokensArraySingle(config))
93-
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs))
92+
repSpecs.Body().SetAttributeRaw(nConfig, hcl.TokensArraySingle(configb))
93+
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs.Body()))
9494
return nil
9595
}
9696

@@ -115,21 +115,21 @@ func fillReplicationSpecs(resourceb *hclwrite.Body) error {
115115
}
116116
repSpecs := hclwrite.NewEmptyFile()
117117
repSpecs.Body().SetAttributeRaw(nConfig, configs)
118-
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs))
118+
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs.Body()))
119119
tags, errTags := getTagsLabelsOpt(nTags, resourceb)
120120
if errTags != nil {
121121
return errTags
122122
}
123123
if tags != nil {
124124
resourceb.SetAttributeRaw(nTags, tags)
125125
}
126-
126+
fillTimeoutsOpt(resourceb)
127127
resourceb.RemoveBlock(repSpecsSrc)
128128
return nil
129129
}
130130

131131
func getRegionConfigs(repSpecsSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) {
132-
var configs []*hclwrite.File
132+
var configs []*hclwrite.Body
133133
for {
134134
configSrc := repSpecsSrc.Body().FirstMatchingBlock(nConfigSrc, nil)
135135
if configSrc == nil {
@@ -139,15 +139,15 @@ func getRegionConfigs(repSpecsSrc *hclwrite.Block, root attrVals) (hclwrite.Toke
139139
if err != nil {
140140
return nil, err
141141
}
142-
configs = append(configs, config)
142+
configs = append(configs, config.Body())
143143
repSpecsSrc.Body().RemoveBlock(configSrc)
144144
}
145145
if len(configs) == 0 {
146146
return nil, fmt.Errorf("%s: %s not found", errRepSpecs, nConfigSrc)
147147
}
148148
sort.Slice(configs, func(i, j int) bool {
149-
pi, _ := hcl.GetAttrInt(configs[i].Body().GetAttribute(nPriority), errPriority)
150-
pj, _ := hcl.GetAttrInt(configs[j].Body().GetAttribute(nPriority), errPriority)
149+
pi, _ := hcl.GetAttrInt(configs[i].GetAttribute(nPriority), errPriority)
150+
pj, _ := hcl.GetAttrInt(configs[j].GetAttribute(nPriority), errPriority)
151151
return pi > pj
152152
})
153153
return hcl.TokensArray(configs), nil
@@ -203,7 +203,7 @@ func getSpecs(countName string, configSrc *hclwrite.Block, root attrVals) (hclwr
203203
if root.opt[nDiskIOPSSrc] != nil {
204204
fileb.SetAttributeRaw(nDiskIOPS, root.opt[nDiskIOPSSrc])
205205
}
206-
return hcl.TokensObject(file), nil
206+
return hcl.TokensObject(fileb), nil
207207
}
208208

209209
func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {
@@ -216,19 +216,20 @@ func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {
216216
{nComputeScaleDownEnabledSrc, nComputeScaleDownEnabled},
217217
}
218218
file = hclwrite.NewEmptyFile()
219+
fileb = file.Body()
219220
found = false
220221
)
221222
for _, tuple := range names {
222223
src, dst := tuple[0], tuple[1]
223224
if tokens := opt[src]; tokens != nil {
224-
file.Body().SetAttributeRaw(dst, tokens)
225+
fileb.SetAttributeRaw(dst, tokens)
225226
found = true
226227
}
227228
}
228229
if !found {
229230
return nil
230231
}
231-
return hcl.TokensObject(file)
232+
return hcl.TokensObject(fileb)
232233
}
233234

234235
func getTagsLabelsOpt(attrName string, resourceb *hclwrite.Body) (hclwrite.Tokens, error) {
@@ -258,7 +259,16 @@ func getTagsLabelsOpt(attrName string, resourceb *hclwrite.Body) (hclwrite.Token
258259
if !found {
259260
return nil, nil
260261
}
261-
return hcl.TokensObject(file), nil
262+
return hcl.TokensObject(fileb), nil
263+
}
264+
265+
func fillTimeoutsOpt(resourceb *hclwrite.Body) {
266+
block := resourceb.FirstMatchingBlock(nTimeouts, nil)
267+
if block == nil {
268+
return
269+
}
270+
resourceb.RemoveBlock(block)
271+
resourceb.SetAttributeRaw(nTimeouts, hcl.TokensObject(block.Body()))
262272
}
263273

264274
func checkDynamicBlock(body *hclwrite.Body) error {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,22 @@ resource "mongodbatlas_cluster" "basictags" {
1717
value = "dev"
1818
}
1919
}
20+
21+
resource "mongodbatlas_cluster" "basictimeouts" {
22+
project_id = var.project_id
23+
name = "basictimeouts"
24+
cluster_type = "REPLICASET"
25+
provider_name = "AWS"
26+
provider_instance_size_name = "M10"
27+
replication_specs {
28+
num_shards = 1
29+
regions_config {
30+
region_name = "US_EAST_1"
31+
electable_nodes = 3
32+
priority = 7
33+
}
34+
}
35+
timeouts {
36+
create = "60m"
37+
}
38+
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,27 @@ resource "mongodbatlas_advanced_cluster" "basictags" {
2020
# Generated by atlas-cli-plugin-terraform.
2121
# Please confirm that all references to this resource are updated.
2222
}
23+
24+
resource "mongodbatlas_advanced_cluster" "basictimeouts" {
25+
project_id = var.project_id
26+
name = "basictimeouts"
27+
cluster_type = "REPLICASET"
28+
replication_specs = [{
29+
region_configs = [{
30+
provider_name = "AWS"
31+
region_name = "US_EAST_1"
32+
priority = 7
33+
electable_specs = {
34+
node_count = 3
35+
instance_size = "M10"
36+
}
37+
}]
38+
}]
39+
timeouts = {
40+
41+
create = "60m"
42+
}
43+
44+
# Generated by atlas-cli-plugin-terraform.
45+
# Please confirm that all references to this resource are updated.
46+
}

internal/hcl/hcl.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ func GetAttrString(attr *hclwrite.Attribute, errPrefix string) (string, error) {
7272
}
7373

7474
// TokensArray creates an array of objects.
75-
func TokensArray(file []*hclwrite.File) hclwrite.Tokens {
75+
func TokensArray(bodies []*hclwrite.Body) hclwrite.Tokens {
7676
ret := hclwrite.Tokens{
7777
{Type: hclsyntax.TokenOBrack, Bytes: []byte("[")},
7878
}
79-
for i := range file {
80-
ret = append(ret, TokensObject(file[i])...)
81-
if i < len(file)-1 {
79+
for i := range bodies {
80+
ret = append(ret, TokensObject(bodies[i])...)
81+
if i < len(bodies)-1 {
8282
ret = append(ret, &hclwrite.Token{Type: hclsyntax.TokenComma, Bytes: []byte(",")})
8383
}
8484
}
@@ -88,17 +88,17 @@ func TokensArray(file []*hclwrite.File) hclwrite.Tokens {
8888
}
8989

9090
// TokensArraySingle creates an array of one object.
91-
func TokensArraySingle(file *hclwrite.File) hclwrite.Tokens {
92-
return TokensArray([]*hclwrite.File{file})
91+
func TokensArraySingle(body *hclwrite.Body) hclwrite.Tokens {
92+
return TokensArray([]*hclwrite.Body{body})
9393
}
9494

9595
// TokensObject creates an object.
96-
func TokensObject(file *hclwrite.File) hclwrite.Tokens {
96+
func TokensObject(body *hclwrite.Body) hclwrite.Tokens {
9797
ret := hclwrite.Tokens{
9898
{Type: hclsyntax.TokenOBrace, Bytes: []byte("{")},
9999
{Type: hclsyntax.TokenNewline, Bytes: []byte("\n")},
100100
}
101-
ret = append(ret, file.BuildTokens(nil)...)
101+
ret = append(ret, body.BuildTokens(nil)...)
102102
ret = append(ret,
103103
&hclwrite.Token{Type: hclsyntax.TokenCBrace, Bytes: []byte("}")})
104104
return ret

0 commit comments

Comments
 (0)