github.com/federicobaldo/terraform@v0.6.15-0.20160323222747-b20f680cbf05/terraform/transform_module.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/dag"
     6  )
     7  
     8  // ModuleInputTransformer is a GraphTransformer that adds a node to the
     9  // graph for setting the module input variables for the remainder of the
    10  // graph.
    11  type ModuleInputTransformer struct {
    12  	Variables map[string]string
    13  }
    14  
    15  func (t *ModuleInputTransformer) Transform(g *Graph) error {
    16  	// Create the node
    17  	n := &graphNodeModuleInput{Variables: t.Variables}
    18  
    19  	// Add it to the graph
    20  	g.Add(n)
    21  
    22  	// Connect the inputs to the bottom of the graph so that it happens
    23  	// first.
    24  	for _, v := range g.Vertices() {
    25  		if v == n {
    26  			continue
    27  		}
    28  
    29  		if g.DownEdges(v).Len() == 0 {
    30  			g.Connect(dag.BasicEdge(v, n))
    31  		}
    32  	}
    33  
    34  	return nil
    35  }
    36  
    37  // ModuleDestroyTransformer is a GraphTransformer that adds a node
    38  // to the graph that will just mark the full module for destroy in
    39  // the destroy scenario.
    40  type ModuleDestroyTransformer struct{}
    41  
    42  func (t *ModuleDestroyTransformer) Transform(g *Graph) error {
    43  	// Create the node
    44  	n := &graphNodeModuleDestroy{Path: g.Path}
    45  
    46  	// Add it to the graph. We don't need any edges because
    47  	// it can happen whenever.
    48  	g.Add(n)
    49  
    50  	return nil
    51  }
    52  
    53  type graphNodeModuleDestroy struct {
    54  	Path []string
    55  }
    56  
    57  func (n *graphNodeModuleDestroy) Name() string {
    58  	return "plan-destroy"
    59  }
    60  
    61  // GraphNodeEvalable impl.
    62  func (n *graphNodeModuleDestroy) EvalTree() EvalNode {
    63  	return &EvalOpFilter{
    64  		Ops:  []walkOperation{walkPlanDestroy},
    65  		Node: &EvalDiffDestroyModule{Path: n.Path},
    66  	}
    67  }
    68  
    69  // GraphNodeFlattenable impl.
    70  func (n *graphNodeModuleDestroy) Flatten(p []string) (dag.Vertex, error) {
    71  	return &graphNodeModuleDestroyFlat{
    72  		graphNodeModuleDestroy: n,
    73  		PathValue:              p,
    74  	}, nil
    75  }
    76  
    77  type graphNodeModuleDestroyFlat struct {
    78  	*graphNodeModuleDestroy
    79  
    80  	PathValue []string
    81  }
    82  
    83  func (n *graphNodeModuleDestroyFlat) Name() string {
    84  	return fmt.Sprintf(
    85  		"%s.%s", modulePrefixStr(n.PathValue), n.graphNodeModuleDestroy.Name())
    86  }
    87  
    88  func (n *graphNodeModuleDestroyFlat) Path() []string {
    89  	return n.PathValue
    90  }
    91  
    92  type graphNodeModuleInput struct {
    93  	Variables map[string]string
    94  }
    95  
    96  func (n *graphNodeModuleInput) Name() string {
    97  	return "module inputs"
    98  }
    99  
   100  // GraphNodeEvalable impl.
   101  func (n *graphNodeModuleInput) EvalTree() EvalNode {
   102  	return &EvalSetVariables{Variables: n.Variables}
   103  }
   104  
   105  // graphNodeModuleSkippable impl.
   106  func (n *graphNodeModuleInput) FlattenSkip() bool {
   107  	return true
   108  }