Ignore terminated instance from enumeration
parent
5bedc7d0e0
commit
1663f0a5bd
|
@ -132,9 +132,23 @@ func (r *ec2Repository) ListAllInstances() ([]*ec2.Instance, error) {
|
|||
if v := r.cache.Get("ec2ListAllInstances"); v != nil {
|
||||
return v.([]*ec2.Instance), nil
|
||||
}
|
||||
|
||||
var instances []*ec2.Instance
|
||||
input := &ec2.DescribeInstancesInput{}
|
||||
input := &ec2.DescribeInstancesInput{
|
||||
Filters: []*ec2.Filter{
|
||||
{
|
||||
// Ignore terminated state from enumeration since terminated means that instance
|
||||
// has been removed
|
||||
Name: aws.String("instance-state-name"),
|
||||
Values: aws.StringSlice([]string{
|
||||
"pending",
|
||||
"running",
|
||||
"stopping",
|
||||
"shutting-down",
|
||||
"stopped",
|
||||
}),
|
||||
},
|
||||
},
|
||||
}
|
||||
err := r.client.DescribeInstancesPages(input, func(res *ec2.DescribeInstancesOutput, lastPage bool) bool {
|
||||
for _, reservation := range res.Reservations {
|
||||
instances = append(instances, reservation.Instances...)
|
||||
|
|
|
@ -365,7 +365,20 @@ func Test_ec2Repository_ListAllInstances(t *testing.T) {
|
|||
{name: "List with 2 pages",
|
||||
mocks: func(client *awstest.MockFakeEC2) {
|
||||
client.On("DescribeInstancesPages",
|
||||
&ec2.DescribeInstancesInput{},
|
||||
&ec2.DescribeInstancesInput{
|
||||
Filters: []*ec2.Filter{
|
||||
{
|
||||
Name: aws.String("instance-state-name"),
|
||||
Values: aws.StringSlice([]string{
|
||||
"pending",
|
||||
"running",
|
||||
"stopping",
|
||||
"shutting-down",
|
||||
"stopped",
|
||||
}),
|
||||
},
|
||||
},
|
||||
},
|
||||
mock.MatchedBy(func(callback func(res *ec2.DescribeInstancesOutput, lastPage bool) bool) bool {
|
||||
callback(&ec2.DescribeInstancesOutput{
|
||||
Reservations: []*ec2.Reservation{
|
||||
|
|
Loading…
Reference in New Issue