2016-09-13 12:56:37 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/config/module"
|
2016-09-21 16:30:41 -05:00
|
|
|
"github.com/hashicorp/terraform/dag"
|
2016-09-13 12:56:37 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// ApplyGraphBuilder implements GraphBuilder and is responsible for building
|
|
|
|
// a graph for applying a Terraform diff.
|
|
|
|
//
|
|
|
|
// Because the graph is built from the diff (vs. the config or state),
|
|
|
|
// this helps ensure that the apply-time graph doesn't modify any resources
|
|
|
|
// that aren't explicitly in the diff. There are other scenarios where the
|
|
|
|
// diff can be deviated, so this is just one layer of protection.
|
|
|
|
type ApplyGraphBuilder struct {
|
2016-09-14 13:44:41 -05:00
|
|
|
// Module is the root module for the graph to build.
|
|
|
|
Module *module.Tree
|
2016-09-13 12:56:37 -05:00
|
|
|
|
|
|
|
// Diff is the diff to apply.
|
|
|
|
Diff *Diff
|
|
|
|
|
2016-09-13 19:52:09 -05:00
|
|
|
// State is the current state
|
|
|
|
State *State
|
|
|
|
|
2016-09-13 12:56:37 -05:00
|
|
|
// Providers is the list of providers supported.
|
|
|
|
Providers []string
|
|
|
|
|
|
|
|
// Provisioners is the list of provisioners supported.
|
|
|
|
Provisioners []string
|
2016-09-22 13:03:03 -05:00
|
|
|
|
|
|
|
// DisableReduce, if true, will not reduce the graph. Great for testing.
|
|
|
|
DisableReduce bool
|
2016-10-21 01:22:33 -05:00
|
|
|
|
|
|
|
// Destroy, if true, represents a pure destroy operation
|
|
|
|
Destroy bool
|
2016-12-02 21:38:49 -06:00
|
|
|
|
|
|
|
// Validate will do structural validation of the graph.
|
|
|
|
Validate bool
|
2016-09-13 12:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// See GraphBuilder
|
|
|
|
func (b *ApplyGraphBuilder) Build(path []string) (*Graph, error) {
|
|
|
|
return (&BasicGraphBuilder{
|
|
|
|
Steps: b.Steps(),
|
2016-12-02 21:38:49 -06:00
|
|
|
Validate: b.Validate,
|
2016-11-15 15:36:10 -06:00
|
|
|
Name: "ApplyGraphBuilder",
|
2016-09-13 12:56:37 -05:00
|
|
|
}).Build(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See GraphBuilder
|
|
|
|
func (b *ApplyGraphBuilder) Steps() []GraphTransformer {
|
2016-09-15 01:58:16 -05:00
|
|
|
// Custom factory for creating providers.
|
2016-12-03 17:27:38 -06:00
|
|
|
concreteProvider := func(a *NodeAbstractProvider) dag.Vertex {
|
2016-09-15 01:58:16 -05:00
|
|
|
return &NodeApplyableProvider{
|
2016-12-03 17:27:38 -06:00
|
|
|
NodeAbstractProvider: a,
|
2016-09-15 01:58:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 16:30:41 -05:00
|
|
|
concreteResource := func(a *NodeAbstractResource) dag.Vertex {
|
|
|
|
return &NodeApplyableResource{
|
|
|
|
NodeAbstractResource: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 12:56:37 -05:00
|
|
|
steps := []GraphTransformer{
|
|
|
|
// Creates all the nodes represented in the diff.
|
2016-09-13 19:52:09 -05:00
|
|
|
&DiffTransformer{
|
2016-09-21 16:30:41 -05:00
|
|
|
Concrete: concreteResource,
|
|
|
|
|
2016-09-13 19:52:09 -05:00
|
|
|
Diff: b.Diff,
|
2016-09-14 13:44:41 -05:00
|
|
|
Module: b.Module,
|
2016-09-13 19:52:09 -05:00
|
|
|
State: b.State,
|
|
|
|
},
|
2016-09-13 16:34:15 -05:00
|
|
|
|
2016-09-18 23:45:19 -05:00
|
|
|
// Create orphan output nodes
|
|
|
|
&OrphanOutputTransformer{Module: b.Module, State: b.State},
|
|
|
|
|
2016-09-20 12:23:48 -05:00
|
|
|
// Attach the configuration to any resources
|
|
|
|
&AttachResourceConfigTransformer{Module: b.Module},
|
|
|
|
|
2016-09-16 22:26:10 -05:00
|
|
|
// Attach the state
|
|
|
|
&AttachStateTransformer{State: b.State},
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// Create all the providers
|
2016-12-03 17:27:38 -06:00
|
|
|
&MissingProviderTransformer{Providers: b.Providers, Concrete: concreteProvider},
|
2016-09-13 19:11:34 -05:00
|
|
|
&ProviderTransformer{},
|
2016-10-19 16:54:00 -05:00
|
|
|
&DisableProviderTransformer{},
|
2016-09-14 16:43:14 -05:00
|
|
|
&ParentProviderTransformer{},
|
2016-09-16 18:30:29 -05:00
|
|
|
&AttachProviderConfigTransformer{Module: b.Module},
|
2016-09-13 19:11:34 -05:00
|
|
|
|
2016-12-10 19:11:24 -06:00
|
|
|
// Destruction ordering
|
|
|
|
&DestroyEdgeTransformer{Module: b.Module, State: b.State},
|
|
|
|
GraphTransformIf(
|
|
|
|
func() bool { return !b.Destroy },
|
|
|
|
&CBDEdgeTransformer{Module: b.Module, State: b.State},
|
|
|
|
),
|
|
|
|
|
2016-09-15 11:32:37 -05:00
|
|
|
// Provisioner-related transformations
|
2016-10-21 01:22:33 -05:00
|
|
|
GraphTransformIf(
|
|
|
|
func() bool { return !b.Destroy },
|
|
|
|
GraphTransformMulti(
|
|
|
|
&MissingProvisionerTransformer{Provisioners: b.Provisioners},
|
|
|
|
&ProvisionerTransformer{},
|
|
|
|
),
|
|
|
|
),
|
2016-09-15 11:32:37 -05:00
|
|
|
|
2016-09-16 16:36:35 -05:00
|
|
|
// Add root variables
|
|
|
|
&RootVariableTransformer{Module: b.Module},
|
|
|
|
|
2016-09-16 01:20:35 -05:00
|
|
|
// Add the outputs
|
|
|
|
&OutputTransformer{Module: b.Module},
|
|
|
|
|
2016-11-04 20:58:03 -05:00
|
|
|
// Add module variables
|
|
|
|
&ModuleVariableTransformer{Module: b.Module},
|
|
|
|
|
2016-09-15 20:30:11 -05:00
|
|
|
// Connect references so ordering is correct
|
|
|
|
&ReferenceTransformer{},
|
|
|
|
|
2016-09-19 11:28:24 -05:00
|
|
|
// Add the node to fix the state count boundaries
|
|
|
|
&CountBoundaryTransformer{},
|
|
|
|
|
2016-09-13 16:34:15 -05:00
|
|
|
// Single root
|
|
|
|
&RootTransformer{},
|
2016-09-22 13:03:03 -05:00
|
|
|
}
|
2016-09-19 11:28:24 -05:00
|
|
|
|
2016-09-22 13:03:03 -05:00
|
|
|
if !b.DisableReduce {
|
2016-09-19 11:28:24 -05:00
|
|
|
// Perform the transitive reduction to make our graph a bit
|
|
|
|
// more sane if possible (it usually is possible).
|
2016-09-22 13:03:03 -05:00
|
|
|
steps = append(steps, &TransitiveReductionTransformer{})
|
2016-09-13 12:56:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|