From ce0f4bc1726ff3f3b9544db2492cd9dd6f22af68 Mon Sep 17 00:00:00 2001 From: thaim Date: Thu, 5 Jan 2023 13:03:51 +0000 Subject: [PATCH 1/4] generate input for aws cli at any time --- ec2id.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/ec2id.go b/ec2id.go index 44dbf14..215fdab 100644 --- a/ec2id.go +++ b/ec2id.go @@ -30,14 +30,11 @@ 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}, }, } } From 857bbea9f2882f7bebae3daf8b0efc079b14e8e1 Mon Sep 17 00:00:00 2001 From: thaim Date: Thu, 5 Jan 2023 13:04:50 +0000 Subject: [PATCH 2/4] create instance state filter --- ec2id.go | 9 +++++---- ec2id_test.go | 4 ++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/ec2id.go b/ec2id.go index 215fdab..0695ecb 100644 --- a/ec2id.go +++ b/ec2id.go @@ -36,7 +36,11 @@ func Ec2id(name string, client EC2DescribeInstancesAPI) (string, error) { Name: aws.String("tag:Name"), Values: []string{name}, }, - } + { + Name: aws.String("instance-state-name"), + Values: []string{"running"}, + }, + }, } result, err := GetInstances(context.TODO(), client, params) @@ -53,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..1fec688 100644 --- a/ec2id_test.go +++ b/ec2id_test.go @@ -21,6 +21,10 @@ 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). From 40390b06240807f28aae2910d026abdd1bae28e7 Mon Sep 17 00:00:00 2001 From: thaim Date: Thu, 5 Jan 2023 13:08:52 +0000 Subject: [PATCH 3/4] add test to return latest instance --- ec2id_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/ec2id_test.go b/ec2id_test.go index 1fec688..414437b 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" @@ -30,6 +31,49 @@ func TestEc2id(t *testing.T) { 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() + cases := []struct { name string client EC2DescribeInstancesAPI @@ -46,6 +90,14 @@ 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: "", + }, } for _, tt := range cases { From 01d9afeb37865bf1787b943140dc86a8221e809e Mon Sep 17 00:00:00 2001 From: thaim Date: Thu, 5 Jan 2023 13:10:33 +0000 Subject: [PATCH 4/4] add test for return single instance id --- ec2id_test.go | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/ec2id_test.go b/ec2id_test.go index 414437b..1bc120f 100644 --- a/ec2id_test.go +++ b/ec2id_test.go @@ -74,6 +74,33 @@ func TestEc2id(t *testing.T) { }, 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 @@ -98,6 +125,14 @@ func TestEc2id(t *testing.T) { wantErr: false, expectErr: "", }, + { + name: "return instance-id", + client: mockClient, + instanceName: "test", + expect: "i-00000000000abcdef", + wantErr: false, + expectErr: "", + }, } for _, tt := range cases {