-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_client.go
112 lines (92 loc) · 2.88 KB
/
aws_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
111
112
package shutter
import (
"errors"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
)
var (
ErrInvalidAPIResponse = errors.New("AWS API response is invalid")
ErrAlreadyTerminated = errors.New("The instance has already terminated")
)
// A minimum AWS client
type AwsClient interface {
DescribeAutoscalingGroup(name string) (*autoscaling.Group, error)
DescribeInstance(instanceId string) (*ec2.Instance, error)
DescribeInstanceDetails(instanceId string) (*autoscaling.InstanceDetails, error)
CompleteLifecycleAction(instanceId string, lifecycleActionResult, lifecycleHook string) error
}
type awsClientImpl struct {
AwsClient
ec2Client *ec2.EC2
asClient *autoscaling.AutoScaling
}
func NewAwsClient(config *Config) (AwsClient, error) {
sess, err := session.NewSession(aws.NewConfig().WithRegion(config.AwsRegion))
if err != nil {
return nil, err
}
return &awsClientImpl{
ec2Client: ec2.New(sess),
asClient: autoscaling.New(sess),
}, nil
}
func (c *awsClientImpl) DescribeAutoscalingGroup(name string) (*autoscaling.Group, error) {
res, err := c.asClient.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{&name},
})
if err != nil {
return nil, err
}
if len(res.AutoScalingGroups) != 1 {
return nil, ErrInvalidAPIResponse
}
return res.AutoScalingGroups[0], nil
}
func (c *awsClientImpl) DescribeInstance(instanceId string) (*ec2.Instance, error) {
name := "instance-id"
res, err := c.ec2Client.DescribeInstances(&ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
{
Name: &name,
Values: []*string{&instanceId},
},
},
})
if err != nil {
return nil, err
}
if len(res.Reservations) != 1 || len(res.Reservations[0].Instances) != 1 {
return nil, ErrInvalidAPIResponse
}
return res.Reservations[0].Instances[0], nil
}
func (c *awsClientImpl) DescribeInstanceDetails(instanceId string) (*autoscaling.InstanceDetails, error) {
res, err := c.asClient.DescribeAutoScalingInstances(&autoscaling.DescribeAutoScalingInstancesInput{
InstanceIds: []*string{&instanceId},
})
if err != nil {
return nil, err
}
if len(res.AutoScalingInstances) != 1 {
return nil, ErrInvalidAPIResponse
}
return res.AutoScalingInstances[0], nil
}
func (c *awsClientImpl) CompleteLifecycleAction(instanceId string, lifecycleActionResult, lifecycleHook string) error {
details, err := c.DescribeInstanceDetails(instanceId)
if err != nil {
return err
}
if *details.LifecycleState == "Terminated" {
return ErrAlreadyTerminated
}
_, err = c.asClient.CompleteLifecycleAction(&autoscaling.CompleteLifecycleActionInput{
AutoScalingGroupName: details.AutoScalingGroupName,
InstanceId: &instanceId,
LifecycleActionResult: &lifecycleActionResult,
LifecycleHookName: &lifecycleHook,
})
return err
}