Skip to content

Commit

Permalink
config: parse TerraformVariables
Browse files Browse the repository at this point in the history
  • Loading branch information
mitchellh committed Mar 13, 2017
1 parent 4d6242d commit 786334b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
25 changes: 25 additions & 0 deletions config/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand All @@ -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.") {
Expand Down Expand Up @@ -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 := ""
Expand Down
8 changes: 8 additions & 0 deletions config/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func TestNewInterpolatedVariable(t *testing.T) {
},
false,
},
{
"terraform.env",
&TerraformVariable{
Field: "env",
key: "terraform.env",
},
false,
},
}

for i, tc := range cases {
Expand Down

0 comments on commit 786334b

Please sign in to comment.