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:
parent
fed4e82c25
commit
bfce78064b
39
lang/eval.go
39
lang/eval.go
|
@ -225,14 +225,14 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// self can only be used within a resource instance
|
|
||||||
subj := selfAddr.(addrs.ResourceInstance)
|
|
||||||
|
|
||||||
if selfAddr == addrs.Self {
|
if selfAddr == addrs.Self {
|
||||||
// Programming error: the self address cannot alias itself.
|
// Programming error: the self address cannot alias itself.
|
||||||
panic("scope SelfAddr attempting to 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))
|
val, valDiags := normalizeRefValue(s.Data.GetResource(subj.ContainingResource(), rng))
|
||||||
|
|
||||||
diags = diags.Append(valDiags)
|
diags = diags.Append(valDiags)
|
||||||
|
@ -249,16 +249,16 @@ func (s *Scope) evalContext(refs []*addrs.Reference, selfAddr addrs.Referenceabl
|
||||||
self = val
|
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
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// This type switch must cover all of the "Referenceable" implementations
|
// 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) {
|
switch subj := rawSubj.(type) {
|
||||||
case addrs.Resource:
|
case addrs.Resource:
|
||||||
var into map[string]map[string]cty.Value
|
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
|
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:
|
case addrs.ModuleCallInstance:
|
||||||
val, valDiags := normalizeRefValue(s.Data.GetModuleInstance(subj, rng))
|
val, valDiags := normalizeRefValue(s.Data.GetModuleInstance(subj, rng))
|
||||||
diags = diags.Append(valDiags)
|
diags = diags.Append(valDiags)
|
||||||
|
|
|
@ -237,19 +237,6 @@ func TestScopeEvalContext(t *testing.T) {
|
||||||
{
|
{
|
||||||
`self.baz`,
|
`self.baz`,
|
||||||
map[string]cty.Value{
|
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{
|
"self": cty.ObjectVal(map[string]cty.Value{
|
||||||
"attr": cty.StringVal("multi1"),
|
"attr": cty.StringVal("multi1"),
|
||||||
}),
|
}),
|
||||||
|
|
Loading…
Reference in New Issue