-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Convert from blocks to nested attributes in advancedClusterToV2 command #61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c0f48cb
basic test (at the moment will fail)
lantoli 49707e1
remove pointer to BaseOpts
lantoli a6f248c
pass basic test
lantoli c5af100
fix e2e test
lantoli ef45574
move tags, labels and timeouts tests
lantoli 00ee8a2
fix test
lantoli ffff1c8
advanced_configuration, bi_connector and pinned_fvc tests
lantoli 4454973
remove unused code
lantoli a4fb134
convert specs
lantoli e025276
Update internal/convert/adv2v2.go
lantoli b1b51ab
fix copilot suggestion
lantoli 65534ef
source tests
lantoli e5fa697
update comment
lantoli f22bb80
typo
lantoli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
package convert | ||
|
||
import "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl" | ||
import ( | ||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl" | ||
) | ||
|
||
// AdvancedClusterToV2 transforms all mongodbatlas_advanced_cluster resource definitions in a | ||
// Terraform configuration file from SDKv2 schema to TPF (Terraform Plugin Framework) schema. | ||
|
@@ -11,5 +14,68 @@ func AdvancedClusterToV2(config []byte) ([]byte, error) { | |
if err != nil { | ||
return nil, err | ||
} | ||
parserb := parser.Body() | ||
for _, block := range parserb.Blocks() { | ||
updated, err := updateResource(block) | ||
if err != nil { | ||
return nil, | ||
err | ||
} | ||
if updated { | ||
|
||
blockb := block.Body() | ||
blockb.AppendNewline() | ||
hcl.AppendComment(blockb, commentUpdatedBy) | ||
} | ||
} | ||
return parser.Bytes(), nil | ||
} | ||
|
||
func updateResource(resource *hclwrite.Block) (bool, error) { | ||
if resource.Type() != resourceType || getResourceName(resource) != advCluster { | ||
return false, nil | ||
} | ||
resourceb := resource.Body() | ||
if err := convertRepSpecs(resourceb); err != nil { | ||
return false, err | ||
} | ||
if err := fillTagsLabelsOpt(resourceb, nTags); err != nil { | ||
return false, err | ||
} | ||
if err := fillTagsLabelsOpt(resourceb, nLabels); err != nil { | ||
return false, err | ||
} | ||
fillBlockOpt(resourceb, nAdvConf) | ||
fillBlockOpt(resourceb, nBiConnector) | ||
fillBlockOpt(resourceb, nPinnedFCV) | ||
fillBlockOpt(resourceb, nTimeouts) | ||
return true, nil | ||
} | ||
|
||
func convertRepSpecs(resourceb *hclwrite.Body) error { | ||
block := resourceb.FirstMatchingBlock(nRepSpecs, nil) | ||
if block == nil { | ||
return nil | ||
} | ||
resourceb.RemoveBlock(block) | ||
if err := convertConfig(block.Body()); err != nil { | ||
return err | ||
} | ||
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(block.Body())) | ||
return nil | ||
} | ||
|
||
func convertConfig(repSpecs *hclwrite.Body) error { | ||
block := repSpecs.FirstMatchingBlock(nConfig, nil) | ||
if block == nil { | ||
return nil | ||
} | ||
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)) | ||
return nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
internal/convert/testdata/adv2v2/adv_config_bi_connector_pinned_fcv.in.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
resource "mongodbatlas_advanced_cluster" "this" { | ||
project_id = var.project_id | ||
name = var.cluster_name | ||
cluster_type = "REPLICASET" | ||
replication_specs { | ||
region_configs { | ||
priority = 7 | ||
provider_name = "AWS" | ||
region_name = "EU_WEST_1" | ||
electable_specs { | ||
instance_size = "M10" | ||
node_count = 3 | ||
} | ||
} | ||
} | ||
|
||
advanced_configuration { | ||
# comments in advanced_configuration are kept | ||
javascript_enabled = true | ||
} | ||
|
||
bi_connector_config { | ||
# comments in bi_connector_config are kept | ||
enabled = true | ||
read_preference = "secondary" | ||
} | ||
|
||
pinned_fcv { | ||
# comments in pinned_fcv are kept | ||
expiration_date = var.fcv_date | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
internal/convert/testdata/adv2v2/adv_config_bi_connector_pinned_fcv.out.tf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
resource "mongodbatlas_advanced_cluster" "this" { | ||
project_id = var.project_id | ||
name = var.cluster_name | ||
cluster_type = "REPLICASET" | ||
|
||
|
||
|
||
replication_specs = [ | ||
{ | ||
region_configs = [ | ||
{ | ||
priority = 7 | ||
provider_name = "AWS" | ||
region_name = "EU_WEST_1" | ||
electable_specs = { | ||
instance_size = "M10" | ||
node_count = 3 | ||
} | ||
} | ||
] | ||
} | ||
] | ||
advanced_configuration = { | ||
# comments in advanced_configuration are kept | ||
javascript_enabled = true | ||
} | ||
bi_connector_config = { | ||
# comments in bi_connector_config are kept | ||
enabled = true | ||
read_preference = "secondary" | ||
} | ||
pinned_fcv = { | ||
# comments in pinned_fcv are kept | ||
expiration_date = var.fcv_date | ||
} | ||
|
||
# Updated by atlas-cli-plugin-terraform, please review the changes. | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,16 @@ | ||
resource "mongodbatlas_advanced_cluster" "basic" { | ||
name = "basic" | ||
// TODO: missing fields as transformation is not implemented yet | ||
resource "mongodbatlas_advanced_cluster" "clu" { | ||
project_id = var.project_id | ||
name = "clu" | ||
cluster_type = "REPLICASET" | ||
replication_specs { | ||
region_configs { | ||
priority = 7 | ||
provider_name = "AWS" | ||
region_name = "EU_WEST_1" | ||
electable_specs { | ||
instance_size = "M10" | ||
node_count = 3 | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
resource "mongodbatlas_advanced_cluster" "basic" { | ||
name = "basic" | ||
// TODO: missing fields as transformation is not implemented yet | ||
resource "mongodbatlas_advanced_cluster" "clu" { | ||
project_id = var.project_id | ||
name = "clu" | ||
cluster_type = "REPLICASET" | ||
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. | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
resource "mongodbatlas_advanced_cluster" "all" { | ||
project_id = var.project_id | ||
name = "all" | ||
cluster_type = "REPLICASET" | ||
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 = 90 | ||
ebs_volume_type = "PROVISIONED" | ||
disk_iops = 100 | ||
} | ||
read_only_specs { | ||
node_count = 1 | ||
instance_size = "M10" | ||
disk_size_gb = 90 | ||
ebs_volume_type = "PROVISIONED" | ||
disk_iops = 100 | ||
} | ||
analytics_specs { | ||
node_count = 2 | ||
instance_size = "M10" | ||
disk_size_gb = 90 | ||
ebs_volume_type = "PROVISIONED" | ||
disk_iops = 100 | ||
} | ||
auto_scaling { | ||
disk_gb_enabled = true | ||
compute_enabled = false | ||
compute_min_instance_size = "M10" | ||
compute_max_instance_size = "M40" | ||
compute_scale_down_enabled = local.scale_down | ||
} | ||
analytics_auto_scaling { | ||
disk_gb_enabled = false | ||
compute_enabled = true | ||
compute_min_instance_size = "M20" | ||
compute_max_instance_size = "M30" | ||
compute_scale_down_enabled = local.analytics_scale_down | ||
} | ||
} | ||
} | ||
} | ||
|
||
resource "mongodbatlas_advanced_cluster" "min" { | ||
project_id = var.project_id | ||
name = "min" | ||
cluster_type = "REPLICASET" | ||
replication_specs { | ||
region_configs { | ||
provider_name = "AWS" | ||
region_name = "US_EAST_1" | ||
priority = 7 | ||
electable_specs { | ||
node_count = 3 | ||
instance_size = "M10" | ||
} | ||
read_only_specs { | ||
node_count = 1 | ||
instance_size = "M10" | ||
} | ||
analytics_specs { | ||
node_count = 2 | ||
instance_size = "M10" | ||
} | ||
auto_scaling { | ||
disk_gb_enabled = true | ||
} | ||
analytics_auto_scaling { | ||
compute_enabled = true | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.