From 39b7b5c015ea906bf75539b511c373fef4129013 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Wed, 13 Jan 2021 17:09:20 +0200 Subject: [PATCH 01/10] adding test case for tf11 --- .../aws_tags_merge/expected/main.terratag.tf | 15 +++++++++++++++ .../aws_tags_merge/expected/main.tf.bak | 10 ++++++++++ .../terraform_11/aws_tags_merge/input/main.tf | 10 ++++++++++ 3 files changed, 35 insertions(+) create mode 100644 test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf create mode 100644 test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak create mode 100644 test/fixture/terraform_11/aws_tags_merge/input/main.tf diff --git a/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf b/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf new file mode 100644 index 0000000..3e873a8 --- /dev/null +++ b/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf @@ -0,0 +1,15 @@ +provider "aws" { + version = "~> 2.0" + region = "us-east-1" +} + +resource "aws_s3_bucket" "a" { + bucket = "my-another-tf-test-bucket" + + tags = "${merge( "${merge(map("a", "b"), map("c", "d"))}", local.terratag_added_main)}" +} + +locals { + terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} +} + diff --git a/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak b/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak new file mode 100644 index 0000000..c798a92 --- /dev/null +++ b/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak @@ -0,0 +1,10 @@ +provider "aws" { + version = "~> 2.0" + region = "us-east-1" +} + +resource "aws_s3_bucket" "a" { + bucket = "my-another-tf-test-bucket" + + tags = "${merge(map("a", "b"), map("c", "d"))}" +} diff --git a/test/fixture/terraform_11/aws_tags_merge/input/main.tf b/test/fixture/terraform_11/aws_tags_merge/input/main.tf new file mode 100644 index 0000000..c798a92 --- /dev/null +++ b/test/fixture/terraform_11/aws_tags_merge/input/main.tf @@ -0,0 +1,10 @@ +provider "aws" { + version = "~> 2.0" + region = "us-east-1" +} + +resource "aws_s3_bucket" "a" { + bucket = "my-another-tf-test-bucket" + + tags = "${merge(map("a", "b"), map("c", "d"))}" +} From 3363ef7ebedc46dbd234dd8da3174d58ec68547f Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 10:23:12 +0200 Subject: [PATCH 02/10] got the basics working --- tagging/tagging.go | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tagging/tagging.go b/tagging/tagging.go index 40abd78..9033e39 100644 --- a/tagging/tagging.go +++ b/tagging/tagging.go @@ -5,7 +5,8 @@ import ( "github.com/env0/terratag/tag_keys" "github.com/env0/terratag/terraform" "github.com/hashicorp/hcl/v2/hclwrite" - "github.com/zclconf/go-cty/cty" + "github.com/hashicorp/hcl/v2" + "log" ) func defaultTaggingFn(args TagBlockArgs) Result { @@ -14,23 +15,36 @@ func defaultTaggingFn(args TagBlockArgs) Result { } } +func ParseHclValueStringToTokens(hclValueString string) hclwrite.Tokens { + file, diags := hclwrite.ParseConfig([]byte("tempKey = " + hclValueString), "", hcl.Pos{Line: 1, Column: 1}) + if diags.HasErrors() { + log.Print("error parsing hcl value string " + hclValueString) + panic(diags.Errs()[0]) + } + tempAttribute := file.Body().GetAttribute("tempKey") + return tempAttribute.Expr().BuildTokens(hclwrite.Tokens{}) +} + func TagBlock(args TagBlockArgs) string { hasExistingTags := convert.MoveExistingTags(args.Filename, args.Terratag, args.Block, args.TagId) + + terratagAddedKey := "local." + tag_keys.GetTerratagAddedKey(args.Filename) + newTagsValue := terratagAddedKey - tagsValue := "" if hasExistingTags { - tagsValue = "merge( " + convert.GetExistingTagsExpression(args.Terratag.Found[tag_keys.GetResourceExistingTagsKey(args.Filename, args.Block)]) + ", local." + tag_keys.GetTerratagAddedKey(args.Filename) + ")" - } else { - tagsValue = "local." + tag_keys.GetTerratagAddedKey(args.Filename) + existingTagsKey := tag_keys.GetResourceExistingTagsKey(args.Filename, args.Block) + existingTagsExpression := convert.GetExistingTagsExpression(args.Terratag.Found[existingTagsKey]) + newTagsValue = "merge( " + existingTagsExpression + ", " + terratagAddedKey + ")" } if args.TfVersion == 11 { - tagsValue = "${" + tagsValue + "}" + newTagsValue = "${" + newTagsValue + "}" } - args.Block.Body().SetAttributeValue(args.TagId, cty.StringVal(tagsValue)) + newTagsValueTokens := ParseHclValueStringToTokens(newTagsValue) + args.Block.Body().SetAttributeRaw(args.TagId, newTagsValueTokens) - return tagsValue + return newTagsValue } func HasResourceTagFn(resourceType string) bool { From 3929877ad1278b5bb570a061a810f2224a2c18d1 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 10:23:28 +0200 Subject: [PATCH 03/10] test case --- .../aws_tags_merge/expected/main.terratag.tf | 32 +++++++++++++++++++ .../aws_tags_merge/expected/main.tf.bak | 27 ++++++++++++++++ .../terraform_12/aws_tags_merge/input/main.tf | 27 ++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 test/fixture/terraform_12/aws_tags_merge/expected/main.terratag.tf create mode 100644 test/fixture/terraform_12/aws_tags_merge/expected/main.tf.bak create mode 100644 test/fixture/terraform_12/aws_tags_merge/input/main.tf diff --git a/test/fixture/terraform_12/aws_tags_merge/expected/main.terratag.tf b/test/fixture/terraform_12/aws_tags_merge/expected/main.terratag.tf new file mode 100644 index 0000000..1615a22 --- /dev/null +++ b/test/fixture/terraform_12/aws_tags_merge/expected/main.terratag.tf @@ -0,0 +1,32 @@ +provider "aws" { + version = "~> 2.0" + region = "us-east-1" +} + +variable "name" { + type = string + default = "" +} + +variable "tags" { + type = map(string) + default = {} +} + +resource "aws_s3_bucket" "example" { + bucket = "example" + acl = "public-read" + force_destroy = true + + tags = merge(merge( + { + "Name" = format("%s", var.name) + }, + var.tags + ), local.terratag_added_main) +} + +locals { + terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} +} + diff --git a/test/fixture/terraform_12/aws_tags_merge/expected/main.tf.bak b/test/fixture/terraform_12/aws_tags_merge/expected/main.tf.bak new file mode 100644 index 0000000..34b2f93 --- /dev/null +++ b/test/fixture/terraform_12/aws_tags_merge/expected/main.tf.bak @@ -0,0 +1,27 @@ +provider "aws" { + version = "~> 2.0" + region = "us-east-1" +} + +variable "name" { + type = string + default = "" +} + +variable "tags" { + type = map(string) + default = {} +} + +resource "aws_s3_bucket" "example" { + bucket = "example" + acl = "public-read" + force_destroy = true + + tags = merge( + { + "Name" = format("%s", var.name) + }, + var.tags + ) +} diff --git a/test/fixture/terraform_12/aws_tags_merge/input/main.tf b/test/fixture/terraform_12/aws_tags_merge/input/main.tf new file mode 100644 index 0000000..34b2f93 --- /dev/null +++ b/test/fixture/terraform_12/aws_tags_merge/input/main.tf @@ -0,0 +1,27 @@ +provider "aws" { + version = "~> 2.0" + region = "us-east-1" +} + +variable "name" { + type = string + default = "" +} + +variable "tags" { + type = map(string) + default = {} +} + +resource "aws_s3_bucket" "example" { + bucket = "example" + acl = "public-read" + force_destroy = true + + tags = merge( + { + "Name" = format("%s", var.name) + }, + var.tags + ) +} From 4b930d0cd134715c3d3a3cc90eaaa0a81ea24dc8 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 11:09:50 +0200 Subject: [PATCH 04/10] putting the tokens option up there --- tagging/tagging.go | 66 ++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/tagging/tagging.go b/tagging/tagging.go index 9033e39..2d4742c 100644 --- a/tagging/tagging.go +++ b/tagging/tagging.go @@ -6,6 +6,7 @@ import ( "github.com/env0/terratag/terraform" "github.com/hashicorp/hcl/v2/hclwrite" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" "log" ) @@ -41,8 +42,69 @@ func TagBlock(args TagBlockArgs) string { newTagsValue = "${" + newTagsValue + "}" } - newTagsValueTokens := ParseHclValueStringToTokens(newTagsValue) - args.Block.Body().SetAttributeRaw(args.TagId, newTagsValueTokens) + mergeCommand := &hclwrite.Token{ + Type: hclsyntax.TokenIdent, + Bytes: []byte("merge"), + SpacesBefore: 0, + } + + localPrefix := &hclwrite.Token{ + Type: hclsyntax.TokenIdent, + Bytes: []byte("local"), + SpacesBefore: 0, + } + + dotToken := &hclwrite.Token{ + Type: hclsyntax.TokenDot, + Bytes: []byte("."), + SpacesBefore: 0, + } + + terraTagLocal := &hclwrite.Token{ + Type: hclsyntax.TokenIdent, + Bytes: []byte(tag_keys.GetTerratagAddedKey(args.Filename)), + SpacesBefore: 0, + } + + comma := &hclwrite.Token{ + Type: hclsyntax.TokenComma, + Bytes: []byte(","), + SpacesBefore: 0, + } + + openParen := &hclwrite.Token{ + Type: hclsyntax.TokenOParen, + Bytes: []byte("("), + SpacesBefore: 0, + } + + newLine := &hclwrite.Token{ + Type: hclsyntax.TokenNewline, + Bytes: []byte(""), + SpacesBefore: 0, + } + + closeParen := &hclwrite.Token{ + Type: hclsyntax.TokenCParen, + Bytes: []byte(")"), + SpacesBefore: 0, + } + + existingTags := args.Block.Body().GetAttribute(args.TagId) + existingTagsTokens := existingTags.Expr().BuildTokens(hclwrite.Tokens{}) + + var newTokens hclwrite.Tokens + newTokens = append(newTokens, mergeCommand, openParen, newLine) + newTokens = append(newTokens, existingTagsTokens...) + newTokens = append(newTokens, newLine, comma, localPrefix, dotToken, terraTagLocal, closeParen) + + for _, token := range existingTagsTokens { + log.Print(token.Type) + log.Print(string(token.Bytes)) + } + + // newTagsValueTokens := ParseHclValueStringToTokens(newTagsValue) + args.Block.Body().SetAttributeRaw(args.TagId, newTokens) return newTagsValue } From 6f031673fcd72ee10dd8faa114afb63fc204491d Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 14:04:08 +0200 Subject: [PATCH 05/10] Revert "putting the tokens option up there" This reverts commit 4b930d0cd134715c3d3a3cc90eaaa0a81ea24dc8. --- tagging/tagging.go | 66 ++-------------------------------------------- 1 file changed, 2 insertions(+), 64 deletions(-) diff --git a/tagging/tagging.go b/tagging/tagging.go index 2d4742c..9033e39 100644 --- a/tagging/tagging.go +++ b/tagging/tagging.go @@ -6,7 +6,6 @@ import ( "github.com/env0/terratag/terraform" "github.com/hashicorp/hcl/v2/hclwrite" "github.com/hashicorp/hcl/v2" - "github.com/hashicorp/hcl/v2/hclsyntax" "log" ) @@ -42,69 +41,8 @@ func TagBlock(args TagBlockArgs) string { newTagsValue = "${" + newTagsValue + "}" } - mergeCommand := &hclwrite.Token{ - Type: hclsyntax.TokenIdent, - Bytes: []byte("merge"), - SpacesBefore: 0, - } - - localPrefix := &hclwrite.Token{ - Type: hclsyntax.TokenIdent, - Bytes: []byte("local"), - SpacesBefore: 0, - } - - dotToken := &hclwrite.Token{ - Type: hclsyntax.TokenDot, - Bytes: []byte("."), - SpacesBefore: 0, - } - - terraTagLocal := &hclwrite.Token{ - Type: hclsyntax.TokenIdent, - Bytes: []byte(tag_keys.GetTerratagAddedKey(args.Filename)), - SpacesBefore: 0, - } - - comma := &hclwrite.Token{ - Type: hclsyntax.TokenComma, - Bytes: []byte(","), - SpacesBefore: 0, - } - - openParen := &hclwrite.Token{ - Type: hclsyntax.TokenOParen, - Bytes: []byte("("), - SpacesBefore: 0, - } - - newLine := &hclwrite.Token{ - Type: hclsyntax.TokenNewline, - Bytes: []byte(""), - SpacesBefore: 0, - } - - closeParen := &hclwrite.Token{ - Type: hclsyntax.TokenCParen, - Bytes: []byte(")"), - SpacesBefore: 0, - } - - existingTags := args.Block.Body().GetAttribute(args.TagId) - existingTagsTokens := existingTags.Expr().BuildTokens(hclwrite.Tokens{}) - - var newTokens hclwrite.Tokens - newTokens = append(newTokens, mergeCommand, openParen, newLine) - newTokens = append(newTokens, existingTagsTokens...) - newTokens = append(newTokens, newLine, comma, localPrefix, dotToken, terraTagLocal, closeParen) - - for _, token := range existingTagsTokens { - log.Print(token.Type) - log.Print(string(token.Bytes)) - } - - // newTagsValueTokens := ParseHclValueStringToTokens(newTagsValue) - args.Block.Body().SetAttributeRaw(args.TagId, newTokens) + newTagsValueTokens := ParseHclValueStringToTokens(newTagsValue) + args.Block.Body().SetAttributeRaw(args.TagId, newTagsValueTokens) return newTagsValue } From 7d472ae4490d742a9d8b5306feddad20da299dd5 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 14:29:44 +0200 Subject: [PATCH 06/10] fixing expected for tf12 --- .../aws_submodule/expected/child/child.terratag.tf | 2 +- .../terraform_12/aws_tags_block/expected/main.terratag.tf | 2 +- .../terraform_12/aws_tags_map/expected/main.terratag.tf | 4 ++-- .../azurerm_kubernetes_cluster/expected/main.terratag.tf | 4 ++-- .../terraform_12/azurerm_tags_map/expected/main.terratag.tf | 2 +- .../azurestack_tags_map/expected/main.terratag.tf | 2 +- .../google_container_cluster/expected/main.terratag.tf | 2 +- .../terraform_12/google_labels_map/expected/main.terratag.tf | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/fixture/terraform_12/aws_submodule/expected/child/child.terratag.tf b/test/fixture/terraform_12/aws_submodule/expected/child/child.terratag.tf index f963791..a0b0136 100644 --- a/test/fixture/terraform_12/aws_submodule/expected/child/child.terratag.tf +++ b/test/fixture/terraform_12/aws_submodule/expected/child/child.terratag.tf @@ -2,7 +2,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket","Environment","Dev"), local.terratag_added_child) + tags = merge(map("Name", "Mybucket", "Environment", "Dev"), local.terratag_added_child) } locals { terratag_added_child = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_12/aws_tags_block/expected/main.terratag.tf b/test/fixture/terraform_12/aws_tags_block/expected/main.terratag.tf index 92967d7..7199e32 100644 --- a/test/fixture/terraform_12/aws_tags_block/expected/main.terratag.tf +++ b/test/fixture/terraform_12/aws_tags_block/expected/main.terratag.tf @@ -7,7 +7,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket"), local.terratag_added_main) + tags = merge(map("Name", "Mybucket"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_12/aws_tags_map/expected/main.terratag.tf b/test/fixture/terraform_12/aws_tags_map/expected/main.terratag.tf index 047ca44..f7a96c1 100644 --- a/test/fixture/terraform_12/aws_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_12/aws_tags_map/expected/main.terratag.tf @@ -7,14 +7,14 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket","Unquoted1","Iwannabequoted","AnotherName","Yo","Unquoted2","Ireallywannabequoted","Unquoted3","IreallyreallywannabequotedandIgotacomma",join("-",["foo","bar"]),"Testfunction",(local.localTagKey),"Testexpression","${local.localTagKey2}","Testvariableaskey","Yo-${local.localTagKey}","Testvariableinsidekey","Testvariableasvalue","${local.localTagKey}","Testvariableinsidevalue","Yo-${local.localTagKey}"), local.terratag_added_main) + tags = merge(map("Name", "Mybucket", "Unquoted1", "Iwannabequoted", "AnotherName", "Yo", "Unquoted2", "Ireallywannabequoted", "Unquoted3", "IreallyreallywannabequotedandIgotacomma", join("-", ["foo", "bar"]), "Testfunction", (local.localTagKey), "Testexpression", "${local.localTagKey2}", "Testvariableaskey", "Yo-${local.localTagKey}", "Testvariableinsidekey", "Testvariableasvalue", "${local.localTagKey}", "Testvariableinsidevalue", "Yo-${local.localTagKey}"), local.terratag_added_main) } resource "aws_s3_bucket" "a" { bucket = "my-another-tf-test-bucket" acl = "private" - tags = merge( local.localMap, local.terratag_added_main) + tags = merge(local.localMap, local.terratag_added_main) } locals { diff --git a/test/fixture/terraform_12/azurerm_kubernetes_cluster/expected/main.terratag.tf b/test/fixture/terraform_12/azurerm_kubernetes_cluster/expected/main.terratag.tf index 86071eb..259b49b 100644 --- a/test/fixture/terraform_12/azurerm_kubernetes_cluster/expected/main.terratag.tf +++ b/test/fixture/terraform_12/azurerm_kubernetes_cluster/expected/main.terratag.tf @@ -56,7 +56,7 @@ resource "azurerm_kubernetes_cluster" "existing_tags_cluster" { name = "pool" node_count = 2 vm_size = "Standard_D2_v2" - tags = merge( map("existing","tag"), local.terratag_added_main) + tags = merge(map("existing", "tag"), local.terratag_added_main) } network_profile { @@ -64,7 +64,7 @@ resource "azurerm_kubernetes_cluster" "existing_tags_cluster" { network_plugin = "kubenet" } - tags = merge( map("existing","tag"), local.terratag_added_main) + tags = merge(map("existing", "tag"), local.terratag_added_main) } locals { diff --git a/test/fixture/terraform_12/azurerm_tags_map/expected/main.terratag.tf b/test/fixture/terraform_12/azurerm_tags_map/expected/main.terratag.tf index 046f56e..804e1f5 100644 --- a/test/fixture/terraform_12/azurerm_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_12/azurerm_tags_map/expected/main.terratag.tf @@ -5,7 +5,7 @@ provider "azurerm" { resource "azurerm_resource_group" "example" { name = "example-resources" location = "West Europe" - tags = merge( map("oh","my"), local.terratag_added_main) + tags = merge(map("oh", "my"), local.terratag_added_main) } resource "azurerm_virtual_network" "example" { diff --git a/test/fixture/terraform_12/azurestack_tags_map/expected/main.terratag.tf b/test/fixture/terraform_12/azurestack_tags_map/expected/main.terratag.tf index a94489f..dfdcbe7 100644 --- a/test/fixture/terraform_12/azurestack_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_12/azurestack_tags_map/expected/main.terratag.tf @@ -21,7 +21,7 @@ resource "azurestack_virtual_network" "test2" { address_space = ["10.0.0.0/16"] location = azurestack_resource_group.test.location resource_group_name = azurestack_resource_group.test.name - tags = merge( map("yo","ho"), local.terratag_added_main) + tags = merge(map("yo", "ho"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_12/google_container_cluster/expected/main.terratag.tf b/test/fixture/terraform_12/google_container_cluster/expected/main.terratag.tf index 0a1a63a..912cdd0 100644 --- a/test/fixture/terraform_12/google_container_cluster/expected/main.terratag.tf +++ b/test/fixture/terraform_12/google_container_cluster/expected/main.terratag.tf @@ -26,7 +26,7 @@ resource "google_container_cluster" "existing-labels-cluster" { name = "cluster2" location = "us-central1" - resource_labels = merge( map("foo","bar"), local.terratag_added_main) + resource_labels = merge(map("foo", "bar"), local.terratag_added_main) node_config { machine_type = "n1-standard-1" diff --git a/test/fixture/terraform_12/google_labels_map/expected/main.terratag.tf b/test/fixture/terraform_12/google_labels_map/expected/main.terratag.tf index 039691f..607f498 100644 --- a/test/fixture/terraform_12/google_labels_map/expected/main.terratag.tf +++ b/test/fixture/terraform_12/google_labels_map/expected/main.terratag.tf @@ -15,7 +15,7 @@ resource "google_storage_bucket" "static-site" { response_header = ["*"] max_age_seconds = 3600 } - labels = merge( map("foo","bar"), local.terratag_added_main) + labels = merge(map("foo", "bar"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} From 251b0383fb42f18970f5b28a71109a1b89235109 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 14:53:28 +0200 Subject: [PATCH 07/10] fixing tf11 tests --- .../aws_submodule/expected/child/child.terratag.tf | 2 +- .../terraform_11/aws_tags_block/expected/main.terratag.tf | 2 +- .../terraform_11/aws_tags_map/expected/main.terratag.tf | 4 ++-- .../terraform_11/aws_tags_merge/expected/main.terratag.tf | 5 ++++- test/fixture/terraform_11/aws_tags_merge/input/main.tf | 5 ++++- .../azurerm_kubernetes_cluster/expected/main.terratag.tf | 4 ++-- .../terraform_11/azurerm_tags_map/expected/main.terratag.tf | 2 +- .../azurestack_tags_map/expected/main.terratag.tf | 2 +- .../google_container_cluster/expected/main.terratag.tf | 2 +- .../terraform_11/google_labels_map/expected/main.terratag.tf | 2 +- 10 files changed, 18 insertions(+), 12 deletions(-) diff --git a/test/fixture/terraform_11/aws_submodule/expected/child/child.terratag.tf b/test/fixture/terraform_11/aws_submodule/expected/child/child.terratag.tf index 9d1d779..d9014a1 100644 --- a/test/fixture/terraform_11/aws_submodule/expected/child/child.terratag.tf +++ b/test/fixture/terraform_11/aws_submodule/expected/child/child.terratag.tf @@ -2,7 +2,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = "${merge( map("Name","Mybucket","Environment","Dev"), local.terratag_added_child)}" + tags = "${merge(map("Name", "Mybucket", "Environment", "Dev"), local.terratag_added_child)}" } locals { terratag_added_child = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_11/aws_tags_block/expected/main.terratag.tf b/test/fixture/terraform_11/aws_tags_block/expected/main.terratag.tf index 0715abb..a5e6177 100644 --- a/test/fixture/terraform_11/aws_tags_block/expected/main.terratag.tf +++ b/test/fixture/terraform_11/aws_tags_block/expected/main.terratag.tf @@ -7,7 +7,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = "${merge( map("Name","Mybucket"), local.terratag_added_main)}" + tags = "${merge(map("Name", "Mybucket"), local.terratag_added_main)}" } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_11/aws_tags_map/expected/main.terratag.tf b/test/fixture/terraform_11/aws_tags_map/expected/main.terratag.tf index 5a35b9a..0cfbf32 100644 --- a/test/fixture/terraform_11/aws_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_11/aws_tags_map/expected/main.terratag.tf @@ -7,14 +7,14 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = "${merge( map("Name","Mybucket","Unquoted1","Iwannabequoted","AnotherName","Yo","Unquoted2","Ireallywannabequoted","Unquoted3","IreallyreallywannabequotedandIgotacomma","${max(1,2)}","Testfunction","${local.localTagKey}","Testexpression","${local.localTagKey2}","Testvariableaskey","Yo-${local.localTagKey}","Testvariableinsidekey","Testvariableasvalue","${local.localTagKey}","Testvariableinsidevalue","Yo-${local.localTagKey}"), local.terratag_added_main)}" + tags = "${merge(map("Name", "Mybucket", "Unquoted1", "Iwannabequoted", "AnotherName", "Yo", "Unquoted2", "Ireallywannabequoted", "Unquoted3", "IreallyreallywannabequotedandIgotacomma", "${max(1, 2)}", "Testfunction", "${local.localTagKey}", "Testexpression", "${local.localTagKey2}", "Testvariableaskey", "Yo-${local.localTagKey}", "Testvariableinsidekey", "Testvariableasvalue", "${local.localTagKey}", "Testvariableinsidevalue", "Yo-${local.localTagKey}"), local.terratag_added_main)}" } resource "aws_s3_bucket" "a" { bucket = "my-another-tf-test-bucket" acl = "private" - tags = "${merge( "${local.localMap}", local.terratag_added_main)}" + tags = "${merge("${local.localMap}", local.terratag_added_main)}" } diff --git a/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf b/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf index 3e873a8..bf78099 100644 --- a/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf +++ b/test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf @@ -6,7 +6,10 @@ provider "aws" { resource "aws_s3_bucket" "a" { bucket = "my-another-tf-test-bucket" - tags = "${merge( "${merge(map("a", "b"), map("c", "d"))}", local.terratag_added_main)}" + tags = "${merge("${merge( + map("a", "b"), + map("c", "d") + )}", local.terratag_added_main)}" } locals { diff --git a/test/fixture/terraform_11/aws_tags_merge/input/main.tf b/test/fixture/terraform_11/aws_tags_merge/input/main.tf index c798a92..494d59b 100644 --- a/test/fixture/terraform_11/aws_tags_merge/input/main.tf +++ b/test/fixture/terraform_11/aws_tags_merge/input/main.tf @@ -6,5 +6,8 @@ provider "aws" { resource "aws_s3_bucket" "a" { bucket = "my-another-tf-test-bucket" - tags = "${merge(map("a", "b"), map("c", "d"))}" + tags = "${merge( + map("a", "b"), + map("c", "d") + )}" } diff --git a/test/fixture/terraform_11/azurerm_kubernetes_cluster/expected/main.terratag.tf b/test/fixture/terraform_11/azurerm_kubernetes_cluster/expected/main.terratag.tf index 04c570d..2de4b97 100644 --- a/test/fixture/terraform_11/azurerm_kubernetes_cluster/expected/main.terratag.tf +++ b/test/fixture/terraform_11/azurerm_kubernetes_cluster/expected/main.terratag.tf @@ -56,7 +56,7 @@ resource "azurerm_kubernetes_cluster" "existing_tags_cluster" { name = "pool" node_count = 2 vm_size = "Standard_D2_v2" - tags = "${merge( map("existing","tag"), local.terratag_added_main)}" + tags = "${merge(map("existing", "tag"), local.terratag_added_main)}" } network_profile { @@ -64,7 +64,7 @@ resource "azurerm_kubernetes_cluster" "existing_tags_cluster" { network_plugin = "kubenet" } - tags = "${merge( map("existing","tag"), local.terratag_added_main)}" + tags = "${merge(map("existing", "tag"), local.terratag_added_main)}" } locals { diff --git a/test/fixture/terraform_11/azurerm_tags_map/expected/main.terratag.tf b/test/fixture/terraform_11/azurerm_tags_map/expected/main.terratag.tf index 982cc54..c00e0c8 100644 --- a/test/fixture/terraform_11/azurerm_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_11/azurerm_tags_map/expected/main.terratag.tf @@ -5,7 +5,7 @@ provider "azurerm" { resource "azurerm_resource_group" "example" { name = "example-resources" location = "West Europe" - tags = "${merge( map("oh","my"), local.terratag_added_main)}" + tags = "${merge(map("oh", "my"), local.terratag_added_main)}" } resource "azurerm_virtual_network" "example" { diff --git a/test/fixture/terraform_11/azurestack_tags_map/expected/main.terratag.tf b/test/fixture/terraform_11/azurestack_tags_map/expected/main.terratag.tf index a28b0cf..f149783 100644 --- a/test/fixture/terraform_11/azurestack_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_11/azurestack_tags_map/expected/main.terratag.tf @@ -21,7 +21,7 @@ resource "azurestack_virtual_network" "test2" { address_space = ["10.0.0.0/16"] location = "${azurestack_resource_group.test.location}" resource_group_name = "${azurestack_resource_group.test.name}" - tags = "${merge( map("yo","ho"), local.terratag_added_main)}" + tags = "${merge(map("yo", "ho"), local.terratag_added_main)}" } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_11/google_container_cluster/expected/main.terratag.tf b/test/fixture/terraform_11/google_container_cluster/expected/main.terratag.tf index 4c3089d..dc05a69 100644 --- a/test/fixture/terraform_11/google_container_cluster/expected/main.terratag.tf +++ b/test/fixture/terraform_11/google_container_cluster/expected/main.terratag.tf @@ -26,7 +26,7 @@ resource "google_container_cluster" "existing-labels-cluster" { name = "cluster2" location = "us-central1" - resource_labels = "${merge( map("foo","bar"), local.terratag_added_main)}" + resource_labels = "${merge(map("foo", "bar"), local.terratag_added_main)}" node_config { machine_type = "n1-standard-1" diff --git a/test/fixture/terraform_11/google_labels_map/expected/main.terratag.tf b/test/fixture/terraform_11/google_labels_map/expected/main.terratag.tf index 9f47c35..d59a94e 100644 --- a/test/fixture/terraform_11/google_labels_map/expected/main.terratag.tf +++ b/test/fixture/terraform_11/google_labels_map/expected/main.terratag.tf @@ -15,7 +15,7 @@ resource "google_storage_bucket" "static-site" { response_header = ["*"] max_age_seconds = 3600 } - labels = "${merge( map("foo","bar"), local.terratag_added_main)}" + labels = "${merge(map("foo", "bar"), local.terratag_added_main)}" } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} From 46f4bf9802ae626b541c7fd82dd33fd4c935e2df Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 14:53:51 +0200 Subject: [PATCH 08/10] adding quotes for tf 11 --- tagging/tagging.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tagging/tagging.go b/tagging/tagging.go index 9033e39..8e897a5 100644 --- a/tagging/tagging.go +++ b/tagging/tagging.go @@ -38,7 +38,7 @@ func TagBlock(args TagBlockArgs) string { } if args.TfVersion == 11 { - newTagsValue = "${" + newTagsValue + "}" + newTagsValue = "\"${" + newTagsValue + "}\"" } newTagsValueTokens := ParseHclValueStringToTokens(newTagsValue) From 9c50d165fbac152911ca57c0fa023a68c0940e3a Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 15:31:09 +0200 Subject: [PATCH 09/10] fixing tf13_14 tests --- .../aws_submodule/expected/child/child.terratag.tf | 2 +- .../terraform_13_14/aws_tags_block/expected/main.terratag.tf | 2 +- .../terraform_13_14/aws_tags_map/expected/main.terratag.tf | 4 ++-- .../azurerm_kubernetes_cluster/expected/main.terratag.tf | 4 ++-- .../azurerm_tags_map/expected/main.terratag.tf | 2 +- .../azurestack_tags_map/expected/main.terratag.tf | 2 +- .../expected/main.terratag.tf | 2 +- .../expected/main.terratag.tf | 2 +- .../google_container_cluster/expected/main.terratag.tf | 2 +- .../google_labels_map/expected/main.terratag.tf | 2 +- 10 files changed, 12 insertions(+), 12 deletions(-) diff --git a/test/fixture/terraform_13_14/aws_submodule/expected/child/child.terratag.tf b/test/fixture/terraform_13_14/aws_submodule/expected/child/child.terratag.tf index f963791..a0b0136 100644 --- a/test/fixture/terraform_13_14/aws_submodule/expected/child/child.terratag.tf +++ b/test/fixture/terraform_13_14/aws_submodule/expected/child/child.terratag.tf @@ -2,7 +2,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket","Environment","Dev"), local.terratag_added_child) + tags = merge(map("Name", "Mybucket", "Environment", "Dev"), local.terratag_added_child) } locals { terratag_added_child = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_13_14/aws_tags_block/expected/main.terratag.tf b/test/fixture/terraform_13_14/aws_tags_block/expected/main.terratag.tf index 49a9592..f591a80 100644 --- a/test/fixture/terraform_13_14/aws_tags_block/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/aws_tags_block/expected/main.terratag.tf @@ -15,7 +15,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket"), local.terratag_added_main) + tags = merge(map("Name", "Mybucket"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_13_14/aws_tags_map/expected/main.terratag.tf b/test/fixture/terraform_13_14/aws_tags_map/expected/main.terratag.tf index 5e7ceb2..6a9cb6d 100644 --- a/test/fixture/terraform_13_14/aws_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/aws_tags_map/expected/main.terratag.tf @@ -15,14 +15,14 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket","Unquoted1","Iwannabequoted","AnotherName","Yo","Unquoted2","Ireallywannabequoted","Unquoted3","IreallyreallywannabequotedandIgotacomma",join("-",["foo","bar"]),"Testfunction",(local.localTagKey),"Testexpression","${local.localTagKey2}","Testvariableaskey","Yo-${local.localTagKey}","Testvariableinsidekey","Testvariableasvalue","${local.localTagKey}","Testvariableinsidevalue","Yo-${local.localTagKey}"), local.terratag_added_main) + tags = merge(map("Name", "Mybucket", "Unquoted1", "Iwannabequoted", "AnotherName", "Yo", "Unquoted2", "Ireallywannabequoted", "Unquoted3", "IreallyreallywannabequotedandIgotacomma", join("-", ["foo", "bar"]), "Testfunction", (local.localTagKey), "Testexpression", "${local.localTagKey2}", "Testvariableaskey", "Yo-${local.localTagKey}", "Testvariableinsidekey", "Testvariableasvalue", "${local.localTagKey}", "Testvariableinsidevalue", "Yo-${local.localTagKey}"), local.terratag_added_main) } resource "aws_s3_bucket" "a" { bucket = "my-another-tf-test-bucket" acl = "private" - tags = merge( local.localMap, local.terratag_added_main) + tags = merge(local.localMap, local.terratag_added_main) } locals { diff --git a/test/fixture/terraform_13_14/azurerm_kubernetes_cluster/expected/main.terratag.tf b/test/fixture/terraform_13_14/azurerm_kubernetes_cluster/expected/main.terratag.tf index 86071eb..259b49b 100644 --- a/test/fixture/terraform_13_14/azurerm_kubernetes_cluster/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/azurerm_kubernetes_cluster/expected/main.terratag.tf @@ -56,7 +56,7 @@ resource "azurerm_kubernetes_cluster" "existing_tags_cluster" { name = "pool" node_count = 2 vm_size = "Standard_D2_v2" - tags = merge( map("existing","tag"), local.terratag_added_main) + tags = merge(map("existing", "tag"), local.terratag_added_main) } network_profile { @@ -64,7 +64,7 @@ resource "azurerm_kubernetes_cluster" "existing_tags_cluster" { network_plugin = "kubenet" } - tags = merge( map("existing","tag"), local.terratag_added_main) + tags = merge(map("existing", "tag"), local.terratag_added_main) } locals { diff --git a/test/fixture/terraform_13_14/azurerm_tags_map/expected/main.terratag.tf b/test/fixture/terraform_13_14/azurerm_tags_map/expected/main.terratag.tf index 6de9afc..f394e62 100644 --- a/test/fixture/terraform_13_14/azurerm_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/azurerm_tags_map/expected/main.terratag.tf @@ -13,7 +13,7 @@ provider "azurerm" { resource "azurerm_resource_group" "example" { name = "example-resources" location = "West Europe" - tags = merge( map("oh","my"), local.terratag_added_main) + tags = merge(map("oh", "my"), local.terratag_added_main) } resource "azurerm_virtual_network" "example" { diff --git a/test/fixture/terraform_13_14/azurestack_tags_map/expected/main.terratag.tf b/test/fixture/terraform_13_14/azurestack_tags_map/expected/main.terratag.tf index 33207ce..67ba953 100644 --- a/test/fixture/terraform_13_14/azurestack_tags_map/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/azurestack_tags_map/expected/main.terratag.tf @@ -26,7 +26,7 @@ resource "azurestack_virtual_network" "test2" { address_space = ["10.0.0.0/16"] location = azurestack_resource_group.test.location resource_group_name = azurestack_resource_group.test.name - tags = merge( map("yo","ho"), local.terratag_added_main) + tags = merge(map("yo", "ho"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_13_14/git_issue_43__invalid_tf_files_in_subfolders/expected/main.terratag.tf b/test/fixture/terraform_13_14/git_issue_43__invalid_tf_files_in_subfolders/expected/main.terratag.tf index 49a9592..f591a80 100644 --- a/test/fixture/terraform_13_14/git_issue_43__invalid_tf_files_in_subfolders/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/git_issue_43__invalid_tf_files_in_subfolders/expected/main.terratag.tf @@ -15,7 +15,7 @@ resource "aws_s3_bucket" "b" { bucket = "my-tf-test-bucket" acl = "private" - tags = merge( map("Name","Mybucket"), local.terratag_added_main) + tags = merge(map("Name", "Mybucket"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} diff --git a/test/fixture/terraform_13_14/git_issue_56__line_ends_with_closing_paren/expected/main.terratag.tf b/test/fixture/terraform_13_14/git_issue_56__line_ends_with_closing_paren/expected/main.terratag.tf index e21b563..dc23cc8 100644 --- a/test/fixture/terraform_13_14/git_issue_56__line_ends_with_closing_paren/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/git_issue_56__line_ends_with_closing_paren/expected/main.terratag.tf @@ -25,7 +25,7 @@ resource "kubernetes_deployment" "audit_server" { resource "google_pubsub_topic" "audit_topic" { name = "yuvu" - labels = merge( local.terratag_added_main, local.terratag_added_main) + labels = merge(local.terratag_added_main, local.terratag_added_main) } locals { diff --git a/test/fixture/terraform_13_14/google_container_cluster/expected/main.terratag.tf b/test/fixture/terraform_13_14/google_container_cluster/expected/main.terratag.tf index ffd8583..2b1b3e0 100644 --- a/test/fixture/terraform_13_14/google_container_cluster/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/google_container_cluster/expected/main.terratag.tf @@ -34,7 +34,7 @@ resource "google_container_cluster" "existing-labels-cluster" { name = "cluster2" location = "us-central1" - resource_labels = merge( map("foo","bar"), local.terratag_added_main) + resource_labels = merge(map("foo", "bar"), local.terratag_added_main) node_config { machine_type = "n1-standard-1" diff --git a/test/fixture/terraform_13_14/google_labels_map/expected/main.terratag.tf b/test/fixture/terraform_13_14/google_labels_map/expected/main.terratag.tf index ad1d166..96c373f 100644 --- a/test/fixture/terraform_13_14/google_labels_map/expected/main.terratag.tf +++ b/test/fixture/terraform_13_14/google_labels_map/expected/main.terratag.tf @@ -23,7 +23,7 @@ resource "google_storage_bucket" "static-site" { response_header = ["*"] max_age_seconds = 3600 } - labels = merge( map("foo","bar"), local.terratag_added_main) + labels = merge(map("foo", "bar"), local.terratag_added_main) } locals { terratag_added_main = {"env0_environment_id"="40907eff-cf7c-419a-8694-e1c6bf1d1168","env0_project_id"="43fd4ff1-8d37-4d9d-ac97-295bd850bf94"} From aa39a0d75c7ceb71e53607c64b669c6169b203b1 Mon Sep 17 00:00:00 2001 From: "avner.sorek" Date: Thu, 14 Jan 2021 15:42:22 +0200 Subject: [PATCH 10/10] bak change --- .../fixture/terraform_11/aws_tags_merge/expected/main.tf.bak | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak b/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak index c798a92..494d59b 100644 --- a/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak +++ b/test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak @@ -6,5 +6,8 @@ provider "aws" { resource "aws_s3_bucket" "a" { bucket = "my-another-tf-test-bucket" - tags = "${merge(map("a", "b"), map("c", "d"))}" + tags = "${merge( + map("a", "b"), + map("c", "d") + )}" }