From 786334b6431c67cbd26144c06759eb89471bb1b7 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 13 Mar 2017 16:09:06 -0700 Subject: [PATCH] config: parse TerraformVariables --- config/interpolate.go | 25 +++++++++++++++++++++++++ config/interpolate_test.go | 8 ++++++++ 2 files changed, 33 insertions(+) diff --git a/config/interpolate.go b/config/interpolate.go index 5867c6333..bbb355541 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -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 := "" diff --git a/config/interpolate_test.go b/config/interpolate_test.go index e5224ee0d..0cdb18b69 100644 --- a/config/interpolate_test.go +++ b/config/interpolate_test.go @@ -63,6 +63,14 @@ func TestNewInterpolatedVariable(t *testing.T) { }, false, }, + { + "terraform.env", + &TerraformVariable{ + Field: "env", + key: "terraform.env", + }, + false, + }, } for i, tc := range cases {