config: parse TerraformVariables

This commit is contained in:
Mitchell Hashimoto 2017-03-13 16:09:06 -07:00
parent 4d6242dfe0
commit 786334b643
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
2 changed files with 33 additions and 0 deletions

View File

@ -84,6 +84,13 @@ type SimpleVariable struct {
Key string
}
// TerraformVariable is a "terraform."-prefixed variable used to access
// metadata about the Terraform run.
type TerraformVariable struct {
Field string
key string
}
// A UserVariable is a variable that is referencing a user variable
// that is inputted from outside the configuration. This looks like
// "${var.foo}"
@ -101,6 +108,8 @@ func NewInterpolatedVariable(v string) (InterpolatedVariable, error) {
return NewPathVariable(v)
} else if strings.HasPrefix(v, "self.") {
return NewSelfVariable(v)
} else if strings.HasPrefix(v, "terraform.") {
return NewTerraformVariable(v)
} else if strings.HasPrefix(v, "var.") {
return NewUserVariable(v)
} else if strings.HasPrefix(v, "module.") {
@ -278,6 +287,22 @@ func (v *SimpleVariable) GoString() string {
return fmt.Sprintf("*%#v", *v)
}
func NewTerraformVariable(key string) (*TerraformVariable, error) {
field := key[len("terraform."):]
return &TerraformVariable{
Field: field,
key: key,
}, nil
}
func (v *TerraformVariable) FullKey() string {
return v.key
}
func (v *TerraformVariable) GoString() string {
return fmt.Sprintf("*%#v", *v)
}
func NewUserVariable(key string) (*UserVariable, error) {
name := key[len("var."):]
elem := ""

View File

@ -63,6 +63,14 @@ func TestNewInterpolatedVariable(t *testing.T) {
},
false,
},
{
"terraform.env",
&TerraformVariable{
Field: "env",
key: "terraform.env",
},
false,
},
}
for i, tc := range cases {