2016-11-06 01:00:05 -06:00
|
|
|
package terraform
|
|
|
|
|
2016-11-06 12:15:09 -06:00
|
|
|
import (
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
2016-11-06 01:00:05 -06:00
|
|
|
// NodePlannableResource represents a resource that is "plannable":
|
|
|
|
// it is ready to be planned in order to create a diff.
|
|
|
|
type NodePlannableResource struct {
|
|
|
|
*NodeAbstractResource
|
2016-11-07 19:45:08 -06:00
|
|
|
|
|
|
|
// Set by GraphNodeTargetable and used during DynamicExpand to
|
|
|
|
// forward targets downwards.
|
|
|
|
targets []ResourceAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeTargetable
|
|
|
|
func (n *NodePlannableResource) SetTargets(targets []ResourceAddress) {
|
|
|
|
n.targets = targets
|
2016-11-06 01:00:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable
|
|
|
|
func (n *NodePlannableResource) EvalTree() EvalNode {
|
|
|
|
return &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
2016-11-06 12:07:17 -06:00
|
|
|
// The EvalTree for a plannable resource primarily involves
|
|
|
|
// interpolating the count since it can contain variables
|
|
|
|
// we only just received access to.
|
|
|
|
//
|
|
|
|
// With the interpolated count, we can then DynamicExpand
|
|
|
|
// into the proper number of instances.
|
|
|
|
&EvalInterpolate{Config: n.Config.RawCount},
|
2016-11-07 12:37:30 -06:00
|
|
|
|
2016-11-11 13:02:49 -06:00
|
|
|
&EvalCountCheckComputed{Resource: n.Config},
|
2016-11-07 12:37:30 -06:00
|
|
|
&EvalCountFixZeroOneBoundary{Resource: n.Config},
|
2016-11-06 01:00:05 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:07:17 -06:00
|
|
|
// GraphNodeDynamicExpandable
|
|
|
|
func (n *NodePlannableResource) DynamicExpand(ctx EvalContext) (*Graph, error) {
|
2016-11-07 15:23:06 -06:00
|
|
|
// Grab the state which we read
|
|
|
|
state, lock := ctx.State()
|
|
|
|
lock.RLock()
|
|
|
|
defer lock.RUnlock()
|
|
|
|
|
2016-11-06 12:15:09 -06:00
|
|
|
// Expand the resource count which must be available by now from EvalTree
|
|
|
|
count, err := n.Config.Count()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// The concrete resource factory we'll use
|
|
|
|
concreteResource := func(a *NodeAbstractResource) dag.Vertex {
|
|
|
|
// Add the config and state since we don't do that via transforms
|
|
|
|
a.Config = n.Config
|
|
|
|
|
|
|
|
return &NodePlannableResourceInstance{
|
|
|
|
NodeAbstractResource: a,
|
|
|
|
}
|
|
|
|
}
|
2016-11-06 12:07:17 -06:00
|
|
|
|
2016-11-07 15:23:06 -06:00
|
|
|
// The concrete resource factory we'll use for oprhans
|
|
|
|
concreteResourceOrphan := func(a *NodeAbstractResource) dag.Vertex {
|
2016-11-07 16:05:21 -06:00
|
|
|
// Add the config and state since we don't do that via transforms
|
|
|
|
a.Config = n.Config
|
|
|
|
|
2016-11-07 15:23:06 -06:00
|
|
|
return &NodePlannableResourceOrphan{
|
|
|
|
NodeAbstractResource: a,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:07:17 -06:00
|
|
|
// Start creating the steps
|
2016-11-07 15:23:06 -06:00
|
|
|
steps := []GraphTransformer{
|
|
|
|
// Expand the count.
|
|
|
|
&ResourceCountTransformer{
|
|
|
|
Concrete: concreteResource,
|
|
|
|
Count: count,
|
|
|
|
Addr: n.ResourceAddr(),
|
|
|
|
},
|
|
|
|
|
|
|
|
// Add the count orphans
|
|
|
|
&OrphanResourceCountTransformer{
|
|
|
|
Concrete: concreteResourceOrphan,
|
|
|
|
Count: count,
|
|
|
|
Addr: n.ResourceAddr(),
|
|
|
|
State: state,
|
|
|
|
},
|
2016-11-06 12:07:17 -06:00
|
|
|
|
2016-11-07 15:23:06 -06:00
|
|
|
// Attach the state
|
|
|
|
&AttachStateTransformer{State: state},
|
|
|
|
|
2016-11-07 19:45:08 -06:00
|
|
|
// Targeting
|
|
|
|
&TargetsTransformer{ParsedTargets: n.targets},
|
|
|
|
|
2016-11-07 15:55:35 -06:00
|
|
|
// Connect references so ordering is correct
|
|
|
|
&ReferenceTransformer{},
|
|
|
|
|
2016-11-07 15:23:06 -06:00
|
|
|
// Make sure there is a single root
|
|
|
|
&RootTransformer{},
|
|
|
|
}
|
2016-11-06 12:07:17 -06:00
|
|
|
|
|
|
|
// Build the graph
|
|
|
|
b := &BasicGraphBuilder{Steps: steps, Validate: true}
|
|
|
|
return b.Build(ctx.Path())
|
2016-11-06 01:00:05 -06:00
|
|
|
}
|