forked from hashicorp/terraform
/
transform_module.go
108 lines (85 loc) · 2.32 KB
/
transform_module.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package terraform
import (
"fmt"
"github.com/hashicorp/terraform/dag"
)
// ModuleInputTransformer is a GraphTransformer that adds a node to the
// graph for setting the module input variables for the remainder of the
// graph.
type ModuleInputTransformer struct {
Variables map[string]string
}
func (t *ModuleInputTransformer) Transform(g *Graph) error {
// Create the node
n := &graphNodeModuleInput{Variables: t.Variables}
// Add it to the graph
g.Add(n)
// Connect the inputs to the bottom of the graph so that it happens
// first.
for _, v := range g.Vertices() {
if v == n {
continue
}
if g.DownEdges(v).Len() == 0 {
g.Connect(dag.BasicEdge(v, n))
}
}
return nil
}
// ModuleDestroyTransformer is a GraphTransformer that adds a node
// to the graph that will just mark the full module for destroy in
// the destroy scenario.
type ModuleDestroyTransformer struct{}
func (t *ModuleDestroyTransformer) Transform(g *Graph) error {
// Create the node
n := &graphNodeModuleDestroy{Path: g.Path}
// Add it to the graph. We don't need any edges because
// it can happen whenever.
g.Add(n)
return nil
}
type graphNodeModuleDestroy struct {
Path []string
}
func (n *graphNodeModuleDestroy) Name() string {
return "plan-destroy"
}
// GraphNodeEvalable impl.
func (n *graphNodeModuleDestroy) EvalTree() EvalNode {
return &EvalOpFilter{
Ops: []walkOperation{walkPlanDestroy},
Node: &EvalDiffDestroyModule{Path: n.Path},
}
}
// GraphNodeFlattenable impl.
func (n *graphNodeModuleDestroy) Flatten(p []string) (dag.Vertex, error) {
return &graphNodeModuleDestroyFlat{
graphNodeModuleDestroy: n,
PathValue: p,
}, nil
}
type graphNodeModuleDestroyFlat struct {
*graphNodeModuleDestroy
PathValue []string
}
func (n *graphNodeModuleDestroyFlat) Name() string {
return fmt.Sprintf(
"%s.%s", modulePrefixStr(n.PathValue), n.graphNodeModuleDestroy.Name())
}
func (n *graphNodeModuleDestroyFlat) Path() []string {
return n.PathValue
}
type graphNodeModuleInput struct {
Variables map[string]string
}
func (n *graphNodeModuleInput) Name() string {
return "module inputs"
}
// GraphNodeEvalable impl.
func (n *graphNodeModuleInput) EvalTree() EvalNode {
return &EvalSetVariables{Variables: n.Variables}
}
// graphNodeModuleSkippable impl.
func (n *graphNodeModuleInput) FlattenSkip() bool {
return true
}