forked from juju/juju
/
api.go
66 lines (57 loc) · 1.87 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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package ecs
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecs"
"github.com/aws/aws-sdk-go/service/ecs/ecsiface"
"github.com/juju/errors"
"github.com/DavinZhang/juju/environs/cloudspec"
)
type awsLogger struct {
session *session.Session
}
func (l awsLogger) Log(args ...interface{}) {
logger.Tracef("awsLogger %p: %s", l.session, fmt.Sprint(args...))
}
func getDefaultRetryer() client.DefaultRetryer {
return client.DefaultRetryer{
NumMaxRetries: 10,
MinRetryDelay: time.Second,
MinThrottleDelay: time.Second,
MaxRetryDelay: time.Minute,
MaxThrottleDelay: time.Minute,
}
}
func cloudSpecToAWSConfig(c cloudspec.CloudSpec) (*aws.Config, error) {
if err := c.Validate(); err != nil {
return nil, errors.Trace(err)
}
if err := validateCloudCredential(c.Credential); err != nil {
return nil, errors.Trace(err)
}
credentialAttr := c.Credential.Attributes()
return &aws.Config{
Retryer: getDefaultRetryer(),
Region: aws.String(credentialAttr[credAttrRegionKey]),
Credentials: credentials.NewStaticCredentialsFromCreds(credentials.Value{
AccessKeyID: credentialAttr[credAttrAccessKey],
SecretAccessKey: credentialAttr[credAttrSecretKey],
}),
}, nil
}
func newECSClient(config *aws.Config) (ecsiface.ECSAPI, error) {
s := session.Must(session.NewSession())
// Enable request and response logging, but only if TRACE is enabled (as
// they're probably fairly expensive to produce).
if logger.IsTraceEnabled() {
config.Logger = awsLogger{s}
config.LogLevel = aws.LogLevel(aws.LogDebug | aws.LogDebugWithRequestErrors | aws.LogDebugWithRequestRetries)
}
return ecs.New(s, config), nil
}