Skip to content

Commit 359ee49

Browse files
committed
basic tags
1 parent 5e717b7 commit 359ee49

File tree

8 files changed

+123
-3
lines changed

8 files changed

+123
-3
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ If you want to overwrite the output file if it exists, or even use the same outp
3636
- The plugin doesn't support `regions_config` without `electable_nodes` as there can be some issues with `priority` when they only have `analytics_nodes` and/or `electable_nodes`.
3737
- `priority` is required in `regions_config` and must be a resolved number between 7 and 1, e.g. `var.priority` is not supported. This is to allow reordering them by descending priority as this is expected in `mongodbatlas_advanced_cluster`.
3838
- `dynamic` blocks to generate `replication_specs`, `regions_config`, etc. are not supported.
39-
39+
- `key` value in `tags` or `labels` must be a resolved string because it will be the key in the map in `mongodb_atlas_advanced_cluster`, e.g. `key = var.tag_key` is not supported.
4040

4141
## Contributing
4242

internal/convert/const_names.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ const (
44
nRepSpecs = "replication_specs"
55
nConfig = "region_configs"
66
nConfigSrc = "regions_config"
7+
nTags = "tags"
8+
nLabels = "labels"
9+
nTimeouts = "timeouts"
710
nElectableSpecs = "electable_specs"
811
nAutoScaling = "auto_scaling"
912
nReadOnlySpecs = "read_only_specs"
@@ -38,4 +41,6 @@ const (
3841
nElectableNodes = "electable_nodes"
3942
nReadOnlyNodes = "read_only_nodes"
4043
nAnalyticsNodes = "analytics_nodes"
44+
nKey = "key"
45+
nValue = "value"
4146
)

internal/convert/convert.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,15 @@ func fillReplicationSpecs(resourceb *hclwrite.Body) error {
115115
}
116116
repSpecs := hclwrite.NewEmptyFile()
117117
repSpecs.Body().SetAttributeRaw(nConfig, configs)
118-
119118
resourceb.SetAttributeRaw(nRepSpecs, hcl.TokensArraySingle(repSpecs))
119+
tags, errTags := getTagsLabelsOpt(nTags, resourceb)
120+
if errTags != nil {
121+
return errTags
122+
}
123+
if tags != nil {
124+
resourceb.SetAttributeRaw(nTags, tags)
125+
}
126+
120127
resourceb.RemoveBlock(repSpecsSrc)
121128
return nil
122129
}
@@ -224,6 +231,36 @@ func getAutoScalingOpt(opt map[string]hclwrite.Tokens) hclwrite.Tokens {
224231
return hcl.TokensObject(file)
225232
}
226233

234+
func getTagsLabelsOpt(attrName string, resourceb *hclwrite.Body) (hclwrite.Tokens, error) {
235+
var (
236+
file = hclwrite.NewEmptyFile()
237+
fileb = file.Body()
238+
found = false
239+
)
240+
for {
241+
block := resourceb.FirstMatchingBlock(attrName, nil)
242+
if block == nil {
243+
break
244+
}
245+
key := block.Body().GetAttribute(nKey)
246+
value := block.Body().GetAttribute(nValue)
247+
if key == nil || value == nil {
248+
return nil, fmt.Errorf("%s: %s or %s not found", attrName, nKey, nValue)
249+
}
250+
keyStr, err := hcl.GetAttrString(key, "unresolved key in "+attrName)
251+
if err != nil {
252+
return nil, err
253+
}
254+
fileb.SetAttributeRaw(keyStr, value.Expr().BuildTokens(nil))
255+
resourceb.RemoveBlock(block)
256+
found = true
257+
}
258+
if !found {
259+
return nil, nil
260+
}
261+
return hcl.TokensObject(file), nil
262+
}
263+
227264
func checkDynamicBlock(body *hclwrite.Body) error {
228265
for _, block := range body.Blocks() {
229266
if block.Type() == "dynamic" {

internal/convert/testdata/clu2adv/errors.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"regions_config_out_of_range_priority": "setting priority: priority is 0 but must be between 1 and 7",
99
"regions_config_unresolved_priority": "setting priority: failed to evaluate number",
1010
"replication_specs_unsupported_dynamic": "dynamic blocks are not supported",
11-
"regions_config_unsupported_dynamic": "dynamic blocks are not supported"
11+
"regions_config_unsupported_dynamic": "dynamic blocks are not supported",
12+
"tags_unresolved_key": "unresolved key in tags"
1213
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
resource "mongodbatlas_cluster" "basictags" {
2+
project_id = var.project_id
3+
name = "basictags"
4+
cluster_type = "REPLICASET"
5+
provider_name = "AWS"
6+
provider_instance_size_name = "M10"
7+
replication_specs {
8+
num_shards = 1
9+
regions_config {
10+
region_name = "US_EAST_1"
11+
electable_nodes = 3
12+
priority = 7
13+
}
14+
}
15+
tags {
16+
key = "environment"
17+
value = "dev"
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
resource "mongodbatlas_advanced_cluster" "basictags" {
2+
project_id = var.project_id
3+
name = "basictags"
4+
cluster_type = "REPLICASET"
5+
replication_specs = [{
6+
region_configs = [{
7+
provider_name = "AWS"
8+
region_name = "US_EAST_1"
9+
priority = 7
10+
electable_specs = {
11+
node_count = 3
12+
instance_size = "M10"
13+
}
14+
}]
15+
}]
16+
tags = {
17+
environment = "dev"
18+
}
19+
20+
# Generated by atlas-cli-plugin-terraform.
21+
# Please confirm that all references to this resource are updated.
22+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
resource "mongodbatlas_cluster" "basictags" {
2+
project_id = var.project_id
3+
name = "basictags"
4+
cluster_type = "REPLICASET"
5+
provider_name = "AWS"
6+
provider_instance_size_name = "M10"
7+
replication_specs {
8+
num_shards = 1
9+
regions_config {
10+
region_name = "US_EAST_1"
11+
electable_nodes = 3
12+
priority = 7
13+
}
14+
}
15+
tags {
16+
# key must be a resolved string because it will be the key in the advanced_cluster map
17+
key = var.env
18+
value = "dev"
19+
}
20+
}

internal/hcl/hcl.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ func GetAttrInt(attr *hclwrite.Attribute, errPrefix string) (int, error) {
5555
return int(num), nil
5656
}
5757

58+
// GetAttrString tries to get an attribute value as a string.
59+
func GetAttrString(attr *hclwrite.Attribute, errPrefix string) (string, error) {
60+
expr, diags := hclsyntax.ParseExpression(attr.Expr().BuildTokens(nil).Bytes(), "", hcl.InitialPos)
61+
if diags.HasErrors() {
62+
return "", fmt.Errorf("%s: failed to parse string: %s", errPrefix, diags.Error())
63+
}
64+
val, diags := expr.Value(nil)
65+
if diags.HasErrors() {
66+
return "", fmt.Errorf("%s: failed to evaluate string: %s", errPrefix, diags.Error())
67+
}
68+
if !val.Type().Equals(cty.String) {
69+
return "", fmt.Errorf("%s: attribute is not a string", errPrefix)
70+
}
71+
return val.AsString(), nil
72+
}
73+
5874
// TokensArray creates an array of objects.
5975
func TokensArray(file []*hclwrite.File) hclwrite.Tokens {
6076
ret := hclwrite.Tokens{

0 commit comments

Comments
 (0)