github.com/hugorut/terraform@v1.1.3/src/terraform/transform_removed_modules.go (about)

     1  package terraform
     2  
     3  import (
     4  	"log"
     5  
     6  	"github.com/hugorut/terraform/src/addrs"
     7  	"github.com/hugorut/terraform/src/configs"
     8  	"github.com/hugorut/terraform/src/states"
     9  )
    10  
    11  // RemovedModuleTransformer implements GraphTransformer to add nodes indicating
    12  // when a module was removed from the configuration.
    13  type RemovedModuleTransformer struct {
    14  	Config *configs.Config // root node in the config tree
    15  	State  *states.State
    16  }
    17  
    18  func (t *RemovedModuleTransformer) Transform(g *Graph) error {
    19  	// nothing to remove if there's no state!
    20  	if t.State == nil {
    21  		return nil
    22  	}
    23  
    24  	removed := map[string]addrs.Module{}
    25  
    26  	for _, m := range t.State.Modules {
    27  		cc := t.Config.DescendentForInstance(m.Addr)
    28  		if cc != nil {
    29  			continue
    30  		}
    31  		removed[m.Addr.Module().String()] = m.Addr.Module()
    32  		log.Printf("[DEBUG] %s is no longer in configuration\n", m.Addr)
    33  	}
    34  
    35  	// add closers to collect any module instances we're removing
    36  	for _, modAddr := range removed {
    37  		closer := &nodeCloseModule{
    38  			Addr: modAddr,
    39  		}
    40  		g.Add(closer)
    41  	}
    42  
    43  	return nil
    44  }