forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph_config_node_module.go
230 lines (193 loc) · 5.81 KB
/
graph_config_node_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package terraform
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/config"
"github.com/hashicorp/terraform/config/module"
"github.com/hashicorp/terraform/dag"
"github.com/hashicorp/terraform/dot"
)
// GraphNodeConfigModule represents a module within the configuration graph.
type GraphNodeConfigModule struct {
Path []string
Module *config.Module
Tree *module.Tree
}
func (n *GraphNodeConfigModule) ConfigType() GraphNodeConfigType {
return GraphNodeConfigTypeModule
}
func (n *GraphNodeConfigModule) DependableName() []string {
config := n.Tree.Config()
result := make([]string, 1, len(config.Outputs)+1)
result[0] = n.Name()
for _, o := range config.Outputs {
result = append(result, fmt.Sprintf("%s.output.%s", n.Name(), o.Name))
}
return result
}
func (n *GraphNodeConfigModule) DependentOn() []string {
vars := n.Module.RawConfig.Variables
result := make([]string, 0, len(vars))
for _, v := range vars {
if vn := varNameForVar(v); vn != "" {
result = append(result, vn)
}
}
return result
}
func (n *GraphNodeConfigModule) Name() string {
return fmt.Sprintf("module.%s", n.Module.Name)
}
// GraphNodeExpandable
func (n *GraphNodeConfigModule) Expand(b GraphBuilder) (GraphNodeSubgraph, error) {
// Build the graph first
graph, err := b.Build(n.Path)
if err != nil {
return nil, err
}
// Add the parameters node to the module
t := &ModuleInputTransformer{Variables: make(map[string]string)}
if err := t.Transform(graph); err != nil {
return nil, err
}
{
// Add the destroy marker to the graph
t := &ModuleDestroyTransformer{}
if err := t.Transform(graph); err != nil {
return nil, err
}
}
// Build the actual subgraph node
return &graphNodeModuleExpanded{
Original: n,
Graph: graph,
Variables: t.Variables,
}, nil
}
// GraphNodeExpandable
func (n *GraphNodeConfigModule) ProvidedBy() []string {
// Build up the list of providers by simply going over our configuration
// to find the providers that are configured there as well as the
// providers that the resources use.
config := n.Tree.Config()
providers := make(map[string]struct{})
for _, p := range config.ProviderConfigs {
providers[p.Name] = struct{}{}
}
for _, r := range config.Resources {
providers[resourceProvider(r.Type, r.Provider)] = struct{}{}
}
// Turn the map into a string. This makes sure that the list is
// de-dupped since we could be going over potentially many resources.
result := make([]string, 0, len(providers))
for p, _ := range providers {
result = append(result, p)
}
return result
}
// graphNodeModuleExpanded represents a module where the graph has
// been expanded. It stores the graph of the module as well as a reference
// to the map of variables.
type graphNodeModuleExpanded struct {
Original *GraphNodeConfigModule
Graph *Graph
// Variables is a map of the input variables. This reference should
// be shared with ModuleInputTransformer in order to create a connection
// where the variables are set properly.
Variables map[string]string
}
func (n *graphNodeModuleExpanded) Name() string {
return fmt.Sprintf("%s (expanded)", dag.VertexName(n.Original))
}
func (n *graphNodeModuleExpanded) ConfigType() GraphNodeConfigType {
return GraphNodeConfigTypeModule
}
// GraphNodeDependable
func (n *graphNodeModuleExpanded) DependableName() []string {
return n.Original.DependableName()
}
// GraphNodeDependent
func (n *graphNodeModuleExpanded) DependentOn() []string {
return n.Original.DependentOn()
}
// GraphNodeDotter impl.
func (n *graphNodeModuleExpanded) DotNode(name string, opts *GraphDotOpts) *dot.Node {
return dot.NewNode(name, map[string]string{
"label": dag.VertexName(n.Original),
"shape": "component",
})
}
// GraphNodeEvalable impl.
func (n *graphNodeModuleExpanded) EvalTree() EvalNode {
var resourceConfig *ResourceConfig
return &EvalSequence{
Nodes: []EvalNode{
&EvalInterpolate{
Config: n.Original.Module.RawConfig,
Output: &resourceConfig,
},
&EvalVariableBlock{
Config: &resourceConfig,
Variables: n.Variables,
},
},
}
}
// GraphNodeFlattenable impl.
func (n *graphNodeModuleExpanded) FlattenGraph() *Graph {
graph := n.Subgraph()
input := n.Original.Module.RawConfig
// Go over each vertex and do some modifications to the graph for
// flattening. We have to skip some nodes (graphNodeModuleSkippable)
// as well as setup the variable values.
for _, v := range graph.Vertices() {
if sn, ok := v.(graphNodeModuleSkippable); ok && sn.FlattenSkip() {
graph.Remove(v)
continue
}
// If this is a variable, then look it up in the raw configuration.
// If it exists in the raw configuration, set the value of it.
if vn, ok := v.(*GraphNodeConfigVariable); ok && input != nil {
key := vn.VariableName()
if v, ok := input.Raw[key]; ok {
config, err := config.NewRawConfig(map[string]interface{}{
key: v,
})
if err != nil {
// This shouldn't happen because it is already in
// a RawConfig above meaning it worked once before.
panic(err)
}
// Set the variable value so it is interpolated properly.
// Also set the module so we set the value on it properly.
vn.Module = graph.Path[len(graph.Path)-1]
vn.Value = config
}
}
}
return graph
}
// GraphNodeSubgraph impl.
func (n *graphNodeModuleExpanded) Subgraph() *Graph {
return n.Graph
}
// This interface can be implemented to be skipped/ignored when
// flattening the module graph.
type graphNodeModuleSkippable interface {
FlattenSkip() bool
}
func modulePrefixStr(p []string) string {
parts := make([]string, 0, len(p)*2)
for _, p := range p[1:] {
parts = append(parts, "module", p)
}
return strings.Join(parts, ".")
}
func modulePrefixList(result []string, prefix string) []string {
if prefix != "" {
for i, v := range result {
result[i] = fmt.Sprintf("%s.%s", prefix, v)
}
}
return result
}