forked from hashicorp/terraform
/
node_module_removed.go
77 lines (64 loc) · 1.71 KB
/
node_module_removed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package terraform
import (
"fmt"
"log"
"reflect"
)
// NodeModuleRemoved represents a module that is no longer in the
// config.
type NodeModuleRemoved struct {
PathValue []string
}
func (n *NodeModuleRemoved) Name() string {
return fmt.Sprintf("%s (removed)", modulePrefixStr(n.PathValue))
}
// GraphNodeSubPath
func (n *NodeModuleRemoved) Path() []string {
return n.PathValue
}
// GraphNodeEvalable
func (n *NodeModuleRemoved) EvalTree() EvalNode {
return &EvalOpFilter{
Ops: []walkOperation{walkRefresh, walkApply, walkDestroy},
Node: &EvalDeleteModule{
PathValue: n.PathValue,
},
}
}
func (n *NodeModuleRemoved) ReferenceGlobal() bool {
return true
}
func (n *NodeModuleRemoved) References() []string {
return []string{modulePrefixStr(n.PathValue)}
}
// EvalDeleteModule is an EvalNode implementation that removes an empty module
// entry from the state.
type EvalDeleteModule struct {
PathValue []string
}
func (n *EvalDeleteModule) Eval(ctx EvalContext) (interface{}, error) {
state, lock := ctx.State()
if state == nil {
return nil, nil
}
// Get a write lock so we can access this instance
lock.Lock()
defer lock.Unlock()
// Make sure we have a clean state
// Destroyed resources aren't deleted, they're written with an ID of "".
state.prune()
// find the module and delete it
for i, m := range state.Modules {
if reflect.DeepEqual(m.Path, n.PathValue) {
if !m.Empty() {
// a targeted apply may leave module resources even without a config,
// so just log this and return.
log.Printf("[DEBUG] cannot remove module %s, not empty", modulePrefixStr(n.PathValue))
break
}
state.Modules = append(state.Modules[:i], state.Modules[i+1:]...)
break
}
}
return nil, nil
}