Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to limit output id length #98

Merged
merged 9 commits into from
Aug 26, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ No provider.
| 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 |
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
| max\_id\_length | Specify max length of output id result, or 0 for unrestricted length | `number` | `0` | 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 | 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 | `string` | `"/[^a-zA-Z0-9-]/"` | no |
Expand All @@ -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 |
| full\_id | Disambiguated ID not restricted to max\_id\_length |
| id | Disambiguated ID restricted to max\_id\_length |
| label\_order | The naming order of the id output and Name tag |
| name | Normalized name |
| namespace | Normalized namespace |
Expand Down Expand Up @@ -706,8 +708,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply

### Contributors

| [![Erik Osterman][osterman_avatar]][osterman_homepage]<br/>[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]<br/>[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]<br/>[Igor Rodionov][goruha_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]<br/>[Sergey Vasilyev][s2504s_homepage] | [![Michael Pereira][MichaelPereira_avatar]][MichaelPereira_homepage]<br/>[Michael Pereira][MichaelPereira_homepage] | [![Jamie Nelson][Jamie-BitFlight_avatar]][Jamie-BitFlight_homepage]<br/>[Jamie Nelson][Jamie-BitFlight_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]<br/>[Vladimir][SweetOps_homepage] | [![Daren Desjardins][darend_avatar]][darend_homepage]<br/>[Daren Desjardins][darend_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]<br/>[Maarten van der Hoef][maartenvanderhoef_homepage] |
|---|---|---|---|---|---|---|---|---|
| [![Erik Osterman][osterman_avatar]][osterman_homepage]<br/>[Erik Osterman][osterman_homepage] | [![Andriy Knysh][aknysh_avatar]][aknysh_homepage]<br/>[Andriy Knysh][aknysh_homepage] | [![Igor Rodionov][goruha_avatar]][goruha_homepage]<br/>[Igor Rodionov][goruha_homepage] | [![Sergey Vasilyev][s2504s_avatar]][s2504s_homepage]<br/>[Sergey Vasilyev][s2504s_homepage] | [![Michael Pereira][MichaelPereira_avatar]][MichaelPereira_homepage]<br/>[Michael Pereira][MichaelPereira_homepage] | [![Jamie Nelson][Jamie-BitFlight_avatar]][Jamie-BitFlight_homepage]<br/>[Jamie Nelson][Jamie-BitFlight_homepage] | [![Vladimir][SweetOps_avatar]][SweetOps_homepage]<br/>[Vladimir][SweetOps_homepage] | [![Daren Desjardins][darend_avatar]][darend_homepage]<br/>[Daren Desjardins][darend_homepage] | [![Maarten van der Hoef][maartenvanderhoef_avatar]][maartenvanderhoef_homepage]<br/>[Maarten van der Hoef][maartenvanderhoef_homepage] | [![Adam Tibbing][tibbing_avatar]][tibbing_homepage]<br/>[Adam Tibbing][tibbing_homepage] |
|---|---|---|---|---|---|---|---|---|---|

[osterman_homepage]: https://github.com/osterman
[osterman_avatar]: https://img.cloudposse.com/150x150/https://github.com/osterman.png
Expand All @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -481,3 +481,5 @@ contributors:
github: darend
- name: Maarten van der Hoef
github: maartenvanderhoef
- name: Adam Tibbing
github: tibbing
4 changes: 3 additions & 1 deletion docs/terraform.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ No provider.
| 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 |
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
| max\_id\_length | Specify max length of output id result, or 0 for unrestricted length | `number` | `0` | 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 | 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 | `string` | `"/[^a-zA-Z0-9-]/"` | no |
Expand All @@ -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 |
| full\_id | Disambiguated ID not restricted to max\_id\_length |
| id | Disambiguated ID restricted to max\_id\_length |
| label\_order | The naming order of the id output and Name tag |
| name | Normalized name |
| namespace | Normalized namespace |
Expand Down
2 changes: 1 addition & 1 deletion examples/autoscalinggroup/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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.max_id_length <= 6 ?
substr(local.id_md5, 0, var.max_id_length) :
"${replace(substr(local.id_full, 0, var.max_id_length - 6), "/-$/", "")}-${substr(local.id_md5, 0, 5)}")
id = var.max_id_length != 0 && length(local.id_full) > var.max_id_length ? local.id_short : local.id_full


# Context of this label to pass to other label modules
output_context = {
Expand Down
7 changes: 6 additions & 1 deletion outputs.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
output "id" {
value = local.enabled ? local.id : ""
description = "Disambiguated ID"
description = "Disambiguated ID restricted to max_id_length"
}

output "id_full" {
value = local.enabled ? local.id_full : ""
description = "Disambiguated ID not restricted to max_id_length"
}

output "name" {
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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 "max_id_length" {
type = number
default = 0
description = "Specify max length of output id result, or 0 for unrestricted length"
}