Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
c7857b0
doc assumption for dynamic blocks in tags and labels
lantoli Mar 17, 2025
72a2fbc
dynamic_regions_config example
lantoli Mar 17, 2025
26d2183
range for priorities
lantoli Mar 17, 2025
cf5e3f7
allow dynamic block in regions_config
lantoli Mar 18, 2025
771845c
doc
lantoli Mar 18, 2025
5dfbede
update comment
lantoli Mar 18, 2025
17a6444
minimum implementation to have test failing because difference in gol…
lantoli Mar 18, 2025
0f10bb9
export enclose funcs
lantoli Mar 18, 2025
44a4bc7
create EncloseNewLines and remove SetAttrExpr
lantoli Mar 18, 2025
130009b
root replication_specs
lantoli Mar 18, 2025
b1dc050
remove priority checks about numerical literal
lantoli Mar 18, 2025
c5e6d5f
reuse getRegionConfig from dynamic block logic
lantoli Mar 18, 2025
ae8471b
only sort by priority if all priorities are numerical literals
lantoli Mar 18, 2025
3f93627
remove limitations for priority and electable_nodes
lantoli Mar 18, 2025
ff3fc22
use config in dynamic blocks from individual
lantoli Mar 18, 2025
87bf0fc
passing test
lantoli Mar 19, 2025
b4db307
add auto_scaling example
lantoli Mar 19, 2025
969da96
fix region_configs name replacement
lantoli Mar 19, 2025
55b93fd
refactor isDynamicBlock
lantoli Mar 19, 2025
6186dcd
go back to unexported tokenNewLine
lantoli Mar 19, 2025
1883ff0
add analytics specs
lantoli Mar 19, 2025
032c072
Merge branch 'main' into CLOUDP-303941_regions_config
lantoli Mar 19, 2025
4684c7c
example in readme
lantoli Mar 19, 2025
861a6c4
clarify num_shards limitation
lantoli Mar 19, 2025
165256e
feedback section
lantoli Mar 19, 2025
11314b5
getDynamicBlockRegionConfigsRegionArray
lantoli Mar 19, 2025
b2c3161
refactor fillRegionConfigsDynamicBlock
lantoli Mar 19, 2025
8f6e967
EncloseBracketsNewLines
lantoli Mar 19, 2025
0f2dfeb
fillRegionConfigsDynamicBlock doc
lantoli Mar 19, 2025
876b6eb
move shards closer to where it's used
lantoli Mar 19, 2025
4ab2d3a
add comment for priority loop
lantoli Mar 19, 2025
32ca654
add dynamic block doc
lantoli Mar 19, 2025
0d61fe7
small doc adjustment
lantoli Mar 19, 2025
5fa1cb7
rename to fillReplicationSpecsWithDynamicRegionConfigs
lantoli Mar 19, 2025
b8258ae
Update README.md
lantoli Mar 19, 2025
f90d5a2
link to limitations
lantoli Mar 19, 2025
2bec1f9
how to handle limitation
lantoli Mar 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

ENHANCEMENTS:

* Supports `dynamic` block for `tags` and `labels`
* Supports `dynamic` block for `tags`, `labels` and `regions_config`

## 1.0.0 (Mar 6, 2025)

Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ Given the different ways of using dynamic blocks, we recommend reviewing the out

#### Dynamic blocks in tags and labels

You can use `dynamic` blocks for `tags` and `labels`. You can also combine the use of dynamic blocks in `tags` and `labels` with individual blocks in the same cluster definition, e.g.:
You can use `dynamic` blocks for `tags` and `labels`. The plugin assumes that `for_each` has an expression which is evaluated to a `map` of strings.
You can also combine the use of dynamic blocks in `tags` and `labels` with individual blocks in the same cluster definition, e.g.:
```hcl
tags {
key = "environment"
Expand All @@ -72,12 +73,17 @@ dynamic "tags" {
}
```

#### Dynamic blocks in regions_config

You can use `dynamic` blocks for `regions_config`. The plugin assumes that `for_each` has an expression which is evaluated to a `list` or `set` of objects.
Dynamic block and individual blocks for `regions_config` are not supported at the same time in a `replication_specs`.

### Limitations

- The plugin doesn't support `regions_config` without `electable_nodes` as there can be some issues with `priority` when they only have `analytics_nodes` and/or `electable_nodes`.
- [`priority`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster#priority-1) is required in `regions_config` and must be a numeric [literal expression](https://developer.hashicorp.com/nomad/docs/job-specification/hcl2/expressions#literal-expressions) between 7 and 1, e.g. `var.priority` is not supported. This is to allow reordering them by descending priority as this is expected in `mongodbatlas_advanced_cluster`.
- [`num_shards`](https://registry.terraform.io/providers/mongodb/mongodbatlas/latest/docs/resources/cluster#num_shards-2) in `replication_specs` must be a numeric [literal expression](https://developer.hashicorp.com/nomad/docs/job-specification/hcl2/expressions#literal-expressions), e.g. `var.num_shards` is not supported. This is to allow creating a `replication_specs` element per shard in `mongodbatlas_advanced_cluster`.
- `dynamic` blocks are currently supported only for `tags` and `labels`. **Coming soon**: support for `replication_specs` and `regions_config`.
- `dynamic` blocks are currently supported only for `tags`, `labels` and `regions_config`. **Coming soon**: support for `replication_specs`.

## Contributing

Expand Down
18 changes: 13 additions & 5 deletions internal/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ const (
errNumShards = "setting " + nNumShards

commentGeneratedBy = "Generated by atlas-cli-plugin-terraform."
commentConfirmReferences = "Please confirm that all references to this resource are updated."
commentConfirmReferences = "Please review the changes and confirm that references to this resource are updated."
commentMovedBlock = "Moved blocks"
commentRemovedOld = "Note: Remember to remove or comment out the old cluster definitions."
)

var (
dynamicBlockAllowList = []string{nTags, nLabels}
dynamicBlockAllowList = []string{nTags, nLabels, nConfigSrc}
)

type attrVals struct {
Expand Down Expand Up @@ -202,11 +202,11 @@ func fillReplicationSpecs(resourceb *hclwrite.Body, root attrVals) error {
break
}
specbSrc := specSrc.Body()
if err := checkDynamicBlock(specbSrc); err != nil {
return err
}
// ok to fail as zone_name is optional
_ = hcl.MoveAttr(specbSrc, specb, nZoneName, nZoneName, errRepSpecs)
if hasDynamicBlock, err := fillRegionConfigsDynamicBlock(specb, specbSrc, root); err != nil || hasDynamicBlock {
return err
}
shards := specbSrc.GetAttribute(nNumShards)
if shards == nil {
return fmt.Errorf("%s: %s not found", errRepSpecs, nNumShards)
Expand Down Expand Up @@ -306,6 +306,14 @@ func fillBlockOpt(resourceb *hclwrite.Body, name string) {
resourceb.SetAttributeRaw(name, hcl.TokensObject(block.Body()))
}

func fillRegionConfigsDynamicBlock(specb, specbSrc *hclwrite.Body, root attrVals) (bool, error) {
d, err := getDynamicBlock(specbSrc, nConfigSrc)
if err != nil || d.forEach == nil {
return false, err
}
return true, nil
}

func fillRegionConfigs(specb, specbSrc *hclwrite.Body, root attrVals) error {
var configs []*hclwrite.Body
for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ resource "mongodbatlas_advanced_cluster" "this" {
}

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ resource "mongodbatlas_advanced_cluster" "ar" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ resource "mongodbatlas_advanced_cluster" "ar" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
2 changes: 1 addition & 1 deletion internal/convert/testdata/clu2adv/autoscaling.out.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ resource "mongodbatlas_advanced_cluster" "autoscaling" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
4 changes: 2 additions & 2 deletions internal/convert/testdata/clu2adv/data_sources.out.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ data "mongodbatlas_advanced_cluster" "singular" {
use_replication_spec_per_shard = true

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

data "mongodbatlas_advanced_clusters" "plural" {
Expand All @@ -15,7 +15,7 @@ data "mongodbatlas_advanced_clusters" "plural" {
use_replication_spec_per_shard = true

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

data "mongodbatlas_advanced_cluster" "adv_singular" {
Expand Down
49 changes: 49 additions & 0 deletions internal/convert/testdata/clu2adv/dynamic_regions_config.in.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
resource "mongodbatlas_cluster" "dynamic_regions_config" {
project_id = var.project_id
name = "cluster"
cluster_type = "SHARDED"
provider_name = "AWS"
provider_instance_size_name = "M10"
replication_specs {
num_shards = var.replication_specs.num_shards
dynamic "regions_config" {
for_each = var.replication_specs.regions_config
content {
region_name = regions_config.value.region_name
electable_nodes = regions_config.value.electable_nodes
priority = regions_config.value.priority
read_only_nodes = regions_config.value.read_only_nodes
}
}
}
}

# example of variable for demostration purposes, not used in the conversion
variable "replication_specs" {
type = object({
num_shards = number
regions_config = set(object({
region_name = string
electable_nodes = number
priority = number
read_only_nodes = number
}))
})
default = {
num_shards = 3
regions_config = [
{
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
},
{
region_name = "US_WEST_2"
electable_nodes = 2
priority = 6
read_only_nodes = 1
}
]
}
}
59 changes: 59 additions & 0 deletions internal/convert/testdata/clu2adv/dynamic_regions_config.out.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "mongodbatlas_advanced_cluster" "dynamic_regions_config" {
project_id = var.project_id
name = "cluster"
cluster_type = "SHARDED"
replication_specs = [
for i in range(var.replication_specs.num_shards) : {
region_configs = flatten([
for priority in range(7, 0, -1) : [
for region in var.replication_specs.regions_config : {
provider_name = "AWS"
region_name = region.region_name
priority = region.priority
electable_specs = region.electable_nodes > 0 ? {
node_count = region.electable_nodes
instance_size = "M10"
} : null
read_only_specs = region.read_only_nodes > 0 ? {
node_count = region.read_only_nodes
instance_size = "M10"
} : null
} if region.priority == priority
]
])
}
]

# Generated by atlas-cli-plugin-terraform.
# Please review the changes and confirm that references to this resource are updated.
}

# example of variable for demostration purposes, not used in the conversion
variable "replication_specs" {
type = object({
num_shards = number
regions_config = set(object({
region_name = string
electable_nodes = number
priority = number
read_only_nodes = number
}))
})
default = {
num_shards = 3
regions_config = [
{
region_name = "US_EAST_1"
electable_nodes = 3
priority = 7
read_only_nodes = 0
},
{
region_name = "US_WEST_2"
electable_nodes = 2
priority = 6
read_only_nodes = 1
}
]
}
}
10 changes: 5 additions & 5 deletions internal/convert/testdata/clu2adv/dynamic_tags_labels.out.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ resource "mongodbatlas_advanced_cluster" "simplified" {
tags = var.tags

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "expression" {
Expand All @@ -47,7 +47,7 @@ resource "mongodbatlas_advanced_cluster" "expression" {
}

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "simplified_individual" {
Expand Down Expand Up @@ -78,7 +78,7 @@ resource "mongodbatlas_advanced_cluster" "simplified_individual" {
)

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "expression_individual" {
Expand Down Expand Up @@ -111,7 +111,7 @@ resource "mongodbatlas_advanced_cluster" "expression_individual" {
)

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "full_example" {
Expand Down Expand Up @@ -157,5 +157,5 @@ resource "mongodbatlas_advanced_cluster" "full_example" {
)

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
1 change: 0 additions & 1 deletion internal/convert/testdata/clu2adv/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"regions_config_out_of_range_priority": "setting priority: priority is 0 but must be between 1 and 7",
"regions_config_non_literal_priority": "setting priority: failed to evaluate number",
"replication_specs_unsupported_dynamic": "dynamic blocks are not supported",
"regions_config_unsupported_dynamic": "dynamic blocks are not supported",
"replication_specs_non_literal_num_shards": "setting num_shards: failed to evaluate number",
"replication_specs_missing_num_shards": "num_shards not found"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ resource "mongodbatlas_advanced_cluster" "free_cluster" { # comment in the resou
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ resource "mongodbatlas_advanced_cluster" "cluster1" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "cluster2" {
Expand All @@ -43,7 +43,7 @@ resource "mongodbatlas_advanced_cluster" "cluster2" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "count" {
Expand All @@ -69,7 +69,7 @@ resource "mongodbatlas_advanced_cluster" "count" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "forEach" {
Expand All @@ -95,7 +95,7 @@ resource "mongodbatlas_advanced_cluster" "forEach" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "another_resource" "another" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ resource "mongodbatlas_advanced_cluster" "cluster" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

# Moved blocks
Expand Down
2 changes: 1 addition & 1 deletion internal/convert/testdata/clu2adv/multi_region.out.tf
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ resource "mongodbatlas_advanced_cluster" "multi_region" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ resource "mongodbatlas_advanced_cluster" "multirep" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}

resource "mongodbatlas_advanced_cluster" "geo" {
Expand Down Expand Up @@ -124,5 +124,5 @@ resource "mongodbatlas_advanced_cluster" "geo" {
]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
# Please review the changes and confirm that references to this resource are updated.
}
Loading
Loading