/
asg.go
48 lines (37 loc) · 1.23 KB
/
asg.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
package services
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
)
type ASGService interface {
DescribeAutoScalingGroups(name string) (int64, error)
UpdateDesiredCapacity(name string, newCapacity int64) error
}
type asgService struct {
svc autoscalingiface.AutoScalingAPI
}
type DescribeAutoScalingGroupsOutput struct {
DesiredCapacity int64
}
func (s *asgService) DescribeAutoScalingGroups(name string) (int64, error) {
input := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{aws.String(name)},
}
resp, err := s.svc.DescribeAutoScalingGroups(input)
if len(resp.AutoScalingGroups) < 1 {
return 0, fmt.Errorf("there is no autoscaling group: %s", name)
}
return *resp.AutoScalingGroups[0].DesiredCapacity, err
}
func (s *asgService) UpdateDesiredCapacity(name string, newCapacity int64) error {
input := &autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: aws.String(name),
DesiredCapacity: aws.Int64(newCapacity),
MaxSize: aws.Int64(newCapacity),
}
_, err := s.svc.UpdateAutoScalingGroup(input)
return err
}
var _ ASGService = (*asgService)(nil)