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

Support to use inbuilt terraform string functions in templatestring function #35794

Open
dhana-relex opened this issue Sep 30, 2024 · 1 comment
Labels
enhancement new new issue not yet triaged

Comments

@dhana-relex
Copy link

Terraform Version

Terraform v1.9.5

Use Cases

Gitlab file data return the content in base64 encoded format. To pass content of the file to the templatestring function it is required to base64 decode. But base64decode function is not allowed inside templatestring

locals {
  boilerplate_file_names = [
    "kustomization.yaml.tftpl",
    "values.yaml.tftpl"
  ]

 environments = {
   "customer-1",
   "customer-2"
 }

chart_version = "1.1.0"

namespace_prefix = "team-a"
}

data "gitlab_repository_file" "boilerplate_yamls" {
  for_each  = toset(local.boilerplate_file_names)
  project   = "1111"
  file_path = "boilerplates/${each.key}"
  ref       = "main"
}

resource "gitlab_repository_file" "kustomization_yaml" {
  for_each = var.environments

  project   = 1112
  branch    = main
  file_path = "${each.key}/kustomization.yaml"
  content   = templatestring(base64decode(data.gitlab_repository_file.boilerplate_yamls["kustomization.yaml.tftpl"].content), {
    helm_version      = local.chart_version,
    releaseName       = each.key,
    namespace         = "${each.key}-${local.namespace_prefix}",
  })

  encoding            = "text"
  overwrite_on_create = true
}

Error message.


│ Error: Invalid function argument

│ on modules/main.tf line 179, in resource "gitlab_repository_file" "kustomization_yaml":
│ 179: content = templatestring(base64decode(data.gitlab_repository_file.boilerplate_yamls["kustomization.yaml.tftpl"].content), {
│ 180: helm_version = local.chart_version,
│ 181: releaseName = each.key,
│ 182: namespace = "${each.key}-${local.namespace_prefix}",
│ 183: })
│ ├────────────────
│ │ while calling templatestring(template, vars)

│ Invalid value for "template" parameter: invalid template expression: must be a direct reference to a single string from elsewhere, containing valid Terraform template
│ syntax.

Attempted Solutions

For every template file read with gitlab file data, adding a local variable to assign the decoded string.

  • This makes it require to add a new local variable defined for every template file
locals {
  boilerplate_file_names = [
    "kustomization.yaml.tftpl",
    "values.yaml.tftpl"
  ]

 environments = {
   "customer-1",
   "customer-2"
 }

chart_version = "1.1.0"

namespace_prefix = "team-a"

kustomization_yaml_content = base64decode(data.gitlab_repository_file.boilerplate_yaml["kustomization.yaml.tftpl"].content)
}

data "gitlab_repository_file" "boilerplate_yamls" {
  for_each  = toset(local.boilerplate_file_names)
  project   = "1111"
  file_path = "boilerplates/${each.key}"
  ref       = "main"
}

resource "gitlab_repository_file" "kustomization_yaml" {
  for_each = var.environments

  project   = 1112
  branch    = main
  file_path = "${each.key}/kustomization.yaml"
  content   = templatestring(local. kustomization_yaml_content, {
    helm_version = local.chart_version,
    releaseName       = each.key,
    namespace         = "${each.key}-${local.namespace_prefix}",
  })

  encoding            = "text"
  overwrite_on_create = true
}

Proposal

Support the inbuilt terraform function usage in the template string function

Eg:-

templatestring(base64decode(data.gitlab_repository_file.boilerplate_yamls["kustomization.yaml.tftpl"].content)

References

No response

@dhana-relex dhana-relex added enhancement new new issue not yet triaged labels Sep 30, 2024
@crw
Copy link
Collaborator

crw commented Sep 30, 2024

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions.

Note that this issue is similar to #35700.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants