2016-09-16 03:30:11 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2016-11-07 22:55:35 +01:00
|
|
|
"log"
|
2019-10-30 20:59:34 +01:00
|
|
|
"sort"
|
2016-09-16 03:30:11 +02:00
|
|
|
|
2019-09-10 00:58:44 +02:00
|
|
|
"github.com/hashicorp/hcl/v2"
|
2021-05-17 21:00:50 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/addrs"
|
2021-05-17 21:17:09 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/configs/configschema"
|
2021-05-17 18:30:37 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/dag"
|
2021-05-17 21:23:42 +02:00
|
|
|
"github.com/hashicorp/terraform/internal/lang"
|
2016-09-16 03:30:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// GraphNodeReferenceable must be implemented by any node that represents
|
|
|
|
// a Terraform thing that can be referenced (resource, module, etc.).
|
2016-11-13 00:38:28 +01:00
|
|
|
//
|
|
|
|
// Even if the thing has no name, this should return an empty list. By
|
|
|
|
// implementing this and returning a non-nil result, you say that this CAN
|
|
|
|
// be referenced and other methods of referencing may still be possible (such
|
|
|
|
// as by path!)
|
2016-09-16 03:30:11 +02:00
|
|
|
type GraphNodeReferenceable interface {
|
2020-03-05 03:00:16 +01:00
|
|
|
GraphNodeModulePath
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
|
|
|
|
// ReferenceableAddrs returns a list of addresses through which this can be
|
|
|
|
// referenced.
|
|
|
|
ReferenceableAddrs() []addrs.Referenceable
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeReferencer must be implemented by nodes that reference other
|
|
|
|
// Terraform items and therefore depend on them.
|
|
|
|
type GraphNodeReferencer interface {
|
2020-03-05 03:00:16 +01:00
|
|
|
GraphNodeModulePath
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
|
|
|
|
// References returns a list of references made by this node, which
|
|
|
|
// include both a referenced address and source location information for
|
|
|
|
// the reference.
|
|
|
|
References() []*addrs.Reference
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|
|
|
|
|
2019-10-17 22:05:27 +02:00
|
|
|
type GraphNodeAttachDependencies interface {
|
2020-03-15 16:32:06 +01:00
|
|
|
GraphNodeConfigResource
|
|
|
|
AttachDependencies([]addrs.ConfigResource)
|
2019-10-17 22:05:27 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
// graphNodeDependsOn is implemented by resources that need to expose any
|
|
|
|
// references set via DependsOn in their configuration.
|
|
|
|
type graphNodeDependsOn interface {
|
2020-09-17 21:32:32 +02:00
|
|
|
GraphNodeReferencer
|
2020-06-04 03:36:42 +02:00
|
|
|
DependsOn() []*addrs.Reference
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:46:28 +01:00
|
|
|
// graphNodeAttachDataResourceDependsOn records all resources that are transitively
|
2020-05-01 16:22:50 +02:00
|
|
|
// referenced through depends_on in the configuration. This is used by data
|
|
|
|
// resources to determine if they can be read during the plan, or if they need
|
|
|
|
// to be further delayed until apply.
|
|
|
|
// We can only use an addrs.ConfigResource address here, because modules are
|
|
|
|
// not yet expended in the graph. While this will cause some extra data
|
|
|
|
// resources to show in the plan when their depends_on references may be in
|
|
|
|
// unrelated module instances, the fact that it only happens when there are any
|
2020-05-09 00:46:32 +02:00
|
|
|
// resource updates pending means we can still avoid the problem of the
|
2020-05-01 16:22:50 +02:00
|
|
|
// "perpetual diff"
|
2021-03-08 19:46:28 +01:00
|
|
|
type graphNodeAttachDataResourceDependsOn interface {
|
2020-05-01 16:22:50 +02:00
|
|
|
GraphNodeConfigResource
|
2020-06-04 03:36:42 +02:00
|
|
|
graphNodeDependsOn
|
2020-06-04 18:39:36 +02:00
|
|
|
|
2021-03-08 19:46:28 +01:00
|
|
|
// AttachDataResourceDependsOn stores the discovered dependencies in the
|
2020-06-04 18:39:36 +02:00
|
|
|
// resource node for evaluation later.
|
|
|
|
//
|
|
|
|
// The force parameter indicates that even if there are no dependencies,
|
|
|
|
// force the data source to act as though there are for refresh purposes.
|
|
|
|
// This is needed because yet-to-be-created resources won't be in the
|
|
|
|
// initial refresh graph, but may still be referenced through depends_on.
|
2021-03-08 19:46:28 +01:00
|
|
|
AttachDataResourceDependsOn(deps []addrs.ConfigResource, force bool)
|
2020-05-01 16:22:50 +02:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// GraphNodeReferenceOutside is an interface that can optionally be implemented.
|
|
|
|
// A node that implements it can specify that its own referenceable addresses
|
|
|
|
// and/or the addresses it references are in a different module than the
|
|
|
|
// node itself.
|
|
|
|
//
|
|
|
|
// Any referenceable addresses returned by ReferenceableAddrs are interpreted
|
|
|
|
// relative to the returned selfPath.
|
2016-09-16 22:56:10 +02:00
|
|
|
//
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// Any references returned by References are interpreted relative to the
|
|
|
|
// returned referencePath.
|
2016-09-16 22:56:10 +02:00
|
|
|
//
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// It is valid but not required for either of these paths to match what is
|
|
|
|
// returned by method Path, though if both match the main Path then there
|
|
|
|
// is no reason to implement this method.
|
|
|
|
//
|
|
|
|
// The primary use-case for this is the nodes representing module input
|
|
|
|
// variables, since their expressions are resolved in terms of their calling
|
|
|
|
// module, but they are still referenced from their own module.
|
|
|
|
type GraphNodeReferenceOutside interface {
|
|
|
|
// ReferenceOutside returns a path in which any references from this node
|
|
|
|
// are resolved.
|
2020-02-24 23:42:32 +01:00
|
|
|
ReferenceOutside() (selfPath, referencePath addrs.Module)
|
2016-09-16 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-09 00:46:32 +02:00
|
|
|
// ReferenceTransformer is a GraphTransformer that connects all the
|
2016-09-16 03:30:11 +02:00
|
|
|
// nodes that reference each other in order to form the proper ordering.
|
|
|
|
type ReferenceTransformer struct{}
|
|
|
|
|
|
|
|
func (t *ReferenceTransformer) Transform(g *Graph) error {
|
2016-09-16 08:20:35 +02:00
|
|
|
// Build a reference map so we can efficiently look up the references
|
|
|
|
vs := g.Vertices()
|
|
|
|
m := NewReferenceMap(vs)
|
|
|
|
|
|
|
|
// Find the things that reference things and connect them
|
|
|
|
for _, v := range vs {
|
2019-12-03 19:13:50 +01:00
|
|
|
if _, ok := v.(GraphNodeDestroyer); ok {
|
|
|
|
// destroy nodes references are not connected, since they can only
|
|
|
|
// use their own state.
|
|
|
|
continue
|
|
|
|
}
|
2020-02-24 23:42:32 +01:00
|
|
|
parents := m.References(v)
|
2016-11-07 22:55:35 +01:00
|
|
|
parentsDbg := make([]string, len(parents))
|
|
|
|
for i, v := range parents {
|
|
|
|
parentsDbg[i] = dag.VertexName(v)
|
|
|
|
}
|
|
|
|
log.Printf(
|
|
|
|
"[DEBUG] ReferenceTransformer: %q references: %v",
|
|
|
|
dag.VertexName(v), parentsDbg)
|
|
|
|
|
2016-09-16 08:20:35 +02:00
|
|
|
for _, parent := range parents {
|
2020-07-17 01:11:08 +02:00
|
|
|
if !graphNodesAreResourceInstancesInDifferentInstancesOfSameModule(v, parent) {
|
|
|
|
g.Connect(dag.BasicEdge(v, parent))
|
|
|
|
} else {
|
|
|
|
log.Printf("[TRACE] ReferenceTransformer: skipping %s => %s inter-module-instance dependency", v, parent)
|
|
|
|
}
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|
2019-10-17 22:05:27 +02:00
|
|
|
|
|
|
|
if len(parents) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-09-16 08:20:35 +02:00
|
|
|
}
|
2016-09-16 03:30:11 +02:00
|
|
|
|
2016-09-16 08:20:35 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:52:45 +02:00
|
|
|
type depMap map[string]addrs.ConfigResource
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
// add stores the vertex if it represents a resource in the
|
2020-05-01 23:52:45 +02:00
|
|
|
// graph.
|
|
|
|
func (m depMap) add(v dag.Vertex) {
|
|
|
|
// we're only concerned with resources which may have changes that
|
|
|
|
// need to be applied.
|
|
|
|
switch v := v.(type) {
|
|
|
|
case GraphNodeResourceInstance:
|
|
|
|
instAddr := v.ResourceInstanceAddr()
|
|
|
|
addr := instAddr.ContainingResource().Config()
|
|
|
|
m[addr.String()] = addr
|
|
|
|
case GraphNodeConfigResource:
|
|
|
|
addr := v.ResourceAddr()
|
|
|
|
m[addr.String()] = addr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-08 19:46:28 +01:00
|
|
|
// attachDataResourceDependsOnTransformer records all resources transitively
|
|
|
|
// referenced through a configuration depends_on.
|
|
|
|
type attachDataResourceDependsOnTransformer struct {
|
2020-05-01 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2021-03-08 19:46:28 +01:00
|
|
|
func (t attachDataResourceDependsOnTransformer) Transform(g *Graph) error {
|
2020-05-01 23:52:45 +02:00
|
|
|
// First we need to make a map of referenceable addresses to their vertices.
|
|
|
|
// This is very similar to what's done in ReferenceTransformer, but we keep
|
|
|
|
// implementation separate as they may need to change independently.
|
|
|
|
vertices := g.Vertices()
|
|
|
|
refMap := NewReferenceMap(vertices)
|
|
|
|
|
|
|
|
for _, v := range vertices {
|
2021-03-08 19:46:28 +01:00
|
|
|
depender, ok := v.(graphNodeAttachDataResourceDependsOn)
|
2020-05-01 23:52:45 +02:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only data need to attach depends_on, so they can determine if they
|
|
|
|
// are eligible to be read during plan.
|
2020-06-04 03:36:42 +02:00
|
|
|
if depender.ResourceAddr().Resource.Mode != addrs.DataResourceMode {
|
2020-05-01 23:52:45 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
// depMap will only add resource references then dedupe
|
|
|
|
deps := make(depMap)
|
2020-06-04 20:21:50 +02:00
|
|
|
dependsOnDeps, fromModule := refMap.dependsOn(g, depender)
|
|
|
|
for _, dep := range dependsOnDeps {
|
2020-05-01 23:52:45 +02:00
|
|
|
// any the dependency
|
2020-06-04 03:36:42 +02:00
|
|
|
deps.add(dep)
|
2020-05-01 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
res := make([]addrs.ConfigResource, 0, len(deps))
|
|
|
|
for _, d := range deps {
|
|
|
|
res = append(res, d)
|
2020-05-01 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
log.Printf("[TRACE] attachDataDependenciesTransformer: %s depends on %s", depender.ResourceAddr(), res)
|
2021-03-08 19:46:28 +01:00
|
|
|
depender.AttachDataResourceDependsOn(res, fromModule)
|
2020-05-01 23:52:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-17 22:05:27 +02:00
|
|
|
// AttachDependenciesTransformer records all resource dependencies for each
|
|
|
|
// instance, and attaches the addresses to the node itself. Managed resource
|
|
|
|
// will record these in the state for proper ordering of destroy operations.
|
|
|
|
type AttachDependenciesTransformer struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t AttachDependenciesTransformer) Transform(g *Graph) error {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
attacher, ok := v.(GraphNodeAttachDependencies)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
selfAddr := attacher.ResourceAddr()
|
|
|
|
|
|
|
|
ans, err := g.Ancestors(v)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// dedupe addrs when there's multiple instances involved, or
|
|
|
|
// multiple paths in the un-reduced graph
|
2020-03-15 16:32:06 +01:00
|
|
|
depMap := map[string]addrs.ConfigResource{}
|
2020-01-07 23:49:34 +01:00
|
|
|
for _, d := range ans {
|
2020-03-15 16:32:06 +01:00
|
|
|
var addr addrs.ConfigResource
|
2019-10-17 22:05:27 +02:00
|
|
|
|
|
|
|
switch d := d.(type) {
|
|
|
|
case GraphNodeResourceInstance:
|
|
|
|
instAddr := d.ResourceInstanceAddr()
|
2020-03-15 16:32:06 +01:00
|
|
|
addr = instAddr.ContainingResource().Config()
|
|
|
|
case GraphNodeConfigResource:
|
2019-10-17 22:05:27 +02:00
|
|
|
addr = d.ResourceAddr()
|
|
|
|
default:
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if addr.Equal(selfAddr) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
depMap[addr.String()] = addr
|
|
|
|
}
|
|
|
|
|
2020-03-15 16:32:06 +01:00
|
|
|
deps := make([]addrs.ConfigResource, 0, len(depMap))
|
2019-10-17 22:05:27 +02:00
|
|
|
for _, d := range depMap {
|
|
|
|
deps = append(deps, d)
|
|
|
|
}
|
2019-10-30 20:59:34 +01:00
|
|
|
sort.Slice(deps, func(i, j int) bool {
|
|
|
|
return deps[i].String() < deps[j].String()
|
|
|
|
})
|
2019-10-17 22:05:27 +02:00
|
|
|
|
|
|
|
log.Printf("[TRACE] AttachDependenciesTransformer: %s depends on %s", attacher.ResourceAddr(), deps)
|
|
|
|
attacher.AttachDependencies(deps)
|
|
|
|
}
|
2019-10-30 20:59:34 +01:00
|
|
|
|
2019-10-17 22:05:27 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-04 20:21:50 +02:00
|
|
|
func isDependableResource(v dag.Vertex) bool {
|
|
|
|
switch v.(type) {
|
|
|
|
case GraphNodeResourceInstance:
|
|
|
|
return true
|
|
|
|
case GraphNodeConfigResource:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-09-16 08:20:35 +02:00
|
|
|
// ReferenceMap is a structure that can be used to efficiently check
|
2020-05-01 23:52:45 +02:00
|
|
|
// for references on a graph, mapping internal reference keys (as produced by
|
|
|
|
// the mapKey method) to one or more vertices that are identified by each key.
|
|
|
|
type ReferenceMap map[string][]dag.Vertex
|
2016-09-16 08:20:35 +02:00
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// References returns the set of vertices that the given vertex refers to,
|
|
|
|
// and any referenced addresses that do not have corresponding vertices.
|
2020-05-01 23:52:45 +02:00
|
|
|
func (m ReferenceMap) References(v dag.Vertex) []dag.Vertex {
|
2016-09-16 08:20:35 +02:00
|
|
|
rn, ok := v.(GraphNodeReferencer)
|
|
|
|
if !ok {
|
2020-02-24 23:42:32 +01:00
|
|
|
return nil
|
2016-09-16 08:20:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var matches []dag.Vertex
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
|
|
|
|
for _, ref := range rn.References() {
|
2018-05-25 15:07:07 +02:00
|
|
|
subject := ref.Subject
|
|
|
|
|
2018-05-30 01:27:30 +02:00
|
|
|
key := m.referenceMapKey(v, subject)
|
2020-05-01 23:52:45 +02:00
|
|
|
if _, exists := m[key]; !exists {
|
2018-05-30 01:27:30 +02:00
|
|
|
// If what we were looking for was a ResourceInstance then we
|
|
|
|
// might be in a resource-oriented graph rather than an
|
|
|
|
// instance-oriented graph, and so we'll see if we have the
|
|
|
|
// resource itself instead.
|
2018-05-30 21:01:05 +02:00
|
|
|
switch ri := subject.(type) {
|
|
|
|
case addrs.ResourceInstance:
|
|
|
|
subject = ri.ContainingResource()
|
|
|
|
case addrs.ResourceInstancePhase:
|
|
|
|
subject = ri.ContainingResource()
|
2020-04-09 21:40:28 +02:00
|
|
|
case addrs.AbsModuleCallOutput:
|
|
|
|
subject = ri.ModuleCallOutput()
|
2020-06-16 02:46:53 +02:00
|
|
|
case addrs.ModuleCallInstance:
|
|
|
|
subject = ri.Call
|
2020-04-09 21:40:28 +02:00
|
|
|
default:
|
2020-10-06 23:14:53 +02:00
|
|
|
log.Printf("[INFO] ReferenceTransformer: reference not found: %q", subject)
|
2020-04-09 21:40:28 +02:00
|
|
|
continue
|
2018-05-30 01:27:30 +02:00
|
|
|
}
|
2018-05-30 21:01:05 +02:00
|
|
|
key = m.referenceMapKey(v, subject)
|
2018-05-25 15:07:07 +02:00
|
|
|
}
|
2020-05-01 23:52:45 +02:00
|
|
|
vertices := m[key]
|
|
|
|
for _, rv := range vertices {
|
|
|
|
// don't include self-references
|
|
|
|
if rv == v {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
matches = append(matches, rv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
// dependsOn returns the set of vertices that the given vertex refers to from
|
2020-06-04 20:21:50 +02:00
|
|
|
// the configured depends_on. The bool return value indicates if depends_on was
|
|
|
|
// found in a parent module configuration.
|
|
|
|
func (m ReferenceMap) dependsOn(g *Graph, depender graphNodeDependsOn) ([]dag.Vertex, bool) {
|
|
|
|
var res []dag.Vertex
|
|
|
|
fromModule := false
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
refs := depender.DependsOn()
|
2020-05-01 23:52:45 +02:00
|
|
|
|
2020-10-01 19:49:27 +02:00
|
|
|
// get any implied dependencies for data sources
|
|
|
|
refs = append(refs, m.dataDependsOn(depender)...)
|
2020-09-17 21:32:32 +02:00
|
|
|
|
2020-06-04 20:21:50 +02:00
|
|
|
// This is where we record that a module has depends_on configured.
|
|
|
|
if _, ok := depender.(*nodeExpandModule); ok && len(refs) > 0 {
|
|
|
|
fromModule = true
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
for _, ref := range refs {
|
2020-05-01 23:52:45 +02:00
|
|
|
subject := ref.Subject
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
key := m.referenceMapKey(depender, subject)
|
2020-05-01 23:52:45 +02:00
|
|
|
vertices, ok := m[key]
|
|
|
|
if !ok {
|
2020-06-04 20:21:50 +02:00
|
|
|
// the ReferenceMap generates all possible keys, so any warning
|
|
|
|
// here is probably not useful for this implementation.
|
2020-05-01 23:52:45 +02:00
|
|
|
continue
|
|
|
|
}
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
for _, rv := range vertices {
|
|
|
|
// don't include self-references
|
2020-06-04 03:36:42 +02:00
|
|
|
if rv == depender {
|
2016-11-08 18:35:57 +01:00
|
|
|
continue
|
|
|
|
}
|
2020-06-04 20:21:50 +02:00
|
|
|
res = append(res, rv)
|
|
|
|
|
|
|
|
// and check any ancestors for transitive dependencies
|
|
|
|
ans, _ := g.Ancestors(rv)
|
|
|
|
for _, v := range ans {
|
|
|
|
if isDependableResource(v) {
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
}
|
2016-09-19 06:31:03 +02:00
|
|
|
}
|
2016-11-05 02:58:03 +01:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:21:50 +02:00
|
|
|
parentDeps, fromParentModule := m.parentModuleDependsOn(g, depender)
|
|
|
|
res = append(res, parentDeps...)
|
2020-06-04 03:36:42 +02:00
|
|
|
|
2020-06-04 20:21:50 +02:00
|
|
|
return res, fromModule || fromParentModule
|
2016-11-05 02:58:03 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 19:49:27 +02:00
|
|
|
// Return extra depends_on references if this is a data source.
|
|
|
|
// For data sources we implicitly treat references to managed resources as
|
|
|
|
// depends_on entries. If a data source references a managed resource, even if
|
|
|
|
// that reference is resolvable, it stands to reason that the user intends for
|
|
|
|
// the data source to require that resource in some way.
|
|
|
|
func (m ReferenceMap) dataDependsOn(depender graphNodeDependsOn) []*addrs.Reference {
|
|
|
|
var refs []*addrs.Reference
|
|
|
|
if n, ok := depender.(GraphNodeConfigResource); ok &&
|
|
|
|
n.ResourceAddr().Resource.Mode == addrs.DataResourceMode {
|
|
|
|
for _, r := range depender.References() {
|
|
|
|
|
|
|
|
var resAddr addrs.Resource
|
|
|
|
switch s := r.Subject.(type) {
|
|
|
|
case addrs.Resource:
|
|
|
|
resAddr = s
|
|
|
|
case addrs.ResourceInstance:
|
|
|
|
resAddr = s.Resource
|
|
|
|
r.Subject = resAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
if resAddr.Mode != addrs.ManagedResourceMode {
|
|
|
|
// We only want to wait on directly referenced managed resources.
|
|
|
|
// Data sources have no external side effects, so normal
|
|
|
|
// references to them in the config will suffice for proper
|
|
|
|
// ordering.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
refs = append(refs, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return refs
|
|
|
|
}
|
|
|
|
|
2020-06-04 03:36:42 +02:00
|
|
|
// parentModuleDependsOn returns the set of vertices that a data sources parent
|
2020-06-04 20:21:50 +02:00
|
|
|
// module references through the module call's depends_on. The bool return
|
|
|
|
// value indicates if depends_on was found in a parent module configuration.
|
2020-10-01 19:49:27 +02:00
|
|
|
func (m ReferenceMap) parentModuleDependsOn(g *Graph, depender graphNodeDependsOn) ([]dag.Vertex, bool) {
|
2020-06-04 03:36:42 +02:00
|
|
|
var res []dag.Vertex
|
2020-06-04 20:21:50 +02:00
|
|
|
fromModule := false
|
2020-06-04 03:36:42 +02:00
|
|
|
|
2020-06-04 20:21:50 +02:00
|
|
|
// Look for containing modules with DependsOn.
|
|
|
|
// This should be connected directly to the module node, so we only need to
|
|
|
|
// look one step away.
|
2020-06-04 03:36:42 +02:00
|
|
|
for _, v := range g.DownEdges(depender) {
|
2020-06-04 20:21:50 +02:00
|
|
|
// we're only concerned with module expansion nodes here.
|
2020-06-04 03:36:42 +02:00
|
|
|
mod, ok := v.(*nodeExpandModule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-10-01 19:49:27 +02:00
|
|
|
deps, fromParentModule := m.dependsOn(g, mod)
|
2020-06-04 20:21:50 +02:00
|
|
|
for _, dep := range deps {
|
2020-06-04 03:36:42 +02:00
|
|
|
// add the dependency
|
|
|
|
res = append(res, dep)
|
2020-06-04 20:21:50 +02:00
|
|
|
|
|
|
|
// and check any transitive resource dependencies for more resources
|
2020-06-04 03:36:42 +02:00
|
|
|
ans, _ := g.Ancestors(dep)
|
|
|
|
for _, v := range ans {
|
2020-06-04 20:21:50 +02:00
|
|
|
if isDependableResource(v) {
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
2020-06-04 03:36:42 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 20:21:50 +02:00
|
|
|
fromModule = fromModule || fromParentModule
|
2020-06-04 03:36:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 20:21:50 +02:00
|
|
|
return res, fromModule
|
2020-06-04 03:36:42 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 03:54:47 +01:00
|
|
|
func (m *ReferenceMap) mapKey(path addrs.Module, addr addrs.Referenceable) string {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return fmt.Sprintf("%s|%s", path.String(), addr.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// vertexReferenceablePath returns the path in which the given vertex can be
|
|
|
|
// referenced. This is the path that its results from ReferenceableAddrs
|
|
|
|
// are considered to be relative to.
|
|
|
|
//
|
2020-03-07 04:31:16 +01:00
|
|
|
// Only GraphNodeModulePath implementations can be referenced, so this method will
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// panic if the given vertex does not implement that interface.
|
2020-03-05 03:54:47 +01:00
|
|
|
func vertexReferenceablePath(v dag.Vertex) addrs.Module {
|
2020-03-05 03:00:16 +01:00
|
|
|
sp, ok := v.(GraphNodeModulePath)
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
if !ok {
|
|
|
|
// Only nodes with paths can participate in a reference map.
|
2020-03-05 03:00:16 +01:00
|
|
|
panic(fmt.Errorf("vertexMapKey on vertex type %T which doesn't implement GraphNodeModulePath", sp))
|
2016-09-16 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
if outside, ok := v.(GraphNodeReferenceOutside); ok {
|
|
|
|
// Vertex is referenced from a different module than where it was
|
|
|
|
// declared.
|
|
|
|
path, _ := outside.ReferenceOutside()
|
2020-03-05 03:54:47 +01:00
|
|
|
return path
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertex is referenced from the same module as where it was declared.
|
2020-03-05 03:54:47 +01:00
|
|
|
return sp.ModulePath()
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 03:39:33 +02:00
|
|
|
// vertexReferencePath returns the path in which references _from_ the given
|
|
|
|
// vertex must be interpreted.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
//
|
2020-03-07 04:31:16 +01:00
|
|
|
// Only GraphNodeModulePath implementations can have references, so this method
|
2018-05-26 03:39:33 +02:00
|
|
|
// will panic if the given vertex does not implement that interface.
|
2020-03-05 03:54:47 +01:00
|
|
|
func vertexReferencePath(v dag.Vertex) addrs.Module {
|
2020-03-05 03:00:16 +01:00
|
|
|
sp, ok := v.(GraphNodeModulePath)
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
if !ok {
|
|
|
|
// Only nodes with paths can participate in a reference map.
|
2020-03-05 03:00:16 +01:00
|
|
|
panic(fmt.Errorf("vertexReferencePath on vertex type %T which doesn't implement GraphNodeModulePath", v))
|
2016-09-16 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
2020-03-05 03:00:16 +01:00
|
|
|
if outside, ok := v.(GraphNodeReferenceOutside); ok {
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
// Vertex makes references to objects in a different module than where
|
|
|
|
// it was declared.
|
2020-02-24 23:42:32 +01:00
|
|
|
_, path := outside.ReferenceOutside()
|
2020-03-05 03:54:47 +01:00
|
|
|
return path
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-26 03:39:33 +02:00
|
|
|
// Vertex makes references to objects in the same module as where it
|
|
|
|
// was declared.
|
2020-03-05 03:54:47 +01:00
|
|
|
return sp.ModulePath()
|
2018-05-26 03:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// referenceMapKey produces keys for the "edges" map. "referrer" is the vertex
|
|
|
|
// that the reference is from, and "addr" is the address of the object being
|
|
|
|
// referenced.
|
|
|
|
//
|
|
|
|
// The result is an opaque string that includes both the address of the given
|
|
|
|
// object and the address of the module instance that object belongs to.
|
|
|
|
//
|
2020-03-07 04:31:16 +01:00
|
|
|
// Only GraphNodeModulePath implementations can be referrers, so this method will
|
2018-05-26 03:39:33 +02:00
|
|
|
// panic if the given vertex does not implement that interface.
|
|
|
|
func (m *ReferenceMap) referenceMapKey(referrer dag.Vertex, addr addrs.Referenceable) string {
|
|
|
|
path := vertexReferencePath(referrer)
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
return m.mapKey(path, addr)
|
2016-09-16 22:56:10 +02:00
|
|
|
}
|
|
|
|
|
2016-09-16 08:20:35 +02:00
|
|
|
// NewReferenceMap is used to create a new reference map for the
|
|
|
|
// given set of vertices.
|
2020-05-01 23:52:45 +02:00
|
|
|
func NewReferenceMap(vs []dag.Vertex) ReferenceMap {
|
2016-09-16 08:20:35 +02:00
|
|
|
// Build the lookup table
|
2020-05-01 23:52:45 +02:00
|
|
|
m := make(ReferenceMap)
|
2016-09-16 08:20:35 +02:00
|
|
|
for _, v := range vs {
|
|
|
|
// We're only looking for referenceable nodes
|
|
|
|
rn, ok := v.(GraphNodeReferenceable)
|
2016-09-16 03:30:11 +02:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-03-05 03:00:16 +01:00
|
|
|
path := vertexReferenceablePath(v)
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
|
2016-09-16 08:20:35 +02:00
|
|
|
// Go through and cache them
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
for _, addr := range rn.ReferenceableAddrs() {
|
|
|
|
key := m.mapKey(path, addr)
|
2020-05-01 23:52:45 +02:00
|
|
|
m[key] = append(m[key], v)
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 23:52:45 +02:00
|
|
|
return m
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReferencesFromConfig returns the references that a configuration has
|
|
|
|
// based on the interpolated variables in a configuration.
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
func ReferencesFromConfig(body hcl.Body, schema *configschema.Block) []*addrs.Reference {
|
|
|
|
if body == nil {
|
|
|
|
return nil
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|
terraform: ugly huge change to weave in new HCL2-oriented types
Due to how deeply the configuration types go into Terraform Core, there
isn't a great way to switch out to HCL2 gradually. As a consequence, this
huge commit gets us from the old state to a _compilable_ new state, but
does not yet attempt to fix any tests and has a number of known missing
parts and bugs. We will continue to iterate on this in forthcoming
commits, heading back towards passing tests and making Terraform
fully-functional again.
The three main goals here are:
- Use the configuration models from the "configs" package instead of the
older models in the "config" package, which is now deprecated and
preserved only to help us write our migration tool.
- Do expression inspection and evaluation using the functionality of the
new "lang" package, instead of the Interpolator type and related
functionality in the main "terraform" package.
- Represent addresses of various objects using types in the addrs package,
rather than hand-constructed strings. This is not critical to support
the above, but was a big help during the implementation of these other
points since it made it much more explicit what kind of address is
expected in each context.
Since our new packages are built to accommodate some future planned
features that are not yet implemented (e.g. the "for_each" argument on
resources, "count"/"for_each" on modules), and since there's still a fair
amount of functionality still using old-style APIs, there is a moderate
amount of shimming here to connect new assumptions with old, hopefully in
a way that makes it easier to find and eliminate these shims later.
I apologize in advance to the person who inevitably just found this huge
commit while spelunking through the commit history.
2018-04-30 19:33:53 +02:00
|
|
|
refs, _ := lang.ReferencesInBlock(body, schema)
|
|
|
|
return refs
|
2016-09-16 03:30:11 +02:00
|
|
|
}
|