forked from rebuy-de/aws-nuke
/
queue.go
118 lines (99 loc) · 2.57 KB
/
queue.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package cmd
import (
"fmt"
"github.com/rebuy-de/aws-nuke/resources"
)
type ItemState int
// States of Items based on the latest request to AWS.
const (
ItemStateNew ItemState = iota
ItemStatePending
ItemStateWaiting
ItemStateFailed
ItemStateFiltered
ItemStateFinished
)
// An Item describes an actual AWS resource entity with the current state and
// some metadata.
type Item struct {
Resource resources.Resource
State ItemState
Reason string
Region Region
Type string
}
func (i *Item) Print() {
switch i.State {
case ItemStateNew:
Log(i.Region, i.Type, i.Resource, ReasonWaitPending, "would remove")
case ItemStatePending:
Log(i.Region, i.Type, i.Resource, ReasonWaitPending, "triggered remove")
case ItemStateWaiting:
Log(i.Region, i.Type, i.Resource, ReasonWaitPending, "waiting")
case ItemStateFailed:
Log(i.Region, i.Type, i.Resource, ReasonError, "failed")
case ItemStateFiltered:
Log(i.Region, i.Type, i.Resource, ReasonSkip, i.Reason)
case ItemStateFinished:
Log(i.Region, i.Type, i.Resource, ReasonSuccess, "removed")
}
}
// List gets all resource items of the same resource type like the Item.
func (i *Item) List() ([]resources.Resource, error) {
listers := resources.GetListers()
return listers[i.Type](i.Region.Session)
}
func (i *Item) GetProperty(key string) (string, error) {
if key == "" {
stringer, ok := i.Resource.(resources.LegacyStringer)
if !ok {
return "", fmt.Errorf("%T does not support legacy IDs", i.Resource)
}
return stringer.String(), nil
}
getter, ok := i.Resource.(resources.ResourcePropertyGetter)
if !ok {
return "", fmt.Errorf("%T does not support custom properties", i.Resource)
}
return getter.Properties().Get(key), nil
}
func (i *Item) Equals(o resources.Resource) bool {
iType := fmt.Sprintf("%T", i.Resource)
oType := fmt.Sprintf("%T", o)
if iType != oType {
return false
}
iStringer, iOK := i.Resource.(resources.LegacyStringer)
oStringer, oOK := o.(resources.LegacyStringer)
if iOK != oOK {
return false
}
if iOK && oOK {
return iStringer.String() == oStringer.String()
}
iGetter, iOK := i.Resource.(resources.ResourcePropertyGetter)
oGetter, oOK := o.(resources.ResourcePropertyGetter)
if iOK != oOK {
return false
}
if iOK && oOK {
return iGetter.Properties().Equals(oGetter.Properties())
}
return false
}
type Queue []*Item
func (q Queue) CountTotal() int {
return len(q)
}
func (q Queue) Count(states ...ItemState) int {
count := 0
for _, item := range q {
for _, state := range states {
if item.State == state {
count = count + 1
break
}
}
}
return count
}