2015-01-26 23:23:27 -06:00
|
|
|
package terraform
|
|
|
|
|
|
|
|
import (
|
2015-02-07 15:29:55 -06:00
|
|
|
"fmt"
|
2015-02-08 19:58:02 -06:00
|
|
|
"log"
|
2015-02-10 01:32:28 -06:00
|
|
|
"strings"
|
2015-01-26 23:23:27 -06:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/dag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RootModuleName is the name given to the root module implicitly.
|
|
|
|
const RootModuleName = "root"
|
|
|
|
|
2015-01-26 23:32:32 -06:00
|
|
|
// RootModulePath is the path for the root module.
|
|
|
|
var RootModulePath = []string{RootModuleName}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
// Graph represents the graph that Terraform uses to represent resources
|
|
|
|
// and their dependencies. Each graph represents only one module, but it
|
|
|
|
// can contain further modules, which themselves have their own graph.
|
|
|
|
type Graph struct {
|
|
|
|
// Graph is the actual DAG. This is embedded so you can call the DAG
|
|
|
|
// methods directly.
|
2015-02-04 10:30:53 -06:00
|
|
|
dag.AcyclicGraph
|
2015-01-26 23:23:27 -06:00
|
|
|
|
|
|
|
// Path is the path in the module tree that this Graph represents.
|
|
|
|
// The root is represented by a single element list containing
|
|
|
|
// RootModuleName
|
|
|
|
Path []string
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// annotations are the annotations that are added to vertices. Annotations
|
|
|
|
// are arbitrary metadata taht is used for various logic. Annotations
|
|
|
|
// should have unique keys that are referenced via constants.
|
|
|
|
annotations map[dag.Vertex]map[string]interface{}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
// dependableMap is a lookaside table for fast lookups for connecting
|
|
|
|
// dependencies by their GraphNodeDependable value to avoid O(n^3)-like
|
|
|
|
// situations and turn them into O(1) with respect to the number of new
|
|
|
|
// edges.
|
|
|
|
dependableMap map[string]dag.Vertex
|
|
|
|
|
|
|
|
once sync.Once
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// Annotations returns the annotations that are configured for the
|
|
|
|
// given vertex. The map is guaranteed to be non-nil but may be empty.
|
|
|
|
//
|
|
|
|
// The returned map may be modified to modify the annotations of the
|
|
|
|
// vertex.
|
|
|
|
func (g *Graph) Annotations(v dag.Vertex) map[string]interface{} {
|
|
|
|
g.once.Do(g.init)
|
|
|
|
|
|
|
|
// If this vertex isn't in the graph, then just return an empty map
|
|
|
|
if !g.HasVertex(v) {
|
|
|
|
return map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the map, if it doesn't exist yet then initialize it
|
|
|
|
m, ok := g.annotations[v]
|
|
|
|
if !ok {
|
|
|
|
m = make(map[string]interface{})
|
|
|
|
g.annotations[v] = m
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
// Add is the same as dag.Graph.Add.
|
|
|
|
func (g *Graph) Add(v dag.Vertex) dag.Vertex {
|
|
|
|
g.once.Do(g.init)
|
|
|
|
|
|
|
|
// Call upwards to add it to the actual graph
|
|
|
|
g.Graph.Add(v)
|
|
|
|
|
|
|
|
// If this is a depend-able node, then store the lookaside info
|
|
|
|
if dv, ok := v.(GraphNodeDependable); ok {
|
|
|
|
for _, n := range dv.DependableName() {
|
|
|
|
g.dependableMap[n] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// If this initializes annotations, then do that
|
|
|
|
if av, ok := v.(GraphNodeAnnotationInit); ok {
|
|
|
|
as := g.Annotations(v)
|
|
|
|
for k, v := range av.AnnotationInit() {
|
|
|
|
as[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2015-05-01 13:20:57 -05:00
|
|
|
// Remove is the same as dag.Graph.Remove
|
|
|
|
func (g *Graph) Remove(v dag.Vertex) dag.Vertex {
|
|
|
|
g.once.Do(g.init)
|
|
|
|
|
|
|
|
// If this is a depend-able node, then remove the lookaside info
|
|
|
|
if dv, ok := v.(GraphNodeDependable); ok {
|
|
|
|
for _, n := range dv.DependableName() {
|
|
|
|
delete(g.dependableMap, n)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// Remove the annotations
|
|
|
|
delete(g.annotations, v)
|
|
|
|
|
2015-05-01 13:20:57 -05:00
|
|
|
// Call upwards to remove it from the actual graph
|
|
|
|
return g.Graph.Remove(v)
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:08:06 -05:00
|
|
|
// Replace is the same as dag.Graph.Replace
|
|
|
|
func (g *Graph) Replace(o, n dag.Vertex) bool {
|
2016-09-13 19:11:34 -05:00
|
|
|
g.once.Do(g.init)
|
|
|
|
|
2015-05-01 20:08:06 -05:00
|
|
|
// Go through and update our lookaside to point to the new vertex
|
|
|
|
for k, v := range g.dependableMap {
|
|
|
|
if v == o {
|
2015-08-10 15:04:48 -05:00
|
|
|
if _, ok := n.(GraphNodeDependable); ok {
|
|
|
|
g.dependableMap[k] = n
|
|
|
|
} else {
|
|
|
|
delete(g.dependableMap, k)
|
|
|
|
}
|
2015-05-01 20:08:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// Move the annotation if it exists
|
|
|
|
if m, ok := g.annotations[o]; ok {
|
|
|
|
g.annotations[n] = m
|
|
|
|
delete(g.annotations, o)
|
|
|
|
}
|
|
|
|
|
2015-05-01 20:08:06 -05:00
|
|
|
return g.Graph.Replace(o, n)
|
|
|
|
}
|
|
|
|
|
2015-01-27 16:56:01 -06:00
|
|
|
// ConnectDependent connects a GraphNodeDependent to all of its
|
|
|
|
// GraphNodeDependables. It returns the list of dependents it was
|
|
|
|
// unable to connect to.
|
|
|
|
func (g *Graph) ConnectDependent(raw dag.Vertex) []string {
|
|
|
|
v, ok := raw.(GraphNodeDependent)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:06:46 -06:00
|
|
|
return g.ConnectTo(v, v.DependentOn())
|
2015-01-26 23:23:27 -06:00
|
|
|
}
|
|
|
|
|
2015-01-27 16:56:01 -06:00
|
|
|
// ConnectDependents goes through the graph, connecting all the
|
|
|
|
// GraphNodeDependents to GraphNodeDependables. This is safe to call
|
|
|
|
// multiple times.
|
|
|
|
//
|
|
|
|
// To get details on whether dependencies could be found/made, the more
|
|
|
|
// specific ConnectDependent should be used.
|
|
|
|
func (g *Graph) ConnectDependents() {
|
|
|
|
for _, v := range g.Vertices() {
|
|
|
|
if dv, ok := v.(GraphNodeDependent); ok {
|
|
|
|
g.ConnectDependent(dv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 23:06:46 -06:00
|
|
|
// ConnectFrom creates an edge by finding the source from a DependableName
|
|
|
|
// and connecting it to the specific vertex.
|
|
|
|
func (g *Graph) ConnectFrom(source string, target dag.Vertex) {
|
|
|
|
g.once.Do(g.init)
|
|
|
|
|
|
|
|
if source := g.dependableMap[source]; source != nil {
|
|
|
|
g.Connect(dag.BasicEdge(source, target))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ConnectTo connects a vertex to a raw string of targets that are the
|
|
|
|
// result of DependableName, and returns the list of targets that are missing.
|
|
|
|
func (g *Graph) ConnectTo(v dag.Vertex, targets []string) []string {
|
|
|
|
g.once.Do(g.init)
|
|
|
|
|
|
|
|
var missing []string
|
|
|
|
for _, t := range targets {
|
|
|
|
if dest := g.dependableMap[t]; dest != nil {
|
|
|
|
g.Connect(dag.BasicEdge(v, dest))
|
|
|
|
} else {
|
|
|
|
missing = append(missing, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return missing
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dependable finds the vertices in the graph that have the given dependable
|
|
|
|
// names and returns them.
|
|
|
|
func (g *Graph) Dependable(n string) dag.Vertex {
|
|
|
|
// TODO: do we need this?
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-02-07 15:29:55 -06:00
|
|
|
// Walk walks the graph with the given walker for callbacks. The graph
|
|
|
|
// will be walked with full parallelism, so the walker should expect
|
|
|
|
// to be called in concurrently.
|
2015-02-10 10:58:14 -06:00
|
|
|
func (g *Graph) Walk(walker GraphWalker) error {
|
|
|
|
return g.walk(walker)
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
func (g *Graph) init() {
|
2016-09-13 19:11:34 -05:00
|
|
|
if g.annotations == nil {
|
|
|
|
g.annotations = make(map[dag.Vertex]map[string]interface{})
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:23:27 -06:00
|
|
|
if g.dependableMap == nil {
|
|
|
|
g.dependableMap = make(map[string]dag.Vertex)
|
|
|
|
}
|
|
|
|
}
|
2015-01-26 23:37:10 -06:00
|
|
|
|
2015-02-08 19:10:54 -06:00
|
|
|
func (g *Graph) walk(walker GraphWalker) error {
|
2015-02-07 15:29:55 -06:00
|
|
|
// The callbacks for enter/exiting a graph
|
2015-05-01 16:19:32 -05:00
|
|
|
ctx := walker.EnterPath(g.Path)
|
|
|
|
defer walker.ExitPath(g.Path)
|
2015-02-07 15:29:55 -06:00
|
|
|
|
2015-02-10 01:32:28 -06:00
|
|
|
// Get the path for logs
|
|
|
|
path := strings.Join(ctx.Path(), ".")
|
|
|
|
|
2015-02-08 16:00:13 -06:00
|
|
|
// Walk the graph.
|
2015-02-08 19:10:54 -06:00
|
|
|
var walkFn dag.WalkFunc
|
|
|
|
walkFn = func(v dag.Vertex) (rerr error) {
|
2015-02-10 01:32:28 -06:00
|
|
|
log.Printf("[DEBUG] vertex %s.%s: walking", path, dag.VertexName(v))
|
2015-02-08 19:58:02 -06:00
|
|
|
|
2015-02-07 15:29:55 -06:00
|
|
|
walker.EnterVertex(v)
|
2015-02-08 19:10:54 -06:00
|
|
|
defer func() { walker.ExitVertex(v, rerr) }()
|
2015-02-07 15:29:55 -06:00
|
|
|
|
2015-05-01 16:19:32 -05:00
|
|
|
// vertexCtx is the context that we use when evaluating. This
|
|
|
|
// is normally the context of our graph but can be overridden
|
|
|
|
// with a GraphNodeSubPath impl.
|
|
|
|
vertexCtx := ctx
|
2015-05-01 18:29:19 -05:00
|
|
|
if pn, ok := v.(GraphNodeSubPath); ok && len(pn.Path()) > 0 {
|
2015-05-01 16:19:32 -05:00
|
|
|
vertexCtx = walker.EnterPath(pn.Path())
|
|
|
|
defer walker.ExitPath(pn.Path())
|
|
|
|
}
|
|
|
|
|
2015-02-07 15:29:55 -06:00
|
|
|
// If the node is eval-able, then evaluate it.
|
|
|
|
if ev, ok := v.(GraphNodeEvalable); ok {
|
|
|
|
tree := ev.EvalTree()
|
|
|
|
if tree == nil {
|
|
|
|
panic(fmt.Sprintf(
|
2015-02-10 01:32:28 -06:00
|
|
|
"%s.%s (%T): nil eval tree", path, dag.VertexName(v), v))
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow the walker to change our tree if needed. Eval,
|
|
|
|
// then callback with the output.
|
2015-02-10 01:32:28 -06:00
|
|
|
log.Printf("[DEBUG] vertex %s.%s: evaluating", path, dag.VertexName(v))
|
2015-02-07 15:29:55 -06:00
|
|
|
tree = walker.EnterEvalTree(v, tree)
|
2015-05-01 16:19:32 -05:00
|
|
|
output, err := Eval(tree, vertexCtx)
|
2015-02-11 20:00:22 -06:00
|
|
|
if rerr = walker.ExitEvalTree(v, output, err); rerr != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
2015-02-08 16:00:13 -06:00
|
|
|
|
|
|
|
// If the node is dynamically expanded, then expand it
|
|
|
|
if ev, ok := v.(GraphNodeDynamicExpandable); ok {
|
2015-02-08 19:58:02 -06:00
|
|
|
log.Printf(
|
2015-02-10 01:32:28 -06:00
|
|
|
"[DEBUG] vertex %s.%s: expanding/walking dynamic subgraph",
|
|
|
|
path,
|
2015-02-08 19:58:02 -06:00
|
|
|
dag.VertexName(v))
|
2015-05-01 16:19:32 -05:00
|
|
|
g, err := ev.DynamicExpand(vertexCtx)
|
2015-02-08 16:00:13 -06:00
|
|
|
if err != nil {
|
2015-02-08 19:10:54 -06:00
|
|
|
rerr = err
|
2015-02-08 16:00:13 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walk the subgraph
|
2015-02-08 19:10:54 -06:00
|
|
|
if rerr = g.walk(walker); rerr != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-02-08 16:00:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-09 18:06:19 -06:00
|
|
|
// If the node has a subgraph, then walk the subgraph
|
|
|
|
if sn, ok := v.(GraphNodeSubgraph); ok {
|
|
|
|
log.Printf(
|
2015-02-10 01:32:28 -06:00
|
|
|
"[DEBUG] vertex %s.%s: walking subgraph",
|
|
|
|
path,
|
2015-02-09 18:06:19 -06:00
|
|
|
dag.VertexName(v))
|
|
|
|
|
|
|
|
if rerr = sn.Subgraph().walk(walker); rerr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 19:10:54 -06:00
|
|
|
return nil
|
2015-02-08 16:00:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-08 19:10:54 -06:00
|
|
|
return g.AcyclicGraph.Walk(walkFn)
|
2015-02-07 15:29:55 -06:00
|
|
|
}
|
|
|
|
|
2016-09-13 19:11:34 -05:00
|
|
|
// GraphNodeAnnotationInit is an interface that allows a node to
|
|
|
|
// initialize it's annotations.
|
|
|
|
//
|
|
|
|
// AnnotationInit will be called _once_ when the node is added to a
|
|
|
|
// graph for the first time and is expected to return it's initial
|
|
|
|
// annotations.
|
|
|
|
type GraphNodeAnnotationInit interface {
|
|
|
|
AnnotationInit() map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2015-01-26 23:37:10 -06:00
|
|
|
// GraphNodeDependable is an interface which says that a node can be
|
|
|
|
// depended on (an edge can be placed between this node and another) according
|
|
|
|
// to the well-known name returned by DependableName.
|
|
|
|
//
|
|
|
|
// DependableName can return multiple names it is known by.
|
|
|
|
type GraphNodeDependable interface {
|
|
|
|
DependableName() []string
|
|
|
|
}
|
2015-01-27 16:56:01 -06:00
|
|
|
|
|
|
|
// GraphNodeDependent is an interface which says that a node depends
|
|
|
|
// on another GraphNodeDependable by some name. By implementing this
|
|
|
|
// interface, Graph.ConnectDependents() can be called multiple times
|
|
|
|
// safely and efficiently.
|
|
|
|
type GraphNodeDependent interface {
|
|
|
|
DependentOn() []string
|
|
|
|
}
|