diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 6e5ec98df..9c076db2d 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -1,5 +1,10 @@ package config +import ( + "fmt" + "strings" +) + // Funcs is the mapping of built-in functions for configuration. var Funcs map[string]InterpolationFunc @@ -13,5 +18,18 @@ func init() { // dynamic lookups of map types within a Terraform configuration. func interpolationFuncLookup( vs map[string]string, args ...string) (string, error) { - return "", nil + 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 } diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go new file mode 100644 index 000000000..20f964c43 --- /dev/null +++ b/config/interpolate_funcs_test.go @@ -0,0 +1,54 @@ +package config + +import ( + "testing" +) + +func TestInterpolateFuncLookup(t *testing.T) { + cases := []struct { + M map[string]string + Args []string + Result string + Error bool + }{ + { + map[string]string{ + "var.foo.bar": "baz", + }, + []string{"foo", "bar"}, + "baz", + false, + }, + + // Invalid key + { + map[string]string{ + "var.foo.bar": "baz", + }, + []string{"foo", "baz"}, + "", + true, + }, + + // Too many args + { + map[string]string{ + "var.foo.bar": "baz", + }, + []string{"foo", "bar", "baz"}, + "", + true, + }, + } + + for i, tc := range cases { + actual, err := interpolationFuncLookup(tc.M, tc.Args...) + if (err != nil) != tc.Error { + t.Fatalf("%d: err: %s", i, err) + } + + if actual != tc.Result { + t.Fatalf("%d: bad: %#v", i, actual) + } + } +}