github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/views/hook_count.go (about)

     1  package views
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/zclconf/go-cty/cty"
     7  
     8  	"github.com/hashicorp/terraform/internal/addrs"
     9  	"github.com/hashicorp/terraform/internal/plans"
    10  	"github.com/hashicorp/terraform/internal/states"
    11  	"github.com/hashicorp/terraform/internal/terraform"
    12  )
    13  
    14  // countHook is a hook that counts the number of resources
    15  // added, removed, changed during the course of an apply.
    16  type countHook struct {
    17  	Added   int
    18  	Changed int
    19  	Removed int
    20  
    21  	ToAdd          int
    22  	ToChange       int
    23  	ToRemove       int
    24  	ToRemoveAndAdd int
    25  
    26  	pending map[string]plans.Action
    27  
    28  	sync.Mutex
    29  	terraform.NilHook
    30  }
    31  
    32  var _ terraform.Hook = (*countHook)(nil)
    33  
    34  func (h *countHook) Reset() {
    35  	h.Lock()
    36  	defer h.Unlock()
    37  
    38  	h.pending = nil
    39  	h.Added = 0
    40  	h.Changed = 0
    41  	h.Removed = 0
    42  }
    43  
    44  func (h *countHook) PreApply(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (terraform.HookAction, error) {
    45  	h.Lock()
    46  	defer h.Unlock()
    47  
    48  	if h.pending == nil {
    49  		h.pending = make(map[string]plans.Action)
    50  	}
    51  
    52  	h.pending[addr.String()] = action
    53  
    54  	return terraform.HookActionContinue, nil
    55  }
    56  
    57  func (h *countHook) PostApply(addr addrs.AbsResourceInstance, gen states.Generation, newState cty.Value, err error) (terraform.HookAction, error) {
    58  	h.Lock()
    59  	defer h.Unlock()
    60  
    61  	if h.pending != nil {
    62  		pendingKey := addr.String()
    63  		if action, ok := h.pending[pendingKey]; ok {
    64  			delete(h.pending, pendingKey)
    65  
    66  			if err == nil {
    67  				switch action {
    68  				case plans.CreateThenDelete, plans.DeleteThenCreate:
    69  					h.Added++
    70  					h.Removed++
    71  				case plans.Create:
    72  					h.Added++
    73  				case plans.Delete:
    74  					h.Removed++
    75  				case plans.Update:
    76  					h.Changed++
    77  				}
    78  			}
    79  		}
    80  	}
    81  
    82  	return terraform.HookActionContinue, nil
    83  }
    84  
    85  func (h *countHook) PostDiff(addr addrs.AbsResourceInstance, gen states.Generation, action plans.Action, priorState, plannedNewState cty.Value) (terraform.HookAction, error) {
    86  	h.Lock()
    87  	defer h.Unlock()
    88  
    89  	// We don't count anything for data resources
    90  	if addr.Resource.Resource.Mode == addrs.DataResourceMode {
    91  		return terraform.HookActionContinue, nil
    92  	}
    93  
    94  	switch action {
    95  	case plans.CreateThenDelete, plans.DeleteThenCreate:
    96  		h.ToRemoveAndAdd += 1
    97  	case plans.Create:
    98  		h.ToAdd += 1
    99  	case plans.Delete:
   100  		h.ToRemove += 1
   101  	case plans.Update:
   102  		h.ToChange += 1
   103  	}
   104  
   105  	return terraform.HookActionContinue, nil
   106  }