Skip to content

Commit af6fd1f

Browse files
authored
Add option to limit output id length (#98) @tibbing
1 parent 46042f4 commit af6fd1f

File tree

7 files changed

+32
-8
lines changed

7 files changed

+32
-8
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ No provider.
537537
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
538538
| enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
539539
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
540+
| id\_max\_length | Specify the max length of the `id` output, or 0 for unrestricted length | `number` | `0` | no |
540541
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
541542
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `""` | no |
542543
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `""` | no |
@@ -552,7 +553,8 @@ No provider.
552553
| context | Context of this module to pass to other label modules |
553554
| delimiter | Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes` |
554555
| environment | Normalized environment |
555-
| id | Disambiguated ID |
556+
| id | Disambiguated ID restricted to id\_max\_length |
557+
| id\_full | Disambiguated ID not restricted to id\_max\_length |
556558
| label\_order | The naming order of the id output and Name tag |
557559
| name | Normalized name |
558560
| namespace | Normalized namespace |
@@ -706,8 +708,8 @@ Check out [our other projects][github], [follow us on twitter][twitter], [apply
706708

707709
### Contributors
708710

709-
| [![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] |
710-
|---|---|---|---|---|---|---|---|---|
711+
| [![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] |
712+
|---|---|---|---|---|---|---|---|---|---|
711713

712714
[osterman_homepage]: https://github.com/osterman
713715
[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
727729
[darend_avatar]: https://img.cloudposse.com/150x150/https://github.com/darend.png
728730
[maartenvanderhoef_homepage]: https://github.com/maartenvanderhoef
729731
[maartenvanderhoef_avatar]: https://img.cloudposse.com/150x150/https://github.com/maartenvanderhoef.png
732+
[tibbing_homepage]: https://github.com/tibbing
733+
[tibbing_avatar]: https://img.cloudposse.com/150x150/https://github.com/tibbing.png
730734

731735
[![README Footer][readme_footer_img]][readme_footer_link]
732736
[![Beacon][beacon]][website]

README.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,3 +481,5 @@ contributors:
481481
github: darend
482482
- name: Maarten van der Hoef
483483
github: maartenvanderhoef
484+
- name: Adam Tibbing
485+
github: tibbing

docs/terraform.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ No provider.
1818
| delimiter | Delimiter to be used between `namespace`, `environment`, `stage`, `name` and `attributes` | `string` | `"-"` | no |
1919
| enabled | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
2020
| environment | Environment, e.g. 'prod', 'staging', 'dev', 'pre-prod', 'UAT' | `string` | `""` | no |
21+
| id\_max\_length | Specify the max length of the `id` output, or 0 for unrestricted length | `number` | `0` | no |
2122
| label\_order | The naming order of the id output and Name tag | `list(string)` | `[]` | no |
2223
| name | Solution name, e.g. 'app' or 'jenkins' | `string` | `""` | no |
2324
| namespace | Namespace, which could be your organization name or abbreviation, e.g. 'eg' or 'cp' | `string` | `""` | no |
@@ -33,7 +34,8 @@ No provider.
3334
| context | Context of this module to pass to other label modules |
3435
| delimiter | Delimiter between `namespace`, `environment`, `stage`, `name` and `attributes` |
3536
| environment | Normalized environment |
36-
| id | Disambiguated ID |
37+
| id | Disambiguated ID restricted to id\_max\_length |
38+
| id\_full | Disambiguated ID not restricted to id\_max\_length |
3739
| label\_order | The naming order of the id output and Name tag |
3840
| name | Normalized name |
3941
| namespace | Normalized namespace |

examples/autoscalinggroup/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ resource "aws_autoscaling_group" "default" {
6464
# Provider #
6565
################################
6666
provider "aws" {
67-
region = "eu-west-1"
67+
region = "eu-west-1"
6868

6969
# Make it faster by skipping unneeded checks here
7070
skip_get_ec2_platforms = true

main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,14 @@ locals {
5656

5757
labels = [for l in local.label_order : local.id_context[l] if length(local.id_context[l]) > 0]
5858

59-
id = lower(join(local.delimiter, local.labels))
59+
id_full = lower(join(local.delimiter, local.labels))
60+
id_md5 = md5(local.id_full)
61+
# Truncates ID to given max length, suffixed by 6 character hash of ID for disambiguation
62+
id_short = (var.id_max_length <= 6 ?
63+
substr(local.id_md5, 0, var.id_max_length) :
64+
"${replace(substr(local.id_full, 0, var.id_max_length - 6), "/-$/", "")}-${substr(local.id_md5, 0, 5)}")
65+
id = var.id_max_length != 0 && length(local.id_full) > var.id_max_length ? local.id_short : local.id_full
66+
6067

6168
# Context of this label to pass to other label modules
6269
output_context = {

outputs.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
output "id" {
22
value = local.enabled ? local.id : ""
3-
description = "Disambiguated ID"
3+
description = "Disambiguated ID restricted to id_max_length"
4+
}
5+
6+
output "id_full" {
7+
value = local.enabled ? local.id_full : ""
8+
description = "Disambiguated ID not restricted to id_max_length"
49
}
510

611
output "name" {
@@ -52,4 +57,3 @@ output "label_order" {
5257
value = local.label_order
5358
description = "The naming order of the id output and Name tag"
5459
}
55-

variables.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ variable "regex_replace_chars" {
9494
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"
9595
}
9696

97+
variable "id_max_length" {
98+
type = number
99+
default = 0
100+
description = "Specify the max length of the `id` output, or 0 for unrestricted length"
101+
}

0 commit comments

Comments
 (0)