core: error instead of panic on self var in wrong scope
Fixes #4808 Fixes #5174
This commit is contained in:
parent
fbe4df8d31
commit
fbc9cf9ddb
|
@ -238,6 +238,10 @@ func (i *Interpolater) valueSelfVar(
|
||||||
n string,
|
n string,
|
||||||
v *config.SelfVariable,
|
v *config.SelfVariable,
|
||||||
result map[string]ast.Variable) error {
|
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(
|
rv, err := config.NewResourceVariable(fmt.Sprintf(
|
||||||
"%s.%s.%d.%s",
|
"%s.%s.%d.%s",
|
||||||
scope.Resource.Type,
|
scope.Resource.Type,
|
||||||
|
|
|
@ -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 {
|
func getInterpolaterFixture(t *testing.T) *Interpolater {
|
||||||
lock := new(sync.RWMutex)
|
lock := new(sync.RWMutex)
|
||||||
state := &State{
|
state := &State{
|
||||||
|
|
Loading…
Reference in New Issue