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

Add templatestring function #28686

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions lang/funcs/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func MakeFileFunc(baseDir string, encBase64 bool) function.Function {
// As a special exception, a referenced template file may not recursively call
// the templatefile function, since that would risk the same file being
// included into itself indefinitely.
func MakeTemplateFileFunc(baseDir string, funcsCb func() map[string]function.Function) function.Function {
func MakeTemplateFileFunc(baseDir string, templateFile bool, funcsCb func() map[string]function.Function) function.Function {

params := []function.Parameter{
{
Expand All @@ -79,9 +79,15 @@ func MakeTemplateFileFunc(baseDir string, funcsCb func() map[string]function.Fun
loadTmpl := func(fn string) (hcl.Expression, error) {
// We re-use File here to ensure the same filename interpretation
// as it does, along with its other safety checks.
tmplVal, err := File(baseDir, cty.StringVal(fn))
if err != nil {
return nil, err
var tmplVal cty.Value
if templateFile {
var err error
tmplVal, err = File(baseDir, cty.StringVal(fn))
if err != nil {
return nil, err
}
} else {
tmplVal = cty.StringVal(fn)
}

expr, diags := hclsyntax.ParseTemplate([]byte(tmplVal.AsString()), fn, hcl.Pos{Line: 1, Column: 1})
Expand Down Expand Up @@ -128,7 +134,7 @@ func MakeTemplateFileFunc(baseDir string, funcsCb func() map[string]function.Fun
givenFuncs := funcsCb() // this callback indirection is to avoid chicken/egg problems
funcs := make(map[string]function.Function, len(givenFuncs))
for name, fn := range givenFuncs {
if name == "templatefile" {
if name == "templatefile" || name == "templatestring" {
// We stub this one out to prevent recursive calls.
funcs[name] = function.New(&function.Spec{
Params: params,
Expand Down
2 changes: 1 addition & 1 deletion lang/funcs/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func TestTemplateFile(t *testing.T) {
},
}

templateFileFn := MakeTemplateFileFunc(".", func() map[string]function.Function {
templateFileFn := MakeTemplateFileFunc(".", true, func() map[string]function.Function {
return map[string]function.Function{
"join": stdlib.JoinFunc,
"templatefile": MakeFileFunc(".", false), // just a placeholder, since templatefile itself overrides this
Expand Down
7 changes: 6 additions & 1 deletion lang/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,16 @@ func (s *Scope) Functions() map[string]function.Function {
"zipmap": stdlib.ZipmapFunc,
}

s.funcs["templatefile"] = funcs.MakeTemplateFileFunc(s.BaseDir, func() map[string]function.Function {
s.funcs["templatefile"] = funcs.MakeTemplateFileFunc(s.BaseDir, true, func() map[string]function.Function {
// The templatefile function prevents recursive calls to itself
// by copying this map and overwriting the "templatefile" entry.
return s.funcs
})
s.funcs["templatestring"] = funcs.MakeTemplateFileFunc(s.BaseDir, false, func() map[string]function.Function {
// The templatestring function prevents recursive calls to itself
// by copying this map and overwriting the "templatestring" entry.
return s.funcs
})

if s.ConsoleMode {
// The type function is only available in terraform console.
Expand Down
7 changes: 7 additions & 0 deletions lang/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,13 @@ func TestFunctions(t *testing.T) {
},
},

"templatestring": {
{
`templatestring("Hello, $${name}!", {name = "Jodie"})`,
cty.StringVal("Hello, Jodie!"),
},
},

"timeadd": {
{
`timeadd("2017-11-22T00:00:00Z", "1s")`,
Expand Down