diff --git a/config/config.go b/config/config.go index 849508cef..39f0cce0b 100644 --- a/config/config.go +++ b/config/config.go @@ -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 } diff --git a/config/config_test.go b/config/config_test.go index 2afc89236..613c6a58b 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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 {