forked from gruntwork-io/terratest
/
asg.go
145 lines (128 loc) · 4.13 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package aws
import (
"fmt"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/stretchr/testify/require"
"github.com/gruntwork-io/terratest/modules/logger"
"github.com/gruntwork-io/terratest/modules/retry"
)
type AsgCapacityInfo struct {
MinCapacity int64
MaxCapacity int64
CurrentCapacity int64
DesiredCapacity int64
}
// GetCapacityInfoForAsg returns the capacity info for the queried asg as a struct, AsgCapacityInfo.
func GetCapacityInfoForAsg(t *testing.T, asgName string, awsRegion string) AsgCapacityInfo {
capacityInfo, err := GetCapacityInfoForAsgE(t, asgName, awsRegion)
require.NoError(t, err)
return capacityInfo
}
// GetCapacityInfoForAsgE returns the capacity info for the queried asg as a struct, AsgCapacityInfo.
func GetCapacityInfoForAsgE(t *testing.T, asgName string, awsRegion string) (AsgCapacityInfo, error) {
asgClient, err := NewAsgClientE(t, awsRegion)
if err != nil {
return AsgCapacityInfo{}, err
}
input := autoscaling.DescribeAutoScalingGroupsInput{AutoScalingGroupNames: []*string{aws.String(asgName)}}
output, err := asgClient.DescribeAutoScalingGroups(&input)
if err != nil {
return AsgCapacityInfo{}, err
}
groups := output.AutoScalingGroups
if len(groups) == 0 {
return AsgCapacityInfo{}, NewNotFoundError("ASG", asgName, awsRegion)
}
capacityInfo := AsgCapacityInfo{
MinCapacity: *groups[0].MinSize,
MaxCapacity: *groups[0].MaxSize,
DesiredCapacity: *groups[0].DesiredCapacity,
CurrentCapacity: int64(len(groups[0].Instances)),
}
return capacityInfo, nil
}
// GetInstanceIdsForAsg gets the IDs of EC2 Instances in the given ASG.
func GetInstanceIdsForAsg(t *testing.T, asgName string, awsRegion string) []string {
ids, err := GetInstanceIdsForAsgE(t, asgName, awsRegion)
if err != nil {
t.Fatal(err)
}
return ids
}
// GetInstanceIdsForAsgE gets the IDs of EC2 Instances in the given ASG.
func GetInstanceIdsForAsgE(t *testing.T, asgName string, awsRegion string) ([]string, error) {
asgClient, err := NewAsgClientE(t, awsRegion)
if err != nil {
return nil, err
}
input := autoscaling.DescribeAutoScalingGroupsInput{AutoScalingGroupNames: []*string{aws.String(asgName)}}
output, err := asgClient.DescribeAutoScalingGroups(&input)
if err != nil {
return nil, err
}
instanceIDs := []string{}
for _, asg := range output.AutoScalingGroups {
for _, instance := range asg.Instances {
instanceIDs = append(instanceIDs, aws.StringValue(instance.InstanceId))
}
}
return instanceIDs, nil
}
// WaitForCapacity waits for the currently set desired capacity to be reached on the ASG
func WaitForCapacity(
t *testing.T,
asgName string,
region string,
maxRetries int,
sleepBetweenRetries time.Duration,
) {
err := WaitForCapacityE(t, asgName, region, maxRetries, sleepBetweenRetries)
require.NoError(t, err)
}
// WaitForCapacityE waits for the currently set desired capacity to be reached on the ASG
func WaitForCapacityE(
t *testing.T,
asgName string,
region string,
maxRetries int,
sleepBetweenRetries time.Duration,
) error {
msg, err := retry.DoWithRetryE(
t,
fmt.Sprintf("Waiting for ASG %s to reach desired capacity.", asgName),
maxRetries,
sleepBetweenRetries,
func() (string, error) {
capacityInfo, err := GetCapacityInfoForAsgE(t, asgName, region)
if err != nil {
return "", err
}
if capacityInfo.CurrentCapacity != capacityInfo.DesiredCapacity {
return "", NewAsgCapacityNotMetError(asgName, capacityInfo.DesiredCapacity, capacityInfo.CurrentCapacity)
} else {
return fmt.Sprintf("ASG %s is now at desired capacity %d", asgName, capacityInfo.DesiredCapacity), nil
}
},
)
logger.Log(t, msg)
return err
}
// NewAsgClient creates an Auto Scaling Group client.
func NewAsgClient(t *testing.T, region string) *autoscaling.AutoScaling {
client, err := NewAsgClientE(t, region)
if err != nil {
t.Fatal(err)
}
return client
}
// NewAsgClientE creates an Auto Scaling Group client.
func NewAsgClientE(t *testing.T, region string) (*autoscaling.AutoScaling, error) {
sess, err := NewAuthenticatedSession(region)
if err != nil {
return nil, err
}
return autoscaling.New(sess), nil
}