From e2b880b2bad3cd8ceaa87c56176c15d92e28775d Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 5 Jun 2019 18:27:08 -0700 Subject: [PATCH] Terraform 0.12 support (#63) * Terraform 0.12 support * add tests scenario * Update README --- README.md | 253 +++++++++++++++++--------- README.yaml | 241 ++++++++++++++++-------- codefresh/test.yml | 4 +- docs/terraform.md | 12 +- examples/autoscalinggroup/main.tf | 29 +-- examples/autoscalinggroup/outputs.tf | 8 +- examples/autoscalinggroup/versions.tf | 3 + examples/complete/label1.tf | 38 ++++ examples/complete/label2.tf | 35 ++++ examples/complete/label3.tf | 32 ++++ examples/complete/label4.tf | 34 ++++ examples/complete/label5.tf | 33 ++++ examples/complete/main.tf | 42 ----- examples/complete/outputs.tf | 53 ------ examples/complete/versions.tf | 4 + main.tf | 140 ++++++-------- outputs.tf | 23 +-- provider.tf | 1 + terraform.tf | 3 +- test/src/Makefile | 40 ++-- test/src/Makefile.alpine | 9 - test/src/examples_complete_test.go | 13 ++ variables.tf | 53 ++++-- 23 files changed, 688 insertions(+), 415 deletions(-) create mode 100644 examples/autoscalinggroup/versions.tf create mode 100644 examples/complete/label1.tf create mode 100644 examples/complete/label2.tf create mode 100644 examples/complete/label3.tf create mode 100644 examples/complete/label4.tf create mode 100644 examples/complete/label5.tf delete mode 100644 examples/complete/main.tf delete mode 100644 examples/complete/outputs.tf create mode 100644 examples/complete/versions.tf delete mode 100644 test/src/Makefile.alpine diff --git a/README.md b/README.md index d48459e..5e06601 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ All [Cloud Posse modules](https://github.com/cloudposse?utf8=%E2%9C%93&q=terrafo **NOTE:** The `null` refers to the primary Terraform [provider](https://www.terraform.io/docs/providers/null/index.html) used in this module. +Releases of this module from `0.12.0` onward support `HCL2` and only work with Terraform 0.12 or newer. Releases prior to this are compatible with earlier versions of terraform like Terraform 0.11. + --- @@ -85,7 +87,7 @@ Now reference the label when creating an instance: ```hcl resource "aws_instance" "eg_prod_bastion_public" { instance_type = "t1.micro" - tags = "${module.eg_prod_bastion_label.tags}" + tags = module.eg_prod_bastion_label.tags } ``` @@ -93,9 +95,9 @@ Or define a security group: ```hcl resource "aws_security_group" "eg_prod_bastion_public" { - vpc_id = "${var.vpc_id}" - name = "${module.eg_prod_bastion_label.id}" - tags = "${module.eg_prod_bastion_label.tags}" + vpc_id = var.vpc_id + name = module.eg_prod_bastion_label.id + tags = module.eg_prod_bastion_label.tags egress { from_port = 0 to_port = 0 @@ -126,8 +128,8 @@ module "eg_prod_bastion_abc_label" { } resource "aws_security_group" "eg_prod_bastion_abc" { - name = "${module.eg_prod_bastion_abc_label.id}" - tags = "${module.eg_prod_bastion_abc_label.tags}" + name = module.eg_prod_bastion_abc_label.id + tags = module.eg_prod_bastion_abc_label.tags ingress { from_port = 22 to_port = 22 @@ -138,8 +140,8 @@ resource "aws_security_group" "eg_prod_bastion_abc" { resource "aws_instance" "eg_prod_bastion_abc" { instance_type = "t1.micro" - tags = "${module.eg_prod_bastion_abc_label.tags}" -   vpc_security_group_ids = ["${aws_security_group.eg_prod_bastion_abc.id}"] + tags = module.eg_prod_bastion_abc_label.tags +   vpc_security_group_ids = [aws_security_group.eg_prod_bastion_abc.id] } module "eg_prod_bastion_xyz_label" { @@ -157,8 +159,8 @@ module "eg_prod_bastion_xyz_label" { } resource "aws_security_group" "eg_prod_bastion_xyz" { - name = "${module.eg_prod_bastion_xyz_label.id}" - tags = "${module.eg_prod_bastion_xyz_label.tags}" + name = module.eg_prod_bastion_xyz_label.id + tags = module.eg_prod_bastion_xyz_label.tags ingress { from_port = 22 to_port = 22 @@ -169,8 +171,8 @@ resource "aws_security_group" "eg_prod_bastion_xyz" { resource "aws_instance" "eg_prod_bastion_xyz" { instance_type = "t1.micro" - tags = "${module.eg_prod_bastion_xyz_label.tags}" - vpc_security_group_ids = ["${aws_security_group.eg_prod_bastion_xyz.id}"] + tags = module.eg_prod_bastion_xyz_label.tags + vpc_security_group_ids = [aws_security_group.eg_prod_bastion_xyz.id] } ``` @@ -226,11 +228,11 @@ module "label" { resource "aws_launch_template" "default" { # terraform-null-label example used here: Set template name prefix name_prefix = "${module.label.id}-" - image_id = "${data.aws_ami.amazon_linux.id}" + image_id = data.aws_ami.amazon_linux.id instance_type = "t2.micro" instance_initiated_shutdown_behavior = "terminate" - vpc_security_group_ids = ["${data.aws_security_group.default.id}"] + vpc_security_group_ids = [data.aws_security_group.default.id] monitoring { enabled = false @@ -238,7 +240,7 @@ resource "aws_launch_template" "default" { # terraform-null-label example used here: Set tags on volumes tag_specifications { resource_type = "volume" - tags = "${module.label.tags}" + tags = module.label.tags } } @@ -248,18 +250,18 @@ resource "aws_launch_template" "default" { resource "aws_autoscaling_group" "default" { # terraform-null-label example used here: Set ASG name prefix name_prefix = "${module.label.id}-" - vpc_zone_identifier = ["${data.aws_subnet_ids.all.ids}"] + vpc_zone_identifier = data.aws_subnet_ids.all.ids max_size = "1" min_size = "1" desired_capacity = "1" launch_template = { - id = "${aws_launch_template.default.id}" + id = "aws_launch_template.default.id version = "$$Latest" } # terraform-null-label example used here: Set tags on ASG and EC2 Servers - tags = ["${module.label.tags_as_list_of_maps}"] + tags = module.label.tags_as_list_of_maps } ``` @@ -291,7 +293,7 @@ module "label1" { module "label2" { source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master" - context = "${module.label1.context}" + context = module.label1.context name = "Charlie" stage = "test" delimiter = "+" @@ -306,7 +308,7 @@ module "label3" { source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master" name = "Starfish" stage = "release" - context = "${module.label1.context}" + context = module.label1.context delimiter = "." tags = { @@ -320,82 +322,163 @@ This creates label outputs like this: ```hcl label1 = { - attributes = [fire water earth air] - id = winstonchurchroom-uat-build-fire-water-earth-air - name = winstonchurchroom - namespace = cloudposse - stage = build + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "-" + "id" = "winstonchurchroom-uat-build-fire-water-earth-air" + "name" = "winstonchurchroom" + "namespace" = "cloudposse" + "stage" = "build" } label1_context = { - attributes = [fire water earth air] - delimiter = [-] - environment = [uat] - label_order = [name environment stage attributes] - name = [winstonchurchroom] - namespace = [cloudposse] - stage = [build] - tags_keys = [City Environment Name Namespace Stage] - tags_values = [Dublin Private winstonchurchroom-uat-build-fire-water-earth-air cloudposse build] + "additional_tag_map" = {} + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "-" + "enabled" = true + "environment" = "uat" + "label_order" = [ + "name", + "environment", + "stage", + "attributes", + ] + "name" = "winstonchurchroom" + "namespace" = "cloudposse" + "regex_replace_chars" = "/[^a-zA-Z0-9-]/" + "stage" = "build" + "tags" = { + "Attributes" = "fire-water-earth-air" + "City" = "Dublin" + "Environment" = "Private" + "Name" = "winstonchurchroom" + "Namespace" = "cloudposse" + "Stage" = "build" + } } label1_tags = { - City = Dublin - Environment = Private - Name = winstonchurchroom-uat-build-fire-water-earth-air - Namespace = cloudposse - Stage = build + "Attributes" = "fire-water-earth-air" + "City" = "Dublin" + "Environment" = "Private" + "Name" = "winstonchurchroom" + "Namespace" = "cloudposse" + "Stage" = "build" } label2 = { - attributes = [fire water earth air] - id = charlie+uat+test+fire+water+earth+air - name = charlie - namespace = cloudposse - stage = test + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "+" + "id" = "charlie+uat+test+firewaterearthair" + "name" = "charlie" + "namespace" = "cloudposse" + "stage" = "test" } label2_context = { - attributes = [fire water earth air] - delimiter = [+] - environment = [uat] - label_order = [name environment stage attributes] - name = [charlie] - namespace = [cloudposse] - stage = [test] - tags_keys = [City Environment Name Namespace Stage] - tags_values = [London Public charlie+uat+test+fire+water+earth+air cloudposse test] + "additional_tag_map" = {} + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "+" + "enabled" = true + "environment" = "uat" + "label_order" = [ + "name", + "environment", + "stage", + "attributes", + ] + "name" = "charlie" + "namespace" = "cloudposse" + "regex_replace_chars" = "/[^a-zA-Z0-9-]/" + "stage" = "test" + "tags" = { + "Attributes" = "firewaterearthair" + "City" = "London" + "Environment" = "Public" + "Name" = "charlie" + "Namespace" = "cloudposse" + "Stage" = "test" + } } label2_tags = { - City = London - Environment = Public - Name = charlie+uat+test+fire+water+earth+air - Namespace = cloudposse - Stage = test + "Attributes" = "firewaterearthair" + "City" = "London" + "Environment" = "Public" + "Name" = "charlie" + "Namespace" = "cloudposse" + "Stage" = "test" } label3 = { - attributes = [fire water earth air] - id = starfish.uat.release.fire.water.earth.air - name = starfish - namespace = cloudposse - stage = release + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "." + "id" = "starfish.uat.release.firewaterearthair" + "name" = "starfish" + "namespace" = "cloudposse" + "stage" = "release" } label3_context = { - attributes = [fire water earth air] - delimiter = [.] - environment = [uat] - label_order = [name environment stage attributes] - name = [starfish] - namespace = [cloudposse] - stage = [release] - tags_keys = [Animal City Eat Environment Name Namespace Stage] - tags_values = [Rabbit Dublin Carrot uat starfish.uat.release.fire.water.earth.air cloudposse release] + "additional_tag_map" = {} + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "." + "enabled" = true + "environment" = "uat" + "label_order" = [ + "name", + "environment", + "stage", + "attributes", + ] + "name" = "starfish" + "namespace" = "cloudposse" + "regex_replace_chars" = "/[^a-zA-Z0-9-]/" + "stage" = "release" + "tags" = { + "Animal" = "Rabbit" + "Attributes" = "firewaterearthair" + "City" = "Dublin" + "Eat" = "Carrot" + "Environment" = "uat" + "Name" = "starfish" + "Namespace" = "cloudposse" + "Stage" = "release" + } } label3_tags = { - Animal = Rabbit - City = Dublin - Eat = Carrot - Environment = uat - Name = starfish.uat.release.fire.water.earth.air - Namespace = cloudposse - Stage = release + "Animal" = "Rabbit" + "Attributes" = "firewaterearthair" + "City" = "Dublin" + "Eat" = "Carrot" + "Environment" = "uat" + "Name" = "starfish" + "Namespace" = "cloudposse" + "Stage" = "release" } + ``` @@ -417,18 +500,18 @@ Available targets: | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| additional_tag_map | Additional tags for appending to each tag map | map | `` | no | -| attributes | Additional attributes (e.g. `1`) | list | `` | no | -| context | Default context to use for passing state between label invocations | map | `` | no | +| additional_tag_map | Additional tags for appending to each tag map | map(string) | `` | no | +| attributes | Additional attributes (e.g. `1`) | list(string) | `` | no | +| context | Default context to use for passing state between label invocations | object | `` | no | | delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | string | `-` | no | -| enabled | Set to false to prevent the module from creating any resources | string | `true` | no | +| enabled | Set to false to prevent the module from creating any resources | bool | `true` | no | | environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | string | `` | no | -| label_order | The naming order of the id output and Name tag | list | `` | no | +| label_order | The naming order of the id output and Name tag | list(string) | `` | no | | name | Solution name, e.g. 'app' or 'jenkins' | string | `` | no | | namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | string | `` | no | | regex_replace_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed | string | `/[^a-zA-Z0-9-]/` | no | | stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no | -| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `` | no | +| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map(string) | `` | no | ## Outputs diff --git a/README.yaml b/README.yaml index 11a77c1..6e915e3 100644 --- a/README.yaml +++ b/README.yaml @@ -63,6 +63,8 @@ description: |- **NOTE:** The `null` refers to the primary Terraform [provider](https://www.terraform.io/docs/providers/null/index.html) used in this module. + Releases of this module from `0.12.0` onward support `HCL2` and only work with Terraform 0.12 or newer. Releases prior to this are compatible with earlier versions of terraform like Terraform 0.11. + # How to use this project usage: |- ### Simple Example @@ -91,7 +93,7 @@ usage: |- ```hcl resource "aws_instance" "eg_prod_bastion_public" { instance_type = "t1.micro" - tags = "${module.eg_prod_bastion_label.tags}" + tags = module.eg_prod_bastion_label.tags } ``` @@ -99,9 +101,9 @@ usage: |- ```hcl resource "aws_security_group" "eg_prod_bastion_public" { - vpc_id = "${var.vpc_id}" - name = "${module.eg_prod_bastion_label.id}" - tags = "${module.eg_prod_bastion_label.tags}" + vpc_id = var.vpc_id + name = module.eg_prod_bastion_label.id + tags = module.eg_prod_bastion_label.tags egress { from_port = 0 to_port = 0 @@ -132,8 +134,8 @@ usage: |- } resource "aws_security_group" "eg_prod_bastion_abc" { - name = "${module.eg_prod_bastion_abc_label.id}" - tags = "${module.eg_prod_bastion_abc_label.tags}" + name = module.eg_prod_bastion_abc_label.id + tags = module.eg_prod_bastion_abc_label.tags ingress { from_port = 22 to_port = 22 @@ -144,8 +146,8 @@ usage: |- resource "aws_instance" "eg_prod_bastion_abc" { instance_type = "t1.micro" - tags = "${module.eg_prod_bastion_abc_label.tags}" -   vpc_security_group_ids = ["${aws_security_group.eg_prod_bastion_abc.id}"] + tags = module.eg_prod_bastion_abc_label.tags +   vpc_security_group_ids = [aws_security_group.eg_prod_bastion_abc.id] } module "eg_prod_bastion_xyz_label" { @@ -163,8 +165,8 @@ usage: |- } resource "aws_security_group" "eg_prod_bastion_xyz" { - name = "${module.eg_prod_bastion_xyz_label.id}" - tags = "${module.eg_prod_bastion_xyz_label.tags}" + name = module.eg_prod_bastion_xyz_label.id + tags = module.eg_prod_bastion_xyz_label.tags ingress { from_port = 22 to_port = 22 @@ -175,8 +177,8 @@ usage: |- resource "aws_instance" "eg_prod_bastion_xyz" { instance_type = "t1.micro" - tags = "${module.eg_prod_bastion_xyz_label.tags}" - vpc_security_group_ids = ["${aws_security_group.eg_prod_bastion_xyz.id}"] + tags = module.eg_prod_bastion_xyz_label.tags + vpc_security_group_ids = [aws_security_group.eg_prod_bastion_xyz.id] } ``` @@ -232,11 +234,11 @@ usage: |- resource "aws_launch_template" "default" { # terraform-null-label example used here: Set template name prefix name_prefix = "${module.label.id}-" - image_id = "${data.aws_ami.amazon_linux.id}" + image_id = data.aws_ami.amazon_linux.id instance_type = "t2.micro" instance_initiated_shutdown_behavior = "terminate" - vpc_security_group_ids = ["${data.aws_security_group.default.id}"] + vpc_security_group_ids = [data.aws_security_group.default.id] monitoring { enabled = false @@ -244,7 +246,7 @@ usage: |- # terraform-null-label example used here: Set tags on volumes tag_specifications { resource_type = "volume" - tags = "${module.label.tags}" + tags = module.label.tags } } @@ -254,18 +256,18 @@ usage: |- resource "aws_autoscaling_group" "default" { # terraform-null-label example used here: Set ASG name prefix name_prefix = "${module.label.id}-" - vpc_zone_identifier = ["${data.aws_subnet_ids.all.ids}"] + vpc_zone_identifier = data.aws_subnet_ids.all.ids max_size = "1" min_size = "1" desired_capacity = "1" launch_template = { - id = "${aws_launch_template.default.id}" + id = "aws_launch_template.default.id version = "$$Latest" } # terraform-null-label example used here: Set tags on ASG and EC2 Servers - tags = ["${module.label.tags_as_list_of_maps}"] + tags = module.label.tags_as_list_of_maps } ``` @@ -297,7 +299,7 @@ usage: |- module "label2" { source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master" - context = "${module.label1.context}" + context = module.label1.context name = "Charlie" stage = "test" delimiter = "+" @@ -312,7 +314,7 @@ usage: |- source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master" name = "Starfish" stage = "release" - context = "${module.label1.context}" + context = module.label1.context delimiter = "." tags = { @@ -326,82 +328,163 @@ usage: |- ```hcl label1 = { - attributes = [fire water earth air] - id = winstonchurchroom-uat-build-fire-water-earth-air - name = winstonchurchroom - namespace = cloudposse - stage = build + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "-" + "id" = "winstonchurchroom-uat-build-fire-water-earth-air" + "name" = "winstonchurchroom" + "namespace" = "cloudposse" + "stage" = "build" } label1_context = { - attributes = [fire water earth air] - delimiter = [-] - environment = [uat] - label_order = [name environment stage attributes] - name = [winstonchurchroom] - namespace = [cloudposse] - stage = [build] - tags_keys = [City Environment Name Namespace Stage] - tags_values = [Dublin Private winstonchurchroom-uat-build-fire-water-earth-air cloudposse build] + "additional_tag_map" = {} + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "-" + "enabled" = true + "environment" = "uat" + "label_order" = [ + "name", + "environment", + "stage", + "attributes", + ] + "name" = "winstonchurchroom" + "namespace" = "cloudposse" + "regex_replace_chars" = "/[^a-zA-Z0-9-]/" + "stage" = "build" + "tags" = { + "Attributes" = "fire-water-earth-air" + "City" = "Dublin" + "Environment" = "Private" + "Name" = "winstonchurchroom" + "Namespace" = "cloudposse" + "Stage" = "build" + } } label1_tags = { - City = Dublin - Environment = Private - Name = winstonchurchroom-uat-build-fire-water-earth-air - Namespace = cloudposse - Stage = build + "Attributes" = "fire-water-earth-air" + "City" = "Dublin" + "Environment" = "Private" + "Name" = "winstonchurchroom" + "Namespace" = "cloudposse" + "Stage" = "build" } label2 = { - attributes = [fire water earth air] - id = charlie+uat+test+fire+water+earth+air - name = charlie - namespace = cloudposse - stage = test + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "+" + "id" = "charlie+uat+test+firewaterearthair" + "name" = "charlie" + "namespace" = "cloudposse" + "stage" = "test" } label2_context = { - attributes = [fire water earth air] - delimiter = [+] - environment = [uat] - label_order = [name environment stage attributes] - name = [charlie] - namespace = [cloudposse] - stage = [test] - tags_keys = [City Environment Name Namespace Stage] - tags_values = [London Public charlie+uat+test+fire+water+earth+air cloudposse test] + "additional_tag_map" = {} + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "+" + "enabled" = true + "environment" = "uat" + "label_order" = [ + "name", + "environment", + "stage", + "attributes", + ] + "name" = "charlie" + "namespace" = "cloudposse" + "regex_replace_chars" = "/[^a-zA-Z0-9-]/" + "stage" = "test" + "tags" = { + "Attributes" = "firewaterearthair" + "City" = "London" + "Environment" = "Public" + "Name" = "charlie" + "Namespace" = "cloudposse" + "Stage" = "test" + } } label2_tags = { - City = London - Environment = Public - Name = charlie+uat+test+fire+water+earth+air - Namespace = cloudposse - Stage = test + "Attributes" = "firewaterearthair" + "City" = "London" + "Environment" = "Public" + "Name" = "charlie" + "Namespace" = "cloudposse" + "Stage" = "test" } label3 = { - attributes = [fire water earth air] - id = starfish.uat.release.fire.water.earth.air - name = starfish - namespace = cloudposse - stage = release + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "." + "id" = "starfish.uat.release.firewaterearthair" + "name" = "starfish" + "namespace" = "cloudposse" + "stage" = "release" } label3_context = { - attributes = [fire water earth air] - delimiter = [.] - environment = [uat] - label_order = [name environment stage attributes] - name = [starfish] - namespace = [cloudposse] - stage = [release] - tags_keys = [Animal City Eat Environment Name Namespace Stage] - tags_values = [Rabbit Dublin Carrot uat starfish.uat.release.fire.water.earth.air cloudposse release] + "additional_tag_map" = {} + "attributes" = [ + "fire", + "water", + "earth", + "air", + ] + "delimiter" = "." + "enabled" = true + "environment" = "uat" + "label_order" = [ + "name", + "environment", + "stage", + "attributes", + ] + "name" = "starfish" + "namespace" = "cloudposse" + "regex_replace_chars" = "/[^a-zA-Z0-9-]/" + "stage" = "release" + "tags" = { + "Animal" = "Rabbit" + "Attributes" = "firewaterearthair" + "City" = "Dublin" + "Eat" = "Carrot" + "Environment" = "uat" + "Name" = "starfish" + "Namespace" = "cloudposse" + "Stage" = "release" + } } label3_tags = { - Animal = Rabbit - City = Dublin - Eat = Carrot - Environment = uat - Name = starfish.uat.release.fire.water.earth.air - Namespace = cloudposse - Stage = release + "Animal" = "Rabbit" + "Attributes" = "firewaterearthair" + "City" = "Dublin" + "Eat" = "Carrot" + "Environment" = "uat" + "Name" = "starfish" + "Namespace" = "cloudposse" + "Stage" = "release" } + ``` include: diff --git a/codefresh/test.yml b/codefresh/test.yml index 7ef1e8a..09a2154 100644 --- a/codefresh/test.yml +++ b/codefresh/test.yml @@ -19,8 +19,10 @@ steps: image: ${{TEST_IMAGE}} stage: Prepare commands: + - cf_export PATH="/usr/local/terraform/0.12/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" - make init - - make -C test/ clean init + - git -C build-harness checkout master + - make -C test/ clean init TEST_HARNESS_BRANCH=master - make -C test/src clean init - find . -type d -name '.terraform' | xargs rm -rf - find . -type f -name 'terraform.tfstate*' -exec rm -f {} \; diff --git a/docs/terraform.md b/docs/terraform.md index c2b5691..461588d 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -2,18 +2,18 @@ | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| -| additional_tag_map | Additional tags for appending to each tag map | map | `` | no | -| attributes | Additional attributes (e.g. `1`) | list | `` | no | -| context | Default context to use for passing state between label invocations | map | `` | no | +| additional_tag_map | Additional tags for appending to each tag map | map(string) | `` | no | +| attributes | Additional attributes (e.g. `1`) | list(string) | `` | no | +| context | Default context to use for passing state between label invocations | object | `` | no | | delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | string | `-` | no | -| enabled | Set to false to prevent the module from creating any resources | string | `true` | no | +| enabled | Set to false to prevent the module from creating any resources | bool | `true` | no | | environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | string | `` | no | -| label_order | The naming order of the id output and Name tag | list | `` | no | +| label_order | The naming order of the id output and Name tag | list(string) | `` | no | | name | Solution name, e.g. 'app' or 'jenkins' | string | `` | no | | namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | string | `` | no | | regex_replace_chars | Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed | string | `/[^a-zA-Z0-9-]/` | no | | stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no | -| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `` | no | +| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map(string) | `` | no | ## Outputs diff --git a/examples/autoscalinggroup/main.tf b/examples/autoscalinggroup/main.tf index 5f93a74..0a09978 100644 --- a/examples/autoscalinggroup/main.tf +++ b/examples/autoscalinggroup/main.tf @@ -2,8 +2,8 @@ # terraform-null-label example # ################################ module "label" { - source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master" - namespace = "cp" + source = "../../" + namespace = "eg" stage = "prod" name = "app" @@ -23,11 +23,11 @@ module "label" { resource "aws_launch_template" "default" { # terraform-null-label example used here: Set template name prefix name_prefix = "${module.label.id}-" - image_id = "${data.aws_ami.amazon_linux.id}" + image_id = data.aws_ami.amazon_linux.id instance_type = "t2.micro" instance_initiated_shutdown_behavior = "terminate" - vpc_security_group_ids = ["${data.aws_security_group.default.id}"] + vpc_security_group_ids = [data.aws_security_group.default.id] monitoring { enabled = false @@ -36,7 +36,7 @@ resource "aws_launch_template" "default" { # terraform-null-label example used here: Set tags on volumes tag_specifications { resource_type = "volume" - tags = "${module.label.tags}" + tags = module.label.tags } } @@ -46,18 +46,18 @@ resource "aws_launch_template" "default" { resource "aws_autoscaling_group" "default" { # terraform-null-label example used here: Set ASG name prefix name_prefix = "${module.label.id}-" - vpc_zone_identifier = ["${data.aws_subnet_ids.all.ids}"] + vpc_zone_identifier = data.aws_subnet_ids.all.ids max_size = "1" min_size = "1" desired_capacity = "1" - launch_template = { - id = "${aws_launch_template.default.id}" - version = "$$Latest" + launch_template { + id = aws_launch_template.default.id + version = "$Latest" } # terraform-null-label example used here: Set tags on ASG and EC2 Servers - tags = ["${module.label.tags_as_list_of_maps}"] + tags = module.label.tags_as_list_of_maps } ################################ @@ -65,7 +65,7 @@ resource "aws_autoscaling_group" "default" { ################################ provider "aws" { region = "eu-west-1" - version = "~> 1.17" + version = "~> 2.7" # Make it faster by skipping unneeded checks here skip_get_ec2_platforms = true @@ -83,17 +83,19 @@ data "aws_vpc" "default" { } data "aws_subnet_ids" "all" { - vpc_id = "${data.aws_vpc.default.id}" + vpc_id = data.aws_vpc.default.id } data "aws_security_group" "default" { - vpc_id = "${data.aws_vpc.default.id}" + vpc_id = data.aws_vpc.default.id name = "default" } data "aws_ami" "amazon_linux" { most_recent = true + owners = ["amazon"] + filter { name = "name" @@ -110,3 +112,4 @@ data "aws_ami" "amazon_linux" { ] } } + diff --git a/examples/autoscalinggroup/outputs.tf b/examples/autoscalinggroup/outputs.tf index 896d1f3..f8fcce5 100644 --- a/examples/autoscalinggroup/outputs.tf +++ b/examples/autoscalinggroup/outputs.tf @@ -1,8 +1,12 @@ # terraform-null-label example used here: Output list of tags applied in each format output "tags_as_list_of_maps" { - value = ["${module.label.tags_as_list_of_maps}"] + value = module.label.tags_as_list_of_maps } output "tags" { - value = ["${module.label.tags}"] + value = module.label.tags +} + +output "id" { + value = module.label.id } diff --git a/examples/autoscalinggroup/versions.tf b/examples/autoscalinggroup/versions.tf new file mode 100644 index 0000000..7000b05 --- /dev/null +++ b/examples/autoscalinggroup/versions.tf @@ -0,0 +1,3 @@ +terraform { + required_version = "~> 0.12.0" +} diff --git a/examples/complete/label1.tf b/examples/complete/label1.tf new file mode 100644 index 0000000..a63ff98 --- /dev/null +++ b/examples/complete/label1.tf @@ -0,0 +1,38 @@ +module "label1" { + source = "../../" + namespace = "CloudPosse" + environment = "UAT" + stage = "build" + name = "Winston Churchroom" + attributes = ["fire", "water", "earth", "air"] + delimiter = "-" + + label_order = ["name", "environment", "stage", "attributes"] + + tags = { + "City" = "Dublin" + "Environment" = "Private" + } +} + +output "label1" { + value = { + id = module.label1.id + name = module.label1.name + namespace = module.label1.namespace + stage = module.label1.stage + attributes = module.label1.attributes + delimiter = module.label1.delimiter + } +} + +output "label1_tags" { + value = module.label1.tags +} + +output "label1_context" { + value = module.label1.context +} + + + diff --git a/examples/complete/label2.tf b/examples/complete/label2.tf new file mode 100644 index 0000000..3f23bf3 --- /dev/null +++ b/examples/complete/label2.tf @@ -0,0 +1,35 @@ +module "label2" { + source = "../../" + context = module.label1.context + name = "Charlie" + stage = "test" + delimiter = "+" + regex_replace_chars = "/[^a-zA-Z0-9-+]/" + + tags = { + "City" = "London" + "Environment" = "Public" + } +} + +output "label2" { + value = { + id = module.label2.id + name = module.label2.name + namespace = module.label2.namespace + stage = module.label2.stage + attributes = module.label2.attributes + delimiter = module.label2.delimiter + } +} + +output "label2_tags" { + value = module.label2.tags +} + +output "label2_context" { + value = module.label2.context +} + + + diff --git a/examples/complete/label3.tf b/examples/complete/label3.tf new file mode 100644 index 0000000..6340bf7 --- /dev/null +++ b/examples/complete/label3.tf @@ -0,0 +1,32 @@ +module "label3" { + source = "../../" + name = "Starfish" + stage = "release" + context = module.label1.context + delimiter = "." + regex_replace_chars = "/[^a-zA-Z0-9-.]/" + + tags = { + "Eat" = "Carrot" + "Animal" = "Rabbit" + } +} + +output "label3" { + value = { + id = module.label3.id + name = module.label3.name + namespace = module.label3.namespace + stage = module.label3.stage + attributes = module.label3.attributes + delimiter = module.label3.delimiter + } +} + +output "label3_tags" { + value = module.label3.tags +} + +output "label3_context" { + value = module.label3.context +} diff --git a/examples/complete/label4.tf b/examples/complete/label4.tf new file mode 100644 index 0000000..74e3ca1 --- /dev/null +++ b/examples/complete/label4.tf @@ -0,0 +1,34 @@ +module "label4" { + source = "../../" + namespace = "CloudPosse" + environment = "UAT" + name = "Example Cluster" + attributes = ["big", "fat", "honking", "cluster"] + delimiter = "-" + + label_order = ["namespace", "stage", "environment", "attributes"] + + tags = { + "City" = "Dublin" + "Environment" = "Private" + } +} + +output "label4" { + value = { + id = module.label4.id + name = module.label4.name + namespace = module.label4.namespace + stage = module.label4.stage + attributes = module.label4.attributes + delimiter = module.label4.delimiter + } +} + +output "label4_tags" { + value = module.label4.tags +} + +output "label4_context" { + value = module.label4.context +} diff --git a/examples/complete/label5.tf b/examples/complete/label5.tf new file mode 100644 index 0000000..0f0a76e --- /dev/null +++ b/examples/complete/label5.tf @@ -0,0 +1,33 @@ +module "label5" { + source = "../../" + enabled = false + namespace = "eg" + environment = "demo" + name = "blue" + attributes = ["cluster"] + delimiter = "-" + + label_order = ["namespace", "stage", "environment", "attributes"] + + tags = { + } +} + +output "label5" { + value = { + id = module.label5.id + name = module.label5.name + namespace = module.label5.namespace + stage = module.label5.stage + attributes = module.label5.attributes + delimiter = module.label5.delimiter + } +} + +output "label5_tags" { + value = module.label5.tags +} + +output "label5_context" { + value = module.label5.context +} diff --git a/examples/complete/main.tf b/examples/complete/main.tf deleted file mode 100644 index 291a75c..0000000 --- a/examples/complete/main.tf +++ /dev/null @@ -1,42 +0,0 @@ -module "label1" { - source = "../../" - namespace = "CloudPosse" - environment = "UAT" - stage = "build" - name = "Winston Churchroom" - attributes = ["fire", "water", "earth", "air"] - delimiter = "-" - - label_order = ["name", "environment", "stage", "attributes"] - - tags = { - "City" = "Dublin" - "Environment" = "Private" - } -} - -module "label2" { - source = "../../" - context = "${module.label1.context}" - name = "Charlie" - stage = "test" - delimiter = "+" - - tags = { - "City" = "London" - "Environment" = "Public" - } -} - -module "label3" { - source = "../../" - name = "Starfish" - stage = "release" - context = "${module.label1.context}" - delimiter = "." - - tags = { - "Eat" = "Carrot" - "Animal" = "Rabbit" - } -} diff --git a/examples/complete/outputs.tf b/examples/complete/outputs.tf deleted file mode 100644 index d89493a..0000000 --- a/examples/complete/outputs.tf +++ /dev/null @@ -1,53 +0,0 @@ -output "label1" { - value = { - id = "${module.label1.id}" - name = "${module.label1.name}" - namespace = "${module.label1.namespace}" - stage = "${module.label1.stage}" - attributes = "${module.label1.attributes}" - } -} - -output "label2" { - value = { - id = "${module.label2.id}" - name = "${module.label2.name}" - namespace = "${module.label2.namespace}" - stage = "${module.label2.stage}" - attributes = "${module.label2.attributes}" - } -} - -output "label3" { - value = { - id = "${module.label3.id}" - name = "${module.label3.name}" - namespace = "${module.label3.namespace}" - stage = "${module.label3.stage}" - attributes = "${module.label3.attributes}" - } -} - -output "label1_tags" { - value = "${module.label1.tags}" -} - -output "label1_context" { - value = "${module.label1.context}" -} - -output "label2_tags" { - value = "${module.label2.tags}" -} - -output "label2_context" { - value = "${module.label2.context}" -} - -output "label3_tags" { - value = "${module.label3.tags}" -} - -output "label3_context" { - value = "${module.label3.context}" -} diff --git a/examples/complete/versions.tf b/examples/complete/versions.tf new file mode 100644 index 0000000..77c9a35 --- /dev/null +++ b/examples/complete/versions.tf @@ -0,0 +1,4 @@ + +terraform { + required_version = "~> 0.12.0" +} diff --git a/main.tf b/main.tf index 483bb90..82d5cd0 100644 --- a/main.tf +++ b/main.tf @@ -1,98 +1,72 @@ locals { - enabled = "${var.enabled == "true" ? true : false }" - # Only maps that contain all the same attribute types can be merged, so the values have been set to list - context_struct = { - name = [] - namespace = [] - environment = [] - stage = [] - attributes = [] - tags_keys = [] - tags_values = [] - delimiter = [] - label_order = [] - regex_replace_chars = [] + defaults = { + label_order = ["namespace", "environment", "stage", "name", "attributes"] + delimiter = "-" + replacement = "" + # The `sentinel` should match the `regex_replace_chars`, so it will be replaced with the `replacement` value + sentinel = "~" + attributes = [""] } - # Merge the map of empty values, with the variable context, so that context_local always contains all map keys - context_local = "${merge(local.context_struct, var.context)}" + # The values provided by variables superceed the values inherited from the context - # Provided variables take precedence over the variables from the provided context IF they're not the default - # if thing == default and if local_context[thing] != "" - # local_context[thing] - # else - # thing + enabled = var.enabled + regex_replace_chars = coalesce(var.regex_replace_chars, var.context.regex_replace_chars) + + name = lower(replace(coalesce(var.name, var.context.name, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement)) + namespace = lower(replace(coalesce(var.namespace, var.context.namespace, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement)) + environment = lower(replace(coalesce(var.environment, var.context.environment, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement)) + stage = lower(replace(coalesce(var.stage, var.context.stage, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement)) + delimiter = coalesce(var.delimiter, var.context.delimiter, local.defaults.delimiter) + label_order = length(var.label_order) > 0 ? var.label_order : (length(var.context.label_order) > 0 ? var.context.label_order : local.defaults.label_order) + additional_tag_map = merge(var.context.additional_tag_map, var.additional_tag_map) - regex_replace_chars_context = "${concat(local.context_local["regex_replace_chars"], list(""))}" - regex_replace_chars_context_or_default = "${length(local.regex_replace_chars_context[0]) > 0 ? local.regex_replace_chars_context[0] : var.regex_replace_chars}" - regex_replace_chars = "${var.regex_replace_chars != "" ? var.regex_replace_chars : local.regex_replace_chars_context_or_default}" - names = "${concat(local.context_local["name"], list(""))}" - name_context_or_default = "${length(local.names[0]) > 0 ? local.names[0] : var.name}" - name_or_context = "${var.name != "" ? var.name : local.name_context_or_default}" - name = "${lower(replace(local.name_or_context, local.regex_replace_chars, ""))}" - namespaces = "${concat(local.context_local["namespace"], list(""))}" - namespace_context_or_default = "${length(local.namespaces[0]) > 0 ? local.namespaces[0] : var.namespace}" - namespace_or_context = "${var.namespace != "" ? var.namespace : local.namespace_context_or_default}" - namespace = "${lower(replace(local.namespace_or_context, local.regex_replace_chars, ""))}" - environments = "${concat(local.context_local["environment"], list(""))}" - environment_context_or_default = "${length(local.environments[0]) > 0 ? local.environments[0] : var.environment}" - environment_or_context = "${var.environment != "" ? var.environment : local.environment_context_or_default}" - environment = "${lower(replace(local.environment_or_context, local.regex_replace_chars, ""))}" - stages = "${concat(local.context_local["stage"], list(""))}" - stage_context_or_default = "${length(local.stages[0]) > 0 ? local.stages[0] : var.stage}" - stage_or_context = "${var.stage != "" ? var.stage : local.stage_context_or_default}" - stage = "${lower(replace(local.stage_or_context, local.regex_replace_chars, ""))}" - delimiters = "${concat(local.context_local["delimiter"], list(""))}" - delimiter_context_or_default = "${length(local.delimiters[0]) > 0 ? local.delimiters[0] : var.delimiter}" - delimiter = "${var.delimiter != "-" ? var.delimiter : local.delimiter_context_or_default}" # Merge attributes - attributes = ["${distinct(compact(concat(var.attributes, local.context_local["attributes"])))}"] - # Generate tags (don't include tags with empty values) - generated_tags = "${zipmap( - compact(list("Name", local.namespace != "" ? "Namespace" : "", local.environment != "" ? "Environment" : "", local.stage != "" ? "Stage" : "")), - compact(list(local.id, local.namespace, local.environment, local.stage)) - )}" - tags = "${merge(zipmap(local.context_local["tags_keys"], local.context_local["tags_values"]), local.generated_tags, var.tags)}" - tags_as_list_of_maps = ["${data.null_data_source.tags_as_list_of_maps.*.outputs}"] - label_order_default_list = "${list("namespace", "environment", "stage", "name", "attributes")}" - label_order_context_list = "${distinct(compact(local.context_local["label_order"]))}" - label_order_final_list = ["${distinct(compact(coalescelist(var.label_order, local.label_order_context_list, local.label_order_default_list)))}"] - label_order_length = "${(length(local.label_order_final_list))}" + attributes = compact(distinct(concat(var.attributes, var.context.attributes, local.defaults.attributes))) + + generated_tags = { for l in keys(local.id_context) : title(l) => local.id_context[l] if length(local.id_context[l]) > 0 } + + tags = merge(var.context.tags, local.generated_tags, var.tags) + tags_as_list_of_maps = data.null_data_source.tags_as_list_of_maps.*.outputs + + id_context = { + name = local.name + namespace = local.namespace + environment = local.environment + stage = local.stage + attributes = lower(replace(join(local.delimiter, local.attributes), local.regex_replace_chars, local.defaults.replacement)) + } + + labels = [for l in local.label_order : local.id_context[l] if length(local.id_context[l]) > 0] + + id = lower(join(local.delimiter, local.labels)) + # Context of this label to pass to other label modules output_context = { - name = ["${local.name}"] - namespace = ["${local.namespace}"] - environment = ["${local.environment}"] - stage = ["${local.stage}"] - attributes = ["${local.attributes}"] - tags_keys = ["${keys(local.tags)}"] - tags_values = ["${values(local.tags)}"] - delimiter = ["${local.delimiter}"] - label_order = ["${local.label_order_final_list}"] - regex_replace_chars = ["${local.regex_replace_chars}"] + enabled = local.enabled + name = local.name + namespace = local.namespace + environment = local.environment + stage = local.stage + attributes = local.attributes + tags = local.tags + delimiter = local.delimiter + label_order = local.label_order + regex_replace_chars = local.regex_replace_chars + additional_tag_map = local.additional_tag_map } - id_context = { - name = "${local.name}" - namespace = "${local.namespace}" - environment = "${local.environment}" - stage = "${local.stage}" - attributes = "${lower(join(local.delimiter, local.attributes))}" - } - id = "${lower(join(local.delimiter, compact(list( - "${local.label_order_length > 0 ? local.id_context[element(local.label_order_final_list, 0)] : ""}", - "${local.label_order_length > 1 ? local.id_context[element(local.label_order_final_list, 1)] : ""}", - "${local.label_order_length > 2 ? local.id_context[element(local.label_order_final_list, 2)] : ""}", - "${local.label_order_length > 3 ? local.id_context[element(local.label_order_final_list, 3)] : ""}", - "${local.label_order_length > 4 ? local.id_context[element(local.label_order_final_list, 4)] : ""}"))))}" + } data "null_data_source" "tags_as_list_of_maps" { - count = "${local.enabled ? length(keys(local.tags)) : 0}" + count = local.enabled ? length(keys(local.tags)) : 0 - inputs = "${merge(map( - "key", "${element(keys(local.tags), count.index)}", - "value", "${element(values(local.tags), count.index)}" - ), - var.additional_tag_map)}" + inputs = merge( + { + "key" = element(keys(local.tags), count.index) + "value" = element(values(local.tags), count.index) + }, + var.additional_tag_map, + ) } diff --git a/outputs.tf b/outputs.tf index 2529859..3a3ae9a 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,54 +1,55 @@ output "id" { - value = "${local.enabled ? local.id : ""}" + value = local.enabled ? local.id : "" description = "Disambiguated ID" } output "name" { - value = "${local.enabled ? local.name : ""}" + value = local.enabled ? local.name : "" description = "Normalized name" } output "namespace" { - value = "${local.enabled ? local.namespace : ""}" + value = local.enabled ? local.namespace : "" description = "Normalized namespace" } output "stage" { - value = "${local.enabled ? local.stage : ""}" + value = local.enabled ? local.stage : "" description = "Normalized stage" } output "environment" { - value = "${local.enabled ? local.environment : ""}" + value = local.enabled ? local.environment : "" description = "Normalized environment" } output "attributes" { - value = "${local.attributes}" + value = local.enabled ? local.attributes : [] description = "List of attributes" } output "delimiter" { - value = "${local.enabled ? local.delimiter : ""}" + value = local.enabled ? local.delimiter : "" description = "Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes`" } output "tags" { - value = "${local.tags}" + value = local.enabled ? local.tags : {} description = "Normalized Tag map" } output "tags_as_list_of_maps" { - value = ["${local.tags_as_list_of_maps}"] + value = local.tags_as_list_of_maps description = "Additional tags as a list of maps, which can be used in several AWS resources" } output "context" { - value = "${local.output_context}" + value = local.output_context description = "Context of this module to pass to other label modules" } output "label_order" { - value = "${local.label_order_final_list}" + value = local.label_order description = "The naming order of the id output and Name tag" } + diff --git a/provider.tf b/provider.tf index 334ca8e..34da279 100644 --- a/provider.tf +++ b/provider.tf @@ -1,3 +1,4 @@ provider "null" { version = "~> 2.1" } + diff --git a/terraform.tf b/terraform.tf index 2b4f2e8..2bf40fc 100644 --- a/terraform.tf +++ b/terraform.tf @@ -1,3 +1,4 @@ terraform { - required_version = "~> 0.11.0" + required_version = "~> 0.12.0" } + diff --git a/test/src/Makefile b/test/src/Makefile index cd643f8..79eee12 100644 --- a/test/src/Makefile +++ b/test/src/Makefile @@ -1,42 +1,50 @@ PACKAGE = terraform-null-label +GOEXE ?= /usr/bin/go GOPATH = $(CURDIR)/.gopath GOBIN = $(GOPATH)/bin BASE = $(GOPATH)/src/$(PACKAGE) PATH := $(PATH):$(GOBIN) +export TF_DATA_DIR ?= $(CURDIR)/.terraform +export TF_CLI_ARGS_init ?= -get-plugins=true +export GOPATH + .PHONY: all ## Default target all: test --include Makefile.* +ifneq (,$(wildcard /sbin/apk)) +## Install go, if not installed +$(GOEXE): + apk add --update go +endif -export GOPATH +ifeq ($(shell uname -s),Linux) +## Install all `dep`, if not installed +$(GOBIN)/dep: + @mkdir -p $(GOBIN) + @curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh +endif ## Prepare the GOPATH -$(BASE): +$(BASE): $(GOEXE) @mkdir -p $(dir $@) @ln -sf $(CURDIR) $@ -## install OS-specific dependencies. See Makefile.* -deps:: - @exit 0 +## Download vendor dependencies to vendor/ +$(BASE)/vendor: $(BASE) $(GOBIN)/dep + cd $(BASE) && dep ensure .PHONY : init ## Initialize tests -init: | $(BASE) - -.PHONY : ensure -## Install test dependencies -ensure: - cd $(BASE) && dep ensure +init: $(BASE)/vendor .PHONY : test ## Run tests -test: deps ensure +test: init cd $(BASE) && go test -v -timeout 30m -run TestExamplesComplete +.PHONY : clean ## Clean up files clean: - rm -rf .gopath/ vendor/ - - + rm -rf .gopath/ vendor/ $(TF_DATA_DIR) diff --git a/test/src/Makefile.alpine b/test/src/Makefile.alpine deleted file mode 100644 index 4480d96..0000000 --- a/test/src/Makefile.alpine +++ /dev/null @@ -1,9 +0,0 @@ -ifneq (,$(wildcard /sbin/apk)) -.PHONY : deps -## Install all dependencies for alpine -deps:: - mkdir -p $(GOBIN) - apk add --update go - @curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh -endif - diff --git a/test/src/examples_complete_test.go b/test/src/examples_complete_test.go index bf16a45..87b906c 100644 --- a/test/src/examples_complete_test.go +++ b/test/src/examples_complete_test.go @@ -14,6 +14,7 @@ func TestExamplesComplete(t *testing.T) { terraformOptions := &terraform.Options{ // The path to where our Terraform code is located TerraformDir: "../../examples/complete", + Upgrade: true, } // At the end of the test, run `terraform destroy` to clean up any resources that were created @@ -39,4 +40,16 @@ func TestExamplesComplete(t *testing.T) { // Verify we're getting back the outputs we expect assert.Equal(t, "starfish.uat.release.fire.water.earth.air", label3["id"]) + + // Run `terraform output` to get the value of an output variable + label4 := terraform.OutputMap(t, terraformOptions, "label4") + + // Verify we're getting back the outputs we expect + assert.Equal(t, "cloudposse-uat-big-fat-honking-cluster", label4["id"]) + + // Run `terraform output` to get the value of an output variable + label5 := terraform.OutputMap(t, terraformOptions, "label5") + + // Verify we're getting back the outputs we expect + assert.Equal(t, "", label5["id"]) } diff --git a/variables.tf b/variables.tf index 00e6a2e..50e35ac 100644 --- a/variables.tf +++ b/variables.tf @@ -1,71 +1,96 @@ variable "namespace" { - type = "string" + type = string default = "" description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'" } variable "environment" { - type = "string" + type = string default = "" description = "Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT'" } variable "stage" { - type = "string" + type = string default = "" description = "Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release'" } variable "name" { - type = "string" + type = string default = "" description = "Solution name, e.g. 'app' or 'jenkins'" } variable "enabled" { - type = "string" - default = "true" + type = bool + default = true description = "Set to false to prevent the module from creating any resources" } variable "delimiter" { - type = "string" + type = string default = "-" description = "Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes`" } variable "attributes" { - type = "list" + type = list(string) default = [] description = "Additional attributes (e.g. `1`)" } variable "tags" { - type = "map" + type = map(string) default = {} description = "Additional tags (e.g. `map('BusinessUnit','XYZ')`" } variable "additional_tag_map" { - type = "map" + type = map(string) default = {} description = "Additional tags for appending to each tag map" } variable "context" { - type = "map" - default = {} + type = object({ + namespace = string + environment = string + stage = string + name = string + enabled = bool + delimiter = string + attributes = list(string) + label_order = list(string) + tags = map(string) + additional_tag_map = map(string) + regex_replace_chars = string + }) + default = { + namespace = "" + environment = "" + stage = "" + name = "" + enabled = true + delimiter = "" + attributes = [] + label_order = [] + tags = {} + additional_tag_map = {} + regex_replace_chars = "" + } description = "Default context to use for passing state between label invocations" } variable "label_order" { - type = "list" + type = list(string) default = [] description = "The naming order of the id output and Name tag" } variable "regex_replace_chars" { - type = "string" + type = string default = "/[^a-zA-Z0-9-]/" description = "Regex to replace chars with empty string in `namespace`, `environment`, `stage` and `name`. By default only hyphens, letters and digits are allowed, all other chars are removed" } +