github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/terraform/node_module_removed.go (about)

     1  package terraform
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"reflect"
     7  )
     8  
     9  // NodeModuleRemoved represents a module that is no longer in the
    10  // config.
    11  type NodeModuleRemoved struct {
    12  	PathValue []string
    13  }
    14  
    15  func (n *NodeModuleRemoved) Name() string {
    16  	return fmt.Sprintf("%s (removed)", modulePrefixStr(n.PathValue))
    17  }
    18  
    19  // GraphNodeSubPath
    20  func (n *NodeModuleRemoved) Path() []string {
    21  	return n.PathValue
    22  }
    23  
    24  // GraphNodeEvalable
    25  func (n *NodeModuleRemoved) EvalTree() EvalNode {
    26  	return &EvalOpFilter{
    27  		Ops: []walkOperation{walkRefresh, walkApply, walkDestroy},
    28  		Node: &EvalDeleteModule{
    29  			PathValue: n.PathValue,
    30  		},
    31  	}
    32  }
    33  
    34  func (n *NodeModuleRemoved) ReferenceGlobal() bool {
    35  	return true
    36  }
    37  
    38  func (n *NodeModuleRemoved) References() []string {
    39  	return []string{modulePrefixStr(n.PathValue)}
    40  }
    41  
    42  // EvalDeleteModule is an EvalNode implementation that removes an empty module
    43  // entry from the state.
    44  type EvalDeleteModule struct {
    45  	PathValue []string
    46  }
    47  
    48  func (n *EvalDeleteModule) Eval(ctx EvalContext) (interface{}, error) {
    49  	state, lock := ctx.State()
    50  	if state == nil {
    51  		return nil, nil
    52  	}
    53  
    54  	// Get a write lock so we can access this instance
    55  	lock.Lock()
    56  	defer lock.Unlock()
    57  
    58  	// Make sure we have a clean state
    59  	// Destroyed resources aren't deleted, they're written with an ID of "".
    60  	state.prune()
    61  
    62  	// find the module and delete it
    63  	for i, m := range state.Modules {
    64  		if reflect.DeepEqual(m.Path, n.PathValue) {
    65  			if !m.Empty() {
    66  				// a targeted apply may leave module resources even without a config,
    67  				// so just log this and return.
    68  				log.Printf("[DEBUG] cannot remove module %s, not empty", modulePrefixStr(n.PathValue))
    69  				break
    70  			}
    71  			state.Modules = append(state.Modules[:i], state.Modules[i+1:]...)
    72  			break
    73  		}
    74  	}
    75  
    76  	return nil, nil
    77  }