forked from hashicorp/terraform
/
graph_dot.go
244 lines (216 loc) · 5.53 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
package terraform
import (
"bytes"
"fmt"
"github.com/hashicorp/terraform/depgraph"
"github.com/hashicorp/terraform/digraph"
)
// GraphDot returns the dot formatting of a visual representation of
// the given Terraform graph.
func GraphDot(g *depgraph.Graph) string {
buf := new(bytes.Buffer)
buf.WriteString("digraph {\n")
// Determine and add the title
// graphDotTitle(buf, g)
// Add all the resource.
graphDotAddResources(buf, g)
// Add all the resource providers
graphDotAddResourceProviders(buf, g)
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 graphDotAddResources(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
}
}
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"
}
}
// Create this node.
buf.WriteString(fmt.Sprintf("\t\t\"%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.
for _, e := range n.Edges() {
target := e.Tail()
edgeBuf.WriteString(fmt.Sprintf(
"\t\"%s\" -> \"%s\";\n",
n,
target))
}
}
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
}
buf.WriteString(fmt.Sprintf(
"\t\t\"%s\" [shape=box,style=filled,color=\"#FF0000\",fillcolor=\"#FF9494\"];\n", n))
for _, e := range n.Edges() {
target := e.Tail()
edgeBuf.WriteString(fmt.Sprintf(
"\t\"%s\" -> \"%s\";\n",
n,
target))
}
}
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
}
for _, e := range edges {
target := e.Tail()
edgeBuf.WriteString(fmt.Sprintf(
"\t\"%s\" -> \"%s\";\n",
n,
target))
}
}
if edgeBuf.Len() > 0 {
buf.WriteString(edgeBuf.String())
buf.WriteString("\n")
}
}
func graphDotAddResourceProviders(buf *bytes.Buffer, g *depgraph.Graph) {
var edgeBuf bytes.Buffer
buf.WriteString("\tsubgraph {\n")
for _, n := range g.Nouns {
_, ok := n.Meta.(*GraphNodeResourceProvider)
if !ok {
continue
}
// Create this node.
buf.WriteString(fmt.Sprintf("\t\t\"%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.
for _, e := range n.Edges() {
target := e.Tail()
edgeBuf.WriteString(fmt.Sprintf(
"\t\"%s\" -> \"%s\";\n",
n,
target))
}
}
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")
}