github.com/tomaszheflik/terraform@v0.7.3-0.20160827060421-32f990b41594/terraform/transform_expand.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/dag"
     7  )
     8  
     9  // GraphNodeExapndable is an interface that nodes can implement to
    10  // signal that they can be expanded. Expanded nodes turn into
    11  // GraphNodeSubgraph nodes within the graph.
    12  type GraphNodeExpandable interface {
    13  	Expand(GraphBuilder) (GraphNodeSubgraph, error)
    14  }
    15  
    16  // GraphNodeDynamicExpandable is an interface that nodes can implement
    17  // to signal that they can be expanded at eval-time (hence dynamic).
    18  // These nodes are given the eval context and are expected to return
    19  // a new subgraph.
    20  type GraphNodeDynamicExpandable interface {
    21  	DynamicExpand(EvalContext) (*Graph, error)
    22  }
    23  
    24  // GraphNodeSubgraph is an interface a node can implement if it has
    25  // a larger subgraph that should be walked.
    26  type GraphNodeSubgraph interface {
    27  	Subgraph() *Graph
    28  }
    29  
    30  // ExpandTransform is a transformer that does a subgraph expansion
    31  // at graph transform time (vs. at eval time). The benefit of earlier
    32  // subgraph expansion is that errors with the graph build can be detected
    33  // at an earlier stage.
    34  type ExpandTransform struct {
    35  	Builder GraphBuilder
    36  }
    37  
    38  func (t *ExpandTransform) Transform(v dag.Vertex) (dag.Vertex, error) {
    39  	ev, ok := v.(GraphNodeExpandable)
    40  	if !ok {
    41  		// This isn't an expandable vertex, so just ignore it.
    42  		return v, nil
    43  	}
    44  
    45  	// Expand the subgraph!
    46  	log.Printf("[DEBUG] vertex %s: static expanding", dag.VertexName(ev))
    47  	return ev.Expand(t.Builder)
    48  }
    49  
    50  type GraphNodeBasicSubgraph struct {
    51  	NameValue string
    52  	Graph     *Graph
    53  }
    54  
    55  func (n *GraphNodeBasicSubgraph) Name() string {
    56  	return n.NameValue
    57  }
    58  
    59  func (n *GraphNodeBasicSubgraph) Subgraph() *Graph {
    60  	return n.Graph
    61  }
    62  
    63  func (n *GraphNodeBasicSubgraph) FlattenGraph() *Graph {
    64  	return n.Graph
    65  }