forked from influxdata/kapacitor
/
client.go
110 lines (91 loc) · 2.99 KB
/
client.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
package client
import (
"fmt"
//"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/pkg/errors"
)
type Config struct {
Region string
AccessKey string
SecretKey string
}
type Client interface {
Update(c Config) error
Version() (string, error)
Group(id string) (*autoscaling.DescribeAutoScalingGroupsOutput, error)
UpdateGroup(id string, desiredcapacity int64) error
}
type autoScaling struct {
client *autoscaling.AutoScaling
}
func New(c Config) (Client, error) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String(c.Region),
Credentials: credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, ""),
})
if err != nil {
fmt.Println(err)
return nil, err
}
svc := autoscaling.New(sess)
return &autoScaling{
client: svc,
}, nil
}
func (svc *autoScaling) Update(new Config) error {
return nil
}
func (svc *autoScaling) Version() (string, error) {
return "2013-10-15", nil
}
func (svc *autoScaling) Group(id string) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(id),
},
}
result, err := svc.client.DescribeAutoScalingGroups(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeInvalidNextToken:
return nil, errors.Wrapf(err, "failed to create GET request for %q", autoscaling.ErrCodeInvalidNextToken)
case autoscaling.ErrCodeResourceContentionFault:
return nil, errors.Wrapf(err, "failed to create GET request for %q", autoscaling.ErrCodeResourceContentionFault)
default:
return nil, errors.Wrapf(err, "failed to create GET request for %s", aerr.Error())
}
} else {
return nil, errors.Wrapf(err, "failed to create GET request for %s", err.Error())
}
}
return result, nil
}
func (svc *autoScaling) UpdateGroup(id string, desiredcapacity int64) error {
input := &autoscaling.SetDesiredCapacityInput{
AutoScalingGroupName: aws.String(id),
DesiredCapacity: aws.Int64(desiredcapacity),
HonorCooldown: aws.Bool(false), //Since kapacitor has the cooldown implementation, Aws implementation is explicitly set to false
}
_, err := svc.client.SetDesiredCapacity(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case autoscaling.ErrCodeScalingActivityInProgressFault:
return errors.Wrapf(err, "Failed to set desired capacity %q", autoscaling.ErrCodeScalingActivityInProgressFault)
case autoscaling.ErrCodeResourceContentionFault:
return errors.Wrapf(err, "Failed to set desired capacity %q", autoscaling.ErrCodeResourceContentionFault)
default:
return errors.Wrapf(err, "Failed to set desired capacity %s", aerr.Error())
}
} else {
return errors.Wrapf(err, "Failed to set desired capacity %s", err.Error())
}
}
return nil
}