config: special detect ResourceVariables for multi-access

This commit is contained in:
Mitchell Hashimoto 2014-07-05 10:34:52 -07:00
parent 4b5f5aec65
commit 07f98f7ee7
2 changed files with 37 additions and 4 deletions

View File

@ -63,9 +63,10 @@ type InterpolatedVariable interface {
// A ResourceVariable is a variable that is referencing the field
// of a resource, such as "${aws_instance.foo.ami}"
type ResourceVariable struct {
Type string
Name string
Field string
Type string // Resource type, i.e. "aws_instance"
Name string // Resource name
Field string // Resource field
Multi bool // True if multi-variable: aws_instance.foo.*.id
key string
}
@ -203,10 +204,19 @@ func (v *Variable) Required() bool {
func NewResourceVariable(key string) (*ResourceVariable, error) {
parts := strings.SplitN(key, ".", 3)
field := parts[2]
multi := false
if idx := strings.Index(field, "."); idx != -1 && field[:idx] == "*" {
multi = true
field = field[idx+1:]
}
return &ResourceVariable{
Type: parts[0],
Name: parts[1],
Field: parts[2],
Field: field,
Multi: multi,
key: key,
}, nil
}

View File

@ -58,12 +58,35 @@ func TestNewResourceVariable(t *testing.T) {
if v.Field != "baz" {
t.Fatalf("bad: %#v", v)
}
if v.Multi {
t.Fatal("should not be multi")
}
if v.FullKey() != "foo.bar.baz" {
t.Fatalf("bad: %#v", v)
}
}
func TestResourceVariable_Multi(t *testing.T) {
v, err := NewResourceVariable("foo.bar.*.baz")
if err != nil {
t.Fatalf("err: %s", err)
}
if v.Type != "foo" {
t.Fatalf("bad: %#v", v)
}
if v.Name != "bar" {
t.Fatalf("bad: %#v", v)
}
if v.Field != "baz" {
t.Fatalf("bad: %#v", v)
}
if !v.Multi {
t.Fatal("should be multi")
}
}
func TestNewUserVariable(t *testing.T) {
v, err := NewUserVariable("var.bar")
if err != nil {