11package convert
22
3- import "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl"
3+ import (
4+ "slices"
5+
6+ "github.com/hashicorp/hcl/v2/hclwrite"
7+ "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl"
8+ )
49
510// AdvancedClusterToV2 transforms all mongodbatlas_advanced_cluster resource definitions in a
611// Terraform configuration file from SDKv2 schema to TPF (Terraform Plugin Framework) schema.
@@ -11,5 +16,91 @@ func AdvancedClusterToV2(config []byte) ([]byte, error) {
1116 if err != nil {
1217 return nil , err
1318 }
19+ parserb := parser .Body ()
20+ for _ , block := range parserb .Blocks () {
21+ updated , err := updateResource (block )
22+ if err != nil {
23+ return nil , err
24+ }
25+ if updated { // If the resource was converted, add a comment at the end so user knows the resource was updated
26+ blockb := block .Body ()
27+ blockb .AppendNewline ()
28+ hcl .AppendComment (blockb , commentUpdatedBy )
29+ }
30+ }
1431 return parser .Bytes (), nil
1532}
33+
34+ func updateResource (resource * hclwrite.Block ) (bool , error ) {
35+ if resource .Type () != resourceType || getResourceName (resource ) != advCluster {
36+ return false , nil
37+ }
38+ resourceb := resource .Body ()
39+ if hasExpectedBlocksAsAttributes (resourceb ) {
40+ return false , nil
41+ }
42+ if err := convertRepSpecs (resourceb ); err != nil {
43+ return false , err
44+ }
45+ if err := fillTagsLabelsOpt (resourceb , nTags ); err != nil {
46+ return false , err
47+ }
48+ if err := fillTagsLabelsOpt (resourceb , nLabels ); err != nil {
49+ return false , err
50+ }
51+ fillBlockOpt (resourceb , nAdvConf )
52+ fillBlockOpt (resourceb , nBiConnector )
53+ fillBlockOpt (resourceb , nPinnedFCV )
54+ fillBlockOpt (resourceb , nTimeouts )
55+ return true , nil
56+ }
57+
58+ func convertRepSpecs (resourceb * hclwrite.Body ) error {
59+ block := resourceb .FirstMatchingBlock (nRepSpecs , nil )
60+ if block == nil {
61+ return nil
62+ }
63+ resourceb .RemoveBlock (block )
64+ if err := convertConfig (block .Body ()); err != nil {
65+ return err
66+ }
67+ resourceb .SetAttributeRaw (nRepSpecs , hcl .TokensArraySingle (block .Body ()))
68+ return nil
69+ }
70+
71+ func convertConfig (repSpecs * hclwrite.Body ) error {
72+ block := repSpecs .FirstMatchingBlock (nConfig , nil )
73+ if block == nil {
74+ return nil
75+ }
76+ repSpecs .RemoveBlock (block )
77+ blockb := block .Body ()
78+ fillBlockOpt (blockb , nElectableSpecs )
79+ fillBlockOpt (blockb , nReadOnlySpecs )
80+ fillBlockOpt (blockb , nAnalyticsSpecs )
81+ fillBlockOpt (blockb , nAutoScaling )
82+ fillBlockOpt (blockb , nAnalyticsAutoScaling )
83+ repSpecs .SetAttributeRaw (nConfig , hcl .TokensArraySingle (blockb ))
84+ return nil
85+ }
86+
87+ // hasExpectedBlocksAsAttributes checks if any of the expected block names
88+ // exist as attributes in the resource body. In that case conversion is not done
89+ // as advanced cluster is not in a valid SDKv2 configuration.
90+ func hasExpectedBlocksAsAttributes (resourceb * hclwrite.Body ) bool {
91+ expectedBlocks := []string {
92+ nRepSpecs ,
93+ nTags ,
94+ nLabels ,
95+ nAdvConf ,
96+ nBiConnector ,
97+ nPinnedFCV ,
98+ nTimeouts ,
99+ }
100+ for name := range resourceb .Attributes () {
101+ if slices .Contains (expectedBlocks , name ) {
102+ return true
103+ }
104+ }
105+ return false
106+ }
0 commit comments