Skip to content

Commit bb8ec13

Browse files
committed
support all specs with dynamic blocks
1 parent 90fc849 commit bb8ec13

File tree

5 files changed

+243
-26
lines changed

5 files changed

+243
-26
lines changed

internal/convert/adv2v2.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func convertDynamicRepSpecs(resourceb *hclwrite.Body, dSpec dynamicBlock, diskSi
201201
}
202202

203203
// Helper function to process blocks for region configs
204-
func processRegionConfigBlocks(targetBody *hclwrite.Body, blocks []*hclwrite.Block) {
204+
func processRegionConfigBlocks(targetBody *hclwrite.Body, blocks []*hclwrite.Block, diskSizeGB hclwrite.Tokens) {
205205
for _, block := range blocks {
206206
blockType := block.Type()
207207
blockFile := hclwrite.NewEmptyFile()
@@ -219,6 +219,12 @@ func processRegionConfigBlocks(targetBody *hclwrite.Body, blocks []*hclwrite.Blo
219219
blockBody.SetAttributeRaw(name, hcl.TokensFromExpr(hcl.GetAttrExpr(attr)))
220220
}
221221

222+
// Add disk_size_gb to specs blocks if needed
223+
if diskSizeGB != nil && (blockType == nElectableSpecs ||
224+
blockType == nReadOnlySpecs || blockType == nAnalyticsSpecs) {
225+
blockBody.SetAttributeRaw(nDiskSizeGB, diskSizeGB)
226+
}
227+
222228
targetBody.SetAttributeRaw(blockType, hcl.TokensObject(blockBody))
223229
}
224230
}
@@ -284,7 +290,7 @@ func convertDynamicRepSpecsWithDynamicConfig(resourceb *hclwrite.Body, dSpec, dC
284290
}
285291

286292
// Add all blocks generically as objects
287-
processRegionConfigBlocks(regionConfigBody, dConfig.content.Body().Blocks())
293+
processRegionConfigBlocks(regionConfigBody, dConfig.content.Body().Blocks(), diskSizeGB)
288294

289295
// Build the region_configs for expression
290296
regionForExpr := fmt.Sprintf("for %s in %s :", nRegion, configForEach)

internal/convert/testdata/adv2v2/dynamic_region_configs.in.tf

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,31 +43,80 @@ resource "mongodbatlas_advanced_cluster" "using_disk_size_gb" {
4343
}
4444
}
4545

46+
resource "mongodbatlas_advanced_cluster" "all_specs" {
47+
project_id = var.project_id
48+
name = "cluster"
49+
cluster_type = "SHARDED"
50+
disk_size_gb = 123
51+
replication_specs {
52+
num_shards = var.replication_specs.num_shards
53+
zone_name = var.zone_name
54+
dynamic "region_configs" {
55+
for_each = var.replication_specs.region_configs
56+
content {
57+
priority = region_configs.value.prio
58+
provider_name = "AWS"
59+
region_name = region_configs.value.region_name
60+
electable_specs {
61+
instance_size = region_configs.value.instance_size
62+
node_count = region_configs.value.node_count
63+
}
64+
read_only_specs {
65+
instance_size = region_configs.value.instance_size
66+
node_count = region_configs.value.node_count_read_only
67+
}
68+
analytics_specs {
69+
instance_size = region_configs.value.instance_size
70+
node_count = region_configs.value.node_count_analytics
71+
}
72+
auto_scaling {
73+
disk_gb_enabled = region_configs.value.enable_disk_gb
74+
}
75+
analytics_auto_scaling {
76+
compute_enabled = region_configs.value.enable_compute
77+
}
78+
}
79+
}
80+
}
81+
}
82+
4683
# example of variable for demostration purposes, not used in the conversion
4784
variable "replication_specs" {
4885
type = object({
4986
num_shards = number
5087
region_configs = list(object({
51-
prio = number
52-
region_name = string
53-
instance_size = string
54-
node_count = number
88+
prio = number
89+
region_name = string
90+
instance_size = string
91+
node_count = number
92+
node_count_read_only = number
93+
node_count_analytics = number
94+
enable_disk_gb = bool
95+
enable_compute = bool
5596
}))
5697
})
5798
default = {
5899
num_shards = 3
59100
region_configs = [
60101
{
61-
prio = 7
62-
region_name = "US_EAST_1"
63-
instance_size = "M10"
64-
node_count = 2
102+
prio = 7
103+
region_name = "US_EAST_1"
104+
instance_size = "M10"
105+
node_count = 2
106+
node_count_read_only = 1
107+
node_count_analytics = 0
108+
enable_disk_gb = true
109+
enable_compute = false
65110
},
66111
{
67-
prio = 6
68-
region_name = "US_WEST_2"
69-
instance_size = "M10"
70-
node_count = 1
112+
prio = 6
113+
region_name = "US_WEST_2"
114+
instance_size = "M10"
115+
node_count = 1
116+
node_count_read_only = 0
117+
node_count_analytics = 1
118+
enable_disk_gb = false
119+
enable_compute = true
71120
}
72121
]
73122
}

internal/convert/testdata/adv2v2/dynamic_region_configs.out.tf

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,84 @@ resource "mongodbatlas_advanced_cluster" "using_disk_size_gb" {
4747
# Updated by atlas-cli-plugin-terraform, please review the changes.
4848
}
4949

50+
resource "mongodbatlas_advanced_cluster" "all_specs" {
51+
project_id = var.project_id
52+
name = "cluster"
53+
cluster_type = "SHARDED"
54+
replication_specs = [
55+
for i in range(var.replication_specs.num_shards) : {
56+
zone_name = var.zone_name
57+
region_configs = [
58+
for region in var.replication_specs.region_configs : {
59+
priority = region.prio
60+
provider_name = "AWS"
61+
region_name = region.region_name
62+
electable_specs = {
63+
instance_size = region.instance_size
64+
node_count = region.node_count
65+
disk_size_gb = 123
66+
}
67+
read_only_specs = {
68+
instance_size = region.instance_size
69+
node_count = region.node_count_read_only
70+
disk_size_gb = 123
71+
}
72+
analytics_specs = {
73+
instance_size = region.instance_size
74+
node_count = region.node_count_analytics
75+
disk_size_gb = 123
76+
}
77+
auto_scaling = {
78+
disk_gb_enabled = region.enable_disk_gb
79+
}
80+
analytics_auto_scaling = {
81+
compute_enabled = region.enable_compute
82+
}
83+
}
84+
]
85+
}
86+
]
87+
88+
# Updated by atlas-cli-plugin-terraform, please review the changes.
89+
}
90+
5091
# example of variable for demostration purposes, not used in the conversion
5192
variable "replication_specs" {
5293
type = object({
5394
num_shards = number
5495
region_configs = list(object({
55-
prio = number
56-
region_name = string
57-
instance_size = string
58-
node_count = number
96+
prio = number
97+
region_name = string
98+
instance_size = string
99+
node_count = number
100+
node_count_read_only = number
101+
node_count_analytics = number
102+
enable_disk_gb = bool
103+
enable_compute = bool
59104
}))
60105
})
61106
default = {
62107
num_shards = 3
63108
region_configs = [
64109
{
65-
prio = 7
66-
region_name = "US_EAST_1"
67-
instance_size = "M10"
68-
node_count = 2
110+
prio = 7
111+
region_name = "US_EAST_1"
112+
instance_size = "M10"
113+
node_count = 2
114+
node_count_read_only = 1
115+
node_count_analytics = 0
116+
enable_disk_gb = true
117+
enable_compute = false
69118
},
70119
{
71-
prio = 6
72-
region_name = "US_WEST_2"
73-
instance_size = "M10"
74-
node_count = 1
120+
prio = 6
121+
region_name = "US_WEST_2"
122+
instance_size = "M10"
123+
node_count = 1
124+
node_count_read_only = 0
125+
node_count_analytics = 1
126+
enable_disk_gb = false
127+
enable_compute = true
75128
}
76129
]
77130
}

internal/convert/testdata/adv2v2/dynamic_replication_specs.in.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,47 @@ resource "mongodbatlas_advanced_cluster" "dynamic_replication_specs" {
4747
}
4848
}
4949

50+
resource "mongodbatlas_advanced_cluster" "all_specs" {
51+
project_id = var.project_id
52+
name = var.cluster_name
53+
cluster_type = "GEOSHARDED"
54+
disk_size_gb = 123
55+
56+
dynamic "replication_specs" {
57+
for_each = var.replication_specs
58+
content {
59+
num_shards = replication_specs.value.num_shards
60+
zone_name = replication_specs.value.zone_name
61+
dynamic "region_configs" {
62+
for_each = replication_specs.value.region_configs
63+
content {
64+
priority = region_configs.value.priority
65+
provider_name = region_configs.value.provider_name
66+
region_name = region_configs.value.region_name
67+
electable_specs {
68+
instance_size = region_configs.value.instance_size
69+
node_count = region_configs.value.electable_node_count
70+
}
71+
read_only_specs {
72+
instance_size = region_configs.value.instance_size
73+
node_count = region_configs.value.read_only_node_count
74+
}
75+
analytics_specs {
76+
instance_size = region_configs.value.instance_size
77+
node_count = region_configs.value.analytics_node_count
78+
}
79+
auto_scaling {
80+
disk_gb_enabled = region_configs.value.enable_disk_gb
81+
}
82+
analytics_auto_scaling {
83+
compute_enabled = region_configs.value.enable_compute
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
5091
# example of variable for demostration purposes, not used in the conversion
5192
variable "replication_specs" {
5293
description = "List of replication specifications in mongodbatlas_advanced_cluster format"
@@ -59,6 +100,9 @@ variable "replication_specs" {
59100
instance_size = string
60101
electable_node_count = number
61102
read_only_node_count = number
103+
analytics_node_count = number
104+
enable_disk_gb = bool
105+
enable_compute = bool
62106
priority = number
63107
}))
64108
}))
@@ -73,6 +117,9 @@ variable "replication_specs" {
73117
instance_size = "M10"
74118
electable_node_count = 3
75119
read_only_node_count = 0
120+
analytics_node_count = 0
121+
enable_disk_gb = true
122+
enable_compute = false
76123
priority = 7
77124
}
78125
]
@@ -86,13 +133,19 @@ variable "replication_specs" {
86133
instance_size = "M10"
87134
electable_node_count = 2
88135
read_only_node_count = 1
136+
analytics_node_count = 1
137+
enable_disk_gb = false
138+
enable_compute = true
89139
priority = 7
90140
}, {
91141
provider_name = "AWS"
92142
region_name = "EU_WEST_1"
93143
instance_size = "M10"
94144
electable_node_count = 1
95145
read_only_node_count = 0
146+
analytics_node_count = 0
147+
enable_disk_gb = true
148+
enable_compute = false
96149
priority = 6
97150
}
98151
]

internal/convert/testdata/adv2v2/dynamic_replication_specs.out.tf

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,50 @@ resource "mongodbatlas_advanced_cluster" "dynamic_replication_specs" {
4343
# Updated by atlas-cli-plugin-terraform, please review the changes.
4444
}
4545

46+
resource "mongodbatlas_advanced_cluster" "all_specs" {
47+
project_id = var.project_id
48+
name = var.cluster_name
49+
cluster_type = "GEOSHARDED"
50+
51+
replication_specs = flatten([
52+
for spec in var.replication_specs : [
53+
for i in range(spec.num_shards) : {
54+
zone_name = spec.zone_name
55+
region_configs = [
56+
for region in spec.region_configs : {
57+
priority = region.priority
58+
provider_name = region.provider_name
59+
region_name = region.region_name
60+
electable_specs = {
61+
instance_size = region.instance_size
62+
node_count = region.electable_node_count
63+
disk_size_gb = 123
64+
}
65+
read_only_specs = {
66+
instance_size = region.instance_size
67+
node_count = region.read_only_node_count
68+
disk_size_gb = 123
69+
}
70+
analytics_specs = {
71+
instance_size = region.instance_size
72+
node_count = region.analytics_node_count
73+
disk_size_gb = 123
74+
}
75+
auto_scaling = {
76+
disk_gb_enabled = region.enable_disk_gb
77+
}
78+
analytics_auto_scaling = {
79+
compute_enabled = region.enable_compute
80+
}
81+
}
82+
]
83+
}
84+
]
85+
])
86+
87+
# Updated by atlas-cli-plugin-terraform, please review the changes.
88+
}
89+
4690
# example of variable for demostration purposes, not used in the conversion
4791
variable "replication_specs" {
4892
description = "List of replication specifications in mongodbatlas_advanced_cluster format"
@@ -55,6 +99,9 @@ variable "replication_specs" {
5599
instance_size = string
56100
electable_node_count = number
57101
read_only_node_count = number
102+
analytics_node_count = number
103+
enable_disk_gb = bool
104+
enable_compute = bool
58105
priority = number
59106
}))
60107
}))
@@ -69,6 +116,9 @@ variable "replication_specs" {
69116
instance_size = "M10"
70117
electable_node_count = 3
71118
read_only_node_count = 0
119+
analytics_node_count = 0
120+
enable_disk_gb = true
121+
enable_compute = false
72122
priority = 7
73123
}
74124
]
@@ -82,13 +132,19 @@ variable "replication_specs" {
82132
instance_size = "M10"
83133
electable_node_count = 2
84134
read_only_node_count = 1
135+
analytics_node_count = 1
136+
enable_disk_gb = false
137+
enable_compute = true
85138
priority = 7
86139
}, {
87140
provider_name = "AWS"
88141
region_name = "EU_WEST_1"
89142
instance_size = "M10"
90143
electable_node_count = 1
91144
read_only_node_count = 0
145+
analytics_node_count = 0
146+
enable_disk_gb = true
147+
enable_compute = false
92148
priority = 6
93149
}
94150
]

0 commit comments

Comments
 (0)