-
Notifications
You must be signed in to change notification settings - Fork 1
feat: Adds optionally moved blocks #26
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 all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
edec959
remove adv2 command
lantoli dc4e4f2
e2e tests
lantoli 109ad0b
flags file
lantoli 1312a94
includeMoved
lantoli cb80821
atlas cli setup
lantoli 4416495
add e2e tests
lantoli ea3491e
fix linter
lantoli d0617be
add moved e2e test
lantoli c0552ec
extract comments to const
lantoli a3b1635
note to remind delete old clusters
lantoli f873ad5
typo
lantoli 2e83a2d
func comments for getResourceName and getResourceLabel
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
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
This file was deleted.
Oops, something went wrong.
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
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 |
---|---|---|
|
@@ -23,11 +23,17 @@ const ( | |
valClusterType = "REPLICASET" | ||
valMaxPriority = 7 | ||
valMinPriority = 1 | ||
errFreeCluster = "free cluster (because no " + nRepSpecs + ")" | ||
errRepSpecs = "setting " + nRepSpecs | ||
errConfigs = "setting " + nConfig | ||
errPriority = "setting " + nPriority | ||
errNumShards = "setting " + nNumShards | ||
|
||
errFreeCluster = "free cluster (because no " + nRepSpecs + ")" | ||
errRepSpecs = "setting " + nRepSpecs | ||
errConfigs = "setting " + nConfig | ||
errPriority = "setting " + nPriority | ||
errNumShards = "setting " + nNumShards | ||
|
||
commentGeneratedBy = "Generated by atlas-cli-plugin-terraform." | ||
commentConfirmReferences = "Please confirm that all references to this resource are updated." | ||
commentMovedBlock = "Moved blocks" | ||
commentRemovedOld = "Note: Remember to remove or comment out the old cluster definitions." | ||
) | ||
|
||
type attrVals struct { | ||
|
@@ -40,24 +46,33 @@ type attrVals struct { | |
// All other resources and data sources are left untouched. | ||
// Note: hclwrite.Tokens are used instead of cty.Value so expressions with interpolations like var.region can be preserved. | ||
// cty.Value only supports literal expressions. | ||
func ClusterToAdvancedCluster(config []byte) ([]byte, error) { | ||
func ClusterToAdvancedCluster(config []byte, includeMoved bool) ([]byte, error) { | ||
var moveLabels []string | ||
parser, err := hcl.GetParser(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, block := range parser.Body().Blocks() { | ||
converted, err := convertResource(block) | ||
parserb := parser.Body() | ||
for _, block := range parserb.Blocks() { | ||
convertedResource, err := convertResource(block) | ||
if err != nil { | ||
return nil, err | ||
return nil, | ||
err | ||
} | ||
converted = converted || convertDataSource(block) | ||
if converted { | ||
if includeMoved && convertedResource { | ||
if moveLabel := getResourceLabel(block); moveLabel != "" { | ||
moveLabels = append(moveLabels, moveLabel) | ||
} | ||
} | ||
convertedDataSource := convertDataSource(block) | ||
if convertedResource || convertedDataSource { | ||
blockb := block.Body() | ||
blockb.AppendNewline() | ||
hcl.AppendComment(blockb, "Generated by atlas-cli-plugin-terraform.") | ||
hcl.AppendComment(blockb, "Please confirm that all references to this resource are updated.") | ||
hcl.AppendComment(blockb, commentGeneratedBy) | ||
hcl.AppendComment(blockb, commentConfirmReferences) | ||
} | ||
} | ||
fillMovedBlocks(parserb, moveLabels) | ||
return parser.Bytes(), nil | ||
} | ||
|
||
|
@@ -99,6 +114,25 @@ func convertDataSource(block *hclwrite.Block) bool { | |
return false | ||
} | ||
|
||
func fillMovedBlocks(body *hclwrite.Body, moveLabels []string) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
if len(moveLabels) == 0 { | ||
return | ||
} | ||
body.AppendNewline() | ||
hcl.AppendComment(body, commentMovedBlock) | ||
hcl.AppendComment(body, commentRemovedOld) | ||
body.AppendNewline() | ||
for i, moveLabel := range moveLabels { | ||
block := body.AppendNewBlock(nMoved, nil) | ||
blockb := block.Body() | ||
hcl.SetAttrExpr(blockb, nFrom, fmt.Sprintf("%s.%s", cluster, moveLabel)) | ||
hcl.SetAttrExpr(blockb, nTo, fmt.Sprintf("%s.%s", advCluster, moveLabel)) | ||
if i < len(moveLabels)-1 { | ||
body.AppendNewline() | ||
} | ||
} | ||
} | ||
|
||
// fillFreeTierCluster is the entry point to convert clusters in free tier | ||
func fillFreeTierCluster(resourceb *hclwrite.Body) error { | ||
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType)) | ||
|
@@ -338,6 +372,8 @@ func setResourceName(resource *hclwrite.Block, name string) { | |
resource.SetLabels(labels) | ||
} | ||
|
||
// getResourceName returns the first label of a block, if it exists. | ||
// e.g. in resource "mongodbatlas_cluster" "mycluster", the first label is "mongodbatlas_cluster". | ||
func getResourceName(resource *hclwrite.Block) string { | ||
labels := resource.Labels() | ||
if len(labels) == 0 { | ||
|
@@ -346,6 +382,16 @@ func getResourceName(resource *hclwrite.Block) string { | |
return labels[0] | ||
} | ||
|
||
// getResourceLabel returns the second label of a block, if it exists. | ||
// e.g. in resource "mongodbatlas_cluster" "mycluster", the second label is "mycluster". | ||
func getResourceLabel(resource *hclwrite.Block) string { | ||
labels := resource.Labels() | ||
if len(labels) <= 1 { | ||
return "" | ||
} | ||
return labels[1] | ||
oarbusi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func checkDynamicBlock(body *hclwrite.Body) error { | ||
for _, block := range body.Blocks() { | ||
if block.Type() == "dynamic" { | ||
|
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
71 changes: 71 additions & 0 deletions
71
internal/convert/testdata/clu2adv/includeMoved_multiple.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,71 @@ | ||
resource "mongodbatlas_cluster" "cluster1" { | ||
project_id = var.project_id | ||
name = "clu1" | ||
cluster_type = "REPLICASET" | ||
provider_name = "AWS" | ||
provider_instance_size_name = "M10" | ||
replication_specs { | ||
num_shards = 1 | ||
regions_config { | ||
region_name = "US_EAST_1" | ||
electable_nodes = 3 | ||
priority = 7 | ||
} | ||
} | ||
} | ||
|
||
resource "mongodbatlas_cluster" "cluster2" { | ||
project_id = var.project_id | ||
name = "clu2" | ||
cluster_type = "REPLICASET" | ||
provider_name = "AWS" | ||
provider_instance_size_name = "M30" | ||
replication_specs { | ||
num_shards = 1 | ||
regions_config { | ||
region_name = "US_WEST_2" | ||
electable_nodes = 3 | ||
priority = 7 | ||
} | ||
} | ||
} | ||
|
||
resource "mongodbatlas_cluster" "count" { | ||
# count doesn't affect moved blocks, it works in the same way | ||
count = local.create_cluster ? 1 : 0 | ||
project_id = var.project_id | ||
name = "count" | ||
cluster_type = "REPLICASET" | ||
provider_name = "AWS" | ||
provider_instance_size_name = "M30" | ||
replication_specs { | ||
num_shards = 1 | ||
regions_config { | ||
region_name = "US_WEST_2" | ||
electable_nodes = 3 | ||
priority = 7 | ||
} | ||
} | ||
} | ||
|
||
resource "mongodbatlas_cluster" "forEach" { | ||
# for_each doesn't affect moved blocks, it works in the same way | ||
for_each = toset(["clu1", "clu2", "clu3"]) | ||
project_id = var.project_id | ||
name = each.key | ||
cluster_type = "REPLICASET" | ||
provider_name = "AWS" | ||
provider_instance_size_name = "M30" | ||
replication_specs { | ||
num_shards = 1 | ||
regions_config { | ||
region_name = "US_WEST_2" | ||
electable_nodes = 3 | ||
priority = 7 | ||
} | ||
} | ||
} | ||
|
||
resource "another_resource" "another" { | ||
hello = "there" | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove skeleton for adv_cluster v1 to v2 conversion to avoid customer confusion as it won't be implemented yet