github.com/ns1/terraform@v0.7.10-0.20161109153551-8949419bef40/terraform/graph_walk.go (about)

     1  package terraform
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/dag"
     5  )
     6  
     7  // GraphWalker is an interface that can be implemented that when used
     8  // with Graph.Walk will invoke the given callbacks under certain events.
     9  type GraphWalker interface {
    10  	EnterPath([]string) EvalContext
    11  	ExitPath([]string)
    12  	EnterVertex(dag.Vertex)
    13  	ExitVertex(dag.Vertex, error)
    14  	EnterEvalTree(dag.Vertex, EvalNode) EvalNode
    15  	ExitEvalTree(dag.Vertex, interface{}, error) error
    16  	Debug() *DebugGraph
    17  }
    18  
    19  // GrpahWalkerPanicwrapper can be optionally implemented to catch panics
    20  // that occur while walking the graph. This is not generally recommended
    21  // since panics should crash Terraform and result in a bug report. However,
    22  // this is particularly useful for situations like the shadow graph where
    23  // you don't ever want to cause a panic.
    24  type GraphWalkerPanicwrapper interface {
    25  	GraphWalker
    26  
    27  	// Panic is called when a panic occurs. This will halt the panic from
    28  	// propogating so if the walker wants it to crash still it should panic
    29  	// again. This is called from within a defer so runtime/debug.Stack can
    30  	// be used to get the stack trace of the panic.
    31  	Panic(dag.Vertex, interface{})
    32  }
    33  
    34  // GraphWalkerPanicwrap wraps an existing Graphwalker to wrap and swallow
    35  // the panics. This doesn't lose the panics since the panics are still
    36  // returned as errors as part of a graph walk.
    37  func GraphWalkerPanicwrap(w GraphWalker) GraphWalkerPanicwrapper {
    38  	return &graphWalkerPanicwrapper{
    39  		GraphWalker: w,
    40  	}
    41  }
    42  
    43  type graphWalkerPanicwrapper struct {
    44  	GraphWalker
    45  }
    46  
    47  func (graphWalkerPanicwrapper) Panic(dag.Vertex, interface{}) {}
    48  
    49  // NullGraphWalker is a GraphWalker implementation that does nothing.
    50  // This can be embedded within other GraphWalker implementations for easily
    51  // implementing all the required functions.
    52  type NullGraphWalker struct{}
    53  
    54  func (NullGraphWalker) EnterPath([]string) EvalContext                  { return new(MockEvalContext) }
    55  func (NullGraphWalker) ExitPath([]string)                               {}
    56  func (NullGraphWalker) EnterVertex(dag.Vertex)                          {}
    57  func (NullGraphWalker) ExitVertex(dag.Vertex, error)                    {}
    58  func (NullGraphWalker) EnterEvalTree(v dag.Vertex, n EvalNode) EvalNode { return n }
    59  func (NullGraphWalker) ExitEvalTree(dag.Vertex, interface{}, error) error {
    60  	return nil
    61  }
    62  func (NullGraphWalker) Debug() *DebugGraph { return nil }