From c3cdf470fa608b609aeb028d4e5190a12ea380cb Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Wed, 14 Feb 2024 15:39:06 -0600 Subject: [PATCH] Feat: set volume_tags on aws_instance resource --- internal/tagging/aws.go | 20 +++++++++++++ internal/tagging/tagging.go | 1 + test/fixture/terraform_latest/config.yaml | 3 +- .../aws_instance_volume_tags/expected/main.tf | 30 +++++++++++++++++++ .../aws_instance_volume_tags/input/main.tf | 24 +++++++++++++++ 5 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 test/tests/aws_instance_volume_tags/expected/main.tf create mode 100644 test/tests/aws_instance_volume_tags/input/main.tf diff --git a/internal/tagging/aws.go b/internal/tagging/aws.go index ad058252..db132f40 100644 --- a/internal/tagging/aws.go +++ b/internal/tagging/aws.go @@ -9,6 +9,26 @@ import ( "github.com/hashicorp/hcl/v2/hclwrite" ) +func tagAwsInstance(args TagBlockArgs) (*Result, error) { + var swappedTagsStrings []string + + tagBlock, err := TagBlock(args) + if err != nil { + return nil, err + } + swappedTagsStrings = append(swappedTagsStrings, tagBlock) + + volumeTagBlockArgs := args + volumeTagBlockArgs.TagId = "volume_tags" + volumeTagBlock, err := TagBlock(volumeTagBlockArgs) + if err != nil { + return nil, err + } + swappedTagsStrings = append(swappedTagsStrings, volumeTagBlock) + + return &Result{SwappedTagsStrings: swappedTagsStrings}, nil +} + func tagAutoscalingGroup(args TagBlockArgs) (*Result, error) { // https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html diff --git a/internal/tagging/tagging.go b/internal/tagging/tagging.go index 3d87fbe8..7dee409c 100644 --- a/internal/tagging/tagging.go +++ b/internal/tagging/tagging.go @@ -69,6 +69,7 @@ func TagResource(args TagBlockArgs) (*Result, error) { var resourceTypeToFnMap = map[string]TagResourceFn{ "aws_autoscaling_group": tagAutoscalingGroup, + "aws_instance": tagAwsInstance, "google_container_cluster": tagContainerCluster, "azurerm_kubernetes_cluster": tagAksK8sCluster, } diff --git a/test/fixture/terraform_latest/config.yaml b/test/fixture/terraform_latest/config.yaml index 818259a5..3ae1125f 100644 --- a/test/fixture/terraform_latest/config.yaml +++ b/test/fixture/terraform_latest/config.yaml @@ -15,4 +15,5 @@ suites: - google_beta_resource_skip - google_container_cluster - google_labels_map - - google_vs_google_beta \ No newline at end of file + - google_vs_google_beta + - aws_instance_volume_tags diff --git a/test/tests/aws_instance_volume_tags/expected/main.tf b/test/tests/aws_instance_volume_tags/expected/main.tf new file mode 100644 index 00000000..8151ef69 --- /dev/null +++ b/test/tests/aws_instance_volume_tags/expected/main.tf @@ -0,0 +1,30 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 2.0" + } + } +} + +provider "aws" { + region = "us-east-1" +} + +resource "aws_instance" "ubuntu" { + ami = "dasdasD" + instance_type = "t3.micro" + availability_zone = "us-west-2" + + + tags = merge({ + "Name" = "terratag-test" + "env" = "test" + }, local.terratag_added_main) + volume_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/tests/aws_instance_volume_tags/input/main.tf b/test/tests/aws_instance_volume_tags/input/main.tf new file mode 100644 index 00000000..1fb45571 --- /dev/null +++ b/test/tests/aws_instance_volume_tags/input/main.tf @@ -0,0 +1,24 @@ +terraform { + required_providers { + aws = { + source = "hashicorp/aws" + version = "~> 2.0" + } + } +} + +provider "aws" { + region = "us-east-1" +} + +resource "aws_instance" "ubuntu" { + ami = "dasdasD" + instance_type = "t3.micro" + availability_zone = "us-west-2" + + + tags = { + Name = "terratag-test" + env = "test" + } +}