generated from patmizi/go-cli-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoscaling_limit.go
47 lines (40 loc) · 1.56 KB
/
autoscaling_limit.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
package chaosutil
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/patmizi/aws-chaos-cli/lib/datautil"
"github.com/pkg/errors"
"strings"
)
func LimitAutoScaling(client *autoscaling.AutoScaling, subnetsToChaos []string) (*autoscaling.Group, error) {
fmt.Print("Limit autoscaling to the remaining subnets\n")
autoscalingResponse, err := client.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: nil,
})
if err != nil {
return nil, errors.Wrap(err, "Unable to describe autoscaling groups")
}
autoScalingGroups := autoscalingResponse.AutoScalingGroups
// Find ASG that needs to be modified assuming only one ASG should be impacted
var subnetsToKeep []string
var asgName string
for _, asg := range autoScalingGroups {
asgName = *asg.AutoScalingGroupName
asgSubnets := strings.Split(*asg.VPCZoneIdentifier, ",")
subnetsToKeep = datautil.ListDiff(asgSubnets, subnetsToChaos)
correctAsg := len(subnetsToKeep) < len(asgSubnets)
if correctAsg {
vpcZoneIdentifier := strings.Join(subnetsToKeep, ",")
_, err := client.UpdateAutoScalingGroup(&autoscaling.UpdateAutoScalingGroupInput{
AutoScalingGroupName: aws.String(asgName),
VPCZoneIdentifier: aws.String(vpcZoneIdentifier),
})
if err != nil {
return nil, errors.Wrapf(err, "Unable to update autoscaling group %s", asgName)
}
return asg, nil
}
}
return nil, errors.New("Cannot find impacted ASG")
}