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

Allow defining own custom interpolation functions based on existing ones #21124

Open
leandro-lucarella-sociomantic opened this issue Apr 26, 2019 · 0 comments
Labels
enhancement functions providers/protocol Potentially affecting the Providers Protocol and SDKs

Comments

@leandro-lucarella-sociomantic

Current Terraform Version

Terraform v0.11.13

Use-cases

My particular case is as follow. I'm trying Terraform to manage both GitHub and GitLab, as we migrate from GitHub to GitLab, we need to keep the structure in both in sync. For that, for example, when creating a team, since both usernames in GitHub and GitLab are not the same, I need to do a mapping between one and the other. To avoid having to write a map for every user (we have a lot), as most of the times the username can be inferred by the name of the person, we have a map of exceptions and if the user is not in the map, we "calculate it". Here is the conversion "function": "${lookup(var.members_map, var.members[0], "${lower(replace(var.members[0], " ", "-"))}-suffix")}"

More contextualized example:

variable "members_map" {
  default = {
    "John Doe" = "john-doe-othersuffix",
  }
}

variable "members" {
  default = [
    "John Doe",
    "Pepe Sanchez",
  ]
}

# outputs: john-doe-othersuffix
output "john" { value = "${lookup(var.members_map, var.members[0], "${lower(replace(var.members[0], " ", "-"))}-suffix")}" }

# outputs: pepe-sanchez-suffix
output "pepe" { value = "${lookup(var.members_map, var.members[1], "${lower(replace(var.members[1], " ", "-"))}-suffix")}" }

I have the feeling this can be applied to many other use cases.

Attempted Solutions

The only current solution I found is just copy&pasting the whole conversion function every time I need to use a member username.

Proposal

Some way to define custom functions. Maybe like variables:

# args is a special value, like count
function "convert_member" {
  result = "${lookup(var.members_map, args[0], "${lower(replace(args[0], " ", "-"))}-suffix")}"
  description = "some function"
}

# Usage: "${fun.convert_member(var.members[0]))}"

This is just the first example that came to mind, how are they exactly defined I don't care much, I guess someone that knows the HCL better can come up with a better approach.

References

@bflad bflad added the providers/protocol Potentially affecting the Providers Protocol and SDKs label Dec 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement functions providers/protocol Potentially affecting the Providers Protocol and SDKs
Projects
None yet
Development

No branches or pull requests

3 participants