Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 70 additions & 32 deletions internal/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@ import (
)

const (
resourceType = "resource"
cluster = "mongodbatlas_cluster"
advCluster = "mongodbatlas_advanced_cluster"
valClusterType = "REPLICASET"
valMaxPriority = 7
valMinPriority = 1
errFreeCluster = "free cluster (because no " + nRepSpecs + ")"
errRepSpecs = "setting " + nRepSpecs
errConfigs = "setting " + nConfig
errPriority = "setting " + nPriority
errNumShards = "setting " + nNumShards
resourceType = "resource"
dataSourceType = "data"
cluster = "mongodbatlas_cluster"
advCluster = "mongodbatlas_advanced_cluster"
clusterPlural = "mongodbatlas_clusters"
advClusterPlural = "mongodbatlas_advanced_clusters"
valClusterType = "REPLICASET"
valMaxPriority = 7
valMinPriority = 1
errFreeCluster = "free cluster (because no " + nRepSpecs + ")"
errRepSpecs = "setting " + nRepSpecs
errConfigs = "setting " + nConfig
errPriority = "setting " + nPriority
errNumShards = "setting " + nNumShards
)

type attrVals struct {
Expand All @@ -43,34 +46,59 @@ func ClusterToAdvancedCluster(config []byte) ([]byte, error) {
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()
if errDyn := checkDynamicBlock(resourceb); errDyn != nil {
return nil, errDyn
}
labels[0] = advCluster
resource.SetLabels(labels)

if resourceb.FirstMatchingBlock(nRepSpecs, nil) != nil {
err = fillCluster(resourceb)
} else {
err = fillFreeTierCluster(resourceb)
}
converted, err := convertResource(resource)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the resource variable name confused me here, this would be a block which can be a resource or data source right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, changed here: b1d2a2b

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.")
converted = converted || convertDataSource(resource)
if converted {
resourceb := resource.Body()
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
}

func convertResource(resource *hclwrite.Block) (bool, error) {
if resource.Type() != resourceType || getResourceName(resource) != cluster {
return false, nil
}
setResourceName(resource, advCluster)
resourceb := resource.Body()
if errDyn := checkDynamicBlock(resourceb); errDyn != nil {
return false, errDyn
}

var err error
if resourceb.FirstMatchingBlock(nRepSpecs, nil) != nil {
err = fillCluster(resourceb)
} else {
err = fillFreeTierCluster(resourceb)
}
if err != nil {
return false, err
}
return true, nil
}

func convertDataSource(resource *hclwrite.Block) bool {
if resource.Type() != dataSourceType {
return false
}
switch getResourceName(resource) {
case cluster:
setResourceName(resource, advCluster)
return true
case clusterPlural:
setResourceName(resource, advClusterPlural)
return true
default:
return false
}
}

// fillFreeTierCluster is the entry point to convert clusters in free tier
func fillFreeTierCluster(resourceb *hclwrite.Body) error {
resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType))
Expand Down Expand Up @@ -301,6 +329,16 @@ func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {
return hcl.TokensObject(fileb)
}

func setResourceName(resource *hclwrite.Block, name string) {
labels := resource.Labels()
labels[0] = name
resource.SetLabels(labels)
}

func getResourceName(resource *hclwrite.Block) string {
return resource.Labels()[0]
}

func checkDynamicBlock(body *hclwrite.Body) error {
for _, block := range body.Blocks() {
if block.Type() == "dynamic" {
Expand Down
22 changes: 22 additions & 0 deletions internal/convert/testdata/clu2adv/data_sources.in.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
data "mongodbatlas_cluster" "singular" {
# data source content is kep, only data source type is changed - singular
project_id = mongodbatlas_advanced_cluster.example.project_id
name = mongodbatlas_advanced_cluster.example.name
depends_on = [mongodbatlas_privatelink_endpoint_service.example_endpoint]
}

data "mongodbatlas_clusters" "plural" {
# data source content is kep, only data source type is changed - plural
project_id = mongodbatlas_advanced_cluster.example.project_id
}

data "mongodbatlas_advanced_cluster" "adv_singular" {
# adv_cluster is not changed - adv_singular
project_id = mongodbatlas_advanced_cluster.example.project_id
name = mongodbatlas_advanced_cluster.example.name
}

data "mongodbatlas_advanced_cluster" "adv_plural" {
# adv_cluster is not changed - adv_plural
project_id = mongodbatlas_advanced_cluster.example.project_id
}
28 changes: 28 additions & 0 deletions internal/convert/testdata/clu2adv/data_sources.out.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
data "mongodbatlas_advanced_cluster" "singular" {
# data source content is kep, only data source type is changed - singular
project_id = mongodbatlas_advanced_cluster.example.project_id
name = mongodbatlas_advanced_cluster.example.name
depends_on = [mongodbatlas_privatelink_endpoint_service.example_endpoint]

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we include use_replication_spec_per_shard = true? Might be the most consistent output given we generated advanced_cluster resources with single replication spec per shard (no use of num_shards).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes we should because eventually in 2.0 use_replication_spec_per_shard = true becomes the standard, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, added this into the v2 release doc as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great point, added here: 9297c5f


data "mongodbatlas_advanced_clusters" "plural" {
# data source content is kep, only data source type is changed - plural
project_id = mongodbatlas_advanced_cluster.example.project_id

# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
}

data "mongodbatlas_advanced_cluster" "adv_singular" {
# adv_cluster is not changed - adv_singular
project_id = mongodbatlas_advanced_cluster.example.project_id
name = mongodbatlas_advanced_cluster.example.name
}

data "mongodbatlas_advanced_cluster" "adv_plural" {
# adv_cluster is not changed - adv_plural
project_id = mongodbatlas_advanced_cluster.example.project_id
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ resource "resource1" "res1" {

resource "mongodbatlas_cluster" "free_cluster" { # comment in the resource
# comment in own line in the beginning
count = local.use_free_cluster ? 1 : 0
project_id = var.project_id # inline comment kept
name = var.cluster_name
count = local.use_free_cluster ? 1 : 0
project_id = var.project_id # inline comment kept
name = var.cluster_name
# comment in own line in the middle is deleted
provider_name = "TENANT" # inline comment for attribute moved is not kept
backing_provider_name = "AWS"
provider_region_name = var.region
provider_instance_size_name = "M0"
# comment in own line at the end happens before replication_specs
}

data "mongodbatlas_cluster" "cluster2" {
name = "name4"
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,3 @@ resource "mongodbatlas_advanced_cluster" "free_cluster" { # comment in the resou
# Generated by atlas-cli-plugin-terraform.
# Please confirm that all references to this resource are updated.
}

data "mongodbatlas_cluster" "cluster2" {
name = "name4"
}