2014-07-18 00:14:26 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CountHook is a hook that counts the number of resources
|
|
|
|
// added, removed, changed during the course of an apply.
|
|
|
|
type CountHook struct {
|
|
|
|
Added int
|
|
|
|
Changed int
|
|
|
|
Removed int
|
|
|
|
|
2015-06-24 12:32:15 +02:00
|
|
|
ToAdd int
|
|
|
|
ToChange int
|
|
|
|
ToRemove int
|
|
|
|
ToRemoveAndAdd int
|
|
|
|
|
2014-07-18 00:14:26 +02:00
|
|
|
pending map[string]countHookAction
|
|
|
|
|
|
|
|
sync.Mutex
|
|
|
|
terraform.NilHook
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CountHook) Reset() {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
h.pending = nil
|
|
|
|
h.Added = 0
|
|
|
|
h.Changed = 0
|
|
|
|
h.Removed = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CountHook) PreApply(
|
2014-09-25 23:52:06 +02:00
|
|
|
n *terraform.InstanceInfo,
|
2014-09-17 20:15:07 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-09-18 01:33:24 +02:00
|
|
|
d *terraform.InstanceDiff) (terraform.HookAction, error) {
|
2014-07-18 00:14:26 +02:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.pending == nil {
|
|
|
|
h.pending = make(map[string]countHookAction)
|
|
|
|
}
|
|
|
|
|
|
|
|
action := countHookActionChange
|
|
|
|
if d.Destroy {
|
|
|
|
action = countHookActionRemove
|
|
|
|
} else if s.ID == "" {
|
|
|
|
action = countHookActionAdd
|
|
|
|
}
|
|
|
|
|
2014-09-25 23:52:06 +02:00
|
|
|
h.pending[n.HumanId()] = action
|
2014-07-18 00:14:26 +02:00
|
|
|
|
|
|
|
return terraform.HookActionContinue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *CountHook) PostApply(
|
2014-09-25 23:52:06 +02:00
|
|
|
n *terraform.InstanceInfo,
|
2014-09-17 20:15:07 +02:00
|
|
|
s *terraform.InstanceState,
|
2014-07-18 00:38:50 +02:00
|
|
|
e error) (terraform.HookAction, error) {
|
2014-07-18 00:14:26 +02:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
if h.pending != nil {
|
2014-09-25 23:52:06 +02:00
|
|
|
if a, ok := h.pending[n.HumanId()]; ok {
|
|
|
|
delete(h.pending, n.HumanId())
|
2014-07-18 00:38:50 +02:00
|
|
|
|
|
|
|
if e == nil {
|
|
|
|
switch a {
|
|
|
|
case countHookActionAdd:
|
|
|
|
h.Added += 1
|
|
|
|
case countHookActionChange:
|
|
|
|
h.Changed += 1
|
|
|
|
case countHookActionRemove:
|
|
|
|
h.Removed += 1
|
|
|
|
}
|
2014-07-18 00:14:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.HookActionContinue, nil
|
|
|
|
}
|
2015-06-24 12:32:15 +02:00
|
|
|
|
|
|
|
func (h *CountHook) PostDiff(
|
|
|
|
n *terraform.InstanceInfo, d *terraform.InstanceDiff) (
|
|
|
|
terraform.HookAction, error) {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
|
|
|
|
switch d.ChangeType() {
|
|
|
|
case terraform.DiffDestroyCreate:
|
|
|
|
h.ToRemoveAndAdd += 1
|
|
|
|
case terraform.DiffCreate:
|
|
|
|
h.ToAdd += 1
|
|
|
|
case terraform.DiffDestroy:
|
|
|
|
h.ToRemove += 1
|
2015-07-03 12:26:38 +02:00
|
|
|
case terraform.DiffUpdate:
|
2015-06-24 12:32:15 +02:00
|
|
|
h.ToChange += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
return terraform.HookActionContinue, nil
|
|
|
|
}
|