-
Notifications
You must be signed in to change notification settings - Fork 0
/
snapshots.go
70 lines (65 loc) · 2.23 KB
/
snapshots.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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// FetchAWSData represents the set of methods used to interact with AWS API.
type FetchAWSData interface {
GetSnapshots(awsFilters []Filters) (*ec2.DescribeSnapshotsOutput, error)
}
// NewEC2Client creates an AWS Session and returns an initialized client with the session object embedded.
func (hub *Hub) NewEC2Client(awsCreds *AWSCreds) (*EC2Client, error) {
// Initialize default config.
config := &aws.Config{
Region: aws.String(awsCreds.Region),
}
// Override Access Key and Secret Key env vars if specified in config.
if awsCreds.AccessKey != "" && awsCreds.SecretKey != "" {
config.Credentials = credentials.NewStaticCredentials(awsCreds.AccessKey, awsCreds.SecretKey, "")
}
// Initialize session with custom config embedded.
sess, err := session.NewSessionWithOptions(session.Options{
Config: *config,
})
if err != nil {
hub.logger.Errorf("Error creating AWS Session %s", err)
return nil, fmt.Errorf("could not create aws session")
}
// Initialize EC2 Client.
var ec2Client *ec2.EC2
if awsCreds.RoleARN != "" {
// Assume Role if specified
hub.logger.Debugf("Assuming Role: %v", awsCreds.RoleARN)
creds := stscreds.NewCredentials(sess, awsCreds.RoleARN)
ec2Client = ec2.New(sess, &aws.Config{Credentials: creds})
} else {
ec2Client = ec2.New(sess)
}
return &EC2Client{
client: ec2Client,
}, nil
}
// GetSnapshots takes awsFilters as input and returns the API response of `DescribeSnapshots` API Call.
func (e *EC2Client) GetSnapshots(awsFilters []Filters) (*ec2.DescribeSnapshotsOutput, error) {
filters := make([]*ec2.Filter, 0, len(awsFilters))
for _, tag := range awsFilters {
filters = append(filters, &ec2.Filter{
Name: aws.String(tag.Name),
Values: []*string{aws.String(tag.Value)},
})
}
// Construct request params for the API Request.
params := &ec2.DescribeSnapshotsInput{}
if len(filters) != 0 {
params = &ec2.DescribeSnapshotsInput{Filters: filters}
}
resp, err := e.client.DescribeSnapshots(params)
if err != nil {
return nil, err
}
return resp, nil
}