opentofu/terraform/graph_builder.go

105 lines
2.9 KiB
Go
Raw Normal View History

2015-02-02 05:04:02 -06:00
package terraform
import (
"github.com/hashicorp/terraform/config/module"
)
// 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(path []string) (*Graph, error)
}
2015-02-07 18:19:08 -06:00
// BasicGraphBuilder is a GraphBuilder that builds a graph out of a
// series of transforms and validates the graph is a valid structure.
type BasicGraphBuilder struct {
Steps []GraphTransformer
}
func (b *BasicGraphBuilder) Build(path []string) (*Graph, error) {
g := &Graph{Path: path}
for _, step := range b.Steps {
if err := step.Transform(g); err != nil {
return g, err
}
}
// Validate the graph structure
if err := g.Validate(); err != nil {
return nil, err
}
return g, nil
}
2015-02-02 05:04:02 -06:00
// BuiltinGraphBuilder is responsible for building the complete graph that
// Terraform uses for execution. It is an opinionated builder that defines
// the step order required to build a complete graph as is used and expected
// by Terraform.
//
// If you require a custom graph, you'll have to build it up manually
// on your own by building a new GraphBuilder implementation.
type BuiltinGraphBuilder struct {
// Root is the root module of the graph to build.
Root *module.Tree
// State is the global state. The proper module states will be looked
// up by graph path.
State *State
// Providers is the list of providers supported.
Providers []string
2015-02-09 13:15:54 -06:00
// Provisioners is the list of provisioners supported.
Provisioners []string
2015-02-02 05:04:02 -06:00
}
// Build builds the graph according to the steps returned by Steps.
func (b *BuiltinGraphBuilder) Build(path []string) (*Graph, error) {
2015-02-07 18:19:08 -06:00
basic := &BasicGraphBuilder{
Steps: b.Steps(),
2015-02-02 05:04:02 -06:00
}
2015-02-07 18:19:08 -06:00
return basic.Build(path)
2015-02-02 05:04:02 -06:00
}
// Steps returns the ordered list of GraphTransformers that must be executed
// to build a complete graph.
func (b *BuiltinGraphBuilder) Steps() []GraphTransformer {
return []GraphTransformer{
2015-02-09 12:14:09 -06:00
// Create all our resources from the configuration and state
2015-02-02 05:04:02 -06:00
&ConfigTransformer{Module: b.Root},
&OrphanTransformer{State: b.State, Module: b.Root},
&TaintedTransformer{State: b.State},
2015-02-09 12:14:09 -06:00
// Provider-related transformations
2015-02-02 05:04:02 -06:00
&MissingProviderTransformer{Providers: b.Providers},
&ProviderTransformer{},
&PruneProviderTransformer{},
2015-02-09 12:14:09 -06:00
// Provisioner-related transformations
2015-02-09 13:15:54 -06:00
&MissingProvisionerTransformer{Provisioners: b.Provisioners},
&ProvisionerTransformer{},
&PruneProvisionerTransformer{},
2015-02-09 12:14:09 -06:00
// Run our vertex-level transforms
&VertexTransformer{
Transforms: []GraphVertexTransformer{
2015-02-09 12:14:09 -06:00
// Expand any statically expanded nodes, such as module graphs
&ExpandTransform{
Builder: b,
},
},
},
2015-02-09 12:14:09 -06:00
// Create the destruction nodes
&DestroyTransformer{},
2015-02-09 12:14:09 -06:00
// Make sure we create one root
2015-02-04 19:02:18 -06:00
&RootTransformer{},
2015-02-02 05:04:02 -06:00
}
}