config: can parse module variables

This commit is contained in:
Mitchell Hashimoto 2014-09-15 11:40:25 -07:00
parent 12e7c75211
commit 46c140c797
3 changed files with 53 additions and 3 deletions

View File

@ -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{

View File

@ -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 {

View File

@ -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 {