-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
411 lines (354 loc) · 14.6 KB
/
api.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/******************************************************************************
Cloud Resource Counter
File: api.go
Summary: ServiceFactory, abstract services and the AWS Service Factory implementation.
******************************************************************************/
package main
import (
"fmt"
"io"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/eks/eksiface"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
"github.com/aws/aws-sdk-go/service/lightsail"
"github.com/aws/aws-sdk-go/service/lightsail/lightsailiface"
"github.com/aws/aws-sdk-go/service/rds"
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/aws/aws-sdk-go/service/sts/stsiface"
)
// DefaultRegion is used if the caller does not supply a region
// on the command line or the profile does not have a default
// region associated with it.
const DefaultRegion = "us-east-1"
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Abstract Services (hides details of Cloud Provider API)
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// AccountIDService is a struct that knows how get the AWS
// Account ID using an object that implements the Security
// Token Service API interface.
type AccountIDService struct {
Client stsiface.STSAPI
}
// Account uses the supplied AccountIDService to invoke
// the associated GetCallerIdentity method on the struct's
// Client object.
func (aids *AccountIDService) Account() (string, error) {
// Construct the input parameter
input := &sts.GetCallerIdentityInput{}
// Get the caller's identity
result, err := aids.Client.GetCallerIdentity(input)
if err != nil {
return "", err
}
return *result.Account, nil
}
// EC2InstanceService is a struct that knows how to get the
// descriptions of all EC2 instances as well as accessbile
// regions using an object that implements the Elastic
// Compute Cloud API interface.
type EC2InstanceService struct {
Client ec2iface.EC2API
}
// InspectInstances takes an input filter specification (for the types of instances)
// and a function to evaluate a DescribeInstanceOutput struct. The supplied function
// can determine when to stop iterating through EC2 instances.
func (ec2i *EC2InstanceService) InspectInstances(input *ec2.DescribeInstancesInput,
fn func(*ec2.DescribeInstancesOutput, bool) bool) error {
return ec2i.Client.DescribeInstancesPages(input, fn)
}
// GetRegions returns the list of available regions for EC2 instances based on the
// set of input parameters.
func (ec2i *EC2InstanceService) GetRegions(input *ec2.DescribeRegionsInput) (*ec2.DescribeRegionsOutput, error) {
return ec2i.Client.DescribeRegions(input)
}
// InspectVolumes takes an input filter specification (for the types of volumes)
// and a function to evalatuate a DescribeVolumesOutput struct. The supplied function
// can determine when to stop iterating through EBS volumes.
func (ec2i *EC2InstanceService) InspectVolumes(input *ec2.DescribeVolumesInput,
fn func(*ec2.DescribeVolumesOutput, bool) bool) error {
return ec2i.Client.DescribeVolumesPages(input, fn)
}
// RDSInstanceService is a struct that knows how to get the
// descriptions of all RDS instances using an object that
// implements the Relational Database Service API interface.
type RDSInstanceService struct {
Client rdsiface.RDSAPI
}
// InspectInstances takes an input filter specification (for the types of instances)
// and a function to evaluate a DescribeDBInstancesOutput struct. The supplied function
// can determine when to stop iterating through RDS instances.
func (rdsis *RDSInstanceService) InspectInstances(input *rds.DescribeDBInstancesInput,
fn func(*rds.DescribeDBInstancesOutput, bool) bool) error {
return rdsis.Client.DescribeDBInstancesPages(input, fn)
}
// S3Service is a struct that knows how to get all of the S3 buckets using an object
// that implements the Simple Storage Service API interface.
type S3Service struct {
Client s3iface.S3API
}
// ListBuckets takes an input filter specification (for the types of S3 buckets) and
// returns a ListBucketsOutput struct.
func (s3s *S3Service) ListBuckets(input *s3.ListBucketsInput) (*s3.ListBucketsOutput, error) {
return s3s.Client.ListBuckets(input)
}
// LambdaService is a struct that knows how to get all of the Lambda functions using
// an object that implements the Lambda API interface
type LambdaService struct {
Client lambdaiface.LambdaAPI
}
// ListFunctions takes an input structure to identify specific lambda functions along
// with a function which is supplied a "page" of lambda functions.
func (ls *LambdaService) ListFunctions(input *lambda.ListFunctionsInput,
fn func(*lambda.ListFunctionsOutput, bool) bool) error {
return ls.Client.ListFunctionsPages(input, fn)
}
// ContainerService is a struct that knows how to get a list of all task definition
// and get a description of each one.
type ContainerService struct {
Client ecsiface.ECSAPI
}
// ListTaskDefinitions takes an input specification (ListTaskDefinitionsInput) and
// a function that is invoked for each page of results (ListTaskDefinitionsOutput).
// This allows a caller to obtain a list of all task definitions.
func (cs *ContainerService) ListTaskDefinitions(input *ecs.ListTaskDefinitionsInput,
fn func(output *ecs.ListTaskDefinitionsOutput, lastPage bool) bool) error {
return cs.Client.ListTaskDefinitionsPages(input, fn)
}
// InspectTaskDefinition takes an input specification (DescribeTaskDefinitionInput)
// that describes a single task definition and returns information about it.
func (cs *ContainerService) InspectTaskDefinition(input *ecs.DescribeTaskDefinitionInput) (*ecs.DescribeTaskDefinitionOutput, error) {
return cs.Client.DescribeTaskDefinition(input)
}
// LightsailService is a struct that knows how to get a list of all Lightsail
// instances and availble regions.
type LightsailService struct {
Client lightsailiface.LightsailAPI
}
// GetRegions returns a list of available regions for Lightsail instances
func (lss *LightsailService) GetRegions(input *lightsail.GetRegionsInput) (*lightsail.GetRegionsOutput, error) {
return lss.Client.GetRegions(input)
}
// InspectInstances returns a full description of all Lightsail instances.
func (lss *LightsailService) InspectInstances(input *lightsail.GetInstancesInput) (*lightsail.GetInstancesOutput, error) {
return lss.Client.GetInstances(input)
}
// EKSService is a struct that knows how to get a list of all EKS clusters and
// describes the clusters
type EKSService struct {
Client eksiface.EKSAPI
}
// ListClusters takes an input filter specification and a function
// to evaluate a ListClustersOutput struct. The supplied function
// can determine when to stop iterating through EKS clusters.
func (eksi *EKSService) ListClusters(input *eks.ListClustersInput,
fn func(*eks.ListClustersOutput, bool) bool) error {
return eksi.Client.ListClustersPages(input, fn)
}
// ListNodeGroups takes an input filter specification and a function
// to evaluate a ListNodeGroupsOutput struct. The supplied function
// can determine when to stop iterating through Nodegroups.
func (eksi *EKSService) ListNodeGroups(input *eks.ListNodegroupsInput,
fn func(*eks.ListNodegroupsOutput, bool) bool) error {
return eksi.Client.ListNodegroupsPages(input, fn)
}
// DescribeNodegroups returns a full description of a Nodegroup
func (eksi *EKSService) DescribeNodegroups(input *eks.DescribeNodegroupInput) (*eks.DescribeNodegroupOutput, error) {
return eksi.Client.DescribeNodegroup(input)
}
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Abstract Service Factory (provides access to all Abstract Services)
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// ServiceFactory is the generic interface for our Cloud Service provider.
type ServiceFactory interface {
Init()
GetCurrentRegion() string
GetAccountIDService() *AccountIDService
GetEC2InstanceService(string) *EC2InstanceService
GetEKSService(string) *EKSService
GetRDSInstanceService(string) *RDSInstanceService
GetS3Service() *S3Service
GetLambdaService(string) *LambdaService
GetContainerService(string) *ContainerService
GetLightsailService(string) *LightsailService
}
// AWSServiceFactory is a struct that holds a reference to
// an actual AWS Session object (pointer) and uses it to return
// other specialized services, such as the AccountIDService.
// It also accepts a profile name, overriding region and file
// to use to send trace information.
type AWSServiceFactory struct {
Session *session.Session
ProfileName string
RegionName string
TraceWriter io.Writer
UseSSO bool
}
// Init initializes the AWS service factory by creating an
// initial AWS Session object (pointer). It inspects the profiles
// in the current user's directories and prepares the session for
// tracing (if requested).
func (awssf *AWSServiceFactory) Init() {
config := &aws.Config{}
// Was a region specified by the user?
if awssf.RegionName != "" {
// Add it to the configuration
config = config.WithRegion(awssf.RegionName)
}
// Was tracing specified by the user?
if awssf.TraceWriter != nil {
// Enable logging of AWS Calls with Body
config = config.WithLogLevel(aws.LogDebugWithHTTPBody)
// Enable a logger function which writes to the Trace file
config = config.WithLogger(aws.LoggerFunc(func(args ...interface{}) {
fmt.Fprintln(awssf.TraceWriter, args...)
}))
}
// Construct our session Options object
options := session.Options{
Config: *config,
}
// options to set if using SSO
if awssf.UseSSO {
options.SharedConfigState = session.SharedConfigEnable
options.Profile = awssf.ProfileName
} else {
// Create an initial configuration object which defines our chain
// of credentials providers: first, honor a supplied profile name,
// if that fails, look for the environment variables.
options.Config.Credentials = credentials.NewChainCredentials(
[]credentials.Provider{
&credentials.SharedCredentialsProvider{
Profile: awssf.ProfileName,
},
&credentials.EnvProvider{},
},
)
}
// Ensure that we have a session
sess := session.Must(session.NewSessionWithOptions(options))
// Does this session have a region? If not, use the default region
if *sess.Config.Region == "" {
sess = sess.Copy(&aws.Config{Region: aws.String(DefaultRegion)})
}
// Store the session in our struct
awssf.Session = sess
}
// GetCurrentRegion returns the name of the current region.
func (awssf *AWSServiceFactory) GetCurrentRegion() string {
return *awssf.Session.Config.Region
}
// GetAccountIDService returns an instance of an AccountIDService associated
// with our session.
func (awssf *AWSServiceFactory) GetAccountIDService() *AccountIDService {
return &AccountIDService{
Client: sts.New(awssf.Session),
}
}
// GetEC2InstanceService returns an instance of an EC2InstanceService associated
// with our session. The caller can supply an optional region name to contruct
// an instance associated with that region.
func (awssf *AWSServiceFactory) GetEC2InstanceService(regionName string) *EC2InstanceService {
// Construct our service client
var client ec2iface.EC2API
if regionName == "" {
client = ec2.New(awssf.Session)
} else {
client = ec2.New(awssf.Session, aws.NewConfig().WithRegion(regionName))
}
return &EC2InstanceService{
Client: client,
}
}
// GetRDSInstanceService returns an instance of an RDSInstanceService associated
// with our session. The caller can supply an optional region name to construct
// an instance associated with that region.
func (awssf *AWSServiceFactory) GetRDSInstanceService(regionName string) *RDSInstanceService {
// Construct our service client
var client rdsiface.RDSAPI
if regionName == "" {
client = rds.New(awssf.Session)
} else {
client = rds.New(awssf.Session, aws.NewConfig().WithRegion(regionName))
}
return &RDSInstanceService{
Client: client,
}
}
// GetS3Service returns an instance of an S3Service associated with the current session.
// There is currently no way to accept a different region name.
func (awssf *AWSServiceFactory) GetS3Service() *S3Service {
return &S3Service{
Client: s3.New(awssf.Session),
}
}
// GetLambdaService returns an instance of a LambdaService associated with the our session.
// The caller can supply an optional region name to construct an instance associated with
// that region.
func (awssf *AWSServiceFactory) GetLambdaService(regionName string) *LambdaService {
// Construct our service client
var client lambdaiface.LambdaAPI
if regionName == "" {
client = lambda.New(awssf.Session)
} else {
client = lambda.New(awssf.Session, aws.NewConfig().WithRegion(regionName))
}
return &LambdaService{
Client: client,
}
}
// GetContainerService returns an instance of a ContainerService associated with our session.
// The caller can supply an optional region name to construct an instance associated with
// that region.
func (awssf *AWSServiceFactory) GetContainerService(regionName string) *ContainerService {
// Construct our service client
var client ecsiface.ECSAPI
if regionName == "" {
client = ecs.New(awssf.Session)
} else {
client = ecs.New(awssf.Session, aws.NewConfig().WithRegion(regionName))
}
return &ContainerService{
Client: client,
}
}
// GetLightsailService returns an instance of a LightsailService associated with our session.
// The caller can supply an optional region name to construct an instance associated with
// that region.
func (awssf *AWSServiceFactory) GetLightsailService(regionName string) *LightsailService {
// Construct our service client
var client lightsailiface.LightsailAPI
if regionName == "" {
client = lightsail.New(awssf.Session)
} else {
client = lightsail.New(awssf.Session, aws.NewConfig().WithRegion(regionName))
}
return &LightsailService{
Client: client,
}
}
// GetEKSService returns an instance of an EKSService associated
// with our session. The caller can supply an optional region name to contruct
// an instance associated with that region.
func (awssf *AWSServiceFactory) GetEKSService(regionName string) *EKSService {
// Construct our service client
var client eksiface.EKSAPI
if regionName == "" {
client = eks.New(awssf.Session)
} else {
client = eks.New(awssf.Session, aws.NewConfig().WithRegion(regionName))
}
return &EKSService{
Client: client,
}
}