Skip to content

Commit 36d404e

Browse files
authored
feat: Don't use num_shards and root disk_size_gb in conversion (#65)
1 parent b37f419 commit 36d404e

File tree

8 files changed

+515
-11
lines changed

8 files changed

+515
-11
lines changed

internal/convert/adv2v2.go

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
4040
if hasExpectedBlocksAsAttributes(resourceb) {
4141
return false, nil
4242
}
43-
if err := convertRepSpecs(resourceb); err != nil {
43+
diskSizeGB, _ := hcl.PopAttr(resourceb, nDiskSizeGB, errRoot) // ok to fail as it's optional
44+
if err := convertRepSpecs(resourceb, diskSizeGB); err != nil {
4445
return false, err
4546
}
4647
if err := fillTagsLabelsOpt(resourceb, nTags); err != nil {
@@ -56,18 +57,29 @@ func updateResource(resource *hclwrite.Block) (bool, error) {
5657
return true, nil
5758
}
5859

59-
func convertRepSpecs(resourceb *hclwrite.Body) error {
60+
func convertRepSpecs(resourceb *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
6061
var repSpecs []*hclwrite.Body
6162
for {
6263
block := resourceb.FirstMatchingBlock(nRepSpecs, nil)
6364
if block == nil {
6465
break
6566
}
6667
resourceb.RemoveBlock(block)
67-
if err := convertConfig(block.Body()); err != nil {
68+
blockb := block.Body()
69+
numShardsVal := 1 // default to 1 if num_shards not present
70+
if numShardsAttr := blockb.GetAttribute(nNumShards); numShardsAttr != nil {
71+
var err error
72+
if numShardsVal, err = hcl.GetAttrInt(numShardsAttr, errNumShards); err != nil {
73+
return err
74+
}
75+
blockb.RemoveAttribute(nNumShards)
76+
}
77+
if err := convertConfig(blockb, diskSizeGB); err != nil {
6878
return err
6979
}
70-
repSpecs = append(repSpecs, block.Body())
80+
for range numShardsVal {
81+
repSpecs = append(repSpecs, blockb)
82+
}
7183
}
7284
if len(repSpecs) == 0 {
7385
return fmt.Errorf("must have at least one replication_specs")
@@ -76,7 +88,7 @@ func convertRepSpecs(resourceb *hclwrite.Body) error {
7688
return nil
7789
}
7890

79-
func convertConfig(repSpecs *hclwrite.Body) error {
91+
func convertConfig(repSpecs *hclwrite.Body, diskSizeGB hclwrite.Tokens) error {
8092
var configs []*hclwrite.Body
8193
for {
8294
block := repSpecs.FirstMatchingBlock(nConfig, nil)
@@ -85,11 +97,11 @@ func convertConfig(repSpecs *hclwrite.Body) error {
8597
}
8698
repSpecs.RemoveBlock(block)
8799
blockb := block.Body()
88-
fillBlockOpt(blockb, nElectableSpecs)
89-
fillBlockOpt(blockb, nReadOnlySpecs)
90-
fillBlockOpt(blockb, nAnalyticsSpecs)
91-
fillBlockOpt(blockb, nAutoScaling)
92-
fillBlockOpt(blockb, nAnalyticsAutoScaling)
100+
fillSpecOpt(blockb, nElectableSpecs, diskSizeGB)
101+
fillSpecOpt(blockb, nReadOnlySpecs, diskSizeGB)
102+
fillSpecOpt(blockb, nAnalyticsSpecs, diskSizeGB)
103+
fillSpecOpt(blockb, nAutoScaling, nil) // auto_scaling doesn't need disk_size_gb
104+
fillSpecOpt(blockb, nAnalyticsAutoScaling, nil) // analytics_auto_scaling doesn't need disk_size_gb
93105
configs = append(configs, blockb)
94106
}
95107
if len(configs) == 0 {
@@ -99,6 +111,19 @@ func convertConfig(repSpecs *hclwrite.Body) error {
99111
return nil
100112
}
101113

114+
func fillSpecOpt(resourceb *hclwrite.Body, name string, diskSizeGBTokens hclwrite.Tokens) {
115+
block := resourceb.FirstMatchingBlock(name, nil)
116+
if block == nil {
117+
return
118+
}
119+
if diskSizeGBTokens != nil {
120+
blockb := block.Body()
121+
blockb.RemoveAttribute(nDiskSizeGB)
122+
blockb.SetAttributeRaw(nDiskSizeGB, diskSizeGBTokens)
123+
}
124+
fillBlockOpt(resourceb, name)
125+
}
126+
102127
// hasExpectedBlocksAsAttributes checks if any of the expected block names
103128
// exist as attributes in the resource body. In that case conversion is not done
104129
// as advanced cluster is not in a valid SDKv2 configuration.

internal/convert/const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
errRepSpecs = "setting " + nRepSpecs
1515
errPriority = "setting " + nPriority
1616
errNumShards = "setting " + nNumShards
17+
errRoot = "setting root attributes"
1718

1819
commentGeneratedBy = "Generated by atlas-cli-plugin-terraform."
1920
commentConfirmReferences = "Please review the changes and confirm that references to this resource are updated."
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
resource "mongodbatlas_advanced_cluster" "clu" {
2+
project_id = var.project_id
3+
name = "clu"
4+
cluster_type = "SHARDED"
5+
disk_size_gb = 100
6+
replication_specs {
7+
region_configs {
8+
priority = 7
9+
provider_name = "AWS"
10+
region_name = "US_EAST_1"
11+
electable_specs {
12+
instance_size = "M10"
13+
node_count = 2
14+
}
15+
}
16+
region_configs {
17+
priority = 6
18+
provider_name = "AWS"
19+
region_name = "US_WEST_2"
20+
electable_specs {
21+
instance_size = "M10"
22+
node_count = 1
23+
}
24+
}
25+
}
26+
}
27+
28+
resource "mongodbatlas_advanced_cluster" "clu_var" {
29+
project_id = var.project_id
30+
name = "clu"
31+
cluster_type = "SHARDED"
32+
disk_size_gb = var.disk_size_gb
33+
replication_specs {
34+
region_configs {
35+
priority = 7
36+
provider_name = "AWS"
37+
region_name = "US_EAST_1"
38+
electable_specs {
39+
instance_size = "M10"
40+
node_count = 2
41+
}
42+
}
43+
region_configs {
44+
priority = 6
45+
provider_name = "AWS"
46+
region_name = "US_WEST_2"
47+
electable_specs {
48+
disk_size_gb = 123 # will be ignored and root value will be used instead
49+
instance_size = "M10"
50+
node_count = 1
51+
}
52+
}
53+
}
54+
}
55+
56+
resource "mongodbatlas_advanced_cluster" "clu_keep" {
57+
project_id = var.project_id
58+
name = "clu"
59+
cluster_type = "SHARDED"
60+
replication_specs {
61+
region_configs {
62+
priority = 7
63+
provider_name = "AWS"
64+
region_name = "US_EAST_1"
65+
electable_specs {
66+
instance_size = "M10"
67+
node_count = 2
68+
}
69+
}
70+
region_configs {
71+
priority = 6
72+
provider_name = "AWS"
73+
region_name = "US_WEST_2"
74+
electable_specs {
75+
disk_size_gb = 123 # will be kept as root value is not defined
76+
instance_size = "M10"
77+
node_count = 1
78+
}
79+
}
80+
}
81+
}
82+
83+
resource "mongodbatlas_advanced_cluster" "auto" {
84+
project_id = var.project_id
85+
name = "clu"
86+
cluster_type = "SHARDED"
87+
disk_size_gb = 100
88+
replication_specs {
89+
region_configs {
90+
priority = 7
91+
provider_name = "AWS"
92+
region_name = "US_EAST_1"
93+
electable_specs {
94+
instance_size = "M10"
95+
node_count = 2
96+
}
97+
read_only_specs {
98+
instance_size = "M10"
99+
node_count = 1
100+
}
101+
analytics_specs {
102+
instance_size = "M10"
103+
node_count = 1
104+
}
105+
auto_scaling {
106+
disk_gb_enabled = true # auto_scaling won't get disk_size_gb
107+
}
108+
analytics_auto_scaling {
109+
compute_enabled = true # analytics_auto_scaling won't get disk_size_gb
110+
}
111+
}
112+
}
113+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
resource "mongodbatlas_advanced_cluster" "clu" {
2+
project_id = var.project_id
3+
name = "clu"
4+
cluster_type = "SHARDED"
5+
replication_specs = [
6+
{
7+
region_configs = [
8+
{
9+
priority = 7
10+
provider_name = "AWS"
11+
region_name = "US_EAST_1"
12+
electable_specs = {
13+
instance_size = "M10"
14+
node_count = 2
15+
disk_size_gb = 100
16+
}
17+
},
18+
{
19+
priority = 6
20+
provider_name = "AWS"
21+
region_name = "US_WEST_2"
22+
electable_specs = {
23+
instance_size = "M10"
24+
node_count = 1
25+
disk_size_gb = 100
26+
}
27+
}
28+
]
29+
}
30+
]
31+
32+
# Updated by atlas-cli-plugin-terraform, please review the changes.
33+
}
34+
35+
resource "mongodbatlas_advanced_cluster" "clu_var" {
36+
project_id = var.project_id
37+
name = "clu"
38+
cluster_type = "SHARDED"
39+
replication_specs = [
40+
{
41+
region_configs = [
42+
{
43+
priority = 7
44+
provider_name = "AWS"
45+
region_name = "US_EAST_1"
46+
electable_specs = {
47+
instance_size = "M10"
48+
node_count = 2
49+
disk_size_gb = var.disk_size_gb
50+
}
51+
},
52+
{
53+
priority = 6
54+
provider_name = "AWS"
55+
region_name = "US_WEST_2"
56+
electable_specs = {
57+
instance_size = "M10"
58+
node_count = 1
59+
disk_size_gb = var.disk_size_gb
60+
}
61+
}
62+
]
63+
}
64+
]
65+
66+
# Updated by atlas-cli-plugin-terraform, please review the changes.
67+
}
68+
69+
resource "mongodbatlas_advanced_cluster" "clu_keep" {
70+
project_id = var.project_id
71+
name = "clu"
72+
cluster_type = "SHARDED"
73+
replication_specs = [
74+
{
75+
region_configs = [
76+
{
77+
priority = 7
78+
provider_name = "AWS"
79+
region_name = "US_EAST_1"
80+
electable_specs = {
81+
instance_size = "M10"
82+
node_count = 2
83+
}
84+
},
85+
{
86+
priority = 6
87+
provider_name = "AWS"
88+
region_name = "US_WEST_2"
89+
electable_specs = {
90+
disk_size_gb = 123 # will be kept as root value is not defined
91+
instance_size = "M10"
92+
node_count = 1
93+
}
94+
}
95+
]
96+
}
97+
]
98+
99+
# Updated by atlas-cli-plugin-terraform, please review the changes.
100+
}
101+
102+
resource "mongodbatlas_advanced_cluster" "auto" {
103+
project_id = var.project_id
104+
name = "clu"
105+
cluster_type = "SHARDED"
106+
replication_specs = [
107+
{
108+
region_configs = [
109+
{
110+
priority = 7
111+
provider_name = "AWS"
112+
region_name = "US_EAST_1"
113+
electable_specs = {
114+
instance_size = "M10"
115+
node_count = 2
116+
disk_size_gb = 100
117+
}
118+
read_only_specs = {
119+
instance_size = "M10"
120+
node_count = 1
121+
disk_size_gb = 100
122+
}
123+
analytics_specs = {
124+
instance_size = "M10"
125+
node_count = 1
126+
disk_size_gb = 100
127+
}
128+
auto_scaling = {
129+
disk_gb_enabled = true # auto_scaling won't get disk_size_gb
130+
}
131+
analytics_auto_scaling = {
132+
compute_enabled = true # analytics_auto_scaling won't get disk_size_gb
133+
}
134+
}
135+
]
136+
}
137+
]
138+
139+
# Updated by atlas-cli-plugin-terraform, please review the changes.
140+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"configuration_file_error": "failed to parse Terraform config file",
33
"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"
4+
"missing_replication_specs": "must have at least one replication_specs",
5+
"num_shards_not_numerical": "setting num_shards: failed to evaluate number"
56
}

0 commit comments

Comments
 (0)