lang/globalref: Global reference analysis utilities
Our existing functionality for dealing with references generally only has
to concern itself with one level of references at a time, and only within
one module, because we use it to draw a dependency graph which then ends
up reflecting the broader context.
However, there are some situations where it's handy to be able to ask
questions about the indirect contributions to a particular expression in
the configuration, particularly for additional hints in the user interface
where we're just providing some extra context rather than changing
behavior.
This new "globalref" package therefore aims to be the home for algorithms
for use-cases like this. It introduces its own special "Reference" type
that wraps addrs.Reference to annotate it also with the usually-implied
context about where the references would be evaluated.
With that building block we can therefore ask questions whose answers
might involve discussing references in multiple packages at once, such as
"which resources directly or indirectly contribute to this expression?",
including indirect hops through input variables or output values which
would therefore change the evaluation context.
The current implementations of this are around mapping references onto the
static configuration expressions that they refer to, which is a pretty
broad and conservative approach that unfortunately therefore loses
accuracy when confronted with complex expressions that might take dynamic
actions on the contents of an object. My hunch is that this'll be good
enough to get some initial small use-cases solved, though there's plenty
room for improvement in accuracy.
It's somewhat ironic that this sort of "what is this value built from?"
question is the use-case I had in mind when I designed the "marks" feature
in cty, yet we've ended up putting it to an unexpected but still valid
use in Terraform for sensitivity analysis and our currently handling of
that isn't really tight enough to permit other concurrent uses of marks
for other use-cases. I expect we can address that later and so maybe we'll
try for a more accurate version of these analyses at a later date, but my
hunch is that this'll be good enough for us to still get some good use out
of it in the near future, particular related to helping understand where
unknown values came from and in tailoring our refresh results in plan
output to deemphasize detected changes that couldn't possibly have
contributed to the proposed plan.
2021-06-09 21:11:44 +02:00
|
|
|
package globalref
|
|
|
|
|
|
|
|
import (
|
2022-02-04 18:58:07 +01:00
|
|
|
"sort"
|
lang/globalref: Global reference analysis utilities
Our existing functionality for dealing with references generally only has
to concern itself with one level of references at a time, and only within
one module, because we use it to draw a dependency graph which then ends
up reflecting the broader context.
However, there are some situations where it's handy to be able to ask
questions about the indirect contributions to a particular expression in
the configuration, particularly for additional hints in the user interface
where we're just providing some extra context rather than changing
behavior.
This new "globalref" package therefore aims to be the home for algorithms
for use-cases like this. It introduces its own special "Reference" type
that wraps addrs.Reference to annotate it also with the usually-implied
context about where the references would be evaluated.
With that building block we can therefore ask questions whose answers
might involve discussing references in multiple packages at once, such as
"which resources directly or indirectly contribute to this expression?",
including indirect hops through input variables or output values which
would therefore change the evaluation context.
The current implementations of this are around mapping references onto the
static configuration expressions that they refer to, which is a pretty
broad and conservative approach that unfortunately therefore loses
accuracy when confronted with complex expressions that might take dynamic
actions on the contents of an object. My hunch is that this'll be good
enough to get some initial small use-cases solved, though there's plenty
room for improvement in accuracy.
It's somewhat ironic that this sort of "what is this value built from?"
question is the use-case I had in mind when I designed the "marks" feature
in cty, yet we've ended up putting it to an unexpected but still valid
use in Terraform for sensitivity analysis and our currently handling of
that isn't really tight enough to permit other concurrent uses of marks
for other use-cases. I expect we can address that later and so maybe we'll
try for a more accurate version of these analyses at a later date, but my
hunch is that this'll be good enough for us to still get some good use out
of it in the near future, particular related to helping understand where
unknown values came from and in tailoring our refresh results in plan
output to deemphasize detected changes that couldn't possibly have
contributed to the proposed plan.
2021-06-09 21:11:44 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAnalyzerContributingResources(t *testing.T) {
|
|
|
|
azr := testAnalyzer(t, "contributing-resources")
|
|
|
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
StartRefs func() []Reference
|
|
|
|
WantAddrs []string
|
|
|
|
}{
|
|
|
|
"root output 'network'": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromOutputValue(
|
|
|
|
addrs.OutputValue{Name: "network"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
`data.test_thing.environment`,
|
|
|
|
`module.network.test_thing.subnet`,
|
|
|
|
`module.network.test_thing.vpc`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"root output 'c10s_url'": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromOutputValue(
|
|
|
|
addrs.OutputValue{Name: "c10s_url"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
`data.test_thing.environment`,
|
|
|
|
`module.compute.test_thing.load_balancer`,
|
|
|
|
`module.network.test_thing.subnet`,
|
|
|
|
`module.network.test_thing.vpc`,
|
|
|
|
|
|
|
|
// NOTE: module.compute.test_thing.controller isn't here
|
|
|
|
// because we can see statically that the output value refers
|
|
|
|
// only to the "string" attribute of
|
|
|
|
// module.compute.test_thing.load_balancer , and so we
|
|
|
|
// don't consider references inside the "list" blocks.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"module.compute.test_thing.load_balancer": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromResourceInstance(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "load_balancer",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("compute", addrs.NoKey)),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
`data.test_thing.environment`,
|
|
|
|
`module.compute.test_thing.controller`,
|
|
|
|
`module.network.test_thing.subnet`,
|
|
|
|
`module.network.test_thing.vpc`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"data.test_thing.environment": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromResourceInstance(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "environment",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
// Nothing! This one only refers to an input variable.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
startRefs := test.StartRefs()
|
|
|
|
addrs := azr.ContributingResources(startRefs...)
|
|
|
|
|
|
|
|
want := test.WantAddrs
|
|
|
|
got := make([]string, len(addrs))
|
|
|
|
for i, addr := range addrs {
|
|
|
|
got[i] = addr.String()
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong addresses\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-02-04 18:58:07 +01:00
|
|
|
|
|
|
|
func TestAnalyzerContributingResourceAttrs(t *testing.T) {
|
|
|
|
azr := testAnalyzer(t, "contributing-resources")
|
|
|
|
|
|
|
|
tests := map[string]struct {
|
|
|
|
StartRefs func() []Reference
|
|
|
|
WantAttrs []string
|
|
|
|
}{
|
|
|
|
"root output 'network'": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromOutputValue(
|
|
|
|
addrs.OutputValue{Name: "network"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
`data.test_thing.environment.any.base_cidr_block`,
|
|
|
|
`data.test_thing.environment.any.subnet_count`,
|
|
|
|
`module.network.test_thing.subnet`,
|
|
|
|
`module.network.test_thing.vpc.string`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"root output 'c10s_url'": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromOutputValue(
|
|
|
|
addrs.OutputValue{Name: "c10s_url"}.Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
`data.test_thing.environment.any.base_cidr_block`,
|
|
|
|
`data.test_thing.environment.any.subnet_count`,
|
|
|
|
`module.compute.test_thing.load_balancer.string`,
|
|
|
|
`module.network.test_thing.subnet`,
|
|
|
|
`module.network.test_thing.vpc.string`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"module.compute.test_thing.load_balancer": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromResourceInstance(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.ManagedResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "load_balancer",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance.Child("compute", addrs.NoKey)),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
`data.test_thing.environment.any.base_cidr_block`,
|
|
|
|
`data.test_thing.environment.any.subnet_count`,
|
|
|
|
`module.compute.test_thing.controller`,
|
|
|
|
`module.network.test_thing.subnet`,
|
|
|
|
`module.network.test_thing.vpc.string`,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"data.test_thing.environment": {
|
|
|
|
func() []Reference {
|
|
|
|
return azr.ReferencesFromResourceInstance(
|
|
|
|
addrs.Resource{
|
|
|
|
Mode: addrs.DataResourceMode,
|
|
|
|
Type: "test_thing",
|
|
|
|
Name: "environment",
|
|
|
|
}.Instance(addrs.NoKey).Absolute(addrs.RootModuleInstance),
|
|
|
|
)
|
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
// Nothing! This one only refers to an input variable.
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, test := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
startRefs := test.StartRefs()
|
|
|
|
refs := azr.ContributingResourceReferences(startRefs...)
|
|
|
|
|
|
|
|
want := test.WantAttrs
|
|
|
|
got := make([]string, len(refs))
|
|
|
|
for i, ref := range refs {
|
|
|
|
resAttr, ok := ref.ResourceAttr()
|
|
|
|
if !ok {
|
|
|
|
t.Errorf("%s is not a resource attr reference", resAttr.DebugString())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
got[i] = resAttr.DebugString()
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(got)
|
|
|
|
|
|
|
|
if diff := cmp.Diff(want, got); diff != "" {
|
|
|
|
t.Errorf("wrong addresses\n%s", diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|