github.com/markdia/terraform@v0.5.1-0.20150508012022-f1ae920aa970/terraform/transform_output.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // GraphNodeOutput is an interface that nodes that are outputs must
     8  // implement. The OutputName returned is the name of the output key
     9  // that they manage.
    10  type GraphNodeOutput interface {
    11  	OutputName() string
    12  }
    13  
    14  // AddOutputOrphanTransformer is a transformer that adds output orphans
    15  // to the graph. Output orphans are outputs that are no longer in the
    16  // configuration and therefore need to be removed from the state.
    17  type AddOutputOrphanTransformer struct {
    18  	State *State
    19  }
    20  
    21  func (t *AddOutputOrphanTransformer) Transform(g *Graph) error {
    22  	// Get the state for this module. If we have no state, we have no orphans
    23  	state := t.State.ModuleByPath(g.Path)
    24  	if state == nil {
    25  		return nil
    26  	}
    27  
    28  	// Create the set of outputs we do have in the graph
    29  	found := make(map[string]struct{})
    30  	for _, v := range g.Vertices() {
    31  		on, ok := v.(GraphNodeOutput)
    32  		if !ok {
    33  			continue
    34  		}
    35  
    36  		found[on.OutputName()] = struct{}{}
    37  	}
    38  
    39  	// Go over all the outputs. If we don't have a graph node for it,
    40  	// create it. It doesn't need to depend on anything, since its just
    41  	// setting it empty.
    42  	for k, _ := range state.Outputs {
    43  		if _, ok := found[k]; ok {
    44  			continue
    45  		}
    46  
    47  		g.Add(&graphNodeOrphanOutput{OutputName: k})
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  type graphNodeOrphanOutput struct {
    54  	OutputName string
    55  }
    56  
    57  func (n *graphNodeOrphanOutput) Name() string {
    58  	return fmt.Sprintf("output.%s (orphan)", n.OutputName)
    59  }
    60  
    61  func (n *graphNodeOrphanOutput) EvalTree() EvalNode {
    62  	return &EvalOpFilter{
    63  		Ops: []walkOperation{walkApply, walkRefresh},
    64  		Node: &EvalDeleteOutput{
    65  			Name: n.OutputName,
    66  		},
    67  	}
    68  }