lang/eval: more evalContext fixups

self references do not need to be added to `managedResource`, and in
fact that could cause issues later if self is allowed in contexts other
than managed resources.

Coalesce 2 cases in the Referenceable switch, be take the
ContainingResource address of an instance beforehand.
This commit is contained in:
James Bardin 2019-10-07 18:13:20 -04:00
parent fed4e82c25
commit bfce78064b
2 changed files with 9 additions and 43 deletions

View File

@ -225,14 +225,14 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
continue
}
// self can only be used within a resource instance
subj := selfAddr.(addrs.ResourceInstance)
if selfAddr == addrs.Self {
// Programming error: the self address cannot alias itself.
panic("scope SelfAddr attempting to alias itself")
}
// self can only be used within a resource instance
subj := selfAddr.(addrs.ResourceInstance)
val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng))
diags = diags.Append(valDiags)
@ -249,16 +249,16 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
self = val
}
r := subj.Resource
if managedResources[r.Type] == nil {
managedResources[r.Type] = make(map[string]cty.Value)
}
managedResources[r.Type][r.Name] = val
continue
}
// This type switch must cover all of the "Referenceable" implementations
// in package addrs.
// in package addrs, however we are removing the possibility of
// ResourceInstance beforehand.
if addr, ok := rawSubj.(addrs.ResourceInstance); ok {
rawSubj = addr.ContainingResource()
}
switch subj := rawSubj.(type) {
case addrs.Resource:
var into map[string]map[string]cty.Value
@ -280,27 +280,6 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
}
into[r.Type][r.Name] = val
case addrs.ResourceInstance:
var into map[string]map[string]cty.Value
switch subj.Resource.Mode {
case addrs.ManagedResourceMode:
into = managedResources
case addrs.DataResourceMode:
into = dataResources
default:
panic(fmt.Errorf("unsupported ResourceMode %s", subj.Resource.Mode))
}
val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng))
diags = diags.Append(valDiags)
r := subj.Resource
if into[r.Type] == nil {
into[r.Type] = make(map[string]cty.Value)
}
into[r.Type][r.Name] = val
case addrs.ModuleCallInstance:
val, valDiags := normalizeRefValue(s.Data.GetModuleInstance(subj, rng))
diags = diags.Append(valDiags)

View File

@ -237,19 +237,6 @@ func TestScopeEvalContext(t *testing.T) {
{
`self.baz`,
map[string]cty.Value{
// In the test function below we set "SelfAddr" to be
// one of the resources in our dataset, causing it to get
// expanded here and then copied into "self".
"null_resource": cty.ObjectVal(map[string]cty.Value{
"multi": cty.TupleVal([]cty.Value{
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("multi0"),
}),
cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("multi1"),
}),
}),
}),
"self": cty.ObjectVal(map[string]cty.Value{
"attr": cty.StringVal("multi1"),
}),