2015-02-27 18:38:17 +01:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2015-03-05 01:01:46 +01:00
|
|
|
// DeposedTransformer is a GraphTransformer that adds deposed resources
|
2015-02-27 18:38:17 +01:00
|
|
|
// to the graph.
|
|
|
|
type DeposedTransformer struct {
|
|
|
|
// State is the global state. We'll automatically find the correct
|
|
|
|
// ModuleState based on the Graph.Path that is being transformed.
|
|
|
|
State *State
|
|
|
|
|
|
|
|
// View, if non-empty, is the ModuleState.View used around the state
|
|
|
|
// to find deposed resources.
|
|
|
|
View string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *DeposedTransformer) Transform(g *Graph) error {
|
|
|
|
state := t.State.ModuleByPath(g.Path)
|
|
|
|
if state == nil {
|
2015-03-02 22:34:05 +01:00
|
|
|
// If there is no state for our module there can't be any deposed
|
2015-02-27 18:38:17 +01:00
|
|
|
// resources, since they live in the state.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a view, apply it now
|
|
|
|
if t.View != "" {
|
|
|
|
state = state.View(t.View)
|
|
|
|
}
|
|
|
|
|
2015-03-02 22:34:05 +01:00
|
|
|
// Go through all the resources in our state to look for deposed resources
|
2015-02-27 18:38:17 +01:00
|
|
|
for k, rs := range state.Resources {
|
2015-03-04 19:15:53 +01:00
|
|
|
// If we have no deposed resources, then move on
|
|
|
|
if len(rs.Deposed) == 0 {
|
2015-02-27 18:38:17 +01:00
|
|
|
continue
|
|
|
|
}
|
2015-03-04 19:15:53 +01:00
|
|
|
deposed := rs.Deposed
|
|
|
|
|
|
|
|
for i, _ := range deposed {
|
|
|
|
g.Add(&graphNodeDeposedResource{
|
|
|
|
Index: i,
|
|
|
|
ResourceName: k,
|
|
|
|
ResourceType: rs.Type,
|
2015-03-23 23:36:53 +01:00
|
|
|
Provider: rs.Provider,
|
2015-03-04 19:15:53 +01:00
|
|
|
})
|
|
|
|
}
|
2015-02-27 18:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// graphNodeDeposedResource is the graph vertex representing a deposed resource.
|
|
|
|
type graphNodeDeposedResource struct {
|
2015-03-04 19:15:53 +01:00
|
|
|
Index int
|
2015-02-27 18:38:17 +01:00
|
|
|
ResourceName string
|
|
|
|
ResourceType string
|
2015-03-23 23:36:53 +01:00
|
|
|
Provider string
|
2015-02-27 18:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeDeposedResource) Name() string {
|
2015-03-04 19:15:53 +01:00
|
|
|
return fmt.Sprintf("%s (deposed #%d)", n.ResourceName, n.Index)
|
2015-02-27 18:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *graphNodeDeposedResource) ProvidedBy() []string {
|
2015-03-23 23:36:53 +01:00
|
|
|
return []string{resourceProvider(n.ResourceName, n.Provider)}
|
2015-02-27 18:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GraphNodeEvalable impl.
|
|
|
|
func (n *graphNodeDeposedResource) EvalTree() EvalNode {
|
|
|
|
var provider ResourceProvider
|
|
|
|
var state *InstanceState
|
|
|
|
|
|
|
|
seq := &EvalSequence{Nodes: make([]EvalNode, 0, 5)}
|
|
|
|
|
|
|
|
// Build instance info
|
|
|
|
info := &InstanceInfo{Id: n.ResourceName, Type: n.ResourceType}
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalInstanceInfo{Info: info})
|
|
|
|
|
|
|
|
// Refresh the resource
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
|
|
|
Ops: []walkOperation{walkRefresh},
|
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
|
|
|
&EvalGetProvider{
|
|
|
|
Name: n.ProvidedBy()[0],
|
|
|
|
Output: &provider,
|
|
|
|
},
|
|
|
|
&EvalReadStateDeposed{
|
|
|
|
Name: n.ResourceName,
|
|
|
|
Output: &state,
|
2015-03-04 19:15:53 +01:00
|
|
|
Index: n.Index,
|
2015-02-27 18:38:17 +01:00
|
|
|
},
|
|
|
|
&EvalRefresh{
|
|
|
|
Info: info,
|
|
|
|
Provider: &provider,
|
|
|
|
State: &state,
|
|
|
|
Output: &state,
|
|
|
|
},
|
2015-03-02 22:34:05 +01:00
|
|
|
&EvalWriteStateDeposed{
|
2015-02-27 18:38:17 +01:00
|
|
|
Name: n.ResourceName,
|
|
|
|
ResourceType: n.ResourceType,
|
2015-03-23 23:36:53 +01:00
|
|
|
Provider: n.Provider,
|
2015-02-27 18:38:17 +01:00
|
|
|
State: &state,
|
2015-03-04 19:15:53 +01:00
|
|
|
Index: n.Index,
|
2015-02-27 18:38:17 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
// Apply
|
|
|
|
var diff *InstanceDiff
|
|
|
|
var err error
|
|
|
|
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
|
2015-10-02 20:18:33 +02:00
|
|
|
Ops: []walkOperation{walkApply, walkDestroy},
|
2015-02-27 18:38:17 +01:00
|
|
|
Node: &EvalSequence{
|
|
|
|
Nodes: []EvalNode{
|
|
|
|
&EvalGetProvider{
|
|
|
|
Name: n.ProvidedBy()[0],
|
|
|
|
Output: &provider,
|
|
|
|
},
|
|
|
|
&EvalReadStateDeposed{
|
|
|
|
Name: n.ResourceName,
|
|
|
|
Output: &state,
|
2015-03-04 19:15:53 +01:00
|
|
|
Index: n.Index,
|
2015-02-27 18:38:17 +01:00
|
|
|
},
|
|
|
|
&EvalDiffDestroy{
|
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Output: &diff,
|
|
|
|
},
|
|
|
|
&EvalApply{
|
|
|
|
Info: info,
|
|
|
|
State: &state,
|
|
|
|
Diff: &diff,
|
|
|
|
Provider: &provider,
|
|
|
|
Output: &state,
|
|
|
|
Error: &err,
|
|
|
|
},
|
2015-03-04 19:15:53 +01:00
|
|
|
// Always write the resource back to the state deposed... if it
|
|
|
|
// was successfully destroyed it will be pruned. If it was not, it will
|
|
|
|
// be caught on the next run.
|
2015-03-02 22:34:05 +01:00
|
|
|
&EvalWriteStateDeposed{
|
2015-02-27 18:38:17 +01:00
|
|
|
Name: n.ResourceName,
|
|
|
|
ResourceType: n.ResourceType,
|
2015-03-23 23:36:53 +01:00
|
|
|
Provider: n.Provider,
|
2015-03-04 19:15:53 +01:00
|
|
|
State: &state,
|
|
|
|
Index: n.Index,
|
2015-02-27 18:38:17 +01:00
|
|
|
},
|
|
|
|
&EvalReturnError{
|
|
|
|
Error: &err,
|
|
|
|
},
|
|
|
|
&EvalUpdateStateHook{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
return seq
|
|
|
|
}
|