Skip to content

Commit 4ab2d3a

Browse files
committed
add comment for priority loop
1 parent 876b6eb commit 4ab2d3a

File tree

4 files changed

+11
-3
lines changed

4 files changed

+11
-3
lines changed

internal/convert/convert.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const (
3333
commentConfirmReferences = "Please review the changes and confirm that references to this resource are updated."
3434
commentMovedBlock = "Moved blocks"
3535
commentRemovedOld = "Note: Remember to remove or comment out the old cluster definitions."
36+
commentPriorityFor = "Regions must be sorted by priority in descending order."
3637
)
3738

3839
var (
@@ -326,7 +327,8 @@ func fillRegionConfigsDynamicBlock(specbSrc *hclwrite.Body, root attrVals) (dyna
326327
if err != nil {
327328
return dynamicBlock{}, err
328329
}
329-
priorityFor := hcl.TokensFromExpr(fmt.Sprintf("for %s in range(%d, %d, -1) : ", nPriority, valMaxPriority, valMinPriority))
330+
priorityFor := hcl.TokensComment(commentPriorityFor)
331+
priorityFor = append(priorityFor, hcl.TokensFromExpr(fmt.Sprintf("for %s in range(%d, %d, -1) : ", nPriority, valMaxPriority, valMinPriority))...)
330332
priorityFor = append(priorityFor, regionFor...)
331333
repSpecb.SetAttributeRaw(nConfig, hcl.TokensFuncFlatten(priorityFor))
332334

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ resource "mongodbatlas_advanced_cluster" "cluster" {
1717
replication_specs = [
1818
for i in range(var.replication_specs.num_shards) : {
1919
region_configs = flatten([
20+
# Regions must be sorted by priority in descending order.
2021
for priority in range(7, 0, -1) : [
2122
for region in var.replication_specs.regions_config : {
2223
provider_name = var.provider_name

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ resource "mongodbatlas_advanced_cluster" "dynamic_regions_config" {
66
for i in range(var.replication_specs.num_shards) : {
77
zone_name = var.zone_name
88
region_configs = flatten([
9+
# Regions must be sorted by priority in descending order.
910
for priority in range(7, 0, -1) : [
1011
for region in var.replication_specs.regions_config : {
1112
provider_name = "AWS"

internal/hcl/hcl.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,14 @@ func RemoveLeadingNewline(tokens hclwrite.Tokens) hclwrite.Tokens {
134134

135135
// AppendComment adds a comment at the end of the body.
136136
func AppendComment(body *hclwrite.Body, comment string) {
137-
tokens := hclwrite.Tokens{
137+
body.AppendUnstructuredTokens(TokensComment(comment))
138+
}
139+
140+
// TokensComment returns the tokens for a comment.
141+
func TokensComment(comment string) hclwrite.Tokens {
142+
return hclwrite.Tokens{
138143
&hclwrite.Token{Type: hclsyntax.TokenComment, Bytes: []byte("# " + comment + "\n")},
139144
}
140-
body.AppendUnstructuredTokens(tokens)
141145
}
142146

143147
// GetParser returns a parser for the given config and checks HCL syntax is valid

0 commit comments

Comments
 (0)