Skip to content

Commit

Permalink
Use regex_replace_chars from context and add it to the output conte…
Browse files Browse the repository at this point in the history
…xt (#56)
  • Loading branch information
aknysh authored Apr 1, 2019
1 parent 8ef218a commit 8ac35bc
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 31 deletions.
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 2017-2018 Cloud Posse, LLC
Copyright 2017-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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
Terraform module designed to generate consistent label names and tags for resources. Use `terraform-null-label` to implement a strict naming convention.

A label follows the following convention: `{namespace}-{environment}-{stage}-{name}-{attributes}`. The delimiter (e.g. `-`) is interchangeable.
The label items are all optional. So if you perfer the term `stage` to `environment` you can exclude environment and the label `id` will look like `{namespace}-{stage}-{name}-{attributes}`.
The label items are all optional. So if you prefer the term `stage` to `environment` you can exclude environment and the label `id` will look like `{namespace}-{stage}-{name}-{attributes}`.
If attributes are excluded but `stage` and `environment` are included, `id` will look like `{namespace}-{environment}-{stage}-{name}`

It's recommended to use one `terraform-null-label` module for every unique resource of a given resource type.
Expand Down
2 changes: 1 addition & 1 deletion README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ description: |-
Terraform module designed to generate consistent label names and tags for resources. Use `terraform-null-label` to implement a strict naming convention.
A label follows the following convention: `{namespace}-{environment}-{stage}-{name}-{attributes}`. The delimiter (e.g. `-`) is interchangeable.
The label items are all optional. So if you perfer the term `stage` to `environment` you can exclude environment and the label `id` will look like `{namespace}-{stage}-{name}-{attributes}`.
The label items are all optional. So if you prefer the term `stage` to `environment` you can exclude environment and the label `id` will look like `{namespace}-{stage}-{name}-{attributes}`.
If attributes are excluded but `stage` and `environment` are included, `id` will look like `{namespace}-{environment}-{stage}-{name}`
It's recommended to use one `terraform-null-label` module for every unique resource of a given resource type.
Expand Down
60 changes: 32 additions & 28 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,28 @@ locals {
# else
# thing

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, var.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, var.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, var.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, var.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}"
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)
Expand All @@ -58,15 +61,16 @@ locals {
label_order_length = "${(length(local.label_order_final_list))}"
# 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}"]
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}"]
}
id_context = {
name = "${local.name}"
Expand Down

0 comments on commit 8ac35bc

Please sign in to comment.