config: parse ${path.module}

This commit is contained in:
Mitchell Hashimoto 2014-10-07 18:03:11 -07:00
parent 28cd738edc
commit 679ab1d515
6 changed files with 78 additions and 11 deletions

View File

@ -201,17 +201,22 @@ func (c *Config) Validate() error {
// Check that all count variables are valid. // Check that all count variables are valid.
for source, vs := range vars { for source, vs := range vars {
for _, v := range vs { for _, rawV := range vs {
cv, ok := v.(*CountVariable) switch v := rawV.(type) {
if !ok { case *CountVariable:
continue if v.Type == CountValueInvalid {
} errs = append(errs, fmt.Errorf(
"%s: invalid count variable: %s",
if cv.Type == CountValueInvalid { source,
errs = append(errs, fmt.Errorf( v.FullKey()))
"%s: invalid count variable: %s", }
source, case *PathVariable:
cv.FullKey())) if v.Type == PathValueInvalid {
errs = append(errs, fmt.Errorf(
"%s: invalid path variable: %s",
source,
v.FullKey()))
}
} }
} }
} }

View File

@ -137,6 +137,20 @@ func TestConfigValidate_outputBadField(t *testing.T) {
} }
} }
func TestConfigValidate_pathVar(t *testing.T) {
c := testConfig(t, "validate-path-var")
if err := c.Validate(); err != nil {
t.Fatal("err: %s", err)
}
}
func TestConfigValidate_pathVarInvalid(t *testing.T) {
c := testConfig(t, "validate-path-var-invalid")
if err := c.Validate(); err == nil {
t.Fatal("should not be valid")
}
}
func TestConfigValidate_unknownThing(t *testing.T) { func TestConfigValidate_unknownThing(t *testing.T) {
c := testConfig(t, "validate-unknownthing") c := testConfig(t, "validate-unknownthing")
if err := c.Validate(); err == nil { if err := c.Validate(); err == nil {

View File

@ -75,6 +75,20 @@ type ModuleVariable struct {
key string key string
} }
// A PathVariable is a variable that references path information about the
// module.
type PathVariable struct {
Type PathValueType
key string
}
type PathValueType byte
const (
PathValueInvalid PathValueType = iota
PathValueModule
)
// A ResourceVariable is a variable that is referencing the field // A ResourceVariable is a variable that is referencing the field
// of a resource, such as "${aws_instance.foo.ami}" // of a resource, such as "${aws_instance.foo.ami}"
type ResourceVariable struct { type ResourceVariable struct {
@ -101,6 +115,8 @@ type UserVariable struct {
func NewInterpolatedVariable(v string) (InterpolatedVariable, error) { func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
if strings.HasPrefix(v, "count.") { if strings.HasPrefix(v, "count.") {
return NewCountVariable(v) return NewCountVariable(v)
} else if strings.HasPrefix(v, "path.") {
return NewPathVariable(v)
} else if strings.HasPrefix(v, "var.") { } else if strings.HasPrefix(v, "var.") {
return NewUserVariable(v) return NewUserVariable(v)
} else if strings.HasPrefix(v, "module.") { } else if strings.HasPrefix(v, "module.") {
@ -206,6 +222,24 @@ func (v *ModuleVariable) FullKey() string {
return v.key return v.key
} }
func NewPathVariable(key string) (*PathVariable, error) {
var fieldType PathValueType
parts := strings.SplitN(key, ".", 2)
switch parts[1] {
case "module":
fieldType = PathValueModule
}
return &PathVariable{
Type: fieldType,
key: key,
}, nil
}
func (v *PathVariable) FullKey() string {
return v.key
}
func NewResourceVariable(key string) (*ResourceVariable, error) { func NewResourceVariable(key string) (*ResourceVariable, error) {
parts := strings.SplitN(key, ".", 3) parts := strings.SplitN(key, ".", 3)
if len(parts) < 3 { if len(parts) < 3 {

View File

@ -45,6 +45,14 @@ func TestNewInterpolatedVariable(t *testing.T) {
}, },
false, false,
}, },
{
"path.module",
&PathVariable{
Type: PathValueModule,
key: "path.module",
},
false,
},
} }
for i, tc := range cases { for i, tc := range cases {

View File

@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
foo = "${path.nope}"
}

View File

@ -0,0 +1,3 @@
resource "aws_instance" "foo" {
foo = "${path.module}"
}