Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: New lines in modified attributes #76

Merged
merged 11 commits into from
Jan 17, 2021
30 changes: 22 additions & 8 deletions tagging/tagging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
}


Expand Down
18 changes: 18 additions & 0 deletions test/fixture/terraform_11/aws_tags_merge/expected/main.terratag.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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"}
}

13 changes: 13 additions & 0 deletions test/fixture/terraform_11/aws_tags_merge/expected/main.tf.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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")
)}"
}
13 changes: 13 additions & 0 deletions test/fixture/terraform_11/aws_tags_merge/input/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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")
)}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ 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 {
load_balancer_sku = "Standard"
network_plugin = "kubenet"
}

tags = "${merge( map("existing","tag"), local.terratag_added_main)}"
tags = "${merge(map("existing", "tag"), local.terratag_added_main)}"
}

locals {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions test/fixture/terraform_12/aws_tags_merge/expected/main.terratag.tf
Original file line number Diff line number Diff line change
@@ -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" {
Copy link
Contributor

Choose a reason for hiding this comment

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

@avnerenv0 is there any special reason why tf12 scenario/tests are different than tf11? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

just because the interpolation syntax "${...}" I wanted to make sure it works as expected

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"}
}

27 changes: 27 additions & 0 deletions test/fixture/terraform_12/aws_tags_merge/expected/main.tf.bak
Original file line number Diff line number Diff line change
@@ -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
)
}
27 changes: 27 additions & 0 deletions test/fixture/terraform_12/aws_tags_merge/input/main.tf
Original file line number Diff line number Diff line change
@@ -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
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ 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 {
load_balancer_sku = "Standard"
network_plugin = "kubenet"
}

tags = merge( map("existing","tag"), local.terratag_added_main)
tags = merge(map("existing", "tag"), local.terratag_added_main)
}

locals {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
Loading