-
Notifications
You must be signed in to change notification settings - Fork 1
chore: Refactor to convert
package
#17
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
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package convert | ||
|
||
const ( | ||
nRepSpecs = "replication_specs" | ||
nConfig = "region_configs" | ||
nConfigSrc = "regions_config" | ||
nElectableSpecs = "electable_specs" | ||
nAutoScaling = "auto_scaling" | ||
nRegionNameSrc = "provider_region_name" | ||
nRegionName = "region_name" | ||
nProviderName = "provider_name" | ||
nBackingProviderName = "backing_provider_name" | ||
nInstanceSizeSrc = "provider_instance_size_name" | ||
nInstanceSize = "instance_size" | ||
nClusterType = "cluster_type" | ||
nPriority = "priority" | ||
nNumShards = "num_shards" | ||
nBackupEnabled = "backup_enabled" | ||
nCloudBackup = "cloud_backup" | ||
nDiskSizeGB = "disk_size_gb" | ||
nDiskGBEnabledSrc = "auto_scaling_disk_gb_enabled" | ||
nComputeEnabledSrc = "auto_scaling_compute_enabled" | ||
nComputeScaleDownEnabledSrc = "auto_scaling_compute_scale_down_enabled" | ||
nComputeMinInstanceSizeSrc = "provider_auto_scaling_compute_min_instance_size" | ||
nComputeMaxInstanceSizeSrc = "provider_auto_scaling_compute_max_instance_size" | ||
nDiskGBEnabled = "disk_gb_enabled" | ||
nComputeEnabled = "compute_enabled" | ||
nComputeScaleDownEnabled = "compute_scale_down_enabled" | ||
nComputeMinInstanceSize = "compute_min_instance_size" | ||
nComputeMaxInstanceSize = "compute_max_instance_size" | ||
nNodeCount = "node_count" | ||
nElectableNodes = "electable_nodes" | ||
) |
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,209 @@ | ||
package convert | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/hcl/v2/hclwrite" | ||
"github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl" | ||
"github.com/zclconf/go-cty/cty" | ||
) | ||
|
||
const ( | ||
resourceType = "resource" | ||
cluster = "mongodbatlas_cluster" | ||
advCluster = "mongodbatlas_advanced_cluster" | ||
valClusterType = "REPLICASET" | ||
valPriority = 7 | ||
errFreeCluster = "free cluster (because no " + nRepSpecs + ")" | ||
errRepSpecs = "setting " + nRepSpecs | ||
) | ||
|
||
type attrVals struct { | ||
req map[string]hclwrite.Tokens | ||
opt map[string]hclwrite.Tokens | ||
} | ||
|
||
// ClusterToAdvancedCluster transforms all mongodbatlas_cluster definitions in a | ||
// Terraform configuration file into mongodbatlas_advanced_cluster schema v2 definitions. | ||
// All other resources and data sources are left untouched. | ||
// Note: hclwrite.Tokens are used instead of cty.Value so expressions like var.region can be preserved. | ||
// cty.Value only supports resolved values. | ||
func ClusterToAdvancedCluster(config []byte) ([]byte, error) { | ||
parser, err := hcl.GetParser(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
for _, resource := range parser.Body().Blocks() { | ||
labels := resource.Labels() | ||
resourceName := labels[0] | ||
if resource.Type() != resourceType || resourceName != cluster { | ||
continue | ||
} | ||
resourceb := resource.Body() | ||
labels[0] = advCluster | ||
resource.SetLabels(labels) | ||
|
||
if resourceb.FirstMatchingBlock(nRepSpecs, nil) != nil { | ||
err = fillReplicationSpecs(resourceb) | ||
} else { | ||
err = fillFreeTier(resourceb) | ||
} | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resourceb.AppendNewline() | ||
hcl.AppendComment(resourceb, "Generated by atlas-cli-plugin-terraform.") | ||
hcl.AppendComment(resourceb, "Please confirm that all references to this resource are updated.") | ||
} | ||
return parser.Bytes(), nil | ||
} | ||
|
||
// fillFreeTier is the entry point to convert clusters in free tier | ||
func fillFreeTier(resourceb *hclwrite.Body) error { | ||
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType)) | ||
config := hclwrite.NewEmptyFile() | ||
configb := config.Body() | ||
hcl.SetAttrInt(configb, "priority", valPriority) | ||
if err := hcl.MoveAttr(resourceb, configb, nRegionNameSrc, nRegionName, errFreeCluster); err != nil { | ||
return err | ||
} | ||
if err := hcl.MoveAttr(resourceb, configb, nProviderName, nProviderName, errFreeCluster); err != nil { | ||
return err | ||
} | ||
if err := hcl.MoveAttr(resourceb, configb, nBackingProviderName, nBackingProviderName, errFreeCluster); err != nil { | ||
return err | ||
} | ||
electableSpec := hclwrite.NewEmptyFile() | ||
if err := hcl.MoveAttr(resourceb, electableSpec.Body(), nInstanceSizeSrc, nInstanceSize, errFreeCluster); err != nil { | ||
return err | ||
} | ||
configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpec)) | ||
|
||
repSpecs := hclwrite.NewEmptyFile() | ||
repSpecs.Body().SetAttributeRaw(nConfig, hcl.TokensArrayObject(config)) | ||
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArrayObject(repSpecs)) | ||
return nil | ||
} | ||
|
||
// fillReplicationSpecs is the entry point to convert clusters with replications_specs (all but free tier) | ||
func fillReplicationSpecs(resourceb *hclwrite.Body) error { | ||
root, errRoot := popRootAttrs(resourceb, errRepSpecs) | ||
if errRoot != nil { | ||
return errRoot | ||
} | ||
repSpecsSrc := resourceb.FirstMatchingBlock(nRepSpecs, nil) | ||
configSrc := repSpecsSrc.Body().FirstMatchingBlock(nConfigSrc, nil) | ||
if configSrc == nil { | ||
return fmt.Errorf("%s: %s not found", errRepSpecs, nConfigSrc) | ||
} | ||
|
||
resourceb.RemoveAttribute(nNumShards) // num_shards in root is not relevant, only in replication_specs | ||
// ok to fail as cloud_backup is optional | ||
_ = hcl.MoveAttr(resourceb, resourceb, nCloudBackup, nBackupEnabled, errRepSpecs) | ||
|
||
config, errConfig := getRegionConfigs(configSrc, root) | ||
if errConfig != nil { | ||
return errConfig | ||
} | ||
repSpecs := hclwrite.NewEmptyFile() | ||
repSpecs.Body().SetAttributeRaw(nConfig, config) | ||
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArrayObject(repSpecs)) | ||
|
||
resourceb.RemoveBlock(repSpecsSrc) | ||
return nil | ||
} | ||
|
||
func getRegionConfigs(configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) { | ||
file := hclwrite.NewEmptyFile() | ||
fileb := file.Body() | ||
fileb.SetAttributeRaw(nProviderName, root.req[nProviderName]) | ||
if err := hcl.MoveAttr(configSrc.Body(), fileb, nRegionName, nRegionName, errRepSpecs); err != nil { | ||
return nil, err | ||
} | ||
if err := hcl.MoveAttr(configSrc.Body(), fileb, nPriority, nPriority, errRepSpecs); err != nil { | ||
return nil, err | ||
} | ||
autoScaling := getAutoScalingOpt(root.opt) | ||
if autoScaling != nil { | ||
fileb.SetAttributeRaw(nAutoScaling, autoScaling) | ||
} | ||
electableSpecs, errElect := getElectableSpecs(configSrc, root) | ||
if errElect != nil { | ||
return nil, errElect | ||
} | ||
fileb.SetAttributeRaw(nElectableSpecs, electableSpecs) | ||
return hcl.TokensArrayObject(file), nil | ||
} | ||
|
||
func getElectableSpecs(configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) { | ||
file := hclwrite.NewEmptyFile() | ||
fileb := file.Body() | ||
if err := hcl.MoveAttr(configSrc.Body(), fileb, nElectableNodes, nNodeCount, errRepSpecs); err != nil { | ||
return nil, err | ||
} | ||
fileb.SetAttributeRaw(nInstanceSize, root.req[nInstanceSizeSrc]) | ||
if root.opt[nDiskSizeGB] != nil { | ||
fileb.SetAttributeRaw(nDiskSizeGB, root.opt[nDiskSizeGB]) | ||
} | ||
return hcl.TokensObject(file), nil | ||
} | ||
|
||
func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens { | ||
var ( | ||
names = [][2]string{ // use slice instead of map to preserve order | ||
{nDiskGBEnabledSrc, nDiskGBEnabled}, | ||
{nComputeEnabledSrc, nComputeEnabled}, | ||
{nComputeMinInstanceSizeSrc, nComputeMinInstanceSize}, | ||
{nComputeMaxInstanceSizeSrc, nComputeMaxInstanceSize}, | ||
{nComputeScaleDownEnabledSrc, nComputeScaleDownEnabled}, | ||
} | ||
file = hclwrite.NewEmptyFile() | ||
found = false | ||
) | ||
for _, tuple := range names { | ||
src, dst := tuple[0], tuple[1] | ||
if tokens := opt[src]; tokens != nil { | ||
file.Body().SetAttributeRaw(dst, tokens) | ||
found = true | ||
} | ||
} | ||
if !found { | ||
return nil | ||
} | ||
return hcl.TokensObject(file) | ||
} | ||
|
||
// popRootAttrs deletes the attributes common to all replication_specs/regions_config and returns them. | ||
func popRootAttrs(body *hclwrite.Body, errPrefix string) (attrVals, error) { | ||
var ( | ||
reqNames = []string{ | ||
nProviderName, | ||
nInstanceSizeSrc, | ||
} | ||
optNames = []string{ | ||
nDiskSizeGB, | ||
nDiskGBEnabledSrc, | ||
nComputeEnabledSrc, | ||
nComputeMinInstanceSizeSrc, | ||
nComputeMaxInstanceSizeSrc, | ||
nComputeScaleDownEnabledSrc, | ||
} | ||
req = make(map[string]hclwrite.Tokens) | ||
opt = make(map[string]hclwrite.Tokens) | ||
) | ||
for _, name := range reqNames { | ||
tokens, err := hcl.PopAttr(body, name, errPrefix) | ||
if err != nil { | ||
return attrVals{}, err | ||
} | ||
req[name] = tokens | ||
} | ||
for _, name := range optNames { | ||
tokens, _ := hcl.PopAttr(body, name, errPrefix) | ||
if tokens != nil { | ||
opt[name] = tokens | ||
} | ||
} | ||
return attrVals{req: req, opt: opt}, 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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.
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.
@EspenAlbert I extracted const names to a file, so I left again in the beginning const and types here