opentofu/terraform/graph_builder.go
Martin Atkins a3403f2766 terraform: Ugly huge change to weave in new State and Plan types
Due to how often the state and plan types are referenced throughout
Terraform, there isn't a great way to switch them out gradually. As a
consequence, this huge commit gets us from the old world to a _compilable_
new world, but still has a large number of known test failures due to
key functionality being stubbed out.

The stubs here are for anything that interacts with providers, since we
now need to do the follow-up work to similarly replace the old
terraform.ResourceProvider interface with its replacement in the new
"providers" package. That work, along with work to fix the remaining
failing tests, will follow in subsequent commits.

The aim here was to replace all references to terraform.State and its
downstream types with states.State, terraform.Plan with plans.Plan,
state.State with statemgr.State, and switch to the new implementations of
the state and plan file formats. However, due to the number of times those
types are used, this also ended up affecting numerous other parts of core
such as terraform.Hook, the backend.Backend interface, and most of the CLI
commands.

Just as with 5861dbf3fc49b19587a31816eb06f511ab861bb4 before, I apologize
in advance to the person who inevitably just found this huge commit while
spelunking through the commit history.
2018-10-16 19:11:09 -07:00

86 lines
2.1 KiB
Go

package terraform
import (
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform/tfdiags"
"github.com/hashicorp/terraform/addrs"
)
// GraphBuilder is an interface that can be implemented and used with
// Terraform to build the graph that Terraform walks.
type GraphBuilder interface {
// Build builds the graph for the given module path. It is up to
// the interface implementation whether this build should expand
// the graph or not.
Build(addrs.ModuleInstance) (*Graph, tfdiags.Diagnostics)
}
// BasicGraphBuilder is a GraphBuilder that builds a graph out of a
// series of transforms and (optionally) validates the graph is a valid
// structure.
type BasicGraphBuilder struct {
Steps []GraphTransformer
Validate bool
// Optional name to add to the graph debug log
Name string
}
func (b *BasicGraphBuilder) Build(path addrs.ModuleInstance) (*Graph, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
g := &Graph{Path: path}
var lastStepStr string
for _, step := range b.Steps {
if step == nil {
continue
}
log.Printf("[TRACE] Executing graph transform %T", step)
stepName := fmt.Sprintf("%T", step)
dot := strings.LastIndex(stepName, ".")
if dot >= 0 {
stepName = stepName[dot+1:]
}
debugOp := g.DebugOperation(stepName, "")
err := step.Transform(g)
errMsg := ""
if err != nil {
errMsg = err.Error()
}
debugOp.End(errMsg)
if thisStepStr := g.StringWithNodeTypes(); thisStepStr != lastStepStr {
log.Printf("[TRACE] Completed graph transform %T with new graph:\n%s------", step, thisStepStr)
lastStepStr = thisStepStr
} else {
log.Printf("[TRACE] Completed graph transform %T (no changes)", step)
}
if err != nil {
if nf, isNF := err.(tfdiags.NonFatalError); isNF {
diags = diags.Append(nf.Diagnostics)
} else {
diags = diags.Append(err)
return g, diags
}
}
}
// Validate the graph structure
if b.Validate {
if err := g.Validate(); err != nil {
log.Printf("[ERROR] Graph validation failed. Graph:\n\n%s", g.String())
diags = diags.Append(err)
return nil, diags
}
}
return g, diags
}