2015-02-08 23:00:13 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/config"
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceCountTransformer is a GraphTransformer that expands the count
|
|
|
|
// out for a specific resource.
|
|
|
|
type ResourceCountTransformer struct {
|
|
|
|
Resource *config.Resource
|
2015-02-12 20:40:48 +01:00
|
|
|
Destroy bool
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *ResourceCountTransformer) Transform(g *Graph) error {
|
|
|
|
// Expand the resource count
|
|
|
|
count, err := t.Resource.Count()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't allow the count to be negative
|
|
|
|
if count < 0 {
|
|
|
|
return fmt.Errorf("negative count: %d", count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// For each count, build and add the node
|
|
|
|
nodes := make([]dag.Vertex, count)
|
|
|
|
for i := 0; i < count; i++ {
|
2015-02-12 02:18:16 +01:00
|
|
|
// Set the index. If our count is 1 we special case it so that
|
|
|
|
// we handle the "resource.0" and "resource" boundary properly.
|
|
|
|
index := i
|
|
|
|
if count == 1 {
|
|
|
|
index = -1
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:40:48 +01:00
|
|
|
// Save the node for later so we can do connections. Make the
|
|
|
|
// proper node depending on if we're just a destroy node or if
|
|
|
|
// were a regular node.
|
|
|
|
var node dag.Vertex = &graphNodeExpandedResource{
|
2015-02-12 02:18:16 +01:00
|
|
|
Index: index,
|
2015-02-08 23:00:13 +01:00
|
|
|
Resource: t.Resource,
|
|
|
|
}
|
2015-02-12 20:40:48 +01:00
|
|
|
if t.Destroy {
|
|
|
|
node = &graphNodeExpandedResourceDestroy{
|
|
|
|
graphNodeExpandedResource: node.(*graphNodeExpandedResource),
|
|
|
|
}
|
|
|
|
}
|
2015-02-08 23:00:13 +01:00
|
|
|
|
|
|
|
// Add the node now
|
2015-02-12 20:40:48 +01:00
|
|
|
nodes[i] = node
|
2015-02-08 23:00:13 +01:00
|
|
|
g.Add(nodes[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make the dependency connections
|
|
|
|
for _, n := range nodes {
|
|
|
|
// Connect the dependents. We ignore the return value for missing
|
|
|
|
// dependents since that should've been caught at a higher level.
|
|
|
|
g.ConnectDependent(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type graphNodeExpandedResource struct {
|
|
|
|
Index int
|
|
|
|
Resource *config.Resource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeExpandedResource) Name() string {
|
2015-02-12 02:20:05 +01:00
|
|
|
if n.Index == -1 {
|
|
|
|
return n.Resource.Id()
|
|
|
|
}
|
|
|
|
|
2015-02-08 23:00:13 +01:00
|
|
|
return fmt.Sprintf("%s #%d", n.Resource.Id(), n.Index)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDependable impl.
|
|
|
|
func (n *graphNodeExpandedResource) DependableName() []string {
|
|
|
|
return []string{
|
|
|
|
n.Resource.Id(),
|
2015-02-10 23:12:49 +01:00
|
|
|
n.stateId(),
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeDependent impl.
|
|
|
|
func (n *graphNodeExpandedResource) DependentOn() []string {
|
|
|
|
config := &GraphNodeConfigResource{Resource: n.Resource}
|
|
|
|
return config.DependentOn()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeProviderConsumer
|
2015-02-11 23:14:05 +01:00
|
|
|
func (n *graphNodeExpandedResource) ProvidedBy() []string {
|
|
|
|
return []string{resourceProvider(n.Resource.Type)}
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable impl.
|
|
|
|
func (n *graphNodeExpandedResource) EvalTree() EvalNode {
|
2015-02-13 04:57:27 +01:00
|
|
|
var diff *InstanceDiff
|
2015-02-12 23:46:22 +01:00
|
|
|
var state *InstanceState
|
|
|
|
|
2015-02-12 07:57:50 +01:00
|
|
|
// Build the resource. If we aren't part of a multi-resource, then
|
|
|
|
// we still consider ourselves as count index zero.
|
|
|
|
index := n.Index
|
|
|
|
if index < 0 {
|
|
|
|
index = 0
|
|
|
|
}
|
|
|
|
resource := &Resource{CountIndex: index}
|
2015-02-12 07:56:35 +01:00
|
|
|
|
|
|
|
// Shared node for interpolation of configuration
|
|
|
|
interpolateNode := &EvalInterpolate{
|
|
|
|
Config: n.Resource.RawConfig,
|
|
|
|
Resource: resource,
|
|
|
|
}
|
|
|
|
|
2015-02-09 20:15:54 +01:00
|
|
|
seq := &EvalSequence{Nodes: make([]EvalNode, 0, 5)}
|
|
|
|
|
|
|
|
// Validate the resource
|
2015-02-12 03:13:15 +01:00
|
|
|
vseq := &EvalSequence{Nodes: make([]EvalNode, 0, 5)}
|
|
|
|
vseq.Nodes = append(vseq.Nodes, &EvalValidateResource{
|
2015-02-11 23:14:05 +01:00
|
|
|
Provider: &EvalGetProvider{Name: n.ProvidedBy()[0]},
|
2015-02-12 07:56:35 +01:00
|
|
|
Config: interpolateNode,
|
2015-02-09 20:15:54 +01:00
|
|
|
ResourceName: n.Resource.Name,
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
})
|
|
|
|
|
|
|
|
// Validate all the provisioners
|
|
|
|
for _, p := range n.Resource.Provisioners {
|
2015-02-12 03:13:15 +01:00
|
|
|
vseq.Nodes = append(vseq.Nodes, &EvalValidateProvisioner{
|
2015-02-09 20:15:54 +01:00
|
|
|
Provisioner: &EvalGetProvisioner{Name: p.Type},
|
2015-02-12 07:56:35 +01:00
|
|
|
Config: &EvalInterpolate{
|
|
|
|
Config: p.RawConfig, Resource: resource},
|
2015-02-09 20:15:54 +01:00
|
|
|
})
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
2015-02-09 20:15:54 +01:00
|
|
|
|
2015-02-12 03:13:15 +01:00
|
|
|
// Add the validation operations
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkValidate},
|
|
|
|
Node: vseq,
|
|
|
|
})
|
|
|
|
|
2015-02-11 22:43:07 +01:00
|
|
|
// Build instance info
|
2015-02-13 21:05:34 +01:00
|
|
|
info := n.instanceInfo()
|
2015-02-11 22:43:07 +01:00
|
|
|
seq.Nodes = append(seq.Nodes, &EvalInstanceInfo{Info: info})
|
|
|
|
|
2015-02-11 17:48:45 +01:00
|
|
|
// Refresh the resource
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkRefresh},
|
2015-02-12 23:46:22 +01:00
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
|
|
|
&EvalReadState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Output: &state,
|
|
|
|
},
|
|
|
|
&EvalRefresh{
|
|
|
|
Info: info,
|
|
|
|
Provider: &EvalGetProvider{Name: n.ProvidedBy()[0]},
|
|
|
|
State: &state,
|
|
|
|
Output: &state,
|
|
|
|
},
|
|
|
|
&EvalWriteState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
Dependencies: n.DependentOn(),
|
|
|
|
State: &state,
|
|
|
|
},
|
2015-02-11 17:48:45 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-12 00:22:03 +01:00
|
|
|
// Diff the resource
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkPlan},
|
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
2015-02-12 23:46:22 +01:00
|
|
|
&EvalDiff{
|
|
|
|
Info: info,
|
|
|
|
Config: interpolateNode,
|
|
|
|
Provider: &EvalGetProvider{Name: n.ProvidedBy()[0]},
|
|
|
|
State: &EvalReadState{Name: n.stateId()},
|
|
|
|
Output: &diff,
|
|
|
|
OutputState: &state,
|
|
|
|
},
|
2015-02-12 00:22:03 +01:00
|
|
|
&EvalWriteState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
Dependencies: n.DependentOn(),
|
2015-02-12 23:46:22 +01:00
|
|
|
State: &state,
|
2015-02-12 00:22:03 +01:00
|
|
|
},
|
2015-02-12 22:10:21 +01:00
|
|
|
&EvalDiffTainted{
|
|
|
|
Diff: &diff,
|
|
|
|
Name: n.stateId(),
|
|
|
|
},
|
2015-02-12 00:22:03 +01:00
|
|
|
&EvalWriteDiff{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Diff: &diff,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-12 21:42:33 +01:00
|
|
|
// Diff the resource for destruction
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkPlanDestroy},
|
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
|
|
|
&EvalDiffDestroy{
|
|
|
|
Info: info,
|
|
|
|
State: &EvalReadState{Name: n.stateId()},
|
|
|
|
Output: &diff,
|
|
|
|
},
|
|
|
|
&EvalWriteDiff{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Diff: &diff,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-12 23:46:22 +01:00
|
|
|
// Diff the resource for destruction
|
|
|
|
var provider ResourceProvider
|
2015-02-13 04:57:27 +01:00
|
|
|
var diffApply *InstanceDiff
|
2015-02-13 18:49:29 +01:00
|
|
|
var err error
|
2015-02-13 19:09:35 +01:00
|
|
|
var createNew, tainted bool
|
2015-02-12 23:46:22 +01:00
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkApply},
|
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
2015-02-13 21:05:34 +01:00
|
|
|
// Get the saved diff for apply
|
|
|
|
&EvalReadDiff{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Diff: &diffApply,
|
|
|
|
},
|
|
|
|
|
|
|
|
// We don't want to do any destroys
|
|
|
|
&EvalIf{
|
|
|
|
If: func(ctx EvalContext) (bool, error) {
|
|
|
|
if diffApply == nil {
|
2015-02-13 21:13:39 +01:00
|
|
|
return true, EvalEarlyExitError{}
|
2015-02-13 21:05:34 +01:00
|
|
|
}
|
|
|
|
|
2015-02-13 23:03:17 +01:00
|
|
|
if diffApply.Destroy && len(diffApply.Attributes) == 0 {
|
2015-02-13 21:05:34 +01:00
|
|
|
return true, EvalEarlyExitError{}
|
|
|
|
}
|
|
|
|
|
2015-02-13 23:03:17 +01:00
|
|
|
diffApply.Destroy = false
|
2015-02-13 21:05:34 +01:00
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
Node: EvalNoop{},
|
|
|
|
},
|
|
|
|
|
2015-02-14 00:57:37 +01:00
|
|
|
&EvalIf{
|
|
|
|
If: func(ctx EvalContext) (bool, error) {
|
|
|
|
return n.Resource.Lifecycle.CreateBeforeDestroy, nil
|
|
|
|
},
|
|
|
|
Node: &EvalDeposeState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
2015-02-13 04:48:57 +01:00
|
|
|
&EvalDiff{
|
|
|
|
Info: info,
|
|
|
|
Config: interpolateNode,
|
|
|
|
Provider: &EvalGetProvider{Name: n.ProvidedBy()[0]},
|
|
|
|
State: &EvalReadState{Name: n.stateId()},
|
2015-02-13 04:57:27 +01:00
|
|
|
Output: &diffApply,
|
2015-02-12 23:46:22 +01:00
|
|
|
},
|
2015-02-13 04:48:57 +01:00
|
|
|
|
|
|
|
// Get the saved diff
|
2015-02-12 23:46:22 +01:00
|
|
|
&EvalReadDiff{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Diff: &diff,
|
|
|
|
},
|
2015-02-13 04:48:57 +01:00
|
|
|
|
|
|
|
// Compare the diffs
|
|
|
|
&EvalCompareDiff{
|
|
|
|
Info: info,
|
|
|
|
One: &diff,
|
2015-02-13 04:57:27 +01:00
|
|
|
Two: &diffApply,
|
2015-02-13 04:48:57 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
&EvalGetProvider{
|
|
|
|
Name: n.ProvidedBy()[0],
|
|
|
|
Output: &provider,
|
|
|
|
},
|
2015-02-12 23:46:22 +01:00
|
|
|
&EvalReadState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Output: &state,
|
|
|
|
},
|
|
|
|
&EvalApply{
|
2015-02-13 19:09:35 +01:00
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Diff: &diffApply,
|
|
|
|
Provider: &provider,
|
|
|
|
Output: &state,
|
|
|
|
Error: &err,
|
|
|
|
CreateNew: &createNew,
|
2015-02-12 23:46:22 +01:00
|
|
|
},
|
|
|
|
&EvalWriteState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
Dependencies: n.DependentOn(),
|
|
|
|
State: &state,
|
|
|
|
},
|
2015-02-13 18:49:29 +01:00
|
|
|
&EvalApplyProvisioners{
|
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Resource: n.Resource,
|
|
|
|
InterpResource: resource,
|
2015-02-13 19:09:35 +01:00
|
|
|
CreateNew: &createNew,
|
2015-02-13 18:49:29 +01:00
|
|
|
Tainted: &tainted,
|
|
|
|
Error: &err,
|
|
|
|
},
|
2015-02-14 00:57:37 +01:00
|
|
|
&EvalIf{
|
|
|
|
If: func(ctx EvalContext) (bool, error) {
|
2015-02-14 01:27:23 +01:00
|
|
|
if n.Resource.Lifecycle.CreateBeforeDestroy {
|
|
|
|
tainted = err != nil
|
|
|
|
}
|
|
|
|
|
2015-02-14 01:05:44 +01:00
|
|
|
failure := tainted || err != nil
|
|
|
|
return n.Resource.Lifecycle.CreateBeforeDestroy && failure, nil
|
2015-02-14 00:57:37 +01:00
|
|
|
},
|
|
|
|
Node: &EvalUndeposeState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
},
|
|
|
|
},
|
2015-02-13 18:49:29 +01:00
|
|
|
&EvalWriteState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
Dependencies: n.DependentOn(),
|
|
|
|
State: &state,
|
|
|
|
Tainted: &tainted,
|
|
|
|
TaintedIndex: -1,
|
2015-02-14 00:57:37 +01:00
|
|
|
TaintedClearPrimary: !n.Resource.Lifecycle.CreateBeforeDestroy,
|
2015-02-13 18:49:29 +01:00
|
|
|
},
|
2015-02-13 18:52:11 +01:00
|
|
|
&EvalApplyPost{
|
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Error: &err,
|
|
|
|
},
|
2015-02-12 23:46:22 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2015-02-09 20:15:54 +01:00
|
|
|
return seq
|
2015-02-08 23:00:13 +01:00
|
|
|
}
|
2015-02-10 23:12:49 +01:00
|
|
|
|
2015-02-13 21:05:34 +01:00
|
|
|
// instanceInfo is used for EvalTree.
|
|
|
|
func (n *graphNodeExpandedResource) instanceInfo() *InstanceInfo {
|
|
|
|
return &InstanceInfo{Id: n.stateId(), Type: n.Resource.Type}
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:12:49 +01:00
|
|
|
// stateId is the name used for the state key
|
|
|
|
func (n *graphNodeExpandedResource) stateId() string {
|
2015-02-12 02:18:16 +01:00
|
|
|
if n.Index == -1 {
|
2015-02-11 17:48:45 +01:00
|
|
|
return n.Resource.Id()
|
|
|
|
}
|
|
|
|
|
2015-02-10 23:12:49 +01:00
|
|
|
return fmt.Sprintf("%s.%d", n.Resource.Id(), n.Index)
|
|
|
|
}
|
2015-02-12 20:40:48 +01:00
|
|
|
|
2015-02-12 20:57:15 +01:00
|
|
|
// GraphNodeStateRepresentative impl.
|
|
|
|
func (n *graphNodeExpandedResource) StateId() []string {
|
|
|
|
return []string{n.stateId()}
|
|
|
|
}
|
|
|
|
|
2015-02-12 20:40:48 +01:00
|
|
|
// graphNodeExpandedResourceDestroy represents an expanded resource that
|
|
|
|
// is to be destroyed.
|
|
|
|
type graphNodeExpandedResourceDestroy struct {
|
|
|
|
*graphNodeExpandedResource
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeExpandedResourceDestroy) Name() string {
|
|
|
|
return fmt.Sprintf("%s (destroy)", n.graphNodeExpandedResource.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable impl.
|
|
|
|
func (n *graphNodeExpandedResourceDestroy) EvalTree() EvalNode {
|
2015-02-13 21:05:34 +01:00
|
|
|
info := n.instanceInfo()
|
|
|
|
|
|
|
|
var diffApply *InstanceDiff
|
|
|
|
var provider ResourceProvider
|
|
|
|
var state *InstanceState
|
|
|
|
var err error
|
|
|
|
return &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkApply},
|
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
|
|
|
// Get the saved diff for apply
|
|
|
|
&EvalReadDiff{
|
|
|
|
Name: n.stateId(),
|
|
|
|
Diff: &diffApply,
|
|
|
|
},
|
|
|
|
|
|
|
|
// If we're not destroying, then compare diffs
|
|
|
|
&EvalIf{
|
|
|
|
If: func(ctx EvalContext) (bool, error) {
|
|
|
|
if diffApply != nil && diffApply.Destroy {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, EvalEarlyExitError{}
|
|
|
|
},
|
|
|
|
Node: EvalNoop{},
|
|
|
|
},
|
|
|
|
|
|
|
|
&EvalGetProvider{
|
|
|
|
Name: n.ProvidedBy()[0],
|
|
|
|
Output: &provider,
|
|
|
|
},
|
|
|
|
&EvalReadState{
|
2015-02-14 00:57:37 +01:00
|
|
|
Name: n.stateId(),
|
|
|
|
Output: &state,
|
|
|
|
Tainted: n.Resource.Lifecycle.CreateBeforeDestroy,
|
|
|
|
TaintedIndex: -1,
|
2015-02-13 21:05:34 +01:00
|
|
|
},
|
|
|
|
&EvalApply{
|
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Diff: &diffApply,
|
|
|
|
Provider: &provider,
|
|
|
|
Output: &state,
|
|
|
|
Error: &err,
|
|
|
|
},
|
|
|
|
&EvalWriteState{
|
|
|
|
Name: n.stateId(),
|
|
|
|
ResourceType: n.Resource.Type,
|
|
|
|
Dependencies: n.DependentOn(),
|
|
|
|
State: &state,
|
|
|
|
},
|
|
|
|
&EvalApplyPost{
|
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Error: &err,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2015-02-12 20:40:48 +01:00
|
|
|
}
|