Skip to content

Commit

Permalink
Merge pull request #6 from thaim/filter-not-running-instance
Browse files Browse the repository at this point in the history
  • Loading branch information
thaim authored Jan 5, 2023
2 parents 1f3a062 + 01d9afe commit 86bd982
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 12 deletions.
22 changes: 10 additions & 12 deletions ec2id.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,17 @@ func NewAwsClient() (EC2DescribeInstancesAPI, error){
}

func Ec2id(name string, client EC2DescribeInstancesAPI) (string, error) {
var params *ec2.DescribeInstancesInput = nil
if len(name) != 0 {
params = &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{name},
},
var params = &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{name},
},
}
{
Name: aws.String("instance-state-name"),
Values: []string{"running"},
},
},
}

result, err := GetInstances(context.TODO(), client, params)
Expand All @@ -56,9 +57,6 @@ func Ec2id(name string, client EC2DescribeInstancesAPI) (string, error) {
var filteredInstance = result.Reservations[0].Instances[0]
for _, v := range result.Reservations {
for _, instance := range v.Instances {
if string(filteredInstance.State.Name) == "running" && string(instance.State.Name) != "running" {
continue
}
if filteredInstance.LaunchTime.After(*instance.LaunchTime) {
continue
}
Expand Down
91 changes: 91 additions & 0 deletions ec2id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"strings"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/aws/aws-sdk-go-v2/aws"
Expand All @@ -21,11 +22,85 @@ func TestEc2id(t *testing.T) {
Name: aws.String("tag:Name"),
Values: []string{"noexist"},
},
{
Name: aws.String("instance-state-name"),
Values: []string{"running"},
},
},
}).
Return(&ec2.DescribeInstancesOutput{}, nil).
AnyTimes()

mockClient.EXPECT().
DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{""},
},
{
Name: aws.String("instance-state-name"),
Values: []string{"running"},
},
},
}).
Return(&ec2.DescribeInstancesOutput{
Reservations: []types.Reservation {
{
Instances: []types.Instance {
{
InstanceId: aws.String("i-0123456789abcdef0"),
LaunchTime: aws.Time(time.Date(2023, 1, 5, 12, 0, 0, 0, time.UTC)),
},
{
InstanceId: aws.String("i-0123456789abcdef1"),
LaunchTime: aws.Time(time.Date(2023, 1, 5, 12, 0, 0, 1, time.UTC)),
},
{
InstanceId: aws.String("i-0123456789abcdef2"),
LaunchTime: aws.Time(time.Date(2023, 1, 5, 12, 0, 0, 2, time.UTC)),
},
},
},
{
Instances: []types.Instance {
{
InstanceId: aws.String("i-00000000000abcdef"),
LaunchTime: aws.Time(time.Date(2023, 1, 4, 12, 0, 0, 0, time.UTC)),
},
},
},
},
}, nil).
AnyTimes()

mockClient.EXPECT().
DescribeInstances(context.TODO(), &ec2.DescribeInstancesInput{
Filters: []types.Filter{
{
Name: aws.String("tag:Name"),
Values: []string{"test"},
},
{
Name: aws.String("instance-state-name"),
Values: []string{"running"},
},
},
}).
Return(&ec2.DescribeInstancesOutput{
Reservations: []types.Reservation {
{
Instances: []types.Instance {
{
InstanceId: aws.String("i-00000000000abcdef"),
LaunchTime: aws.Time(time.Date(2023, 1, 4, 12, 0, 0, 0, time.UTC)),
},
},
},
},
}, nil).
AnyTimes()

cases := []struct {
name string
client EC2DescribeInstancesAPI
Expand All @@ -42,6 +117,22 @@ func TestEc2id(t *testing.T) {
wantErr: false,
expectErr: "",
},
{
name: "return latest instance-id by no input",
client: mockClient,
instanceName: "",
expect: "i-0123456789abcdef2",
wantErr: false,
expectErr: "",
},
{
name: "return instance-id",
client: mockClient,
instanceName: "test",
expect: "i-00000000000abcdef",
wantErr: false,
expectErr: "",
},
}

for _, tt := range cases {
Expand Down

0 comments on commit 86bd982

Please sign in to comment.