Skip to content

Commit b37f419

Browse files
authored
feat: Convert multi replication specs multi regions advanced clusters (#62)
1 parent 0900ebc commit b37f419

16 files changed

+467
-19
lines changed

internal/convert/adv2v2.go

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package convert
22

33
import (
4+
"fmt"
45
"slices"
56

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

5859
func convertRepSpecs(resourceb *hclwrite.Body) error {
59-
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
60-
if block == nil {
61-
return nil
60+
var repSpecs []*hclwrite.Body
61+
for {
62+
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
63+
if block == nil {
64+
break
65+
}
66+
resourceb.RemoveBlock(block)
67+
if err := convertConfig(block.Body()); err != nil {
68+
return err
69+
}
70+
repSpecs = append(repSpecs, block.Body())
6271
}
63-
resourceb.RemoveBlock(block)
64-
if err := convertConfig(block.Body()); err != nil {
65-
return err
72+
if len(repSpecs) == 0 {
73+
return fmt.Errorf("must have at least one replication_specs")
6674
}
67-
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(block.Body()))
75+
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArray(repSpecs))
6876
return nil
6977
}
7078

7179
func convertConfig(repSpecs *hclwrite.Body) error {
72-
block := repSpecs.FirstMatchingBlock(nConfig, nil)
73-
if block == nil {
74-
return nil
80+
var configs []*hclwrite.Body
81+
for {
82+
block := repSpecs.FirstMatchingBlock(nConfig, nil)
83+
if block == nil {
84+
break
85+
}
86+
repSpecs.RemoveBlock(block)
87+
blockb := block.Body()
88+
fillBlockOpt(blockb, nElectableSpecs)
89+
fillBlockOpt(blockb, nReadOnlySpecs)
90+
fillBlockOpt(blockb, nAnalyticsSpecs)
91+
fillBlockOpt(blockb, nAutoScaling)
92+
fillBlockOpt(blockb, nAnalyticsAutoScaling)
93+
configs = append(configs, blockb)
94+
}
95+
if len(configs) == 0 {
96+
return fmt.Errorf("replication_specs must have at least one region_configs")
7597
}
76-
repSpecs.RemoveBlock(block)
77-
blockb := block.Body()
78-
fillBlockOpt(blockb, nElectableSpecs)
79-
fillBlockOpt(blockb, nReadOnlySpecs)
80-
fillBlockOpt(blockb, nAnalyticsSpecs)
81-
fillBlockOpt(blockb, nAutoScaling)
82-
fillBlockOpt(blockb, nAnalyticsAutoScaling)
83-
repSpecs.SetAttributeRaw(nConfig, hcl.TokensArraySingle(blockb))
98+
repSpecs.SetAttributeRaw(nConfig, hcl.TokensArray(configs))
8499
return nil
85100
}
86101

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
resource "mongodbatlas_advanced_cluster" "this" {
22
# updated, doesn't have nested attributes
3+
4+
replication_specs {
5+
region_configs {
6+
priority = 7
7+
provider_name = "AWS"
8+
region_name = "EU_WEST_1"
9+
electable_specs {
10+
instance_size = "M10"
11+
node_count = 3
12+
}
13+
}
14+
}
315
}
416

517
resource "mongodbatlas_advanced_cluster" "this" {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
resource "mongodbatlas_advanced_cluster" "this" {
22
# updated, doesn't have nested attributes
33

4+
replication_specs = [
5+
{
6+
region_configs = [
7+
{
8+
priority = 7
9+
provider_name = "AWS"
10+
region_name = "EU_WEST_1"
11+
electable_specs = {
12+
instance_size = "M10"
13+
node_count = 3
14+
}
15+
}
16+
]
17+
}
18+
]
19+
420
# Updated by atlas-cli-plugin-terraform, please review the changes.
521
}
622

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
{
2-
"configuration_file_error": "failed to parse Terraform config file"
2+
"configuration_file_error": "failed to parse Terraform config file",
3+
"replication_specs_missing_region_configs": "replication_specs must have at least one region_configs",
4+
"missing_replication_specs": "must have at least one replication_specs"
35
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource "mongodbatlas_advanced_cluster" "this" {
2+
project_id = "<YOUR-PROJECT-ID>"
3+
name = "flex-cluster"
4+
cluster_type = "REPLICASET"
5+
6+
replication_specs {
7+
region_configs {
8+
provider_name = "FLEX"
9+
backing_provider_name = "AWS"
10+
region_name = "US_EAST_1"
11+
priority = 7
12+
}
13+
}
14+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "mongodbatlas_advanced_cluster" "this" {
2+
project_id = "<YOUR-PROJECT-ID>"
3+
name = "flex-cluster"
4+
cluster_type = "REPLICASET"
5+
6+
replication_specs = [
7+
{
8+
region_configs = [
9+
{
10+
provider_name = "FLEX"
11+
backing_provider_name = "AWS"
12+
region_name = "US_EAST_1"
13+
priority = 7
14+
}
15+
]
16+
}
17+
]
18+
19+
# Updated by atlas-cli-plugin-terraform, please review the changes.
20+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "mongodbatlas_advanced_cluster" "no_replication_specs" {
2+
# missing replication_specs
3+
project_id = var.project_id
4+
name = "cluster-multi-region"
5+
cluster_type = "REPLICASET"
6+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
resource "mongodbatlas_advanced_cluster" "multi_region" {
2+
project_id = var.project_id
3+
name = "cluster-multi-region"
4+
cluster_type = "REPLICASET"
5+
backup_enabled = true
6+
7+
replication_specs {
8+
region_configs {
9+
provider_name = "AWS"
10+
region_name = "US_EAST_1"
11+
priority = 7
12+
electable_specs = {
13+
node_count = 3
14+
instance_size = "M10"
15+
disk_size_gb = 100
16+
}
17+
}
18+
region_configs {
19+
provider_name = "AWS"
20+
region_name = "US_WEST_2"
21+
priority = 6
22+
electable_specs = {
23+
node_count = 3
24+
instance_size = "M10"
25+
disk_size_gb = 100
26+
}
27+
}
28+
region_configs {
29+
provider_name = "AWS"
30+
region_name = "US_WEST_1"
31+
priority = 5
32+
electable_specs = {
33+
node_count = 1
34+
instance_size = "M10"
35+
disk_size_gb = 100
36+
}
37+
}
38+
}
39+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
resource "mongodbatlas_advanced_cluster" "multi_region" {
2+
project_id = var.project_id
3+
name = "cluster-multi-region"
4+
cluster_type = "REPLICASET"
5+
backup_enabled = true
6+
7+
replication_specs = [
8+
{
9+
region_configs = [
10+
{
11+
provider_name = "AWS"
12+
region_name = "US_EAST_1"
13+
priority = 7
14+
electable_specs = {
15+
node_count = 3
16+
instance_size = "M10"
17+
disk_size_gb = 100
18+
}
19+
},
20+
{
21+
provider_name = "AWS"
22+
region_name = "US_WEST_2"
23+
priority = 6
24+
electable_specs = {
25+
node_count = 3
26+
instance_size = "M10"
27+
disk_size_gb = 100
28+
}
29+
},
30+
{
31+
provider_name = "AWS"
32+
region_name = "US_WEST_1"
33+
priority = 5
34+
electable_specs = {
35+
node_count = 1
36+
instance_size = "M10"
37+
disk_size_gb = 100
38+
}
39+
}
40+
]
41+
}
42+
]
43+
44+
# Updated by atlas-cli-plugin-terraform, please review the changes.
45+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
resource "mongodbatlas_advanced_cluster" "multirep" {
2+
project_id = var.project_id
3+
name = "multirep"
4+
cluster_type = "GEOSHARDED"
5+
backup_enabled = false
6+
replication_specs {
7+
zone_name = "Zone 1"
8+
region_configs {
9+
provider_name = "AWS"
10+
region_name = "US_EAST_1"
11+
priority = 7
12+
electable_specs {
13+
node_count = 3
14+
instance_size = "M10"
15+
disk_size_gb = 80
16+
}
17+
}
18+
}
19+
replication_specs {
20+
zone_name = "Zone 2"
21+
region_configs {
22+
provider_name = "AWS"
23+
region_name = "US_WEST_2"
24+
priority = 7
25+
electable_specs {
26+
node_count = 3
27+
instance_size = "M10"
28+
disk_size_gb = 80
29+
}
30+
}
31+
}
32+
}
33+
34+
resource "mongodbatlas_advanced_cluster" "geo" {
35+
project_id = var.project_id
36+
name = "geo"
37+
cluster_type = "GEOSHARDED"
38+
backup_enabled = false
39+
replication_specs {
40+
zone_name = "Zone 1"
41+
region_configs {
42+
provider_name = "AWS"
43+
region_name = "US_EAST_1"
44+
priority = 7
45+
electable_specs {
46+
node_count = 3
47+
instance_size = "M10"
48+
disk_size_gb = 80
49+
}
50+
}
51+
}
52+
replication_specs {
53+
zone_name = "Zone 1"
54+
region_configs {
55+
provider_name = "AWS"
56+
region_name = "US_EAST_1"
57+
priority = 7
58+
electable_specs {
59+
node_count = 3
60+
instance_size = "M10"
61+
disk_size_gb = 80
62+
}
63+
}
64+
}
65+
replication_specs {
66+
zone_name = "Zone 2"
67+
region_configs {
68+
provider_name = "AWS"
69+
region_name = "US_WEST_2"
70+
priority = 7
71+
electable_specs {
72+
node_count = 3
73+
instance_size = "M10"
74+
disk_size_gb = 80
75+
}
76+
}
77+
}
78+
replication_specs {
79+
zone_name = "Zone 2"
80+
region_configs {
81+
provider_name = "AWS"
82+
region_name = "US_WEST_2"
83+
priority = 7
84+
electable_specs {
85+
node_count = 3
86+
instance_size = "M10"
87+
disk_size_gb = 80
88+
}
89+
}
90+
}
91+
replication_specs {
92+
zone_name = "Zone 2"
93+
region_configs {
94+
provider_name = "AWS"
95+
region_name = "US_WEST_2"
96+
priority = 7
97+
electable_specs {
98+
node_count = 3
99+
instance_size = "M10"
100+
disk_size_gb = 80
101+
}
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)