|
| 1 | +package convert |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + "github.com/hashicorp/hcl/v2/hclwrite" |
| 7 | + "github.com/mongodb-labs/atlas-cli-plugin-terraform/internal/hcl" |
| 8 | + "github.com/zclconf/go-cty/cty" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + resourceType = "resource" |
| 13 | + cluster = "mongodbatlas_cluster" |
| 14 | + advCluster = "mongodbatlas_advanced_cluster" |
| 15 | + valClusterType = "REPLICASET" |
| 16 | + valPriority = 7 |
| 17 | + errFreeCluster = "free cluster (because no " + nRepSpecs + ")" |
| 18 | + errRepSpecs = "setting " + nRepSpecs |
| 19 | +) |
| 20 | + |
| 21 | +type attrVals struct { |
| 22 | + req map[string]hclwrite.Tokens |
| 23 | + opt map[string]hclwrite.Tokens |
| 24 | +} |
| 25 | + |
| 26 | +// ClusterToAdvancedCluster transforms all mongodbatlas_cluster definitions in a |
| 27 | +// Terraform configuration file into mongodbatlas_advanced_cluster schema v2 definitions. |
| 28 | +// All other resources and data sources are left untouched. |
| 29 | +// Note: hclwrite.Tokens are used instead of cty.Value so expressions like var.region can be preserved. |
| 30 | +// cty.Value only supports resolved values. |
| 31 | +func ClusterToAdvancedCluster(config []byte) ([]byte, error) { |
| 32 | + parser, err := hcl.GetParser(config) |
| 33 | + if err != nil { |
| 34 | + return nil, err |
| 35 | + } |
| 36 | + for _, resource := range parser.Body().Blocks() { |
| 37 | + labels := resource.Labels() |
| 38 | + resourceName := labels[0] |
| 39 | + if resource.Type() != resourceType || resourceName != cluster { |
| 40 | + continue |
| 41 | + } |
| 42 | + resourceb := resource.Body() |
| 43 | + labels[0] = advCluster |
| 44 | + resource.SetLabels(labels) |
| 45 | + |
| 46 | + if resourceb.FirstMatchingBlock(nRepSpecs, nil) != nil { |
| 47 | + err = fillReplicationSpecs(resourceb) |
| 48 | + } else { |
| 49 | + err = fillFreeTier(resourceb) |
| 50 | + } |
| 51 | + if err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + |
| 55 | + resourceb.AppendNewline() |
| 56 | + hcl.AppendComment(resourceb, "Generated by atlas-cli-plugin-terraform.") |
| 57 | + hcl.AppendComment(resourceb, "Please confirm that all references to this resource are updated.") |
| 58 | + } |
| 59 | + return parser.Bytes(), nil |
| 60 | +} |
| 61 | + |
| 62 | +// fillFreeTier is the entry point to convert clusters in free tier |
| 63 | +func fillFreeTier(resourceb *hclwrite.Body) error { |
| 64 | + resourceb.SetAttributeValue(nClusterType, cty.StringVal(valClusterType)) |
| 65 | + config := hclwrite.NewEmptyFile() |
| 66 | + configb := config.Body() |
| 67 | + hcl.SetAttrInt(configb, "priority", valPriority) |
| 68 | + if err := hcl.MoveAttr(resourceb, configb, nRegionNameSrc, nRegionName, errFreeCluster); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + if err := hcl.MoveAttr(resourceb, configb, nProviderName, nProviderName, errFreeCluster); err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + if err := hcl.MoveAttr(resourceb, configb, nBackingProviderName, nBackingProviderName, errFreeCluster); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + electableSpec := hclwrite.NewEmptyFile() |
| 78 | + if err := hcl.MoveAttr(resourceb, electableSpec.Body(), nInstanceSizeSrc, nInstanceSize, errFreeCluster); err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + configb.SetAttributeRaw(nElectableSpecs, hcl.TokensObject(electableSpec)) |
| 82 | + |
| 83 | + repSpecs := hclwrite.NewEmptyFile() |
| 84 | + repSpecs.Body().SetAttributeRaw(nConfig, hcl.TokensArrayObject(config)) |
| 85 | + resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArrayObject(repSpecs)) |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +// fillReplicationSpecs is the entry point to convert clusters with replications_specs (all but free tier) |
| 90 | +func fillReplicationSpecs(resourceb *hclwrite.Body) error { |
| 91 | + root, errRoot := popRootAttrs(resourceb, errRepSpecs) |
| 92 | + if errRoot != nil { |
| 93 | + return errRoot |
| 94 | + } |
| 95 | + repSpecsSrc := resourceb.FirstMatchingBlock(nRepSpecs, nil) |
| 96 | + configSrc := repSpecsSrc.Body().FirstMatchingBlock(nConfigSrc, nil) |
| 97 | + if configSrc == nil { |
| 98 | + return fmt.Errorf("%s: %s not found", errRepSpecs, nConfigSrc) |
| 99 | + } |
| 100 | + |
| 101 | + resourceb.RemoveAttribute(nNumShards) // num_shards in root is not relevant, only in replication_specs |
| 102 | + // ok to fail as cloud_backup is optional |
| 103 | + _ = hcl.MoveAttr(resourceb, resourceb, nCloudBackup, nBackupEnabled, errRepSpecs) |
| 104 | + |
| 105 | + config, errConfig := getRegionConfigs(configSrc, root) |
| 106 | + if errConfig != nil { |
| 107 | + return errConfig |
| 108 | + } |
| 109 | + repSpecs := hclwrite.NewEmptyFile() |
| 110 | + repSpecs.Body().SetAttributeRaw(nConfig, config) |
| 111 | + resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArrayObject(repSpecs)) |
| 112 | + |
| 113 | + resourceb.RemoveBlock(repSpecsSrc) |
| 114 | + return nil |
| 115 | +} |
| 116 | + |
| 117 | +func getRegionConfigs(configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) { |
| 118 | + file := hclwrite.NewEmptyFile() |
| 119 | + fileb := file.Body() |
| 120 | + fileb.SetAttributeRaw(nProviderName, root.req[nProviderName]) |
| 121 | + if err := hcl.MoveAttr(configSrc.Body(), fileb, nRegionName, nRegionName, errRepSpecs); err != nil { |
| 122 | + return nil, err |
| 123 | + } |
| 124 | + if err := hcl.MoveAttr(configSrc.Body(), fileb, nPriority, nPriority, errRepSpecs); err != nil { |
| 125 | + return nil, err |
| 126 | + } |
| 127 | + autoScaling := getAutoScalingOpt(root.opt) |
| 128 | + if autoScaling != nil { |
| 129 | + fileb.SetAttributeRaw(nAutoScaling, autoScaling) |
| 130 | + } |
| 131 | + electableSpecs, errElect := getElectableSpecs(configSrc, root) |
| 132 | + if errElect != nil { |
| 133 | + return nil, errElect |
| 134 | + } |
| 135 | + fileb.SetAttributeRaw(nElectableSpecs, electableSpecs) |
| 136 | + return hcl.TokensArrayObject(file), nil |
| 137 | +} |
| 138 | + |
| 139 | +func getElectableSpecs(configSrc *hclwrite.Block, root attrVals) (hclwrite.Tokens, error) { |
| 140 | + file := hclwrite.NewEmptyFile() |
| 141 | + fileb := file.Body() |
| 142 | + if err := hcl.MoveAttr(configSrc.Body(), fileb, nElectableNodes, nNodeCount, errRepSpecs); err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + fileb.SetAttributeRaw(nInstanceSize, root.req[nInstanceSizeSrc]) |
| 146 | + if root.opt[nDiskSizeGB] != nil { |
| 147 | + fileb.SetAttributeRaw(nDiskSizeGB, root.opt[nDiskSizeGB]) |
| 148 | + } |
| 149 | + return hcl.TokensObject(file), nil |
| 150 | +} |
| 151 | + |
| 152 | +func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens { |
| 153 | + var ( |
| 154 | + names = [][2]string{ // use slice instead of map to preserve order |
| 155 | + {nDiskGBEnabledSrc, nDiskGBEnabled}, |
| 156 | + {nComputeEnabledSrc, nComputeEnabled}, |
| 157 | + {nComputeMinInstanceSizeSrc, nComputeMinInstanceSize}, |
| 158 | + {nComputeMaxInstanceSizeSrc, nComputeMaxInstanceSize}, |
| 159 | + {nComputeScaleDownEnabledSrc, nComputeScaleDownEnabled}, |
| 160 | + } |
| 161 | + file = hclwrite.NewEmptyFile() |
| 162 | + found = false |
| 163 | + ) |
| 164 | + for _, tuple := range names { |
| 165 | + src, dst := tuple[0], tuple[1] |
| 166 | + if tokens := opt[src]; tokens != nil { |
| 167 | + file.Body().SetAttributeRaw(dst, tokens) |
| 168 | + found = true |
| 169 | + } |
| 170 | + } |
| 171 | + if !found { |
| 172 | + return nil |
| 173 | + } |
| 174 | + return hcl.TokensObject(file) |
| 175 | +} |
| 176 | + |
| 177 | +// popRootAttrs deletes the attributes common to all replication_specs/regions_config and returns them. |
| 178 | +func popRootAttrs(body *hclwrite.Body, errPrefix string) (attrVals, error) { |
| 179 | + var ( |
| 180 | + reqNames = []string{ |
| 181 | + nProviderName, |
| 182 | + nInstanceSizeSrc, |
| 183 | + } |
| 184 | + optNames = []string{ |
| 185 | + nDiskSizeGB, |
| 186 | + nDiskGBEnabledSrc, |
| 187 | + nComputeEnabledSrc, |
| 188 | + nComputeMinInstanceSizeSrc, |
| 189 | + nComputeMaxInstanceSizeSrc, |
| 190 | + nComputeScaleDownEnabledSrc, |
| 191 | + } |
| 192 | + req = make(map[string]hclwrite.Tokens) |
| 193 | + opt = make(map[string]hclwrite.Tokens) |
| 194 | + ) |
| 195 | + for _, name := range reqNames { |
| 196 | + tokens, err := hcl.PopAttr(body, name, errPrefix) |
| 197 | + if err != nil { |
| 198 | + return attrVals{}, err |
| 199 | + } |
| 200 | + req[name] = tokens |
| 201 | + } |
| 202 | + for _, name := range optNames { |
| 203 | + tokens, _ := hcl.PopAttr(body, name, errPrefix) |
| 204 | + if tokens != nil { |
| 205 | + opt[name] = tokens |
| 206 | + } |
| 207 | + } |
| 208 | + return attrVals{req: req, opt: opt}, nil |
| 209 | +} |
0 commit comments