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

Ability to set max length of ID output #93

Closed
tibbing opened this issue Jun 10, 2020 · 2 comments · Fixed by #98
Closed

Ability to set max length of ID output #93

tibbing opened this issue Jun 10, 2020 · 2 comments · Fixed by #98
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@tibbing
Copy link
Contributor

tibbing commented Jun 10, 2020

I'm a heavy user of this module for naming resources, but often find myself writing workarounds for limiting the length of the output ID. Many resources have a max allowed length of the names (such as aws_lb and aws_alb_target_group (32 characters), aws_iam_role (64 characters) etc.

It would be really nice to have an input parameter max_id_length capping the output ID, while still keeping it unique. Suggested implementation:

....
variable "max_id_length" {
  type        = number
  default     = 64
  description = "Specify max length of output id result"
}

locals {
  ...
  full_id = lower(join(local.delimiter, local.labels))
  id_hash   = md5(local.full_id)
  id_capped = var.max_id_length <= 6 ? substr(local.id_hash, 0, var.max_id_length) : "${replace(substr(local.full_id, 0, var.max_id_length - 6), "/-$/", "")}-${substr(local.id_hash, 0, 5)}"
  id        = var.max_id_length != 0 && length(local.full_id) > var.max_id_length ? local.id_capped : local.full_id
}

I can give it a go and make a PR if you like the idea.

@osterman
Copy link
Member

@tibbing yes, this has come up quite a bit. We would accept a PR for this functionality. I like your proposal of offering a few different kinds of outputs. I'd maybe call id_capped instead id_truncated or id_short. Let's call id_hash just id_md5 since there are multiple ways to hash.

@osterman osterman added enhancement New feature or request help wanted Extra attention is needed labels Aug 10, 2020
@tibbing
Copy link
Contributor Author

tibbing commented Aug 11, 2020

@osterman Good points! Please see the referenced PR. I was considering whether to use the given delimiter, or force a dash before the hash, or even skip it altogether. We just need to make sure there won't be duplicate delimiters if the ID happens to be truncated just after a delimiter somehow. Do you have any preference?

@Nuru Nuru closed this as completed in #98 Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants