-
Notifications
You must be signed in to change notification settings - Fork 25
/
options.go
130 lines (110 loc) · 2.8 KB
/
options.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
package awsets
import (
ctx2 "context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
cfg "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/trek10inc/awsets/context"
)
// config is a struct that holds all the configuration values for the List method. This allows for a "functional option"
// approach to the API so it can be extended down the road without modifying the signature
type config struct {
AWSCfg *aws.Config
AccountId string
WorkerCount int
Context ctx2.Context
Regions []string
Listers []ListerName
Cache Cacher
StatusChan chan<- context.StatusUpdate
}
func (c *config) close() {
if c.StatusChan != nil {
close(c.StatusChan)
}
}
func (c *config) validate() error {
if c.AWSCfg == nil {
awsCfg, err := cfg.LoadDefaultConfig(ctx2.Background())
if err != nil {
return fmt.Errorf("failed to load AWS config: %w", err)
}
c.AWSCfg = &awsCfg
}
c.AWSCfg.Region = "us-east-1"
svc := sts.NewFromConfig(*c.AWSCfg)
res, err := svc.GetCallerIdentity(ctx2.Background(), &sts.GetCallerIdentityInput{})
if err != nil {
return fmt.Errorf("failed to get account id: %w", err)
}
c.AccountId = *res.Account
// Get Cache, default to NoOp if none is specified
if c.Cache == nil {
c.Cache = NoOpCache{}
}
// Initialize cache
err = c.Cache.Initialize(c.AccountId)
if err != nil {
return fmt.Errorf("failed to initialize cache: %w", err)
}
// Get regions, query all available if none are specified
if len(c.Regions) == 0 {
regions, err := Regions(*c.AWSCfg)
if err != nil {
return fmt.Errorf("failed to get regions: %w", err)
}
c.Regions = regions
}
// Get listers, default to all if none are specified
if len(c.Listers) == 0 {
c.Listers = Listers(nil, nil)
}
if c.Context == nil {
c.Context = ctx2.Background()
}
if c.WorkerCount == 0 {
c.WorkerCount = 10
}
return nil
}
type Option func(o *config)
// Creates new AWSets config struct with default values. It also queries AWS to get the current Account ID. Failures to
// query AWS can cause an error condition to be returned
func WithAWSConfig(awsCfg aws.Config) Option {
return func(o *config) {
o.AWSCfg = &awsCfg
}
}
func WithContext(ctx ctx2.Context) Option {
return func(o *config) {
o.Context = ctx
}
}
func WithRegions(regions []string) Option {
return func(o *config) {
o.Regions = regions
}
}
func WithListers(listers []ListerName) Option {
return func(o *config) {
o.Listers = listers
}
}
func WithCache(cache Cacher) Option {
return func(o *config) {
o.Cache = cache
}
}
func WithStatus(ch chan<- context.StatusUpdate) Option {
return func(o *config) {
o.StatusChan = ch
}
}
func WithWorkerCount(numWorkers int) Option {
return func(o *config) {
if numWorkers > 0 && numWorkers < 1000 {
o.WorkerCount = numWorkers
}
}
}