opentofu/terraform/transform_tainted.go

152 lines
3.6 KiB
Go
Raw Normal View History

2015-01-27 16:28:33 -06:00
package terraform
import (
"fmt"
)
// TraintedTransformer is a GraphTransformer that adds tainted resources
// to the graph.
type TaintedTransformer struct {
// State is the global state. We'll automatically find the correct
// ModuleState based on the Graph.Path that is being transformed.
State *State
}
func (t *TaintedTransformer) Transform(g *Graph) error {
state := t.State.ModuleByPath(g.Path)
if state == nil {
// If there is no state for our module there can't be any tainted
// resources, since they live in the state.
return nil
}
// Go through all the resources in our state to look for tainted resources
for k, rs := range state.Resources {
// If we have no tainted resources, then move on
if len(rs.Tainted) == 0 {
continue
}
for i, _ := range rs.Tainted {
// Add the graph node and make the connection from any untainted
// resources with this name to the tainted resource, so that
// the tainted resource gets destroyed first.
g.ConnectFrom(k, g.Add(&graphNodeTaintedResource{
2015-01-27 16:28:33 -06:00
Index: i,
ResourceName: k,
2015-02-11 16:14:05 -06:00
ResourceType: rs.Type,
}))
2015-01-27 16:28:33 -06:00
}
}
// TODO: Any other dependencies?
2015-01-27 16:28:33 -06:00
return nil
}
// graphNodeTaintedResource is the graph vertex representing a tainted resource.
type graphNodeTaintedResource struct {
Index int
ResourceName string
2015-02-11 16:14:05 -06:00
ResourceType string
2015-01-27 16:28:33 -06:00
}
func (n *graphNodeTaintedResource) DependentOn() []string {
return []string{n.ResourceName}
2015-01-27 16:28:33 -06:00
}
func (n *graphNodeTaintedResource) Name() string {
return fmt.Sprintf("%s (tainted #%d)", n.ResourceName, n.Index+1)
}
func (n *graphNodeTaintedResource) ProvidedBy() []string {
return []string{resourceProvider(n.ResourceName)}
}
2015-02-11 16:14:05 -06:00
// GraphNodeEvalable impl.
func (n *graphNodeTaintedResource) EvalTree() EvalNode {
2015-02-12 16:46:22 -06:00
var state *InstanceState
2015-02-11 16:14:05 -06:00
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},
2015-02-12 16:46:22 -06:00
Node: &EvalSequence{
Nodes: []EvalNode{
&EvalReadState{
Name: n.ResourceName,
Tainted: true,
TaintedIndex: n.Index,
Output: &state,
},
&EvalRefresh{
Info: info,
Provider: &EvalGetProvider{Name: n.ProvidedBy()[0]},
State: &state,
Output: &state,
},
&EvalWriteState{
2015-02-11 16:14:05 -06:00
Name: n.ResourceName,
2015-02-12 16:46:22 -06:00
ResourceType: n.ResourceType,
Dependencies: n.DependentOn(),
State: &state,
2015-02-11 16:14:05 -06:00
Tainted: true,
TaintedIndex: n.Index,
},
},
},
})
2015-02-13 10:50:59 -06:00
// Apply
var provider ResourceProvider
var diff *InstanceDiff
seq.Nodes = append(seq.Nodes, &EvalOpFilter{
Ops: []walkOperation{walkApply},
Node: &EvalSequence{
Nodes: []EvalNode{
&EvalGetProvider{
Name: n.ProvidedBy()[0],
Output: &provider,
},
&EvalReadState{
Name: n.ResourceName,
Tainted: true,
TaintedIndex: n.Index,
Output: &state,
},
&EvalDiffDestroy{
Info: info,
State: &EvalReadState{
Name: n.ResourceName,
Tainted: true,
TaintedIndex: n.Index,
},
Output: &diff,
},
&EvalApply{
Info: info,
State: &state,
Diff: &diff,
Provider: &provider,
Output: &state,
},
&EvalWriteState{
Name: n.ResourceName,
ResourceType: n.ResourceType,
Dependencies: n.DependentOn(),
State: &state,
Tainted: true,
TaintedIndex: n.Index,
},
},
},
})
2015-02-11 16:14:05 -06:00
return seq
}