Skip to content

Commit

Permalink
core: error instead of panic on self var in wrong scope
Browse files Browse the repository at this point in the history
Fixes #4808
Fixes #5174
  • Loading branch information
phinze committed Feb 23, 2016
1 parent fbe4df8 commit 45d65ee
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion command/plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func TestPlan_outPath(t *testing.T) {

args := []string{
"-out", outPath,
testFixturePath("plan"),
testFixturePath("apply-tainted-targets"),
}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
Expand Down
4 changes: 4 additions & 0 deletions terraform/interpolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 18 additions & 0 deletions terraform/interpolate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 45d65ee

Please sign in to comment.