2016-11-05 18:26:12 -05:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2017-01-25 14:24:48 -06:00
|
|
|
"sync"
|
|
|
|
|
2016-11-05 18:26:12 -05:00
|
|
|
"github.com/hashicorp/terraform/config/module"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PlanGraphBuilder implements GraphBuilder and is responsible for building
|
|
|
|
// a graph for planning (creating a Terraform Diff).
|
|
|
|
//
|
|
|
|
// The primary difference between this graph and others:
|
|
|
|
//
|
|
|
|
// * Based on the config since it represents the target state
|
|
|
|
//
|
|
|
|
// * Ignores lifecycle options since no lifecycle events occur here. This
|
|
|
|
// simplifies the graph significantly since complex transforms such as
|
|
|
|
// create-before-destroy can be completely ignored.
|
|
|
|
//
|
|
|
|
type PlanGraphBuilder struct {
|
|
|
|
// Module is the root module for the graph to build.
|
|
|
|
Module *module.Tree
|
|
|
|
|
|
|
|
// State is the current state
|
|
|
|
State *State
|
|
|
|
|
|
|
|
// Providers is the list of providers supported.
|
|
|
|
Providers []string
|
|
|
|
|
2017-01-25 14:24:48 -06:00
|
|
|
// Provisioners is the list of provisioners supported.
|
|
|
|
Provisioners []string
|
|
|
|
|
2016-11-07 16:09:11 -06:00
|
|
|
// Targets are resources to target
|
|
|
|
Targets []string
|
|
|
|
|
2016-11-05 18:26:12 -05:00
|
|
|
// DisableReduce, if true, will not reduce the graph. Great for testing.
|
|
|
|
DisableReduce bool
|
2016-12-02 21:38:49 -06:00
|
|
|
|
|
|
|
// Validate will do structural validation of the graph.
|
|
|
|
Validate bool
|
2017-01-23 23:48:22 -06:00
|
|
|
|
2017-01-25 14:24:48 -06:00
|
|
|
// CustomConcrete can be set to customize the node types created
|
|
|
|
// for various parts of the plan. This is useful in order to customize
|
|
|
|
// the plan behavior.
|
|
|
|
CustomConcrete bool
|
|
|
|
ConcreteProvider ConcreteProviderNodeFunc
|
|
|
|
ConcreteResource ConcreteResourceNodeFunc
|
|
|
|
ConcreteResourceOrphan ConcreteResourceNodeFunc
|
|
|
|
|
|
|
|
once sync.Once
|
2016-11-05 18:26:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// See GraphBuilder
|
|
|
|
func (b *PlanGraphBuilder) 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: "PlanGraphBuilder",
|
2016-11-05 18:26:12 -05:00
|
|
|
}).Build(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
// See GraphBuilder
|
|
|
|
func (b *PlanGraphBuilder) Steps() []GraphTransformer {
|
2017-01-25 14:24:48 -06:00
|
|
|
b.once.Do(b.init)
|
2016-11-07 10:57:27 -06:00
|
|
|
|
2016-11-05 18:26:12 -05:00
|
|
|
steps := []GraphTransformer{
|
|
|
|
// Creates all the resources represented in the config
|
|
|
|
&ConfigTransformer{
|
2017-01-25 14:24:48 -06:00
|
|
|
Concrete: b.ConcreteResource,
|
2016-11-05 18:26:12 -05:00
|
|
|
Module: b.Module,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Add the outputs
|
|
|
|
&OutputTransformer{Module: b.Module},
|
|
|
|
|
2016-11-07 10:57:27 -06:00
|
|
|
// Add orphan resources
|
|
|
|
&OrphanResourceTransformer{
|
2017-01-25 14:24:48 -06:00
|
|
|
Concrete: b.ConcreteResourceOrphan,
|
2016-11-07 10:57:27 -06:00
|
|
|
State: b.State,
|
|
|
|
Module: b.Module,
|
|
|
|
},
|
|
|
|
|
2016-11-05 18:26:12 -05:00
|
|
|
// Attach the configuration to any resources
|
|
|
|
&AttachResourceConfigTransformer{Module: b.Module},
|
|
|
|
|
|
|
|
// Attach the state
|
|
|
|
&AttachStateTransformer{State: b.State},
|
|
|
|
|
2016-12-12 22:54:46 -06:00
|
|
|
// Add root variables
|
|
|
|
&RootVariableTransformer{Module: b.Module},
|
|
|
|
|
2016-12-13 23:22:21 -06:00
|
|
|
// Create all the providers
|
2017-01-25 14:24:48 -06:00
|
|
|
&MissingProviderTransformer{Providers: b.Providers, Concrete: b.ConcreteProvider},
|
2016-12-13 23:22:21 -06:00
|
|
|
&ProviderTransformer{},
|
|
|
|
&DisableProviderTransformer{},
|
|
|
|
&ParentProviderTransformer{},
|
|
|
|
&AttachProviderConfigTransformer{Module: b.Module},
|
|
|
|
|
2017-01-25 14:24:48 -06:00
|
|
|
// Provisioner-related transformations. Only add these if requested.
|
|
|
|
GraphTransformIf(
|
|
|
|
func() bool { return b.Provisioners != nil },
|
|
|
|
GraphTransformMulti(
|
|
|
|
&MissingProvisionerTransformer{Provisioners: b.Provisioners},
|
|
|
|
&ProvisionerTransformer{},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2016-12-12 22:54:46 -06:00
|
|
|
// Add module variables
|
|
|
|
&ModuleVariableTransformer{Module: b.Module},
|
|
|
|
|
2016-11-07 19:54:06 -06:00
|
|
|
// Connect so that the references are ready for targeting. We'll
|
|
|
|
// have to connect again later for providers and so on.
|
|
|
|
&ReferenceTransformer{},
|
|
|
|
|
|
|
|
// Target
|
|
|
|
&TargetsTransformer{Targets: b.Targets},
|
|
|
|
|
2016-11-05 18:26:12 -05:00
|
|
|
// Single root
|
|
|
|
&RootTransformer{},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !b.DisableReduce {
|
|
|
|
// Perform the transitive reduction to make our graph a bit
|
|
|
|
// more sane if possible (it usually is possible).
|
|
|
|
steps = append(steps, &TransitiveReductionTransformer{})
|
|
|
|
}
|
|
|
|
|
|
|
|
return steps
|
|
|
|
}
|
2017-01-25 14:24:48 -06:00
|
|
|
|
|
|
|
func (b *PlanGraphBuilder) init() {
|
|
|
|
// Do nothing if the user requests customizing the fields
|
|
|
|
if b.CustomConcrete {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ConcreteProvider = func(a *NodeAbstractProvider) dag.Vertex {
|
|
|
|
return &NodeApplyableProvider{
|
|
|
|
NodeAbstractProvider: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ConcreteResource = func(a *NodeAbstractResource) dag.Vertex {
|
|
|
|
return &NodePlannableResource{
|
|
|
|
NodeAbstractResource: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.ConcreteResourceOrphan = func(a *NodeAbstractResource) dag.Vertex {
|
|
|
|
return &NodePlannableResourceOrphan{
|
|
|
|
NodeAbstractResource: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|