mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-04 13:17:43 -06:00
c937c06a03
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.
173 lines
4.9 KiB
Go
173 lines
4.9 KiB
Go
package terraform
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"runtime/debug"
|
|
|
|
"github.com/hashicorp/terraform/tfdiags"
|
|
|
|
"github.com/hashicorp/terraform/addrs"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
)
|
|
|
|
// Graph represents the graph that Terraform uses to represent resources
|
|
// and their dependencies.
|
|
type Graph struct {
|
|
// Graph is the actual DAG. This is embedded so you can call the DAG
|
|
// methods directly.
|
|
dag.AcyclicGraph
|
|
|
|
// Path is the path in the module tree that this Graph represents.
|
|
Path addrs.ModuleInstance
|
|
|
|
// debugName is a name for reference in the debug output. This is usually
|
|
// to indicate what topmost builder was, and if this graph is a shadow or
|
|
// not.
|
|
debugName string
|
|
}
|
|
|
|
func (g *Graph) DirectedGraph() dag.Grapher {
|
|
return &g.AcyclicGraph
|
|
}
|
|
|
|
// Walk walks the graph with the given walker for callbacks. The graph
|
|
// will be walked with full parallelism, so the walker should expect
|
|
// to be called in concurrently.
|
|
func (g *Graph) Walk(walker GraphWalker) tfdiags.Diagnostics {
|
|
return g.walk(walker)
|
|
}
|
|
|
|
func (g *Graph) walk(walker GraphWalker) tfdiags.Diagnostics {
|
|
// The callbacks for enter/exiting a graph
|
|
ctx := walker.EnterPath(g.Path)
|
|
defer walker.ExitPath(g.Path)
|
|
|
|
// Get the path for logs
|
|
path := ctx.Path().String()
|
|
|
|
// Determine if our walker is a panic wrapper
|
|
panicwrap, ok := walker.(GraphWalkerPanicwrapper)
|
|
if !ok {
|
|
panicwrap = nil // just to be sure
|
|
}
|
|
|
|
debugName := "walk-graph.json"
|
|
if g.debugName != "" {
|
|
debugName = g.debugName + "-" + debugName
|
|
}
|
|
|
|
debugBuf := dbug.NewFileWriter(debugName)
|
|
g.SetDebugWriter(debugBuf)
|
|
defer debugBuf.Close()
|
|
|
|
// Walk the graph.
|
|
var walkFn dag.WalkFunc
|
|
walkFn = func(v dag.Vertex) (diags tfdiags.Diagnostics) {
|
|
log.Printf("[TRACE] vertex %q: starting visit (%T)", dag.VertexName(v), v)
|
|
g.DebugVisitInfo(v, g.debugName)
|
|
|
|
// If we have a panic wrap GraphWalker and a panic occurs, recover
|
|
// and call that. We ensure the return value is an error, however,
|
|
// so that future nodes are not called.
|
|
defer func() {
|
|
log.Printf("[TRACE] vertex %q: visit complete", dag.VertexName(v))
|
|
|
|
// If no panicwrap, do nothing
|
|
if panicwrap == nil {
|
|
return
|
|
}
|
|
|
|
// If no panic, do nothing
|
|
err := recover()
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
// Modify the return value to show the error
|
|
diags = diags.Append(fmt.Errorf("vertex %q captured panic: %s\n\n%s", dag.VertexName(v), err, debug.Stack()))
|
|
|
|
// Call the panic wrapper
|
|
panicwrap.Panic(v, err)
|
|
}()
|
|
|
|
walker.EnterVertex(v)
|
|
defer walker.ExitVertex(v, diags)
|
|
|
|
// vertexCtx is the context that we use when evaluating. This
|
|
// is normally the context of our graph but can be overridden
|
|
// with a GraphNodeSubPath impl.
|
|
vertexCtx := ctx
|
|
if pn, ok := v.(GraphNodeSubPath); ok && len(pn.Path()) > 0 {
|
|
vertexCtx = walker.EnterPath(pn.Path())
|
|
defer walker.ExitPath(pn.Path())
|
|
}
|
|
|
|
// If the node is eval-able, then evaluate it.
|
|
if ev, ok := v.(GraphNodeEvalable); ok {
|
|
tree := ev.EvalTree()
|
|
if tree == nil {
|
|
panic(fmt.Sprintf("%q (%T): nil eval tree", dag.VertexName(v), v))
|
|
}
|
|
|
|
// Allow the walker to change our tree if needed. Eval,
|
|
// then callback with the output.
|
|
log.Printf("[TRACE] vertex %q: evaluating", dag.VertexName(v))
|
|
|
|
g.DebugVertexInfo(v, fmt.Sprintf("evaluating %T(%s)", v, path))
|
|
|
|
tree = walker.EnterEvalTree(v, tree)
|
|
output, err := Eval(tree, vertexCtx)
|
|
diags = diags.Append(walker.ExitEvalTree(v, output, err))
|
|
if diags.HasErrors() {
|
|
return
|
|
}
|
|
}
|
|
|
|
// If the node is dynamically expanded, then expand it
|
|
if ev, ok := v.(GraphNodeDynamicExpandable); ok {
|
|
log.Printf("[TRACE] vertex %q: expanding dynamic subgraph", dag.VertexName(v))
|
|
|
|
g.DebugVertexInfo(v, fmt.Sprintf("expanding %T(%s)", v, path))
|
|
|
|
g, err := ev.DynamicExpand(vertexCtx)
|
|
if err != nil {
|
|
diags = diags.Append(err)
|
|
return
|
|
}
|
|
if g != nil {
|
|
// Walk the subgraph
|
|
log.Printf("[TRACE] vertex %q: entering dynamic subgraph", dag.VertexName(v))
|
|
subDiags := g.walk(walker)
|
|
diags = diags.Append(subDiags)
|
|
if subDiags.HasErrors() {
|
|
log.Printf("[TRACE] vertex %q: dynamic subgraph encountered errors", dag.VertexName(v))
|
|
return
|
|
}
|
|
log.Printf("[TRACE] vertex %q: dynamic subgraph completed successfully", dag.VertexName(v))
|
|
} else {
|
|
log.Printf("[TRACE] vertex %q: produced no dynamic subgraph", dag.VertexName(v))
|
|
}
|
|
}
|
|
|
|
// If the node has a subgraph, then walk the subgraph
|
|
if sn, ok := v.(GraphNodeSubgraph); ok {
|
|
log.Printf("[TRACE] vertex %q: entering static subgraph", dag.VertexName(v))
|
|
|
|
g.DebugVertexInfo(v, fmt.Sprintf("subgraph: %T(%s)", v, path))
|
|
|
|
subDiags := sn.Subgraph().(*Graph).walk(walker)
|
|
if subDiags.HasErrors() {
|
|
log.Printf("[TRACE] vertex %q: static subgraph encountered errors", dag.VertexName(v))
|
|
return
|
|
}
|
|
log.Printf("[TRACE] vertex %q: static subgraph completed successfully", dag.VertexName(v))
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
return g.AcyclicGraph.Walk(walkFn)
|
|
}
|