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-07-21 15:12:43 -05:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-07-21 14:56:03 -05:00
|
|
|
// Funcs is the mapping of built-in functions for configuration.
|
|
|
|
var Funcs map[string]InterpolationFunc
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Funcs = map[string]InterpolationFunc{
|
2014-07-22 20:35:36 -05:00
|
|
|
"file": interpolationFuncFile,
|
2014-07-21 14:56:03 -05:00
|
|
|
"lookup": interpolationFuncLookup,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-22 20:35:36 -05:00
|
|
|
// interpolationFuncFile implements the "file" function that allows
|
|
|
|
// loading contents from a file.
|
|
|
|
func interpolationFuncFile(
|
|
|
|
vs map[string]string, args ...string) (string, error) {
|
|
|
|
if len(args) != 1 {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"file expects 1 arguments, got %d", len(args))
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(args[0])
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
|
|
|
|
2014-07-21 14:56:03 -05:00
|
|
|
// interpolationFuncLookup implements the "lookup" function that allows
|
|
|
|
// dynamic lookups of map types within a Terraform configuration.
|
|
|
|
func interpolationFuncLookup(
|
|
|
|
vs map[string]string, args ...string) (string, error) {
|
2014-07-21 15:12:43 -05:00
|
|
|
if len(args) != 2 {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"lookup expects 2 arguments, got %d", len(args))
|
|
|
|
}
|
|
|
|
|
|
|
|
k := fmt.Sprintf("var.%s", strings.Join(args, "."))
|
|
|
|
v, ok := vs[k]
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf(
|
|
|
|
"lookup in '%s' failed to find '%s'",
|
|
|
|
args[0], args[1])
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
2014-07-21 14:56:03 -05:00
|
|
|
}
|