Skip to content

Commit

Permalink
Convert to TF 0.12. Add Codefresh. Add tests (#15)
Browse files Browse the repository at this point in the history
* Update to Terraform `0.12`. Add Codefresh. Add tests

* Update to Terraform `0.12`. Add Codefresh. Add tests

* Update README

* Update Gopkg.toml
  • Loading branch information
aknysh authored Jun 18, 2019
1 parent a873805 commit 0cc7237
Show file tree
Hide file tree
Showing 21 changed files with 388 additions and 153 deletions.
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2018 Cloud Posse, LLC
Copyright 2018-2019 Cloud Posse, LLC

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
71 changes: 43 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

[![Cloud Posse][logo]](https://cpco.io/homepage)

# terraform-terraform-label [![Build Status](https://travis-ci.org/cloudposse/terraform-terraform-label.svg?branch=master)](https://travis-ci.org/cloudposse/terraform-terraform-label) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-terraform-label.svg)](https://github.com/cloudposse/terraform-terraform-label/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)
# terraform-terraform-label [![Codefresh Build Status](https://g.codefresh.io/api/badges/pipeline/cloudposse/cloudposse%2Fterraform-null-label%2Ftest?type=cf-1)](https://g.codefresh.io/public/accounts/cloudposse/pipelines/cloudposse/terraform-null-label/test) [![Latest Release](https://img.shields.io/github/release/cloudposse/terraform-terraform-label.svg)](https://github.com/cloudposse/terraform-terraform-label/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)


Terraform module designed to generate consistent label names and tags for resources. Use `terraform-terraform-label` to implement a strict naming convention.
Expand All @@ -15,7 +15,7 @@ A label follows the following convention: `{namespace}-{stage}-{name}-{attribute

It's recommended to use one `terraform-terraform-label` module for every unique resource of a given resource type.
For example, if you have 10 instances, there should be 10 different labels.
However, if you have multiple different kinds of resources (e.g. instances, security groups, file systems, and elastic ips), then they can all share the same label assuming they are logically related.
However, if you have multiple different kinds of resources (e.g. instances, security groups, file systems, and elastic IPs), then they can all share the same label assuming they are logically related.

All [Cloud Posse modules](https://github.com/cloudposse?utf8=%E2%9C%93&q=terraform-&type=&language=) use this module to ensure resources can be instantiated multiple times within an account and without conflict.

Expand Down Expand Up @@ -55,6 +55,11 @@ We literally have [*hundreds of terraform modules*][terraform_modules] that are

## Usage


**IMPORTANT:** The `master` branch is used in `source` just as an example. In your code, do not pin to `master` because there may be breaking changes between releases.
Instead pin to the release tag (e.g. `?ref=tags/x.y.z`) of one of our [latest releases](https://github.com/cloudposse/terraform-terraform-label/releases).


### Simple Example

Include this repository as a module in your existing terraform code:
Expand All @@ -67,7 +72,11 @@ module "eg_prod_bastion_label" {
name = "bastion"
attributes = ["public"]
delimiter = "-"
tags = "${map("BusinessUnit", "XYZ", "Snapshot", "true")}"
tags = {
"BusinessUnit" = "XYZ",
"Snapshot" = "true"
}
}
```

Expand All @@ -78,17 +87,17 @@ Now reference the label when creating an instance (for example):
```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
}
```

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
Expand All @@ -111,12 +120,15 @@ module "eg_prod_bastion_abc_label" {
name = "bastion"
attributes = ["abc"]
delimiter = "-"
tags = "${map("BusinessUnit", "ABC")}"
tags = {
"BusinessUnit" = "ABC"
}
}
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
Expand All @@ -127,23 +139,26 @@ 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" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=master"
namespace = "eg"
stage = "prod"
name = "bastion"
attributes = ["xyz"]
delimiter = "-"
tags = "${map("BusinessUnit", "XYZ")}"
tags = {
"BusinessUnit" = "XYZ"
}
}
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
Expand All @@ -154,8 +169,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]
}
```

Expand All @@ -174,25 +189,25 @@ Available targets:
lint Lint terraform code
```

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|:----:|:-----:|:-----:|
| attributes | Additional attributes, e.g. `1` | list | `<list>` | no |
| convert_case | Convert fields to lower case | string | `true` | no |
| delimiter | Delimiter to be used between `namespace`, `name`, `stage` and `attributes` | string | `-` | no |
| enabled | Set to false to prevent the module from creating any resources | string | `true` | no |
| name | Solution name, e.g. `app` | string | - | yes |
| namespace | Namespace, which could be your organization name, e.g. `cp` or `cloudposse` | string | - | yes |
| stage | Stage, e.g. `prod`, `staging`, `dev`, or `test` | string | - | yes |
| tags | Additional tags (e.g. `map(`BusinessUnit`,`XYZ`) | map | `<map>` | no |
| attributes | Additional attributes (e.g. `1`) | list(string) | `<list>` | no |
| convert_case | Convert fields to lower case | bool | `true` | no |
| delimiter | Delimiter to be used between `namespace`, `stage`, `name` and `attributes` | string | `-` | no |
| enabled | Set to false to prevent the module from creating any resources | bool | `true` | 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 |
| stage | Stage, e.g. 'prod', 'staging', 'dev' | string | `` | no |
| tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map(string) | `<map>` | no |

## Outputs

| Name | Description |
|------|-------------|
| attributes | Normalized attributes |
| delimiter | Delimiter between `namespace`, `stage`, `name` and `attributes` |
| id | Disambiguated ID |
| name | Normalized name |
| namespace | Normalized namespace |
Expand Down Expand Up @@ -279,7 +294,7 @@ In general, PRs are welcome. We follow the typical "fork-and-pull" Git workflow.

## Copyright

Copyright © 2017-2018 [Cloud Posse, LLC](https://cpco.io/copyright)
Copyright © 2017-2019 [Cloud Posse, LLC](https://cpco.io/copyright)



Expand Down
50 changes: 30 additions & 20 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ github_repo: cloudposse/terraform-terraform-label

# Badges to display
badges:
- name: "Build Status"
image: "https://travis-ci.org/cloudposse/terraform-terraform-label.svg?branch=master"
url: "https://travis-ci.org/cloudposse/terraform-terraform-label"
- name: "Codefresh Build Status"
image: "https://g.codefresh.io/api/badges/pipeline/cloudposse/cloudposse%2Fterraform-null-label%2Ftest?type=cf-1"
url: "https://g.codefresh.io/public/accounts/cloudposse/pipelines/cloudposse/terraform-null-label/test"
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/terraform-terraform-label.svg"
url: "https://github.com/cloudposse/terraform-terraform-label/releases/latest"
Expand All @@ -44,7 +44,7 @@ description: |-
It's recommended to use one `terraform-terraform-label` module for every unique resource of a given resource type.
For example, if you have 10 instances, there should be 10 different labels.
However, if you have multiple different kinds of resources (e.g. instances, security groups, file systems, and elastic ips), then they can all share the same label assuming they are logically related.
However, if you have multiple different kinds of resources (e.g. instances, security groups, file systems, and elastic IPs), then they can all share the same label assuming they are logically related.
All [Cloud Posse modules](https://github.com/cloudposse?utf8=%E2%9C%93&q=terraform-&type=&language=) use this module to ensure resources can be instantiated multiple times within an account and without conflict.
Expand All @@ -64,7 +64,11 @@ usage: |-
name = "bastion"
attributes = ["public"]
delimiter = "-"
tags = "${map("BusinessUnit", "XYZ", "Snapshot", "true")}"
tags = {
"BusinessUnit" = "XYZ",
"Snapshot" = "true"
}
}
```
Expand All @@ -75,17 +79,17 @@ 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
}
```
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
Expand All @@ -108,12 +112,15 @@ usage: |-
name = "bastion"
attributes = ["abc"]
delimiter = "-"
tags = "${map("BusinessUnit", "ABC")}"
tags = {
"BusinessUnit" = "ABC"
}
}
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
Expand All @@ -124,23 +131,26 @@ 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" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
source = "git::https://github.com/cloudposse/terraform-terraform-label.git?ref=master"
namespace = "eg"
stage = "prod"
name = "bastion"
attributes = ["xyz"]
delimiter = "-"
tags = "${map("BusinessUnit", "XYZ")}"
tags = {
"BusinessUnit" = "XYZ"
}
}
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
Expand All @@ -151,8 +161,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]
}
```
Expand Down
63 changes: 63 additions & 0 deletions codefresh/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
version: '1.0'

stages:
- Prepare
- Test

steps:
main_clone:
title: "Clone repository"
type: git-clone
stage: Prepare
description: "Initialize"
repo: ${{CF_REPO_OWNER}}/${{CF_REPO_NAME}}
git: CF-default
revision: ${{CF_REVISION}}

clean_init:
title: Prepare build-harness and test-harness
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
- 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 {} \;

test:
type: "parallel"
title: "Run tests"
description: "Run all tests in parallel"
stage: Test
steps:
test_readme_lint:
title: "Test README.md updated"
stage: "Test"
image: ${{TEST_IMAGE}}
description: Test "readme/lint"
commands:
- make readme/lint

test_module:
title: Test module with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ module

test_examples_complete:
title: Test "examples/complete" with bats
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/ examples/complete

test_examples_complete_terratest:
title: Test "examples/complete" with terratest
image: ${{TEST_IMAGE}}
stage: Test
commands:
- make -C test/src
Loading

0 comments on commit 0cc7237

Please sign in to comment.