From af6fd1fa902fb33e2b2807fbf0c287c51c0a33b5 Mon Sep 17 00:00:00 2001 From: Adam Tibbing Date: Wed, 26 Aug 2020 15:56:53 -0700 Subject: [PATCH] Add option to limit output id length (#98) @tibbing --- README.md | 10 +++++++--- README.yaml | 2 ++ docs/terraform.md | 4 +++- examples/autoscalinggroup/main.tf | 2 +- main.tf | 9 ++++++++- outputs.tf | 8 ++++++-- variables.tf | 5 +++++ 7 files changed, 32 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d214df6..953250c 100644 --- a/README.md +++ b/README.md @@ -537,6 +537,7 @@ No provider. | delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no | | enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no | | environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no | +| id\_max\_length | Specify the max length of the `id` output, or 0 for unrestricted length | `number` | `0` | no | | label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | 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 | @@ -552,7 +553,8 @@ No provider. | context | Context of this module to pass to other label modules | | delimiter | Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes` | | environment | Normalized environment | -| id | Disambiguated ID | +| id | Disambiguated ID restricted to id\_max\_length | +| id\_full | Disambiguated ID not restricted to id\_max\_length | | label\_order | The naming order of the id output and Name tag | | name | Normalized name | | namespace | Normalized namespace | @@ -706,8 +708,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply ### Contributors -| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]
[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]
[Igor Rodionov][goruha_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]
[Sergey Vasilyev][s2504s_homepage] | [![Michael Pereira][MichaelPereira_avatar]][MichaelPereira_homepage]
[Michael Pereira][MichaelPereira_homepage] | [![Jamie Nelson][Jamie-BitFlight_avatar]][Jamie-BitFlight_homepage]
[Jamie Nelson][Jamie-BitFlight_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]
[Vladimir][SweetOps_homepage] | [![Daren Desjardins][darend_avatar]][darend_homepage]
[Daren Desjardins][darend_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]
[Maarten van der Hoef][maartenvanderhoef_homepage] | -|---|---|---|---|---|---|---|---|---| +| [![Erik Osterman][osterman_avatar]][osterman_homepage]
[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]
[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]
[Igor Rodionov][goruha_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]
[Sergey Vasilyev][s2504s_homepage] | [![Michael Pereira][MichaelPereira_avatar]][MichaelPereira_homepage]
[Michael Pereira][MichaelPereira_homepage] | [![Jamie Nelson][Jamie-BitFlight_avatar]][Jamie-BitFlight_homepage]
[Jamie Nelson][Jamie-BitFlight_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]
[Vladimir][SweetOps_homepage] | [![Daren Desjardins][darend_avatar]][darend_homepage]
[Daren Desjardins][darend_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]
[Maarten van der Hoef][maartenvanderhoef_homepage] | [![Adam Tibbing][tibbing_avatar]][tibbing_homepage]
[Adam Tibbing][tibbing_homepage] | +|---|---|---|---|---|---|---|---|---|---| [osterman_homepage]: https://github.com/osterman [osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png @@ -727,6 +729,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply [darend_avatar]: https://img.cloudposse.com/150x150/https://github.com/darend.png [maartenvanderhoef_homepage]: https://github.com/maartenvanderhoef [maartenvanderhoef_avatar]: https://img.cloudposse.com/150x150/https://github.com/maartenvanderhoef.png + [tibbing_homepage]: https://github.com/tibbing + [tibbing_avatar]: https://img.cloudposse.com/150x150/https://github.com/tibbing.png [![README Footer][readme_footer_img]][readme_footer_link] [![Beacon][beacon]][website] diff --git a/README.yaml b/README.yaml index 114d7b9..e483fd7 100644 --- a/README.yaml +++ b/README.yaml @@ -481,3 +481,5 @@ contributors: github: darend - name: Maarten van der Hoef github: maartenvanderhoef +- name: Adam Tibbing + github: tibbing diff --git a/docs/terraform.md b/docs/terraform.md index 970fe78..5023b22 100644 --- a/docs/terraform.md +++ b/docs/terraform.md @@ -18,6 +18,7 @@ No provider. | delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no | | enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no | | environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no | +| id\_max\_length | Specify the max length of the `id` output, or 0 for unrestricted length | `number` | `0` | no | | label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | 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 | @@ -33,7 +34,8 @@ No provider. | context | Context of this module to pass to other label modules | | delimiter | Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes` | | environment | Normalized environment | -| id | Disambiguated ID | +| id | Disambiguated ID restricted to id\_max\_length | +| id\_full | Disambiguated ID not restricted to id\_max\_length | | label\_order | The naming order of the id output and Name tag | | name | Normalized name | | namespace | Normalized namespace | diff --git a/examples/autoscalinggroup/main.tf b/examples/autoscalinggroup/main.tf index 27b809b..0ea3e1a 100644 --- a/examples/autoscalinggroup/main.tf +++ b/examples/autoscalinggroup/main.tf @@ -64,7 +64,7 @@ resource "aws_autoscaling_group" "default" { # Provider # ################################ provider "aws" { - region = "eu-west-1" + region = "eu-west-1" # Make it faster by skipping unneeded checks here skip_get_ec2_platforms = true diff --git a/main.tf b/main.tf index 3e053a8..33a06dd 100644 --- a/main.tf +++ b/main.tf @@ -56,7 +56,14 @@ locals { labels = [for l in local.label_order : local.id_context[l] if length(local.id_context[l]) > 0] - id = lower(join(local.delimiter, local.labels)) + id_full = lower(join(local.delimiter, local.labels)) + id_md5 = md5(local.id_full) + # Truncates ID to given max length, suffixed by 6 character hash of ID for disambiguation + id_short = (var.id_max_length <= 6 ? + substr(local.id_md5, 0, var.id_max_length) : + "${replace(substr(local.id_full, 0, var.id_max_length - 6), "/-$/", "")}-${substr(local.id_md5, 0, 5)}") + id = var.id_max_length != 0 && length(local.id_full) > var.id_max_length ? local.id_short : local.id_full + # Context of this label to pass to other label modules output_context = { diff --git a/outputs.tf b/outputs.tf index 3a3ae9a..df9bb87 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,6 +1,11 @@ output "id" { value = local.enabled ? local.id : "" - description = "Disambiguated ID" + description = "Disambiguated ID restricted to id_max_length" +} + +output "id_full" { + value = local.enabled ? local.id_full : "" + description = "Disambiguated ID not restricted to id_max_length" } output "name" { @@ -52,4 +57,3 @@ output "label_order" { value = local.label_order description = "The naming order of the id output and Name tag" } - diff --git a/variables.tf b/variables.tf index 50e35ac..c50a505 100644 --- a/variables.tf +++ b/variables.tf @@ -94,3 +94,8 @@ variable "regex_replace_chars" { 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" } +variable "id_max_length" { + type = number + default = 0 + description = "Specify the max length of the `id` output, or 0 for unrestricted length" +}