forked from hashicorp/terraform
/
graph_dot.go
376 lines (320 loc) · 9.39 KB
/
graph_dot.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
package terraform
import (
"bufio"
"bytes"
"fmt"
"strings"
"github.com/hashicorp/terraform/depgraph"
)
// GraphDotOpts are options for turning a graph into dot format.
type GraphDotOpts struct {
// ModuleDepth is the depth of modules to expand. Zero is no expansion,
// one expands the first set of modules, etc. If this is set to -1, then
// all modules are expanded.
ModuleDepth int
// Depth is an internal track of what depth we're at within
// the graph, used to control indentation and other such things.
depth int
}
// GraphDot returns the dot formatting of a visual representation of
// the given Terraform graph.
func GraphDot(g *depgraph.Graph, opts *GraphDotOpts) string {
buf := new(bytes.Buffer)
if opts.depth == 0 {
buf.WriteString("digraph {\n")
buf.WriteString("\tcompound = true;\n")
}
// Determine and add the title
// graphDotTitle(buf, g)
// Add all the resource.
graphDotAddResources(buf, g, opts)
// Add all the resource providers
graphDotAddResourceProviders(buf, g, opts)
// Add all the modules
graphDotAddModules(buf, g, opts)
if opts.depth == 0 {
buf.WriteString("}\n")
}
return buf.String()
}
func graphDotAddRoot(buf *bytes.Buffer, n *depgraph.Noun) {
buf.WriteString(fmt.Sprintf("\t\"%s\" [shape=circle];\n", "root"))
for _, e := range n.Edges() {
target := e.Tail()
buf.WriteString(fmt.Sprintf(
"\t\"%s\" -> \"%s\";\n",
"root",
target))
}
}
func graphDotAddModules(buf *bytes.Buffer, g *depgraph.Graph, opts *GraphDotOpts) {
for _, n := range g.Nouns {
_, ok := n.Meta.(*GraphNodeModule)
if !ok {
continue
}
if graphExpand(opts) {
// We're expanding
graphDotAddModuleExpand(buf, n, opts)
} else {
// We're not expanding, so just add the module on its own
graphDotAddModuleSingle(buf, n, opts)
}
graphWriteEdges(buf, n, opts)
}
}
func graphDotAddModuleExpand(
buf *bytes.Buffer, n *depgraph.Noun, opts *GraphDotOpts) {
m := n.Meta.(*GraphNodeModule)
tab := strings.Repeat("\t", opts.depth+1)
uniqueName := graphUniqueName(n, opts)
// Wrap ourselves in a subgraph
buf.WriteString(fmt.Sprintf("%ssubgraph \"cluster_%s\" {\n", tab, uniqueName))
defer buf.WriteString(fmt.Sprintf("%s}\n", tab))
// Add our label so that we have the proper name.
buf.WriteString(fmt.Sprintf("%s\tlabel = \"%s\";\n", tab, n))
// Add a hidden name for edges to point from/to
buf.WriteString(fmt.Sprintf("%s\t\"%s_hidden\" [fixedsize=true,width=0,height=0,label=\"\",style=invisible];\n", tab, uniqueName))
// Graph the subgraph just as we would any other graph
subOpts := *opts
subOpts.depth++
subStr := GraphDot(m.Graph, &subOpts)
// Tab all the lines of the subgraph
s := bufio.NewScanner(strings.NewReader(subStr))
for s.Scan() {
buf.WriteString(fmt.Sprintf("%s%s\n", tab, s.Text()))
}
}
func graphDotAddModuleSingle(
buf *bytes.Buffer, n *depgraph.Noun, opts *GraphDotOpts) {
tab := strings.Repeat("\t", opts.depth+1)
uniqueName := graphUniqueName(n, opts)
// Create this node.
buf.WriteString(fmt.Sprintf("%s\"%s\" [\n", tab, uniqueName))
buf.WriteString(fmt.Sprintf("%s\tlabel=\"%s\"\n", tab, n))
buf.WriteString(fmt.Sprintf("%s\tshape=component\n", tab))
buf.WriteString(fmt.Sprintf("%s];\n", tab))
}
func graphDotAddResources(
buf *bytes.Buffer, g *depgraph.Graph, opts *GraphDotOpts) {
// Determine if we have diffs. If we do, then we're graphing a
// plan, which alters our graph a bit.
hasDiff := false
for _, n := range g.Nouns {
rn, ok := n.Meta.(*GraphNodeResource)
if !ok {
continue
}
if rn.Resource.Diff != nil && !rn.Resource.Diff.Empty() {
hasDiff = true
break
}
}
var edgeBuf bytes.Buffer
// Do all the non-destroy resources
buf.WriteString("\tsubgraph {\n")
for _, n := range g.Nouns {
rn, ok := n.Meta.(*GraphNodeResource)
if !ok {
continue
}
if rn.Resource.Diff != nil && rn.Resource.Diff.Destroy {
continue
}
// If we have diffs then we're graphing a plan. If we don't have
// have a diff on this resource, don't graph anything, since the
// plan wouldn't do anything to this resource.
if hasDiff {
if rn.Resource.Diff == nil || rn.Resource.Diff.Empty() {
continue
}
}
// Determine the colors. White = no change, yellow = change,
// green = create. Destroy is in the next section.
var color, fillColor string
if rn.Resource.Diff != nil && !rn.Resource.Diff.Empty() {
if rn.Resource.State != nil && rn.Resource.State.ID != "" {
color = "#FFFF00"
fillColor = "#FFFF94"
} else {
color = "#00FF00"
fillColor = "#9EFF9E"
}
}
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
// Create this node.
buf.WriteString(fmt.Sprintf("\t\t\"%s\" [\n", uniqueName))
buf.WriteString(fmt.Sprintf("\t\t\tlabel=\"%s\"\n", n))
buf.WriteString("\t\t\tshape=box\n")
if color != "" {
buf.WriteString("\t\t\tstyle=filled\n")
buf.WriteString(fmt.Sprintf("\t\t\tcolor=\"%s\"\n", color))
buf.WriteString(fmt.Sprintf("\t\t\tfillcolor=\"%s\"\n", fillColor))
}
buf.WriteString("\t\t];\n")
// Build up all the edges in a separate buffer so they're not in the
// subgraph.
graphWriteEdges(&edgeBuf, n, opts)
}
buf.WriteString("\t}\n\n")
if edgeBuf.Len() > 0 {
buf.WriteString(edgeBuf.String())
buf.WriteString("\n")
}
// Do all the destroy resources
edgeBuf.Reset()
buf.WriteString("\tsubgraph {\n")
for _, n := range g.Nouns {
rn, ok := n.Meta.(*GraphNodeResource)
if !ok {
continue
}
if rn.Resource.Diff == nil || !rn.Resource.Diff.Destroy {
continue
}
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
buf.WriteString(fmt.Sprintf(
"\t\t\"%s\" [label=\"%s\",shape=box,style=filled,color=\"#FF0000\",fillcolor=\"#FF9494\"];\n", uniqueName, n))
graphWriteEdges(&edgeBuf, n, opts)
}
buf.WriteString("\t}\n\n")
if edgeBuf.Len() > 0 {
buf.WriteString(edgeBuf.String())
buf.WriteString("\n")
}
// Handle the meta resources
/*
edgeBuf.Reset()
for _, n := range g.Nouns {
_, ok := n.Meta.(*GraphNodeResourceMeta)
if !ok {
continue
}
// Determine which edges to add
var edges []digraph.Edge
if hasDiff {
for _, e := range n.Edges() {
rn, ok := e.Tail().(*depgraph.Noun).Meta.(*GraphNodeResource)
if !ok {
continue
}
if rn.Resource.Diff == nil || rn.Resource.Diff.Empty() {
continue
}
edges = append(edges, e)
}
} else {
edges = n.Edges()
}
// Do not draw if we have no edges
if len(edges) == 0 {
continue
}
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
for _, e := range edges {
target := e.Tail()
uniqueTarget := fmt.Sprintf("%d_%s", opts.depth, target)
edgeBuf.WriteString(fmt.Sprintf(
"\t\"%s\" -> \"%s\";\n",
uniqueName,
uniqueTarget))
}
}
if edgeBuf.Len() > 0 {
buf.WriteString(edgeBuf.String())
buf.WriteString("\n")
}
*/
}
func graphDotAddResourceProviders(
buf *bytes.Buffer, g *depgraph.Graph, opts *GraphDotOpts) {
var edgeBuf bytes.Buffer
buf.WriteString("\tsubgraph {\n")
for _, n := range g.Nouns {
_, ok := n.Meta.(*GraphNodeResourceProvider)
if !ok {
continue
}
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
// Create this node.
buf.WriteString(fmt.Sprintf("\t\t\"%s\" [\n", uniqueName))
buf.WriteString(fmt.Sprintf("\t\t\tlabel=\"%s\"\n", n))
buf.WriteString("\t\t\tshape=diamond\n")
buf.WriteString("\t\t];\n")
// Build up all the edges in a separate buffer so they're not in the
// subgraph.
graphWriteEdges(&edgeBuf, n, opts)
}
buf.WriteString("\t}\n\n")
if edgeBuf.Len() > 0 {
buf.WriteString(edgeBuf.String())
buf.WriteString("\n")
}
}
func graphDotTitle(buf *bytes.Buffer, g *depgraph.Graph) {
// Determine if we have diffs. If we do, then we're graphing a
// plan, which alters our graph a bit.
hasDiff := false
for _, n := range g.Nouns {
rn, ok := n.Meta.(*GraphNodeResource)
if !ok {
continue
}
if rn.Resource.Diff != nil && !rn.Resource.Diff.Empty() {
hasDiff = true
break
}
}
graphType := "Configuration"
if hasDiff {
graphType = "Plan"
}
title := fmt.Sprintf("Terraform %s Resource Graph", graphType)
buf.WriteString(fmt.Sprintf("\tlabel=\"%s\\n\\n\\n\";\n", title))
buf.WriteString("\tlabelloc=\"t\";\n\n")
}
func graphExpand(opts *GraphDotOpts) bool {
return opts.ModuleDepth > opts.depth || opts.ModuleDepth == -1
}
func graphUniqueName(n *depgraph.Noun, opts *GraphDotOpts) string {
return fmt.Sprintf("%d_%s", opts.depth, n)
}
func graphWriteEdges(
buf *bytes.Buffer, n *depgraph.Noun, opts *GraphDotOpts) {
tab := strings.Repeat("\t", opts.depth+1)
uniqueName := graphUniqueName(n, opts)
var ltail string
if _, ok := n.Meta.(*GraphNodeModule); ok && graphExpand(opts) {
ltail = "cluster_" + uniqueName
uniqueName = uniqueName + "_hidden"
}
for _, e := range n.Edges() {
target := e.Tail()
targetN := target.(*depgraph.Noun)
uniqueTarget := graphUniqueName(targetN, opts)
var lhead string
if _, ok := targetN.Meta.(*GraphNodeModule); ok && graphExpand(opts) {
lhead = "cluster_" + uniqueTarget
uniqueTarget = uniqueTarget + "_hidden"
}
var attrs string
if lhead != "" || ltail != "" {
var attrList []string
if lhead != "" {
attrList = append(attrList, fmt.Sprintf(
"lhead=\"%s\"", lhead))
}
if ltail != "" {
attrList = append(attrList, fmt.Sprintf(
"ltail=\"%s\"", ltail))
}
attrs = fmt.Sprintf(" [%s]", strings.Join(attrList, ","))
}
buf.WriteString(fmt.Sprintf(
"%s\"%s\" -> \"%s\"%s;\n",
tab,
uniqueName,
uniqueTarget,
attrs))
}
}