Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
snemetz committed Dec 30, 2017
0 parents commit c5737ad
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.tfstate
*.tfstate.backup
.terraform
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# terraform-local-label

Terraform module to provide consistent label names and tags for resources.

A single name format will not solve every use case, so multiple variants are returned and there is a few options to affect how they get build. The general name convention is `{organization}-{environment}-{name}-{attributes}`. `Name` is required, the other 3 can be turned on/off individually. The delimiter (`-`) can be changed

All [devops-workflow](https://registry.terraform.io/search?q=devops-workflow&verified=false) modules will eventually use this.

**NOTE:** `local` refers to this using [locals](https://www.terraform.io/docs/configuration/locals.html) and does not create any resources. It just builds new variables.

Terraform registry: https://registry.terraform.io/modules/devops-workflow/label/local

## Usage:

#### Basic Example

```hcl
module "name" {
source = "devops-workflow/label/local"
version = "0.1.2"
name = "name"
environment = "qa"
}
```
This will create an `id` with the value of `qa-name`

#### S3 Example

```hcl
module "s3-name" {
source = "devops-workflow/label/local"
version = "0.1.2"
name = "data"
environment = "qa"
organization = "corp"
namespace-org = "true"
}
```
This will create an `id` with the value of `corp-qa-data`

Now reference `label` outputs to create the S3 bucket

```hcl
resource "aws_s3_bucket" "data" {
bucket = "${module.s3-name.id}"
tags = "${module.s3-name.tags}"
}
```

#### All Variables Example
Using in a module and exposing all settings to upstream caller.

```hcl
module "label" {
source = "devops-workflow/label/local"
version = "0.1.2"
organization = "${var.organization}"
name = "${var.name}"
namespace-env = "${var.namespace-env}"
namespace-org = "${var.namespace-org}"
environment = "${var.environment}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
tags = "${var.tags}"
}
```
127 changes: 127 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#
# Terraform module to provide consistent naming
#
# TODO:
# Change where replace is done. Move to earlier in process. On initial `name`?
# Create tags_asg list from tags map. If possible
# New input tags_asg -> tags_asg with standard tags added

module "autoscaling_group" {
source = "devops-workflow/boolean/local"
version = "0.1.1"
value = "${var.autoscaling_group}"
}
module "enabled" {
source = "devops-workflow/boolean/local"
version = "0.1.1"
value = "${var.enabled}"
}
module "namespace-env" {
source = "devops-workflow/boolean/local"
version = "0.1.1"
value = "${var.namespace-env}"
}
module "namespace-org" {
source = "devops-workflow/boolean/local"
version = "0.1.1"
value = "${var.namespace-org}"
}

locals = {
attr = "${lower(format("%s", join(var.delimiter, compact(var.attributes))))}"
env = "${lower(format("%s", var.environment))}"
org = "${lower(format("%s", var.organization))}"
}
resource "null_resource" "names" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
id = "${replace(lower(format("%s", element(var.names, count.index))),"_","-")}"
}
}
resource "null_resource" "env" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
id = "${module.namespace-env.value ?
join(var.delimiter, list(local.env, element(null_resource.names.*.triggers.id, count.index))) :
element(null_resource.names.*.triggers.id, count.index)}"
}
}
resource "null_resource" "org" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
id = "${module.namespace-org.value ?
join(var.delimiter, list(local.org, element(null_resource.env.*.triggers.id, count.index))) :
element(null_resource.env.*.triggers.id, count.index)}"
}
}
resource "null_resource" "ids" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
id = "${length(local.attr) > 0 ?
join(var.delimiter, list(element(null_resource.org.*.triggers.id, count.index), local.attr)) :
element(null_resource.org.*.triggers.id, count.index)}"
}
}
resource "null_resource" "ids-trunc" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
id_20 = "${substr(element(null_resource.ids.*.triggers.id, count.index),0,19 <= length(element(null_resource.ids.*.triggers.id, count.index)) ? 19 : length(element(null_resource.ids.*.triggers.id, count.index)))}"
id_32 = "${substr(element(null_resource.ids.*.triggers.id, count.index),0,31 <= length(element(null_resource.ids.*.triggers.id, count.index)) ? 31 : length(element(null_resource.ids.*.triggers.id, count.index)))}"
org_attr_20 = "${min(18 - length(local.attr), length(element(null_resource.org.*.triggers.id, count.index)))}"
org_attr_32 = "${min(30 - length(local.attr), length(element(null_resource.org.*.triggers.id, count.index)))}"
}
}
resource "null_resource" "ids-trunc-attr" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
id_attr_20 = "${19 <= length(element(null_resource.ids.*.triggers.id, count.index)) ?
join(var.delimiter,
list(
substr(element(null_resource.org.*.triggers.id, count.index),0,
element(null_resource.ids-trunc.*.triggers.org_attr_20, count.index) >= 0 ?
element(null_resource.ids-trunc.*.triggers.org_attr_20, count.index) : 0)
),
list(local.attr)
)
: element(null_resource.ids.*.triggers.id, count.index)}"
id_attr_32 = "${31 <= length(element(null_resource.ids.*.triggers.id, count.index)) ?
join(var.delimiter,
list(
substr(element(null_resource.org.*.triggers.id, count.index),0,
element(null_resource.ids-trunc.*.triggers.org_attr_32, count.index) >= 0 ?
element(null_resource.ids-trunc.*.triggers.org_attr_32, count.index) : 0)
),
list(local.attr)
)
: element(null_resource.ids.*.triggers.id, count.index)}"
}
}
/*
resource "null_resource" "tags" {
count = "${module.enabled.value ? length(var.names) : 0}"
triggers = {
#TODO: only add Organization if not ""
tags = "${ merge(
var.tags,
map(
"Name", "${element(null_resource.ids.*.triggers.id, count.index)}",
"Environment", "${local.env}",
"Organization", "${local.org}",
"Terraform", "true"
)
)}"
}
}
*/
/*
resource "null_resource" "this" {
count = "${module.enabled.value ? length(var.names) : 0}"
# TODO: change all name related refs to array refs
# split into multi resources to be able to reference created labels
triggers = {
}
lifecycle {
create_before_destroy = true
}
}
*/
60 changes: 60 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

output "attributes" {
description = "Attribute string lowercase"
value = "${local.attr}"
}
output "environment" {
description = "Environment name lowercase"
value = "${local.env}"
}
output "id" {
description = "Fully formatted name ID"
value = "${compact(concat(null_resource.ids.*.triggers.id, list("")))}"
}
output "id_20" {
description = "ID truncated to 20 characters"
value = "${compact(concat(null_resource.ids-trunc.*.triggers.id_20, list("")))}"
}
output "id_32" {
description = "ID truncated to 32 characters"
value = "${compact(concat(null_resource.ids-trunc.*.triggers.id_32, list("")))}"
}
output "id_attr_20" {
description = "ID max size 20 characters by truncating `id_org` then appending `attributes`"
value = "${compact(concat(null_resource.ids-trunc-attr.*.triggers.id_attr_20, list("")))}"
}
output "id_attr_32" {
description = "ID max size 32 characters by truncating `id_org` then appending `attributes`"
value = "${compact(concat(null_resource.ids-trunc-attr.*.triggers.id_attr_32, list("")))}"
}
output "id_env" {
description = "If env namespace enabled <env>-<name> else <name>"
value = "${compact(concat(null_resource.env.*.triggers.id, list("")))}"
}
output "id_org" {
description = "If org namespace enabled <org>-<id_env> else <id_env>"
value = "${compact(concat(null_resource.org.*.triggers.id, list("")))}"
}
output "name" {
description = "Name lowercase"
value = "${compact(concat(null_resource.names.*.triggers.id, list("")))}"
}
output "organization" {
description = "Organization name lowercase"
value = "${local.org}"
}
/*
output "tags" {
description = "Tags map merged with standard tags"
value = "${compact(concat(null_resource.tags.*.triggers.tag, list("")))}"
}
*/
//debugging
output "org_attr_20" {
description = "Internal debugging. DO NOT USE"
value = "${compact(concat(null_resource.ids-trunc.*.triggers.org_attr_20, list("")))}"
}
output "org_attr_32" {
description = "Internal debugging. DO NOT USE"
value = "${compact(concat(null_resource.ids-trunc.*.triggers.org_attr_32, list("")))}"
}
2 changes: 2 additions & 0 deletions test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Test cases
===
57 changes: 57 additions & 0 deletions test/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

module "labels" {
source = "../"
names = ["CapMe", "Test2"]
environment = "Dev"
organization = "CorpXyZ"
namespace-org = true
#attributes = ["role", "policy", "use", ""]
attributes = ["8080"]
tags = "${map("Key", "Value")}"
autoscaling_group = true
}

/*
module "labels-tags" {
source = "../"
name = "CapMe"
environment = "Dev"
organization = "CorpXyZ"
attributes = ["role", "policy", "use", ""]
tags = "${map("Key", "Value")}"
}
*/
module "labels-disabled" {
source = "../"
names = ["CapMe"]
environment = "Dev"
organization = "CorpXyZ"
enabled = false
}
/*
module "labels-env" {
source = "../"
name = "CapMe"
environment = "Dev"
organization = "CorpXyZ"
namespace-env = true
namespace-org = false
}
module "labels-org" {
source = "../"
name = "CapMe"
environment = "Dev"
organization = "CorpXyZ"
namespace-env = false
namespace-org = true
}
module "labels-org-env" {
source = "../"
name = "Application"
environment = "Environment"
organization = "Organization"
namespace-env = true
namespace-org = true
}
*/
62 changes: 62 additions & 0 deletions test/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

output "attributes" {
description = "Attribute string lowercase"
value = "${module.labels.attributes}"
}
output "environment" {
description = "Environment name lowercase"
value = "${module.labels.environment}"
}
output "id" {
description = "Full combined ID"
value = "${module.labels.id}"
}
output "name" {
description = "Name lowercase"
value = "${module.labels.name}"
}
output "id_20" {
description = "ID truncated to 20 characters"
value = "${module.labels.id_20}"
}
output "id_32" {
description = "ID truncated to 32 characters"
value = "${module.labels.id_32}"
}
output "id_attr_20" {
description = "ID max size 20 characters by truncating `id_org` then appending `attributes`"
value = "${module.labels.id_attr_20}"
}
output "id_attr_32" {
description = "ID max size 32 characters by truncating `id_org` then appending `attributes`"
value = "${module.labels.id_attr_32}"
}
output "id_env" {
description = "If env namespace enabled <env>-<name> else <name>"
value = "${module.labels.id_env}"
}
output "id_org" {
description = "If org namespace enabled <org>-<id_env> else <id_env>"
value = "${module.labels.id_org}"
}
output "organization" {
description = "Organization name lowercase"
value = "${module.labels.organization}"
}
/*
output "tags" {
description = "Tags with standard tags added"
value = "${module.labels.tags}"
}
*/
output "org_attr_20" {
value = "${module.labels.org_attr_20}"
}
output "org_attr_32" {
value = "${module.labels.org_attr_32}"
}

output "disabled-name" {
description = "Name lowercase"
value = "${module.labels-disabled.name}"
}
Loading

0 comments on commit c5737ad

Please sign in to comment.