2014-07-14 20:34:52 +02:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2014-09-25 02:36:27 +02:00
|
|
|
"bufio"
|
2014-09-25 08:46:22 +02:00
|
|
|
"bytes"
|
2014-07-14 20:34:52 +02:00
|
|
|
"fmt"
|
2014-09-25 02:36:27 +02:00
|
|
|
"strings"
|
2014-07-14 20:34:52 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/depgraph"
|
|
|
|
)
|
|
|
|
|
2014-09-25 02:36:27 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2014-07-14 20:34:52 +02:00
|
|
|
// GraphDot returns the dot formatting of a visual representation of
|
|
|
|
// the given Terraform graph.
|
2014-09-25 02:36:27 +02:00
|
|
|
func GraphDot(g *depgraph.Graph, opts *GraphDotOpts) string {
|
2014-07-14 20:34:52 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2014-09-25 02:36:27 +02:00
|
|
|
|
|
|
|
if opts.depth == 0 {
|
|
|
|
buf.WriteString("digraph {\n")
|
2014-09-25 08:46:22 +02:00
|
|
|
buf.WriteString("\tcompound = true;\n")
|
2014-09-25 02:36:27 +02:00
|
|
|
}
|
2014-07-14 20:34:52 +02:00
|
|
|
|
|
|
|
// Determine and add the title
|
|
|
|
// graphDotTitle(buf, g)
|
|
|
|
|
|
|
|
// Add all the resource.
|
2014-09-25 08:46:22 +02:00
|
|
|
graphDotAddResources(buf, g, opts)
|
2014-07-14 20:34:52 +02:00
|
|
|
|
|
|
|
// Add all the resource providers
|
2014-09-25 08:46:22 +02:00
|
|
|
graphDotAddResourceProviders(buf, g, opts)
|
2014-07-14 20:34:52 +02:00
|
|
|
|
2014-09-25 02:36:27 +02:00
|
|
|
// Add all the modules
|
|
|
|
graphDotAddModules(buf, g, opts)
|
|
|
|
|
|
|
|
if opts.depth == 0 {
|
|
|
|
buf.WriteString("}\n")
|
|
|
|
}
|
|
|
|
|
2014-07-14 20:34:52 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 02:36:27 +02:00
|
|
|
func graphDotAddModules(buf *bytes.Buffer, g *depgraph.Graph, opts *GraphDotOpts) {
|
|
|
|
for _, n := range g.Nouns {
|
|
|
|
_, ok := n.Meta.(*GraphNodeModule)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
if graphExpand(opts) {
|
2014-09-25 02:36:27 +02:00
|
|
|
// We're expanding
|
|
|
|
graphDotAddModuleExpand(buf, n, opts)
|
2014-09-25 08:46:22 +02:00
|
|
|
} else {
|
|
|
|
// We're not expanding, so just add the module on its own
|
|
|
|
graphDotAddModuleSingle(buf, n, opts)
|
2014-09-25 02:36:27 +02:00
|
|
|
}
|
2014-09-25 08:46:22 +02:00
|
|
|
|
|
|
|
graphWriteEdges(buf, n, opts)
|
2014-09-25 02:36:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func graphDotAddModuleExpand(
|
|
|
|
buf *bytes.Buffer, n *depgraph.Noun, opts *GraphDotOpts) {
|
|
|
|
m := n.Meta.(*GraphNodeModule)
|
|
|
|
tab := strings.Repeat("\t", opts.depth+1)
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName := graphUniqueName(n, opts)
|
2014-09-25 02:36:27 +02:00
|
|
|
|
|
|
|
// Wrap ourselves in a subgraph
|
2014-09-25 08:46:22 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%ssubgraph \"cluster_%s\" {\n", tab, uniqueName))
|
2014-09-25 02:36:27 +02:00
|
|
|
defer buf.WriteString(fmt.Sprintf("%s}\n", tab))
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
// 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))
|
|
|
|
|
2014-09-25 02:36:27 +02:00
|
|
|
// 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)
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName := graphUniqueName(n, opts)
|
2014-09-25 02:36:27 +02:00
|
|
|
|
|
|
|
// Create this node.
|
2014-09-25 08:46:22 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s\"%s\" [\n", tab, uniqueName))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s\tlabel=\"%s\"\n", tab, n))
|
2014-09-25 02:36:27 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("%s\tshape=component\n", tab))
|
|
|
|
buf.WriteString(fmt.Sprintf("%s];\n", tab))
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
func graphDotAddResources(
|
|
|
|
buf *bytes.Buffer, g *depgraph.Graph, opts *GraphDotOpts) {
|
2014-07-14 20:34:52 +02:00
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
|
|
|
|
|
2014-07-14 20:34:52 +02:00
|
|
|
// Create this node.
|
2014-09-25 08:46:22 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("\t\t\"%s\" [\n", uniqueName))
|
|
|
|
buf.WriteString(fmt.Sprintf("\t\t\tlabel=\"%s\"\n", n))
|
2014-07-14 20:34:52 +02:00
|
|
|
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.
|
2014-09-26 04:06:29 +02:00
|
|
|
graphWriteEdges(&edgeBuf, n, opts)
|
2014-07-14 20:34:52 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
|
|
|
|
|
2014-07-14 20:34:52 +02:00
|
|
|
buf.WriteString(fmt.Sprintf(
|
2014-09-25 08:46:22 +02:00
|
|
|
"\t\t\"%s\" [label=\"%s\",shape=box,style=filled,color=\"#FF0000\",fillcolor=\"#FF9494\"];\n", uniqueName, n))
|
2014-07-14 20:34:52 +02:00
|
|
|
|
2014-09-26 04:06:29 +02:00
|
|
|
graphWriteEdges(&edgeBuf, n, opts)
|
2014-07-14 20:34:52 +02:00
|
|
|
}
|
|
|
|
buf.WriteString("\t}\n\n")
|
|
|
|
if edgeBuf.Len() > 0 {
|
|
|
|
buf.WriteString(edgeBuf.String())
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
2014-07-25 00:56:05 +02:00
|
|
|
|
|
|
|
// Handle the meta resources
|
2014-10-02 03:08:52 +02:00
|
|
|
/*
|
2014-07-25 00:56:05 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
|
2014-07-25 00:56:05 +02:00
|
|
|
for _, e := range edges {
|
|
|
|
target := e.Tail()
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueTarget := fmt.Sprintf("%d_%s", opts.depth, target)
|
2014-07-25 00:56:05 +02:00
|
|
|
edgeBuf.WriteString(fmt.Sprintf(
|
|
|
|
"\t\"%s\" -> \"%s\";\n",
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName,
|
|
|
|
uniqueTarget))
|
2014-07-25 00:56:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if edgeBuf.Len() > 0 {
|
|
|
|
buf.WriteString(edgeBuf.String())
|
|
|
|
buf.WriteString("\n")
|
|
|
|
}
|
2014-10-02 03:08:52 +02:00
|
|
|
*/
|
2014-07-14 20:34:52 +02:00
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
func graphDotAddResourceProviders(
|
|
|
|
buf *bytes.Buffer, g *depgraph.Graph, opts *GraphDotOpts) {
|
2014-07-14 20:34:52 +02:00
|
|
|
var edgeBuf bytes.Buffer
|
|
|
|
buf.WriteString("\tsubgraph {\n")
|
|
|
|
for _, n := range g.Nouns {
|
|
|
|
_, ok := n.Meta.(*GraphNodeResourceProvider)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-25 08:46:22 +02:00
|
|
|
uniqueName := fmt.Sprintf("%d_%s", opts.depth, n)
|
|
|
|
|
2014-07-14 20:34:52 +02:00
|
|
|
// Create this node.
|
2014-09-25 08:46:22 +02:00
|
|
|
buf.WriteString(fmt.Sprintf("\t\t\"%s\" [\n", uniqueName))
|
|
|
|
buf.WriteString(fmt.Sprintf("\t\t\tlabel=\"%s\"\n", n))
|
2014-07-14 20:34:52 +02:00
|
|
|
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.
|
2014-09-26 04:06:29 +02:00
|
|
|
graphWriteEdges(&edgeBuf, n, opts)
|
2014-07-14 20:34:52 +02:00
|
|
|
}
|
|
|
|
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")
|
|
|
|
}
|
2014-09-25 08:46:22 +02:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|