Skip to content

Commit ca9f1d9

Browse files
authored
Merge pull request #2 from oscr/feature/add-tag-conversion
Add tag conversion support
2 parents 1c4f238 + a10a0b2 commit ca9f1d9

File tree

13 files changed

+216
-19
lines changed

13 files changed

+216
-19
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
crash.log
1313

1414
# Exclude all .tfvars files, which are likely to contain sentitive data, such as
15-
# password, private keys, and other secrets. These should not be part of version
16-
# control as they are data points which are potentially sensitive and subject
15+
# password, private keys, and other secrets. These should not be part of version
16+
# control as they are data points which are potentially sensitive and subject
1717
# to change depending on the environment.
1818
#
1919
*.tfvars

Makefile

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
.PHONY: static-tests unit-tests integration-tests e2e-tests init
2+
3+
# OS can be "Linux" or "macOS"
4+
OS ?= Linux
5+
# ARCH can be "x86_64" or "arm64"
6+
ARCH ?= x86_64
7+
8+
TERRAFORM_VERSION := 1.0.10
9+
TFLINT_VERSION := 0.33.1
10+
11+
12+
SHELL := /usr/bin/env bash
13+
14+
static-tests: setup-env
15+
rm .terraform.lock.hcl plan.out plan.out.json 2> /dev/null || true
16+
# should not require any aws credentials to test against, should be safe to run as github checks on pull requests
17+
terraform init || ( echo 'FAILED: terraform init failed'; exit 1 )
18+
terraform validate || ( echo 'FAILED: terraform validate failed'; exit 1 )
19+
terraform fmt -check -recursive ./ || ( echo 'FAILED: all tf files should be formatted using "terraform fmt -recursive ./"'; exit 1 )
20+
tflint --init && tflint --var='region=us-west-1' --var='profile=default' ./ || ( echo 'FAILED: tflint found issues'; exit 1 )
21+
22+
unit-tests: setup-env
23+
# Should test code paths in an individual module. terratest, or `terraform test`, this is where you want to test different regions, use retries to smooth transient errors
24+
# Should not run automatically on PR's from un-trusted contributors
25+
export PATH=$(shell pwd)/build/bin:$${PATH} &&\
26+
cd test && \
27+
go test -timeout 30m -json | tee >(go-test-report) | jq -jr .Output 2> /dev/null | sed 's/null//g';\
28+
retval_bash="$${PIPESTATUS[0]}" retval_zsh="$${pipestatus[1]}" ;\
29+
exit $$retval_bash $$retval_zsh
30+
31+
integration-tests:
32+
# Should test code paths in a module of modules and run when on eof the sub-modules is updated. terratest, or `terraform test` use retries to smooth transient errors
33+
# Should not run automatically on PR's from un-trusted contributors, and should only be run on modules where one sub-module is changed
34+
echo "todo"
35+
exit 1
36+
37+
e2e-tests:
38+
# Should test code paths in `deploy/` module. Unsure whether it should use tf cloud. terratest, or `terraform test`.
39+
# For deploys that take long you could skip destroy between runs, so e2e is just updating what changed from last iteration, use retries to smooth transient errors.
40+
# Should not run automatically on PR's from any contributors. Update(no destroy) tests run on `/do-e2e-tests` PR comment from maintainers. Full e2e run on release.
41+
echo "todo"
42+
exit 1
43+
44+
setup-env:
45+
# using a bin path specific to this project so that different projects can use different versions of the tooling
46+
mkdir -p build/bin/ &&\
47+
export PATH=$(shell pwd)/build/bin:$${PATH} &&\
48+
export TF_ARCH=$(shell echo $(ARCH) | sed 's/x86_64/amd64/') &&\
49+
export TF_OS=$(shell echo $(OS) | tr '[:upper:]' '[:lower:]' | sed 's/macos/darwin/') &&\
50+
export CT_OS=$(shell echo $(OS) | sed 's/macOS/Darwin/') &&\
51+
if [ "$$(terraform -v | head -n 1 | sed 's/Terraform v//')" != "$(TERRAFORM_VERSION)" ]; then \
52+
wget -O tf.zip https://releases.hashicorp.com/terraform/$(TERRAFORM_VERSION)/terraform_$(TERRAFORM_VERSION)_$${TF_OS}_$${TF_ARCH}.zip &&\
53+
unzip -o tf.zip terraform &&\
54+
rm tf.zip &&\
55+
mv -fv terraform build/bin/ ;\
56+
fi ;\
57+
if [ "$$(tflint --version | head -n 1 | sed 's/TFLint version //')" != "$(TFLINT_VERSION)" ]; then \
58+
wget -O tflint.zip https://github.com/terraform-linters/tflint/releases/download/v$(TFLINT_VERSION)/tflint_$${TF_OS}_$${TF_ARCH}.zip &&\
59+
unzip -o tflint.zip tflint &&\
60+
rm tflint.zip &&\
61+
mv -fv tflint build/bin/ ;\
62+
fi

examples/main.tf renamed to examples/basic/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ provider "awscc" {
33
}
44

55
module "labels" {
6-
source = "../"
6+
source = "../.."
77

88
name = "measurements"
99
namespace = "link"
File renamed without changes.
File renamed without changes.

examples/formatted_tags/main.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
provider "aws" {
2+
region = var.region
3+
}
4+
5+
provider "awscc" {
6+
region = var.region
7+
}
8+
9+
locals {
10+
# In the AWS provider format
11+
aws_tags = {
12+
"service" : "authorize",
13+
"managed_by" : "terraform"
14+
}
15+
16+
# In the AWSCC provider format
17+
awcc_tags = [
18+
{ "key" : "service", "value" : "measurements" },
19+
{ "key" = "managed_by", "value" : "terraform" }
20+
]
21+
}
22+
23+
# Using aws provider format as input
24+
module "aws_labels" {
25+
source = "../.."
26+
27+
tags = local.aws_tags
28+
}
29+
30+
# Using awscc provider format as input
31+
module "awscc_labels" {
32+
source = "../.."
33+
34+
tags = local.awcc_tags
35+
}
36+
37+
module "test_aws_to_aws" {
38+
source = "../../internal/modules/aws"
39+
40+
tags = module.aws_labels.tags_aws
41+
}
42+
43+
module "test_aws_to_awscc" {
44+
source = "../../internal/modules/awscc"
45+
46+
tags = module.aws_labels.tags
47+
}
48+
49+
module "test_awscc_to_aws" {
50+
source = "../../internal/modules/aws"
51+
52+
tags = module.awscc_labels.tags_aws
53+
}
54+
55+
module "test_awscc_to_awscc" {
56+
source = "../../internal/modules/awscc"
57+
58+
tags = module.awscc_labels.tags
59+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
variable "region" {
2+
type = string
3+
default = "eu-west-1"
4+
description = "What AWS region to deploy resources in."
5+
}

internal/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Warning
2+
Do not use anything within the `internal` folder. It is only used for testing and verification of the module.

internal/modules/aws/main.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Do not use. This This module only exists to verify the tag formatting.
3+
*/
4+
5+
variable "tags" {
6+
description = "tags, which could be used for additional tags"
7+
type = map(string)
8+
}
9+
10+
resource "aws_iam_policy" "policy" {
11+
description = "A temporary policy. Should be deleted by the test."
12+
name_prefix = "test_policy"
13+
14+
policy = jsonencode({
15+
Version = "2012-10-17"
16+
Statement = [
17+
{
18+
Action = [
19+
"ec2:Describe*",
20+
]
21+
Effect = "Deny"
22+
Resource = "*"
23+
},
24+
]
25+
})
26+
27+
tags = var.tags
28+
}
29+

internal/modules/awscc/main.tf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Do not use. This This module only exists to verify the tag formatting.
3+
*/
4+
5+
variable "tags" {
6+
description = "tags, which could be used for additional tags"
7+
}
8+
9+
resource "awscc_iam_role" "role" {
10+
description = "A temporary role. Should be deleted by the test."
11+
12+
assume_role_policy_document = jsonencode({
13+
Version = "2012-10-17"
14+
Statement = [
15+
{
16+
Action = "sts:AssumeRole"
17+
Effect = "Allow"
18+
Principal = {
19+
Service = "ec2.amazonaws.com"
20+
}
21+
},
22+
]
23+
})
24+
25+
tags = var.tags
26+
}
27+

0 commit comments

Comments
 (0)