From fbc9cf9ddb2bc9e2968d9c8ac29f7ef329f4c960 Mon Sep 17 00:00:00 2001 From: Paul Hinze Date: Tue, 23 Feb 2016 11:42:55 -0600 Subject: [PATCH] core: error instead of panic on self var in wrong scope Fixes #4808 Fixes #5174 --- terraform/interpolate.go | 4 ++++ terraform/interpolate_test.go | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/terraform/interpolate.go b/terraform/interpolate.go index 70a6e8b0e..3db060b0b 100644 --- a/terraform/interpolate.go +++ b/terraform/interpolate.go @@ -238,6 +238,10 @@ func (i *Interpolater) valueSelfVar( n string, v *config.SelfVariable, result map[string]ast.Variable) error { + if scope == nil || scope.Resource == nil { + return fmt.Errorf( + "%s: invalid scope, self variables are only valid on resources", n) + } rv, err := config.NewResourceVariable(fmt.Sprintf( "%s.%s.%d.%s", scope.Resource.Type, diff --git a/terraform/interpolate_test.go b/terraform/interpolate_test.go index 6475ad9ae..31d066ba9 100644 --- a/terraform/interpolate_test.go +++ b/terraform/interpolate_test.go @@ -386,6 +386,24 @@ func TestInterpolator_resourceMultiAttributesComputed(t *testing.T) { }) } +func TestInterpolater_selfVarWithoutResource(t *testing.T) { + i := &Interpolater{} + + scope := &InterpolationScope{ + Path: rootModulePath, + } + + v, err := config.NewInterpolatedVariable("self.name") + if err != nil { + t.Fatalf("err: %s", err) + } + + _, err = i.Values(scope, map[string]config.InterpolatedVariable{"foo": v}) + if err == nil { + t.Fatalf("expected err, got none") + } +} + func getInterpolaterFixture(t *testing.T) *Interpolater { lock := new(sync.RWMutex) state := &State{