From c4e0de350b6426473fe6f3bff5e3e8c586bc6ad4 Mon Sep 17 00:00:00 2001 From: r-divakaran Date: Wed, 13 Feb 2019 18:53:14 +0100 Subject: [PATCH] To allow special characters in module id (#48) * To allow special characters in module id * Adjusting spacing * Adjustments as per review comments * More meaningful variable name --- README.md | 4 ++-- docs/terraform.md | 2 +- main.tf | 27 ++++++++++++++------------- variables.tf | 6 ++++++ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1384ed9..74b8b02 100644 --- a/README.md +++ b/README.md @@ -396,7 +396,6 @@ Available targets: lint Lint terraform code ``` - ## Inputs | Name | Description | Type | Default | Required | @@ -410,6 +409,7 @@ Available targets: | label_order | The naming order of the id output and Name tag | list | `` | 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 | +| regex_replace_chars | By default only letters and digits are allowed in `namespace`, `environment`, `stage` and `name`. All other chars are removed | string | `[^a-zA-Z0-9]` | no | | stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no | | tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `` | no | @@ -509,7 +509,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) diff --git a/docs/terraform.md b/docs/terraform.md index b43096b..10d807c 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -1,4 +1,3 @@ - ## Inputs | Name | Description | Type | Default | Required | @@ -12,6 +11,7 @@ | label_order | The naming order of the id output and Name tag | list | `` | 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 | +| regex_replace_chars | By default only letters and digits are allowed in `namespace`, `environment`, `stage` and `name`. All other chars are removed | string | `[^a-zA-Z0-9]` | no | | stage | Stage, e.g. 'prod', 'staging', 'dev', OR 'source', 'build', 'test', 'deploy', 'release' | string | `` | no | | tags | Additional tags (e.g. `map('BusinessUnit','XYZ')` | map | `` | no | diff --git a/main.tf b/main.tf index 39794e4..ecf021f 100644 --- a/main.tf +++ b/main.tf @@ -3,15 +3,16 @@ locals { # Only maps that contain all the same attribute types can be merged, so the values have been set to list context_struct = { - name = [] - namespace = [] - environment = [] - stage = [] - attributes = [] - tags_keys = [] - tags_values = [] - delimiter = [] - label_order = [] + name = [] + namespace = [] + environment = [] + stage = [] + attributes = [] + tags_keys = [] + tags_values = [] + delimiter = [] + label_order = [] + regex_replace_chars = [] } # Merge the map of empty values, with the variable context, so that context_local always contains all map keys @@ -26,19 +27,19 @@ locals { 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, "/[^a-zA-Z0-9]/", ""))}" + 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, "/[^a-zA-Z0-9]/", ""))}" + 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, "/[^a-zA-Z0-9]/", ""))}" + 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, "/[^a-zA-Z0-9]/", ""))}" + 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}" diff --git a/variables.tf b/variables.tf index b8eb2df..f389113 100644 --- a/variables.tf +++ b/variables.tf @@ -63,3 +63,9 @@ variable "label_order" { default = [] description = "The naming order of the id output and Name tag" } + +variable "regex_replace_chars" { + type = "string" + default = "[^a-zA-Z0-9]" + description = "By default only letters and digits are allowed in `namespace`, `environment`, `stage` and `name`. All other chars are removed" +}