-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathstats.go
80 lines (65 loc) · 1.74 KB
/
stats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package terranova
import (
"fmt"
"github.com/hashicorp/terraform/addrs"
"github.com/hashicorp/terraform/backend/local"
"github.com/hashicorp/terraform/plans"
)
// Stats encapsulate the statistics of changes to apply or applied
type Stats struct {
Add, Change, Destroy int
fromPlan bool
}
// NewStats creates an empty stats
func NewStats() *Stats {
return &Stats{}
}
// Reset resets, set everything to zeros, the current stats
func (s *Stats) Reset() {
s.Add, s.Change, s.Destroy = 0, 0, 0
}
// FromPlan return stats from a given plan
func (s *Stats) FromPlan(plan *plans.Plan) *Stats {
s.Reset()
s.fromPlan = true
if plan == nil || plan.Changes == nil || plan.Changes.Empty() {
return s
}
for _, r := range plan.Changes.Resources {
// Do not count data resources
if r.Addr.Resource.Resource.Mode == addrs.DataResourceMode {
continue
}
switch r.Action {
case plans.Create:
s.Add++
case plans.Update:
s.Change++
case plans.DeleteThenCreate, plans.CreateThenDelete:
s.Add++
s.Destroy++
case plans.Delete:
s.Destroy++
}
}
return s
}
// FromCountHook return stats from a given count hook
func (s *Stats) FromCountHook(countHook *local.CountHook) *Stats {
if countHook == nil {
return s
}
s.Add, s.Change, s.Destroy = countHook.Added, countHook.Changed, countHook.Removed
s.fromPlan = false
return s
}
func (s *Stats) String() string {
if s.fromPlan {
return fmt.Sprintf("resources: %d to add, %d to change, %d to destroy", s.Add, s.Change, s.Destroy)
}
return fmt.Sprintf("resources: %d added, %d changed, %d destroyed", s.Add, s.Change, s.Destroy)
}
// Stats return the current status from the count hook
func (p *Platform) Stats() *Stats {
return NewStats().FromCountHook(p.countHook)
}