diff --git a/config/expr_parse_test.go b/config/expr_parse_test.go index a8d34d553..da77c7bda 100644 --- a/config/expr_parse_test.go +++ b/config/expr_parse_test.go @@ -34,6 +34,18 @@ func TestExprParse(t *testing.T) { false, }, + { + "module.foo.bar", + &VariableInterpolation{ + Variable: &ModuleVariable{ + Name: "foo", + Field: "bar", + key: "module.foo.bar", + }, + }, + false, + }, + { "lookup(var.foo, var.bar)", &FunctionInterpolation{ diff --git a/config/interpolate.go b/config/interpolate.go index cffd58b17..b6d84d425 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -52,6 +52,14 @@ type VariableInterpolation struct { Variable InterpolatedVariable } +// A ModuleVariable is a variable that is referencing the output +// of a module, such as "${module.foo.bar}" +type ModuleVariable struct { + Name string + Field string + key string +} + // A ResourceVariable is a variable that is referencing the field // of a resource, such as "${aws_instance.foo.ami}" type ResourceVariable struct { @@ -76,11 +84,13 @@ type UserVariable struct { } func NewInterpolatedVariable(v string) (InterpolatedVariable, error) { - if !strings.HasPrefix(v, "var.") { + if strings.HasPrefix(v, "var.") { + return NewUserVariable(v) + } else if strings.HasPrefix(v, "module.") { + return NewModuleVariable(v) + } else { return NewResourceVariable(v) } - - return NewUserVariable(v) } func (i *FunctionInterpolation) Interpolate( @@ -142,6 +152,25 @@ func (i *VariableInterpolation) Variables() map[string]InterpolatedVariable { return map[string]InterpolatedVariable{i.Variable.FullKey(): i.Variable} } +func NewModuleVariable(key string) (*ModuleVariable, error) { + parts := strings.SplitN(key, ".", 3) + if len(parts) < 3 { + return nil, fmt.Errorf( + "%s: module variables must be three parts: module.name.attr", + key) + } + + return &ModuleVariable{ + Name: parts[1], + Field: parts[2], + key: key, + }, nil +} + +func (v *ModuleVariable) FullKey() string { + return v.key +} + func NewResourceVariable(key string) (*ResourceVariable, error) { parts := strings.SplitN(key, ".", 3) if len(parts) < 3 { diff --git a/config/interpolate_test.go b/config/interpolate_test.go index 92bb0f15c..61d188d22 100644 --- a/config/interpolate_test.go +++ b/config/interpolate_test.go @@ -20,6 +20,15 @@ func TestNewInterpolatedVariable(t *testing.T) { }, false, }, + { + "module.foo.bar", + &ModuleVariable{ + Name: "foo", + Field: "bar", + key: "module.foo.bar", + }, + false, + }, } for i, tc := range cases {