-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.go
67 lines (53 loc) · 1.73 KB
/
aws.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
package clon
import (
"regexp"
"strings"
"github.com/juju/errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/cloudformation/cloudformationiface"
"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"
)
type awsClient struct {
sess *session.Session
s3conn s3iface.S3API
cfnconn cloudformationiface.CloudFormationAPI
accountID string
region string
sessionName string
}
func newAWSClient() (a *awsClient, err error) {
a = &awsClient{}
if a.sess, err = session.NewSession(); err != nil {
return nil, errors.Annotatef(err, "cannot create awsClient")
}
a.s3conn = s3.New(a.sess)
cfnconn := cloudformation.New(a.sess)
cfnconn.Handlers.Retry.PushBack(func(r *request.Request) {
if r.Operation.Name == "DescribeStackEvents" || r.Operation.Name == "DescribeStacks" {
if e, ok := r.Error.(awserr.Error); ok && e.Code() == "Throttling" && strings.Contains(e.Message(), "Rate exceeded") {
r.Retryable = aws.Bool(true)
}
}
})
a.cfnconn = cfnconn
stsConn := sts.New(a.sess)
out, err := stsConn.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
return nil, errors.Annotatef(err, "cannot identify user identity")
}
identityArn, err := arn.Parse(aws.StringValue(out.Arn))
if err != nil {
return nil, err
}
a.accountID = identityArn.AccountID
a.sessionName = regexp.MustCompile("[^A-Za-z0-9-]").ReplaceAllString(identityArn.Resource, "-")
a.region = aws.StringValue(a.sess.Config.Region)
return
}