2015-02-02 05:04:02 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2016-11-04 10:30:51 -05:00
|
|
|
"fmt"
|
2015-02-13 12:04:11 -06:00
|
|
|
"log"
|
2016-11-04 10:30:51 -05:00
|
|
|
"strings"
|
2015-02-13 12:04:11 -06:00
|
|
|
|
2015-02-02 05:04:02 -06:00
|
|
|
"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
|
2015-04-23 10:52:31 -05:00
|
|
|
// series of transforms and (optionally) validates the graph is a valid
|
|
|
|
// structure.
|
2015-02-07 18:19:08 -06:00
|
|
|
type BasicGraphBuilder struct {
|
2015-04-23 10:52:31 -05:00
|
|
|
Steps []GraphTransformer
|
|
|
|
Validate bool
|
2016-11-04 10:30:51 -05:00
|
|
|
// Optional name to add to the graph debug log
|
|
|
|
Name string
|
2015-02-07 18:19:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BasicGraphBuilder) Build(path []string) (*Graph, error) {
|
|
|
|
g := &Graph{Path: path}
|
|
|
|
for _, step := range b.Steps {
|
2016-10-21 01:22:33 -05:00
|
|
|
if step == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-11-04 10:30:51 -05:00
|
|
|
stepName := fmt.Sprintf("%T", step)
|
|
|
|
dot := strings.LastIndex(stepName, ".")
|
|
|
|
if dot >= 0 {
|
|
|
|
stepName = stepName[dot+1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
err := step.Transform(g)
|
|
|
|
|
|
|
|
// always log the graph state to see what transformations may have happened
|
|
|
|
debugName := "build-" + stepName
|
|
|
|
if b.Name != "" {
|
|
|
|
debugName = b.Name + "-" + debugName
|
2015-02-07 18:19:08 -06:00
|
|
|
}
|
2015-02-13 14:05:34 -06:00
|
|
|
|
2016-11-04 11:03:16 -05:00
|
|
|
log.Printf(
|
|
|
|
"[TRACE] Graph after step %T:\n\n%s",
|
|
|
|
step, g.StringWithNodeTypes())
|
|
|
|
|
2016-11-04 10:30:51 -05:00
|
|
|
dg, _ := NewDebugGraph(debugName, g, nil)
|
|
|
|
dbug.WriteGraph(dg)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return g, err
|
|
|
|
}
|
2015-02-07 18:19:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the graph structure
|
2015-04-23 10:52:31 -05:00
|
|
|
if b.Validate {
|
|
|
|
if err := g.Validate(); err != nil {
|
|
|
|
log.Printf("[ERROR] Graph validation failed. Graph:\n\n%s", g.String())
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-07 18:19:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-02-13 14:05:34 -06:00
|
|
|
// Diff is the diff. The proper module diffs will be looked up.
|
|
|
|
Diff *Diff
|
|
|
|
|
2015-02-02 05:04:02 -06:00
|
|
|
// 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-03-24 11:18:15 -05:00
|
|
|
|
|
|
|
// Targets is the user-specified list of resources to target.
|
|
|
|
Targets []string
|
|
|
|
|
|
|
|
// Destroy is set to true when we're in a `terraform destroy` or a
|
|
|
|
// `terraform plan -destroy`
|
|
|
|
Destroy bool
|
2015-04-23 10:52:31 -05:00
|
|
|
|
|
|
|
// Determines whether the GraphBuilder should perform graph validation before
|
|
|
|
// returning the Graph. Generally you want this to be done, except when you'd
|
|
|
|
// like to inspect a problematic graph.
|
|
|
|
Validate bool
|
|
|
|
|
|
|
|
// Verbose is set to true when the graph should be built "worst case",
|
|
|
|
// skipping any prune steps. This is used for early cycle detection during
|
|
|
|
// Validate and for manual inspection via `terraform graph -verbose`.
|
|
|
|
Verbose bool
|
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{
|
2015-05-02 20:21:00 -05:00
|
|
|
Steps: b.Steps(path),
|
2015-04-23 10:52:31 -05:00
|
|
|
Validate: b.Validate,
|
2016-11-04 10:30:51 -05:00
|
|
|
Name: "builtin",
|
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.
|
2015-05-02 20:21:00 -05:00
|
|
|
func (b *BuiltinGraphBuilder) Steps(path []string) []GraphTransformer {
|
2015-04-23 10:52:31 -05:00
|
|
|
steps := []GraphTransformer{
|
2015-02-09 12:14:09 -06:00
|
|
|
// Create all our resources from the configuration and state
|
2016-11-05 18:26:12 -05:00
|
|
|
&ConfigTransformerOld{Module: b.Root},
|
2015-03-30 19:02:36 -05:00
|
|
|
&OrphanTransformer{
|
2015-11-13 13:17:14 -06:00
|
|
|
State: b.State,
|
|
|
|
Module: b.Root,
|
2015-03-30 19:02:36 -05:00
|
|
|
},
|
2015-02-09 12:14:09 -06:00
|
|
|
|
2015-04-29 13:27:12 -05:00
|
|
|
// Output-related transformations
|
|
|
|
&AddOutputOrphanTransformer{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{},
|
2016-10-19 16:54:00 -05:00
|
|
|
&DisableProviderTransformerOld{},
|
2015-02-09 12:14:09 -06:00
|
|
|
|
|
|
|
// Provisioner-related transformations
|
2015-02-09 13:15:54 -06:00
|
|
|
&MissingProvisionerTransformer{Provisioners: b.Provisioners},
|
|
|
|
&ProvisionerTransformer{},
|
2015-02-09 12:14:09 -06:00
|
|
|
|
|
|
|
// Run our vertex-level transforms
|
2015-02-07 18:29:04 -06:00
|
|
|
&VertexTransformer{
|
|
|
|
Transforms: []GraphVertexTransformer{
|
2015-02-09 12:14:09 -06:00
|
|
|
// Expand any statically expanded nodes, such as module graphs
|
2015-02-07 18:29:04 -06:00
|
|
|
&ExpandTransform{
|
|
|
|
Builder: b,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2015-02-09 12:14:09 -06:00
|
|
|
|
2015-05-01 17:18:40 -05:00
|
|
|
// Flatten stuff
|
|
|
|
&FlattenTransformer{},
|
|
|
|
|
|
|
|
// Make sure all the connections that are proxies are connected through
|
|
|
|
&ProxyTransformer{},
|
2015-05-02 20:21:00 -05:00
|
|
|
}
|
2015-02-27 21:23:20 -06:00
|
|
|
|
2015-05-02 20:21:00 -05:00
|
|
|
// If we're on the root path, then we do a bunch of other stuff.
|
|
|
|
// We don't do the following for modules.
|
|
|
|
if len(path) <= 1 {
|
|
|
|
steps = append(steps,
|
2015-06-29 13:19:37 -05:00
|
|
|
// Optionally reduces the graph to a user-specified list of targets and
|
|
|
|
// their dependencies.
|
|
|
|
&TargetsTransformer{Targets: b.Targets, Destroy: b.Destroy},
|
|
|
|
|
2016-11-08 15:21:50 -06:00
|
|
|
// Create orphan output nodes
|
|
|
|
&OrphanOutputTransformer{Module: b.Root, State: b.State},
|
|
|
|
|
2016-02-04 06:46:45 -06:00
|
|
|
// Prune the providers. This must happen only once because flattened
|
|
|
|
// modules might depend on empty providers.
|
2015-06-24 19:48:31 -05:00
|
|
|
&PruneProviderTransformer{},
|
|
|
|
|
2015-05-02 20:21:00 -05:00
|
|
|
// Create the destruction nodes
|
2015-05-06 22:55:14 -05:00
|
|
|
&DestroyTransformer{FullDestroy: b.Destroy},
|
2016-02-10 16:30:38 -06:00
|
|
|
b.conditional(&conditionalOpts{
|
|
|
|
If: func() bool { return !b.Destroy },
|
|
|
|
Then: &CreateBeforeDestroyTransformer{},
|
|
|
|
}),
|
2015-05-02 20:21:00 -05:00
|
|
|
b.conditional(&conditionalOpts{
|
|
|
|
If: func() bool { return !b.Verbose },
|
|
|
|
Then: &PruneDestroyTransformer{Diff: b.Diff, State: b.State},
|
|
|
|
}),
|
|
|
|
|
2015-07-17 18:52:15 -05:00
|
|
|
// Remove the noop nodes
|
|
|
|
&PruneNoopTransformer{Diff: b.Diff, State: b.State},
|
|
|
|
|
2015-06-29 12:40:20 -05:00
|
|
|
// Insert nodes to close opened plugin connections
|
|
|
|
&CloseProviderTransformer{},
|
|
|
|
&CloseProvisionerTransformer{},
|
|
|
|
|
2015-05-02 20:21:00 -05:00
|
|
|
// Perform the transitive reduction to make our graph a bit
|
|
|
|
// more sane if possible (it usually is possible).
|
|
|
|
&TransitiveReductionTransformer{},
|
|
|
|
)
|
2015-02-02 05:04:02 -06:00
|
|
|
}
|
2015-04-23 10:52:31 -05:00
|
|
|
|
2016-01-28 09:14:15 -06:00
|
|
|
// Make sure we have a single root
|
|
|
|
steps = append(steps, &RootTransformer{})
|
|
|
|
|
2015-04-23 10:52:31 -05:00
|
|
|
// Remove nils
|
|
|
|
for i, s := range steps {
|
|
|
|
if s == nil {
|
|
|
|
steps = append(steps[:i], steps[i+1:]...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|
|
|
|
|
|
|
|
type conditionalOpts struct {
|
|
|
|
If func() bool
|
|
|
|
Then GraphTransformer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BuiltinGraphBuilder) conditional(o *conditionalOpts) GraphTransformer {
|
|
|
|
if o.If != nil && o.Then != nil && o.If() {
|
|
|
|
return o.Then
|
|
|
|
}
|
|
|
|
return nil
|
2015-02-02 05:04:02 -06:00
|
|
|
}
|