From 85edb57a9c032796144dd944899e3c98315c9cfb Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 10:38:23 +0200 Subject: [PATCH 1/8] flex clusters --- internal/convert/testdata/adv2v2/flex.in.tf | 14 ++++++++++++++ internal/convert/testdata/adv2v2/flex.out.tf | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 internal/convert/testdata/adv2v2/flex.in.tf create mode 100644 internal/convert/testdata/adv2v2/flex.out.tf diff --git a/internal/convert/testdata/adv2v2/flex.in.tf b/internal/convert/testdata/adv2v2/flex.in.tf new file mode 100644 index 0000000..ea406c0 --- /dev/null +++ b/internal/convert/testdata/adv2v2/flex.in.tf @@ -0,0 +1,14 @@ +resource "mongodbatlas_advanced_cluster" "this" { + 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 + } + } +} diff --git a/internal/convert/testdata/adv2v2/flex.out.tf b/internal/convert/testdata/adv2v2/flex.out.tf new file mode 100644 index 0000000..6abe720 --- /dev/null +++ b/internal/convert/testdata/adv2v2/flex.out.tf @@ -0,0 +1,20 @@ +resource "mongodbatlas_advanced_cluster" "this" { + 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. +} From 09da6284f7927e18e25e41fc6f3ba0a7127dc692 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 10:44:32 +0200 Subject: [PATCH 2/8] tenant --- internal/convert/testdata/adv2v2/tenant.in.tf | 17 ++++++++++++++ .../convert/testdata/adv2v2/tenant.out.tf | 23 +++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 internal/convert/testdata/adv2v2/tenant.in.tf create mode 100644 internal/convert/testdata/adv2v2/tenant.out.tf diff --git a/internal/convert/testdata/adv2v2/tenant.in.tf b/internal/convert/testdata/adv2v2/tenant.in.tf new file mode 100644 index 0000000..96cd1fa --- /dev/null +++ b/internal/convert/testdata/adv2v2/tenant.in.tf @@ -0,0 +1,17 @@ +resource "mongodbatlas_advanced_cluster" "this" { + project_id = var.project_id + name = "cluster-tenant" + cluster_type = "REPLICASET" + + replication_specs { + region_configs { + electable_specs { + instance_size = "M0" + } + provider_name = "TENANT" + backing_provider_name = "AWS" + region_name = "US_EAST_1" + priority = 7 + } + } +} diff --git a/internal/convert/testdata/adv2v2/tenant.out.tf b/internal/convert/testdata/adv2v2/tenant.out.tf new file mode 100644 index 0000000..f919f8e --- /dev/null +++ b/internal/convert/testdata/adv2v2/tenant.out.tf @@ -0,0 +1,23 @@ +resource "mongodbatlas_advanced_cluster" "this" { + project_id = var.project_id + name = "cluster-tenant" + cluster_type = "REPLICASET" + + replication_specs = [ + { + region_configs = [ + { + provider_name = "TENANT" + backing_provider_name = "AWS" + region_name = "US_EAST_1" + priority = 7 + electable_specs = { + instance_size = "M0" + } + } + ] + } + ] + + # Updated by atlas-cli-plugin-terraform, please review the changes. +} From 7dd88fc28463f0bcbd3449b96717f1666248a853 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 10:47:48 +0200 Subject: [PATCH 3/8] fix typo in filenames --- ...lti_replicaction_specs.in.tf => multi_replication_specs.in.tf} | 0 ...i_replicaction_specs.out.tf => multi_replication_specs.out.tf} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename internal/convert/testdata/clu2adv/{multi_replicaction_specs.in.tf => multi_replication_specs.in.tf} (100%) rename internal/convert/testdata/clu2adv/{multi_replicaction_specs.out.tf => multi_replication_specs.out.tf} (100%) diff --git a/internal/convert/testdata/clu2adv/multi_replicaction_specs.in.tf b/internal/convert/testdata/clu2adv/multi_replication_specs.in.tf similarity index 100% rename from internal/convert/testdata/clu2adv/multi_replicaction_specs.in.tf rename to internal/convert/testdata/clu2adv/multi_replication_specs.in.tf diff --git a/internal/convert/testdata/clu2adv/multi_replicaction_specs.out.tf b/internal/convert/testdata/clu2adv/multi_replication_specs.out.tf similarity index 100% rename from internal/convert/testdata/clu2adv/multi_replicaction_specs.out.tf rename to internal/convert/testdata/clu2adv/multi_replication_specs.out.tf From 8e713dc9c4df6d3b97a66d52b1560e07a4f7cd41 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 10:50:59 +0200 Subject: [PATCH 4/8] failing multi_config test --- .../testdata/adv2v2/multi_region.in.tf | 39 ++++++++++++++++ .../testdata/adv2v2/multi_region.out.tf | 45 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 internal/convert/testdata/adv2v2/multi_region.in.tf create mode 100644 internal/convert/testdata/adv2v2/multi_region.out.tf diff --git a/internal/convert/testdata/adv2v2/multi_region.in.tf b/internal/convert/testdata/adv2v2/multi_region.in.tf new file mode 100644 index 0000000..8dba6fb --- /dev/null +++ b/internal/convert/testdata/adv2v2/multi_region.in.tf @@ -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 + } + } + } +} diff --git a/internal/convert/testdata/adv2v2/multi_region.out.tf b/internal/convert/testdata/adv2v2/multi_region.out.tf new file mode 100644 index 0000000..fb6de94 --- /dev/null +++ b/internal/convert/testdata/adv2v2/multi_region.out.tf @@ -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. +} From 4ec05c346e56d0cf9ed455496248ca4c69d230f0 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 10:59:56 +0200 Subject: [PATCH 5/8] multi region_configs --- internal/convert/adv2v2.go | 30 ++++++++++++------- internal/convert/testdata/adv2v2/errors.json | 3 +- ...ication_specs_missing_region_configs.in.tf | 9 ++++++ 3 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 internal/convert/testdata/adv2v2/replication_specs_missing_region_configs.in.tf diff --git a/internal/convert/adv2v2.go b/internal/convert/adv2v2.go index f70cf59..0a6b7b8 100644 --- a/internal/convert/adv2v2.go +++ b/internal/convert/adv2v2.go @@ -1,6 +1,7 @@ package convert import ( + "fmt" "slices" "github.com/hashicorp/hcl/v2/hclwrite" @@ -69,18 +70,25 @@ func convertRepSpecs(resourceb *hclwrite.Body) error { } 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 } diff --git a/internal/convert/testdata/adv2v2/errors.json b/internal/convert/testdata/adv2v2/errors.json index 3de0218..1c9d171 100644 --- a/internal/convert/testdata/adv2v2/errors.json +++ b/internal/convert/testdata/adv2v2/errors.json @@ -1,3 +1,4 @@ { - "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" } diff --git a/internal/convert/testdata/adv2v2/replication_specs_missing_region_configs.in.tf b/internal/convert/testdata/adv2v2/replication_specs_missing_region_configs.in.tf new file mode 100644 index 0000000..19deba0 --- /dev/null +++ b/internal/convert/testdata/adv2v2/replication_specs_missing_region_configs.in.tf @@ -0,0 +1,9 @@ +resource "mongodbatlas_advanced_cluster" "multi_region_no_region_configs" { + # missing region_configs + project_id = var.project_id + name = "cluster-multi-region" + cluster_type = "REPLICASET" + + replication_specs { + } +} From 983259c348b8850518c4dc22067210415fde98f7 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 12:18:02 +0200 Subject: [PATCH 6/8] failing replication specs --- .../adv2v2/multi_replication_specs.in.tf | 104 +++++++++++++++ .../adv2v2/multi_replication_specs.out.tf | 126 ++++++++++++++++++ 2 files changed, 230 insertions(+) create mode 100644 internal/convert/testdata/adv2v2/multi_replication_specs.in.tf create mode 100644 internal/convert/testdata/adv2v2/multi_replication_specs.out.tf diff --git a/internal/convert/testdata/adv2v2/multi_replication_specs.in.tf b/internal/convert/testdata/adv2v2/multi_replication_specs.in.tf new file mode 100644 index 0000000..197bcd7 --- /dev/null +++ b/internal/convert/testdata/adv2v2/multi_replication_specs.in.tf @@ -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 + } + } + } +} diff --git a/internal/convert/testdata/adv2v2/multi_replication_specs.out.tf b/internal/convert/testdata/adv2v2/multi_replication_specs.out.tf new file mode 100644 index 0000000..9643057 --- /dev/null +++ b/internal/convert/testdata/adv2v2/multi_replication_specs.out.tf @@ -0,0 +1,126 @@ +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 + } + } + ] + }, + { + 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 + } + } + ] + } + ] + + # Updated by atlas-cli-plugin-terraform, please review the changes. +} + +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 + } + } + ] + }, + { + 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 + } + } + ] + }, + { + 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 + } + } + ] + }, + { + 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 + } + } + ] + }, + { + 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 + } + } + ] + } + ] + + # Updated by atlas-cli-plugin-terraform, please review the changes. +} From 5e60e8a280c951780ec6a0d10ad0ad2357671e43 Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 12:29:52 +0200 Subject: [PATCH 7/8] multi replication_specs --- internal/convert/adv2v2.go | 21 ++++++++++++------- .../testdata/adv2v2/choose_sources.in.tf | 12 +++++++++++ .../testdata/adv2v2/choose_sources.out.tf | 16 ++++++++++++++ 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/internal/convert/adv2v2.go b/internal/convert/adv2v2.go index 0a6b7b8..24001f6 100644 --- a/internal/convert/adv2v2.go +++ b/internal/convert/adv2v2.go @@ -57,15 +57,22 @@ 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 } diff --git a/internal/convert/testdata/adv2v2/choose_sources.in.tf b/internal/convert/testdata/adv2v2/choose_sources.in.tf index 42fb4ca..a513e43 100644 --- a/internal/convert/testdata/adv2v2/choose_sources.in.tf +++ b/internal/convert/testdata/adv2v2/choose_sources.in.tf @@ -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" { diff --git a/internal/convert/testdata/adv2v2/choose_sources.out.tf b/internal/convert/testdata/adv2v2/choose_sources.out.tf index 4783384..2c30b7e 100644 --- a/internal/convert/testdata/adv2v2/choose_sources.out.tf +++ b/internal/convert/testdata/adv2v2/choose_sources.out.tf @@ -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. } From d946b52806e17e266c1e8d0d7dc3fb584c014dbb Mon Sep 17 00:00:00 2001 From: Leo Antoli <430982+lantoli@users.noreply.github.com> Date: Tue, 19 Aug 2025 12:32:40 +0200 Subject: [PATCH 8/8] test no replication_specs --- internal/convert/testdata/adv2v2/errors.json | 3 ++- .../convert/testdata/adv2v2/missing_replication_specs.in.tf | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 internal/convert/testdata/adv2v2/missing_replication_specs.in.tf diff --git a/internal/convert/testdata/adv2v2/errors.json b/internal/convert/testdata/adv2v2/errors.json index 1c9d171..1a758de 100644 --- a/internal/convert/testdata/adv2v2/errors.json +++ b/internal/convert/testdata/adv2v2/errors.json @@ -1,4 +1,5 @@ { "configuration_file_error": "failed to parse Terraform config file", - "replication_specs_missing_region_configs": "replication_specs must have at least one region_configs" + "replication_specs_missing_region_configs": "replication_specs must have at least one region_configs", + "missing_replication_specs": "must have at least one replication_specs" } diff --git a/internal/convert/testdata/adv2v2/missing_replication_specs.in.tf b/internal/convert/testdata/adv2v2/missing_replication_specs.in.tf new file mode 100644 index 0000000..247a176 --- /dev/null +++ b/internal/convert/testdata/adv2v2/missing_replication_specs.in.tf @@ -0,0 +1,6 @@ +resource "mongodbatlas_advanced_cluster" "no_replication_specs" { + # missing replication_specs + project_id = var.project_id + name = "cluster-multi-region" + cluster_type = "REPLICASET" +}