Skip to content

Commit

Permalink
Customize to our own naming/tagging convention
Browse files Browse the repository at this point in the history
  • Loading branch information
roivaz committed Dec 3, 2019
1 parent f25b7cb commit 1af6fae
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
31 changes: 18 additions & 13 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
locals {

defaults = {
label_order = ["namespace", "environment", "stage", "name", "attributes"]
label_order = ["environment", "project", "workload", "type", "attributes"]
delimiter = "-"
tf_config = ""
replacement = ""
# The `sentinel` should match the `regex_replace_chars`, so it will be replaced with the `replacement` value
sentinel = "~"
Expand All @@ -14,11 +15,12 @@ locals {
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))
project = lower(replace(coalesce(var.project, var.context.project, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement))
workload = lower(replace(coalesce(var.workload, var.context.workload, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement))
type = lower(replace(coalesce(var.type, var.context.type, local.defaults.sentinel), local.regex_replace_chars, local.defaults.replacement))
delimiter = coalesce(var.delimiter, var.context.delimiter, local.defaults.delimiter)
tf_config = coalesce(var.tf_config, var.context.tf_config, local.defaults.tf_config)
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)

Expand All @@ -37,20 +39,22 @@ locals {

tags_context = {
# For AWS we need `Name` to be disambiguated sine it has a special meaning
name = local.id
namespace = local.namespace
environment = local.environment
stage = local.stage
project = local.project
workload = local.workload
name = local.id
terraform = local.tf_config
type = local.type
attributes = local.id_context.attributes
}

generated_tags = { for l in keys(local.tags_context) : title(l) => local.tags_context[l] if length(local.tags_context[l]) > 0 }

id_context = {
name = local.name
namespace = local.namespace
environment = local.environment
stage = local.stage
project = local.project
workload = local.workload
type = local.type
attributes = lower(replace(join(local.delimiter, local.attributes), local.regex_replace_chars, local.defaults.replacement))
}

Expand All @@ -61,13 +65,14 @@ locals {
# Context of this label to pass to other label modules
output_context = {
enabled = local.enabled
name = local.name
namespace = local.namespace
environment = local.environment
stage = local.stage
project = local.project
workload = local.workload
type = local.type
attributes = local.attributes
tags = local.tags
delimiter = local.delimiter
tf_config = local.tf_config
label_order = local.label_order
regex_replace_chars = local.regex_replace_chars
additional_tag_map = local.additional_tag_map
Expand Down
24 changes: 12 additions & 12 deletions outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ output "id" {
description = "Disambiguated ID"
}

output "name" {
value = local.enabled ? local.name : ""
description = "Normalized name"
output "workload" {
value = local.enabled ? local.workload : ""
description = "Normalized workload"
}

output "namespace" {
value = local.enabled ? local.namespace : ""
description = "Normalized namespace"
}

output "stage" {
value = local.enabled ? local.stage : ""
description = "Normalized stage"
output "project" {
value = local.enabled ? local.project : ""
description = "Normalized project"
}

output "environment" {
value = local.enabled ? local.environment : ""
description = "Normalized environment"
}

output "type" {
value = local.enabled ? local.type : ""
description = "Normalized resource type"
}

output "attributes" {
value = local.enabled ? local.attributes : []
description = "List of attributes"
}

output "delimiter" {
value = local.enabled ? local.delimiter : ""
description = "Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes`"
description = "Delimiter between `project`, `environment`, `workload`, `type` and `attributes`"
}

output "tags" {
Expand Down
42 changes: 26 additions & 16 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
variable "namespace" {
variable "project" {
type = string
default = ""
description = "Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp'"
description = "Project name: eng/saas"
}

variable "environment" {
type = string
default = ""
description = "Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT'"
description = "Environment: pro/stg/dev"
}

variable "stage" {
variable "workload" {
type = string
default = ""
description = "Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release'"
description = "Solution name: system/zync/backend/keycloak/apicast/apicurio"
}

variable "name" {
variable "type" {
type = string
default = ""
description = "Solution name, e.g. 'app' or 'jenkins'"
description = "Abreviated resource type: i, sg, vpc, rt, s3, rds, es, ec ..."
}

variable "enabled" {
Expand All @@ -40,6 +40,12 @@ variable "attributes" {
description = "Additional attributes (e.g. `1`)"
}

variable "tf_config" {
type = string
default = ""
description = "The Terraform Config that owns the resource"
}

variable "tags" {
type = map(string)
default = {}
Expand All @@ -54,25 +60,29 @@ variable "additional_tag_map" {

variable "context" {
type = object({
namespace = string
environment = string
stage = string
name = string
environment = string
project = string
workload = string
type = string

enabled = bool
delimiter = string
tf_config = 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 = ""
environment = ""
project = ""
workload = ""
type = ""

enabled = true
delimiter = ""
tf_config = ""
attributes = []
label_order = []
tags = {}
Expand All @@ -91,6 +101,6 @@ variable "label_order" {
variable "regex_replace_chars" {
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"
description = "Regex to replace chars with empty string in `project`, `environment` and `workload`. By default only hyphens, letters and digits are allowed, all other chars are removed"
}

0 comments on commit 1af6fae

Please sign in to comment.