From 9c612964d82da8dfa17014646b0c76db74450852 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 23 Feb 2015 14:34:25 -0800 Subject: [PATCH] config: self variables --- config/interpolate.go | 28 ++++++++++++++++++++++++++++ config/interpolate_test.go | 8 ++++++++ 2 files changed, 36 insertions(+) diff --git a/config/interpolate.go b/config/interpolate.go index b13cdb405c..af0a84da49 100644 --- a/config/interpolate.go +++ b/config/interpolate.go @@ -68,6 +68,14 @@ type ResourceVariable struct { key string } +// SelfVariable is a variable that is referencing the same resource +// it is running on: "${self.address}" +type SelfVariable 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}" @@ -83,6 +91,8 @@ func NewInterpolatedVariable(v string) (InterpolatedVariable, error) { return NewCountVariable(v) } else if strings.HasPrefix(v, "path.") { return NewPathVariable(v) + } else if strings.HasPrefix(v, "self.") { + return NewSelfVariable(v) } else if strings.HasPrefix(v, "var.") { return NewUserVariable(v) } else if strings.HasPrefix(v, "module.") { @@ -199,6 +209,24 @@ func (v *ResourceVariable) FullKey() string { return v.key } +func NewSelfVariable(key string) (*SelfVariable, error) { + field := key[len("self."):] + + return &SelfVariable{ + Field: field, + + key: key, + }, nil +} + +func (v *SelfVariable) FullKey() string { + return v.key +} + +func (v *SelfVariable) 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 46945ee810..69a6ca2293 100644 --- a/config/interpolate_test.go +++ b/config/interpolate_test.go @@ -54,6 +54,14 @@ func TestNewInterpolatedVariable(t *testing.T) { }, false, }, + { + "self.address", + &SelfVariable{ + Field: "address", + key: "self.address", + }, + false, + }, } for i, tc := range cases {