github.com/rebelkathi/terraform-@v0.11.12-beta1/backend/local/hook_count.go (about)

     1  package local
     2  
     3  import (
     4  	"strings"
     5  	"sync"
     6  
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  // CountHook is a hook that counts the number of resources
    11  // added, removed, changed during the course of an apply.
    12  type CountHook struct {
    13  	Added   int
    14  	Changed int
    15  	Removed int
    16  
    17  	ToAdd          int
    18  	ToChange       int
    19  	ToRemove       int
    20  	ToRemoveAndAdd int
    21  
    22  	pending map[string]countHookAction
    23  
    24  	sync.Mutex
    25  	terraform.NilHook
    26  }
    27  
    28  func (h *CountHook) Reset() {
    29  	h.Lock()
    30  	defer h.Unlock()
    31  
    32  	h.pending = nil
    33  	h.Added = 0
    34  	h.Changed = 0
    35  	h.Removed = 0
    36  }
    37  
    38  func (h *CountHook) PreApply(
    39  	n *terraform.InstanceInfo,
    40  	s *terraform.InstanceState,
    41  	d *terraform.InstanceDiff) (terraform.HookAction, error) {
    42  	h.Lock()
    43  	defer h.Unlock()
    44  
    45  	if d.Empty() {
    46  		return terraform.HookActionContinue, nil
    47  	}
    48  
    49  	if h.pending == nil {
    50  		h.pending = make(map[string]countHookAction)
    51  	}
    52  
    53  	action := countHookActionChange
    54  	if d.GetDestroy() {
    55  		action = countHookActionRemove
    56  	} else if s.ID == "" {
    57  		action = countHookActionAdd
    58  	}
    59  
    60  	h.pending[n.HumanId()] = action
    61  
    62  	return terraform.HookActionContinue, nil
    63  }
    64  
    65  func (h *CountHook) PostApply(
    66  	n *terraform.InstanceInfo,
    67  	s *terraform.InstanceState,
    68  	e error) (terraform.HookAction, error) {
    69  	h.Lock()
    70  	defer h.Unlock()
    71  
    72  	if h.pending != nil {
    73  		if a, ok := h.pending[n.HumanId()]; ok {
    74  			delete(h.pending, n.HumanId())
    75  
    76  			if e == nil {
    77  				switch a {
    78  				case countHookActionAdd:
    79  					h.Added += 1
    80  				case countHookActionChange:
    81  					h.Changed += 1
    82  				case countHookActionRemove:
    83  					h.Removed += 1
    84  				}
    85  			}
    86  		}
    87  	}
    88  
    89  	return terraform.HookActionContinue, nil
    90  }
    91  
    92  func (h *CountHook) PostDiff(
    93  	n *terraform.InstanceInfo, d *terraform.InstanceDiff) (
    94  	terraform.HookAction, error) {
    95  	h.Lock()
    96  	defer h.Unlock()
    97  
    98  	// We don't count anything for data sources
    99  	if strings.HasPrefix(n.Id, "data.") {
   100  		return terraform.HookActionContinue, nil
   101  	}
   102  
   103  	switch d.ChangeType() {
   104  	case terraform.DiffDestroyCreate:
   105  		h.ToRemoveAndAdd += 1
   106  	case terraform.DiffCreate:
   107  		h.ToAdd += 1
   108  	case terraform.DiffDestroy:
   109  		h.ToRemove += 1
   110  	case terraform.DiffUpdate:
   111  		h.ToChange += 1
   112  	}
   113  
   114  	return terraform.HookActionContinue, nil
   115  }