Skip to content

Commit a4e0c70

Browse files
committed
implementation
1 parent ad0a164 commit a4e0c70

File tree

1 file changed

+88
-2
lines changed

1 file changed

+88
-2
lines changed

internal/convert/adv2v2.go

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,94 @@ func convertRepSpecsWithDynamicBlock(resourceb *hclwrite.Body, diskSizeGB hclwri
129129
if err != nil {
130130
return dynamicBlock{}, err
131131
}
132-
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
133-
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, dConfig.tokens...))
132+
133+
// Check if we have a dynamic region_configs block that was successfully processed
134+
if dConfig.tokens != nil {
135+
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
136+
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, dConfig.tokens...))
137+
return dSpec, nil
138+
}
139+
140+
// Handle static region_configs blocks inside dynamic replication_specs
141+
specBody := dSpec.content.Body()
142+
143+
// Collect static region_configs blocks
144+
staticConfigs := collectBlocks(specBody, nConfig)
145+
if len(staticConfigs) == 0 {
146+
// No static blocks found, this might be an error case
147+
// Check if there's also no dynamic block (which would have been handled above)
148+
hasDynamicBlock := false
149+
for _, block := range specBody.Blocks() {
150+
if block.Type() == nDynamic && getResourceName(block) == nConfig {
151+
hasDynamicBlock = true
152+
break
153+
}
154+
}
155+
if !hasDynamicBlock {
156+
return dynamicBlock{}, fmt.Errorf("replication_specs must have at least one region_configs")
157+
}
158+
// There's a dynamic block but convertConfigsWithDynamicBlock returned empty
159+
// This shouldn't happen, but return the error from that function
160+
return dynamicBlock{}, nil
161+
}
162+
163+
repSpecb := hclwrite.NewEmptyFile().Body()
164+
165+
// Handle zone_name attribute
166+
if zoneNameAttr := specBody.GetAttribute(nZoneName); zoneNameAttr != nil {
167+
zoneNameExpr := transformReference(hcl.GetAttrExpr(zoneNameAttr), nRepSpecs, nSpec)
168+
repSpecb.SetAttributeRaw(nZoneName, hcl.TokensFromExpr(zoneNameExpr))
169+
}
170+
171+
// Process static region_configs blocks
172+
var configs []*hclwrite.Body
173+
for _, configBlock := range staticConfigs {
174+
configBlockb := configBlock.Body()
175+
// Create a new body with sorted attributes
176+
newConfigBody := hclwrite.NewEmptyFile().Body()
177+
178+
// Copy attributes in the expected order
179+
attrs := configBlockb.Attributes()
180+
// Priority, provider_name, region_name should come first
181+
if priority := attrs["priority"]; priority != nil {
182+
newConfigBody.SetAttributeRaw("priority", priority.Expr().BuildTokens(nil))
183+
}
184+
if provider := attrs["provider_name"]; provider != nil {
185+
newConfigBody.SetAttributeRaw("provider_name", provider.Expr().BuildTokens(nil))
186+
}
187+
if region := attrs["region_name"]; region != nil {
188+
newConfigBody.SetAttributeRaw("region_name", region.Expr().BuildTokens(nil))
189+
}
190+
191+
// Process spec blocks and convert them to attributes
192+
for _, block := range configBlockb.Blocks() {
193+
blockType := block.Type()
194+
blockBody := hclwrite.NewEmptyFile().Body()
195+
copyAttributesSorted(blockBody, block.Body().Attributes())
196+
if diskSizeGB != nil &&
197+
(blockType == nElectableSpecs || blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
198+
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
199+
}
200+
newConfigBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
201+
}
202+
203+
configs = append(configs, newConfigBody)
204+
}
205+
206+
repSpecb.SetAttributeRaw(nConfig, hcl.TokensArray(configs))
207+
208+
// Handle num_shards attribute
209+
if numShardsAttr := specBody.GetAttribute(nNumShards); numShardsAttr != nil {
210+
numShardsExpr := transformReference(hcl.GetAttrExpr(numShardsAttr), nRepSpecs, nSpec)
211+
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
212+
innerFor := hcl.TokensFromExpr(buildForExpr("i", fmt.Sprintf("range(%s)", numShardsExpr), false))
213+
innerFor = append(innerFor, hcl.TokensObject(repSpecb)...)
214+
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, hcl.EncloseBracketsNewLines(innerFor)...))
215+
} else {
216+
forSpec := hcl.TokensFromExpr(buildForExpr(nSpec, hcl.GetAttrExpr(dSpec.forEach), true))
217+
dSpec.tokens = hcl.TokensFuncFlatten(append(forSpec, hcl.TokensArraySingle(repSpecb)...))
218+
}
219+
134220
return dSpec, nil
135221
}
136222

0 commit comments

Comments
 (0)