diff --git a/ec2id.go b/ec2id.go index 44dbf14..0695ecb 100644 --- a/ec2id.go +++ b/ec2id.go @@ -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) @@ -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 } diff --git a/ec2id_test.go b/ec2id_test.go index 655d6c0..1bc120f 100644 --- a/ec2id_test.go +++ b/ec2id_test.go @@ -4,6 +4,7 @@ import ( "context" "strings" "testing" + "time" "github.com/golang/mock/gomock" "github.com/aws/aws-sdk-go-v2/aws" @@ -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 @@ -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 {