/
iterators.go
153 lines (146 loc) · 4.62 KB
/
iterators.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package awsutil
import (
"errors"
"github.com/Cloud-Foundations/Dominator/lib/log"
"github.com/Cloud-Foundations/Dominator/lib/log/prefixlogger"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
type resultsType struct {
numTargets int
err error
}
func forEachTarget(targets TargetList, skipList TargetList,
targetFunc func(awsService *ec2.EC2, accountName, regionName string,
logger log.Logger),
logger log.Logger) (int, error) {
cs, err := LoadCredentials()
if err != nil {
return 0, err
}
return cs.ForEachEC2Target(targets, skipList, targetFunc, false, logger)
}
func (cs *CredentialsStore) forEachEC2Target(targets TargetList,
skipList TargetList,
targetFunc func(awsService *ec2.EC2, accountName, regionName string,
logger log.Logger),
wait bool, logger log.Logger) (int, error) {
return cs.ForEachTarget(targets, skipList,
func(awsSession *session.Session, accountName, regionName string,
logger log.Logger) {
targetFunc(cs.GetEC2Service(accountName, regionName), accountName,
regionName, logger)
},
wait, logger)
}
func (cs *CredentialsStore) forEachTarget(targets TargetList,
skipList TargetList,
targetFunc func(awsSession *session.Session, accountName, regionName string,
logger log.Logger),
wait bool, logger log.Logger) (int, error) {
if len(targets) < 1 { // Full wildcard.
targets = make(TargetList, 1)
}
accountMap := make(map[string][]string) // Key: accountName, value: regions.
skipTargets := make(map[Target]struct{})
for _, target := range skipList {
if target.AccountName != "" || target.Region != "" {
skipTargets[Target{target.AccountName, target.Region}] = struct{}{}
}
}
// Expand any wildcard account names.
for _, target := range targets {
if target.AccountName == "" {
for _, accountName := range cs.ListAccountsWithCredentials() {
regions := accountMap[accountName]
regions = append(regions, target.Region)
accountMap[accountName] = regions
}
} else {
regions := accountMap[target.AccountName]
regions = append(regions, target.Region)
accountMap[target.AccountName] = regions
}
}
// Verify we have credentials for all accounts.
for accountName := range accountMap {
if cs.GetSessionForAccount(accountName) == nil {
return 0, errors.New("no session for account: " + accountName)
}
}
accountResultsChannel := make(chan resultsType, 1)
var numTargets int
var waitChannel chan struct{}
if wait {
waitChannel = make(chan struct{}, 1)
}
for accountName, regions := range accountMap {
// Remove duplicate/redundant regions.
regionMap := make(map[string]struct{})
for _, region := range regions {
if region == "" {
regionMap = nil
break
}
regionMap[region] = struct{}{}
}
regionList := make([]string, 0, len(regionMap))
for region := range regionMap {
regionList = append(regionList, region)
}
awsSession := cs.GetSessionForAccount(accountName)
go cs.forEachRegionInAccount(awsSession, accountName, regionList,
accountResultsChannel, skipTargets, targetFunc, waitChannel, logger)
}
var firstError error
// Collect account results.
for range accountMap {
result := <-accountResultsChannel
if result.err != nil && firstError == nil {
firstError = result.err
}
numTargets += result.numTargets
}
if waitChannel != nil {
for count := 0; count < numTargets; count++ {
<-waitChannel
}
}
return numTargets, firstError
}
func (cs *CredentialsStore) forEachRegionInAccount(awsSession *session.Session,
accountName string, regions []string,
resultsChannel chan<- resultsType, skipTargets map[Target]struct{},
targetFunc func(*session.Session, string, string, log.Logger),
waitChannel chan<- struct{}, logger log.Logger) {
if len(regions) < 1 {
regions = cs.ListRegionsForAccount(accountName)
}
if _, ok := skipTargets[Target{accountName, ""}]; ok {
logger.Println(accountName + ": skipping account")
resultsChannel <- resultsType{0, nil}
return
}
// Start goroutine for each target ((account,region) tuple).
numRegions := 0
for _, region := range regions {
logger := prefixlogger.New(accountName+": "+region+": ", logger)
if _, ok := skipTargets[Target{accountName, region}]; ok {
logger.Println("skipping target")
continue
}
if _, ok := skipTargets[Target{"", region}]; ok {
logger.Println("skipping region")
continue
}
go func(awsSession *session.Session, accountName, regionName string,
logger log.Logger) {
targetFunc(awsSession, accountName, regionName, logger)
if waitChannel != nil {
waitChannel <- struct{}{}
}
}(awsSession, accountName, region, logger)
numRegions++
}
resultsChannel <- resultsType{numRegions, nil}
}