hashicorp / terraform-provider-template

Terraform template provider
https://www.terraform.io/docs/providers/template/
Mozilla Public License 2.0
131 stars 89 forks source link

Unsupported functions in template file #82

Open cathode911 opened 4 years ago

cathode911 commented 4 years ago

Hello!

Looks like there are unsupported functions in template file.

Documentation says "The template may also use any other function available in the Terraform language, except that recursive calls to templatefile are not permitted" but i found out that neither 'regex' nor 'regexall' are in fact supported:

Terraform Version

me@my-mac ~/Code/consul-vault/app/staging $ terraform -v Terraform v0.12.24

Affected Resource(s)

Terraform Configuration Files

data "template_file" "consul_config" {
  template = file("${path.module}/consul_config.json.tmpl")
  count = length(var.ipv4_addresses)

  vars = {
    node_name         = format("${var.vm_prefix}-%02d", count.index +1)
    datacenter        = "dc"
    svc_domain        = "service.local"
    bind_addr         = var.ipv4_addresses[count.index]
    advertise_addr    = var.ipv4_addresses[count.index]
    client_addr       = "0.0.0.0"
    bootstrap_expect  = length(var.ipv4_addresses)
    retry_join        = jsonencode(var.ipv4_addresses)
  }
}

Template file

{
%{ if regex(".+consul.+", node_name) }
  "server": true,
  "domain": "${svc_domain}",
  "bootstrap_expect": ${bootstrap_expect},
  "ui": true,
%{ endif ~}
  "node_name": "${node_name}",
  "datacenter": "${datacenter}",
  "data_dir": "/consul/data",
  "bind_addr": "${bind_addr}",
  "advertise_addr": "${advertise_addr}",
  "client_addr": "${client_addr}",
  "retry_join": ${retry_join},
  "log_level": "INFO",
  "acl_enforce_version_8": false
}

Panic Output

Error: failed to render : :2,7-12: Call to unknown function; There is no function named "regex".

on ../../modules/provision.tf line 1, in data "template_file" "consul_config": 1: data "template_file" "consul_config" {

Similar error is produced if i try to use %{ if length(regexall(".+consul.+", node_name)) > 0 } in template:

Error: failed to render : :2,14-22: Call to unknown function; There is no function named "regexall".

on ../../modules/provision.tf line 1, in data "template_file" "consul_config": 1: data "template_file" "consul_config" {

Expected Behavior

As documentation implies all terraform functions should be supported in template file except recursive calls to templatefile

Actual Behavior

Template fails to render

Steps to Reproduce

  1. Create a template using regex or regexall function in it
  2. run terraform apply
scorillo commented 4 years ago

https://github.com/hashicorp/terraform-provider-template/blob/master/template/datasource_template_file.go#L125