github.com/ctrox/terraform@v0.11.12-beta1/terraform/transform_orphan_output.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hashicorp/terraform/config"
     7  	"github.com/hashicorp/terraform/config/module"
     8  )
     9  
    10  // OrphanOutputTransformer finds the outputs that aren't present
    11  // in the given config that are in the state and adds them to the graph
    12  // for deletion.
    13  type OrphanOutputTransformer struct {
    14  	Module *module.Tree // Root module
    15  	State  *State       // State is the root state
    16  }
    17  
    18  func (t *OrphanOutputTransformer) Transform(g *Graph) error {
    19  	if t.State == nil {
    20  		log.Printf("[DEBUG] No state, no orphan outputs")
    21  		return nil
    22  	}
    23  
    24  	for _, ms := range t.State.Modules {
    25  		if err := t.transform(g, ms); err != nil {
    26  			return err
    27  		}
    28  	}
    29  	return nil
    30  }
    31  
    32  func (t *OrphanOutputTransformer) transform(g *Graph, ms *ModuleState) error {
    33  	if ms == nil {
    34  		return nil
    35  	}
    36  
    37  	path := normalizeModulePath(ms.Path)
    38  
    39  	// Get the config for this path, which is nil if the entire module has been
    40  	// removed.
    41  	var c *config.Config
    42  	if m := t.Module.Child(path[1:]); m != nil {
    43  		c = m.Config()
    44  	}
    45  
    46  	// add all the orphaned outputs to the graph
    47  	for _, n := range ms.RemovedOutputs(c) {
    48  		g.Add(&NodeOutputOrphan{OutputName: n, PathValue: path})
    49  
    50  	}
    51  
    52  	return nil
    53  }