2014-07-21 14:56:03 -05:00
|
|
|
package config
|
|
|
|
|
2014-07-21 15:12:43 -05:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-07-22 20:35:36 -05:00
|
|
|
"io/ioutil"
|
2014-11-07 12:23:02 -06:00
|
|
|
"strconv"
|
2014-07-21 15:12:43 -05:00
|
|
|
"strings"
|
2015-01-13 13:50:44 -06:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config/lang"
|
|
|
|
"github.com/hashicorp/terraform/config/lang/ast"
|
2014-07-21 15:12:43 -05:00
|
|
|
)
|
|
|
|
|
2014-07-21 14:56:03 -05:00
|
|
|
// Funcs is the mapping of built-in functions for configuration.
|
2015-01-13 13:50:44 -06:00
|
|
|
var Funcs map[string]lang.Function
|
2014-07-21 14:56:03 -05:00
|
|
|
|
|
|
|
func init() {
|
2015-01-13 13:50:44 -06:00
|
|
|
Funcs = map[string]lang.Function{
|
2015-01-13 14:06:04 -06:00
|
|
|
"file": interpolationFuncFile(),
|
|
|
|
"join": interpolationFuncJoin(),
|
2015-01-13 13:50:44 -06:00
|
|
|
"element": interpolationFuncElement(),
|
2014-07-21 14:56:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 20:35:36 -05:00
|
|
|
// interpolationFuncFile implements the "file" function that allows
|
|
|
|
// loading contents from a file.
|
2015-01-13 13:50:44 -06:00
|
|
|
func interpolationFuncFile() lang.Function {
|
|
|
|
return lang.Function{
|
|
|
|
ArgTypes: []ast.Type{ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
data, err := ioutil.ReadFile(args[0].(string))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
},
|
2014-07-22 20:35:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-09 23:22:35 -05:00
|
|
|
// interpolationFuncJoin implements the "join" function that allows
|
|
|
|
// multi-variable values to be joined by some character.
|
2015-01-13 13:50:44 -06:00
|
|
|
func interpolationFuncJoin() lang.Function {
|
|
|
|
return lang.Function{
|
|
|
|
ArgTypes: []ast.Type{ast.TypeString, ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
var list []string
|
|
|
|
for _, arg := range args[1:] {
|
|
|
|
parts := strings.Split(arg.(string), InterpSplitDelim)
|
|
|
|
list = append(list, parts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(list, args[0].(string)), nil
|
|
|
|
},
|
2014-10-09 23:22:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-21 14:56:03 -05:00
|
|
|
// interpolationFuncLookup implements the "lookup" function that allows
|
|
|
|
// dynamic lookups of map types within a Terraform configuration.
|
2015-01-13 14:06:04 -06:00
|
|
|
func interpolationFuncLookup(vs map[string]string) lang.Function {
|
|
|
|
return lang.Function{
|
|
|
|
ArgTypes: []ast.Type{ast.TypeString, ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
k := fmt.Sprintf("var.%s.%s", args[0].(string), args[1].(string))
|
|
|
|
v, ok := vs[k]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"lookup in '%s' failed to find '%s'",
|
|
|
|
args[0].(string), args[1].(string))
|
|
|
|
}
|
2014-07-21 15:12:43 -05:00
|
|
|
|
2015-01-13 14:06:04 -06:00
|
|
|
return v, nil
|
|
|
|
},
|
2014-07-21 15:12:43 -05:00
|
|
|
}
|
2014-07-21 14:56:03 -05:00
|
|
|
}
|
2014-11-07 12:23:02 -06:00
|
|
|
|
|
|
|
// interpolationFuncElement implements the "element" function that allows
|
|
|
|
// a specific index to be looked up in a multi-variable value. Note that this will
|
|
|
|
// wrap if the index is larger than the number of elements in the multi-variable value.
|
2015-01-13 13:50:44 -06:00
|
|
|
func interpolationFuncElement() lang.Function {
|
|
|
|
return lang.Function{
|
|
|
|
ArgTypes: []ast.Type{ast.TypeString, ast.TypeString},
|
|
|
|
ReturnType: ast.TypeString,
|
|
|
|
Callback: func(args []interface{}) (interface{}, error) {
|
|
|
|
list := strings.Split(args[0].(string), InterpSplitDelim)
|
|
|
|
|
|
|
|
index, err := strconv.Atoi(args[1].(string))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"invalid number for index, got %s", args[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
v := list[index%len(list)]
|
|
|
|
return v, nil
|
|
|
|
},
|
2014-11-07 12:23:02 -06:00
|
|
|
}
|
|
|
|
}
|