Skip to content

Commit e27d5a4

Browse files
committed
move info in file guide_adv2v2_dynamic_block.md to command_adv2v2.md
1 parent da42d28 commit e27d5a4

File tree

3 files changed

+259
-282
lines changed

3 files changed

+259
-282
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ atlas terraform advancedClusterToV2 --file in.tf --output out.tf
3030
atlas tf adv2v2 -f in.tf -o out.tf
3131
```
3232

33-
[📖 Full Documentation](./docs/command_adv2v2.md) | [Dynamic Blocks Guide](./docs/guide_adv2v2_dynamic_block.md)
33+
[📖 Full Documentation](./docs/command_adv2v2.md)
3434

3535
## Installation
3636

docs/command_adv2v2.md

Lines changed: 258 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,264 @@ replication_specs = [
216216
]
217217
```
218218

219+
## Dynamic Block Handling
220+
221+
The converter intelligently handles `dynamic` blocks, transforming them to work with the new Provider 2.0.0 structure.
222+
223+
### Dynamic Blocks in replication_specs
224+
225+
When using dynamic blocks for `replication_specs`, the converter transforms them into a flattened structure:
226+
227+
#### Basic Dynamic replication_specs
228+
229+
**Input (Legacy Format):**
230+
```hcl
231+
dynamic "replication_specs" {
232+
for_each = var.replication_specs
233+
content {
234+
num_shards = replication_specs.value.num_shards
235+
zone_name = replication_specs.value.zone_name
236+
region_configs {
237+
region_name = replication_specs.value.region_name
238+
priority = 7
239+
provider_name = "AWS"
240+
electable_specs {
241+
instance_size = replication_specs.value.instance_size
242+
node_count = 3
243+
}
244+
}
245+
}
246+
}
247+
```
248+
249+
**Output (Provider 2.0.0 Format):**
250+
```hcl
251+
replication_specs = flatten([
252+
for spec in var.replication_specs : [
253+
for i in range(spec.num_shards) : {
254+
zone_name = spec.zone_name
255+
config = [{
256+
region_name = spec.region_name
257+
priority = 7
258+
provider_name = "AWS"
259+
electable_specs = {
260+
instance_size = spec.instance_size
261+
node_count = 3
262+
}
263+
}]
264+
}
265+
]
266+
])
267+
```
268+
269+
#### Nested Dynamic Blocks
270+
271+
When you have nested dynamic blocks (dynamic `replication_specs` containing dynamic `region_configs`), the converter handles both levels:
272+
273+
**Input:**
274+
```hcl
275+
dynamic "replication_specs" {
276+
for_each = var.replication_specs
277+
content {
278+
num_shards = replication_specs.value.num_shards
279+
zone_name = replication_specs.value.zone_name
280+
dynamic "region_configs" {
281+
for_each = replication_specs.value.regions
282+
content {
283+
region_name = region_configs.value.region_name
284+
priority = region_configs.value.priority
285+
provider_name = region_configs.value.provider_name
286+
electable_specs {
287+
instance_size = region_configs.value.instance_size
288+
node_count = region_configs.value.node_count
289+
}
290+
}
291+
}
292+
}
293+
}
294+
```
295+
296+
**Output:**
297+
```hcl
298+
replication_specs = flatten([
299+
for spec in var.replication_specs : [
300+
for i in range(spec.num_shards) : {
301+
zone_name = spec.zone_name
302+
config = [
303+
for region in spec.regions : {
304+
region_name = region.region_name
305+
priority = region.priority
306+
provider_name = region.provider_name
307+
electable_specs = {
308+
instance_size = region.instance_size
309+
node_count = region.node_count
310+
}
311+
}
312+
]
313+
}
314+
]
315+
])
316+
```
317+
318+
### Dynamic Blocks in region_configs
319+
320+
Dynamic blocks within `region_configs` (now `config` in Provider 2.0.0) are transformed to use list comprehensions:
321+
322+
**Input:**
323+
```hcl
324+
replication_specs {
325+
num_shards = 1
326+
dynamic "region_configs" {
327+
for_each = var.regions
328+
content {
329+
region_name = region_configs.value.region_name
330+
priority = region_configs.value.priority
331+
provider_name = "AWS"
332+
electable_specs {
333+
instance_size = region_configs.value.instance_size
334+
node_count = 3
335+
}
336+
}
337+
}
338+
}
339+
```
340+
341+
**Output:**
342+
```hcl
343+
replication_specs = [{
344+
config = [
345+
for region in var.regions : {
346+
region_name = region.region_name
347+
priority = region.priority
348+
provider_name = "AWS"
349+
electable_specs = {
350+
instance_size = region.instance_size
351+
node_count = 3
352+
}
353+
}
354+
]
355+
}]
356+
```
357+
358+
### Dynamic Blocks in tags and labels
359+
360+
Dynamic blocks for `tags` and `labels` are converted from block syntax to object syntax:
361+
362+
**Tags Input:**
363+
```hcl
364+
dynamic "tags" {
365+
for_each = var.tags
366+
content {
367+
key = tags.key
368+
value = tags.value
369+
}
370+
}
371+
```
372+
373+
**Tags Output:**
374+
```hcl
375+
tags = {
376+
for tag in var.tags : tag.key => tag.value
377+
}
378+
```
379+
380+
### Handling num_shards with Dynamic Blocks
381+
382+
The converter properly handles `num_shards` expansion:
383+
384+
1. **For literal num_shards values**: The converter expands the replication spec for each shard
385+
2. **For variable num_shards values**: The converter uses `range()` function to handle the expansion dynamically
386+
387+
**Example with variable num_shards:**
388+
```hcl
389+
# Input
390+
dynamic "replication_specs" {
391+
for_each = var.specs
392+
content {
393+
num_shards = replication_specs.value.shards
394+
# ... config
395+
}
396+
}
397+
398+
# Output
399+
replication_specs = flatten([
400+
for spec in var.specs : [
401+
for i in range(spec.shards) : {
402+
# ... config
403+
}
404+
]
405+
])
406+
```
407+
408+
### Mixed Static and Dynamic Blocks
409+
410+
If you have both static and dynamic blocks for `replication_specs` or `region_configs`, the converter handles them separately:
411+
412+
**Input:**
413+
```hcl
414+
replication_specs {
415+
num_shards = 1
416+
region_configs {
417+
region_name = "US_EAST_1"
418+
# ... config
419+
}
420+
}
421+
422+
dynamic "replication_specs" {
423+
for_each = var.additional_specs
424+
content {
425+
# ... config
426+
}
427+
}
428+
```
429+
430+
**Output:**
431+
```hcl
432+
replication_specs = concat(
433+
[{
434+
config = [{
435+
region_name = "US_EAST_1"
436+
# ... config
437+
}]
438+
}],
439+
flatten([
440+
for spec in var.additional_specs : [
441+
# ... transformed dynamic content
442+
]
443+
])
444+
)
445+
```
446+
219447
## Limitations
220448

449+
### Dynamic Block Limitations
450+
451+
1. **Complex for_each expressions**: While the converter preserves complex expressions in `for_each`, they should be verified after conversion to ensure they work with the new structure.
452+
453+
2. **Custom functions in dynamic blocks**: If you use custom functions or complex conditionals within dynamic blocks, these are preserved but must be tested thoroughly.
454+
455+
3. **Variable references transformation**: The converter updates variable references (e.g., `replication_specs.value` to `spec`), but complex nested references should be reviewed.
456+
457+
4. **Block ordering**: The Provider 2.0.0 format may handle block ordering differently. Ensure any dependencies on block order are maintained.
458+
459+
### General Limitations
460+
221461
- The converter requires valid HCL syntax in the input file
222-
- Complex expressions in dynamic blocks may require manual review
223-
- Custom functions or complex conditionals in `for_each` expressions are preserved but should be tested
224-
- See the [dynamic blocks guide](./guide_adv2v2_dynamic_block.md) for more details on dynamic block handling
462+
- Manual review is recommended for complex configurations
463+
- Always run `terraform plan` after conversion to verify the changes
464+
465+
If you encounter use cases not yet supported, please send us [feedback](https://github.com/mongodb-labs/atlas-cli-plugin-terraform/issues).
466+
467+
## Best Practices
468+
469+
1. **Review the output**: Always review the converted configuration to ensure it matches your intentions.
470+
2. **Test incrementally**: Test the converted configuration in a development environment before applying to production.
471+
3. **Simplify when possible**: If the converter produces complex nested expressions, consider simplifying them manually for better readability.
472+
4. **Use terraform plan**: Always run `terraform plan` after conversion to verify that the changes are as expected.
473+
474+
## More Examples
475+
476+
You can find more examples of dynamic block conversions in the [test data directory](https://github.com/mongodb-labs/atlas-cli-plugin-terraform/tree/main/internal/convert/testdata/adv2v2), particularly:
477+
- `dynamic_replication_specs.in.tf` / `.out.tf`
478+
- `dynamic_region_configs.in.tf` / `.out.tf`
479+
- `dynamic_tags_labels.in.tf` / `.out.tf`

0 commit comments

Comments
 (0)