Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 33 additions & 18 deletions internal/convert/adv2v2.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package convert

import (
"fmt"
"slices"

"github.com/hashicorp/hcl/v2/hclwrite"
Expand Down Expand Up @@ -56,31 +57,45 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
}

func convertRepSpecs(resourceb *hclwrite.Body) error {
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
if block == nil {
return nil
var repSpecs []*hclwrite.Body
for {
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
if block == nil {
break
}
resourceb.RemoveBlock(block)
if err := convertConfig(block.Body()); err != nil {
return err
}
repSpecs = append(repSpecs, block.Body())
}
resourceb.RemoveBlock(block)
if err := convertConfig(block.Body()); err != nil {
return err
if len(repSpecs) == 0 {
return fmt.Errorf("must have at least one replication_specs")
}
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(block.Body()))
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArray(repSpecs))
return nil
}

func convertConfig(repSpecs *hclwrite.Body) error {
block := repSpecs.FirstMatchingBlock(nConfig, nil)
if block == nil {
return nil
var configs []*hclwrite.Body
for {
block := repSpecs.FirstMatchingBlock(nConfig, nil)
if block == nil {
break
}
repSpecs.RemoveBlock(block)
blockb := block.Body()
fillBlockOpt(blockb, nElectableSpecs)
fillBlockOpt(blockb, nReadOnlySpecs)
fillBlockOpt(blockb, nAnalyticsSpecs)
fillBlockOpt(blockb, nAutoScaling)
fillBlockOpt(blockb, nAnalyticsAutoScaling)
configs = append(configs, blockb)
}
if len(configs) == 0 {
return fmt.Errorf("replication_specs must have at least one region_configs")
}
repSpecs.RemoveBlock(block)
blockb := block.Body()
fillBlockOpt(blockb, nElectableSpecs)
fillBlockOpt(blockb, nReadOnlySpecs)
fillBlockOpt(blockb, nAnalyticsSpecs)
fillBlockOpt(blockb, nAutoScaling)
fillBlockOpt(blockb, nAnalyticsAutoScaling)
repSpecs.SetAttributeRaw(nConfig, hcl.TokensArraySingle(blockb))
repSpecs.SetAttributeRaw(nConfig, hcl.TokensArray(configs))
return nil
}

Expand Down
12 changes: 12 additions & 0 deletions internal/convert/testdata/adv2v2/choose_sources.in.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
resource "mongodbatlas_advanced_cluster" "this" {
# updated, doesn't have nested attributes

replication_specs {
region_configs {
priority = 7
provider_name = "AWS"
region_name = "EU_WEST_1"
electable_specs {
instance_size = "M10"
node_count = 3
}
}
}
}

resource "mongodbatlas_advanced_cluster" "this" {
Expand Down
16 changes: 16 additions & 0 deletions internal/convert/testdata/adv2v2/choose_sources.out.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
resource "mongodbatlas_advanced_cluster" "this" {
# updated, doesn't have nested attributes

replication_specs = [
{
region_configs = [
{
priority = 7
provider_name = "AWS"
region_name = "EU_WEST_1"
electable_specs = {
instance_size = "M10"
node_count = 3
}
}
]
}
]

# Updated by atlas-cli-plugin-terraform, please review the changes.
}

Expand Down
4 changes: 3 additions & 1 deletion internal/convert/testdata/adv2v2/errors.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{
"configuration_file_error": "failed to parse Terraform config file"
"configuration_file_error": "failed to parse Terraform config file",
"replication_specs_missing_region_configs": "replication_specs must have at least one region_configs",
"missing_replication_specs": "must have at least one replication_specs"
}
14 changes: 14 additions & 0 deletions internal/convert/testdata/adv2v2/flex.in.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
resource "mongodbatlas_advanced_cluster" "this" {
project_id = "<YOUR-PROJECT-ID>"
name = "flex-cluster"
cluster_type = "REPLICASET"

replication_specs {
region_configs {
provider_name = "FLEX"
backing_provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
}
}
}
20 changes: 20 additions & 0 deletions internal/convert/testdata/adv2v2/flex.out.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
resource "mongodbatlas_advanced_cluster" "this" {
project_id = "<YOUR-PROJECT-ID>"
name = "flex-cluster"
cluster_type = "REPLICASET"

replication_specs = [
{
region_configs = [
{
provider_name = "FLEX"
backing_provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
}
]
}
]

# Updated by atlas-cli-plugin-terraform, please review the changes.
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
resource "mongodbatlas_advanced_cluster" "no_replication_specs" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in all the examples I noticed we don't use num_shards: am I right? I am guessing that's on purpose because you don't still support it but you will?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same question applies to the .in.tf examples where there's usage of deprecated fields, such as root disk_size_gb

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, the next PR is precisely about removing num_shards and disk_size_gb.

i prefer to do smaller more focused PRs

# missing replication_specs
project_id = var.project_id
name = "cluster-multi-region"
cluster_type = "REPLICASET"
}
39 changes: 39 additions & 0 deletions internal/convert/testdata/adv2v2/multi_region.in.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
resource "mongodbatlas_advanced_cluster" "multi_region" {
project_id = var.project_id
name = "cluster-multi-region"
cluster_type = "REPLICASET"
backup_enabled = true

replication_specs {
region_configs {
provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
electable_specs = {
node_count = 3
instance_size = "M10"
disk_size_gb = 100
}
}
region_configs {
provider_name = "AWS"
region_name = "US_WEST_2"
priority = 6
electable_specs = {
node_count = 3
instance_size = "M10"
disk_size_gb = 100
}
}
region_configs {
provider_name = "AWS"
region_name = "US_WEST_1"
priority = 5
electable_specs = {
node_count = 1
instance_size = "M10"
disk_size_gb = 100
}
}
}
}
45 changes: 45 additions & 0 deletions internal/convert/testdata/adv2v2/multi_region.out.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
resource "mongodbatlas_advanced_cluster" "multi_region" {
project_id = var.project_id
name = "cluster-multi-region"
cluster_type = "REPLICASET"
backup_enabled = true

replication_specs = [
{
region_configs = [
{
provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
electable_specs = {
node_count = 3
instance_size = "M10"
disk_size_gb = 100
}
},
{
provider_name = "AWS"
region_name = "US_WEST_2"
priority = 6
electable_specs = {
node_count = 3
instance_size = "M10"
disk_size_gb = 100
}
},
{
provider_name = "AWS"
region_name = "US_WEST_1"
priority = 5
electable_specs = {
node_count = 1
instance_size = "M10"
disk_size_gb = 100
}
}
]
}
]

# Updated by atlas-cli-plugin-terraform, please review the changes.
}
104 changes: 104 additions & 0 deletions internal/convert/testdata/adv2v2/multi_replication_specs.in.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
resource "mongodbatlas_advanced_cluster" "multirep" {
project_id = var.project_id
name = "multirep"
cluster_type = "GEOSHARDED"
backup_enabled = false
replication_specs {
zone_name = "Zone 1"
region_configs {
provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
replication_specs {
zone_name = "Zone 2"
region_configs {
provider_name = "AWS"
region_name = "US_WEST_2"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
}

resource "mongodbatlas_advanced_cluster" "geo" {
project_id = var.project_id
name = "geo"
cluster_type = "GEOSHARDED"
backup_enabled = false
replication_specs {
zone_name = "Zone 1"
region_configs {
provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
replication_specs {
zone_name = "Zone 1"
region_configs {
provider_name = "AWS"
region_name = "US_EAST_1"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
replication_specs {
zone_name = "Zone 2"
region_configs {
provider_name = "AWS"
region_name = "US_WEST_2"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
replication_specs {
zone_name = "Zone 2"
region_configs {
provider_name = "AWS"
region_name = "US_WEST_2"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
replication_specs {
zone_name = "Zone 2"
region_configs {
provider_name = "AWS"
region_name = "US_WEST_2"
priority = 7
electable_specs {
node_count = 3
instance_size = "M10"
disk_size_gb = 80
}
}
}
}
Loading