Merge pull request #7138 from hashicorp/b-better-dot-index-error

core: Better error for dot indexing on user vars
This commit is contained in:
Paul Hinze 2016-06-12 11:20:30 -05:00 committed by GitHub
commit 2127466280
3 changed files with 10 additions and 15 deletions

View File

@ -282,6 +282,10 @@ func NewUserVariable(key string) (*UserVariable, error) {
name = name[:idx] name = name[:idx]
} }
if len(elem) > 0 {
return nil, fmt.Errorf("Invalid dot index found: 'var.%s.%s'. Values in maps and lists can be referenced using square bracket indexing, like: 'var.mymap[\"key\"]' or 'var.mylist[1]'.", name, elem)
}
return &UserVariable{ return &UserVariable{
key: key, key: key,

View File

@ -2,6 +2,7 @@ package config
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
"github.com/hashicorp/hil" "github.com/hashicorp/hil"
@ -143,20 +144,10 @@ func TestNewUserVariable(t *testing.T) {
} }
} }
func TestNewUserVariable_map(t *testing.T) { func TestNewUserVariable_oldMapDotIndexErr(t *testing.T) {
v, err := NewUserVariable("var.bar.baz") _, err := NewUserVariable("var.bar.baz")
if err != nil { if err == nil || !strings.Contains(err.Error(), "Invalid dot index") {
t.Fatalf("err: %s", err) t.Fatalf("Expected dot index err, got: %#v", err)
}
if v.Name != "bar" {
t.Fatalf("bad: %#v", v.Name)
}
if v.Elem != "baz" {
t.Fatalf("bad: %#v", v.Elem)
}
if v.FullKey() != "var.bar.baz" {
t.Fatalf("bad: %#v", v)
} }
} }

View File

@ -22,7 +22,7 @@ resource "aws_security_group" "firewall" {
} }
resource aws_instance "web" { resource aws_instance "web" {
ami = "${var.amis.east}" ami = "${var.amis["east"]}"
security_groups = [ security_groups = [
"foo", "foo",
"${aws_security_group.firewall.foo}" "${aws_security_group.firewall.foo}"