Skip to content

Commit

Permalink
execute method via client
Browse files Browse the repository at this point in the history
  • Loading branch information
kenzo0107 committed Dec 12, 2019
1 parent eddce1f commit f946ed4
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 141 deletions.
39 changes: 28 additions & 11 deletions pkg/awsapi/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,18 @@ import (
fuzzyfinder "github.com/ktr0731/go-fuzzyfinder"
)

// EC2Instance : required ec2 instance information
// EC2Iface : ec2 interface
type EC2Iface interface {
DescribeRunningEC2s() ([]EC2, error)
}

// EC2Instance : ec2 instance
type EC2Instance struct {
client ec2iface.EC2API
}

// EC2 : required ec2 instance information
type EC2 struct {
InstanceID string
PublicIPAddress string
PrivateIPAddress string
Expand All @@ -19,8 +29,15 @@ type EC2Instance struct {
AvailabilityZone string
}

// DescribeRunningEC2Instances : get list of running ec2 instances
func DescribeRunningEC2Instances(svc ec2iface.EC2API) ([]EC2Instance, error) {
// NewEC2Client : new ec2 client
func NewEC2Client(svc ec2iface.EC2API) EC2Iface {
return &EC2Instance{
client: svc,
}
}

// DescribeRunningEC2s : get list of running ec2 instances
func (i *EC2Instance) DescribeRunningEC2s() ([]EC2, error) {
// condition: running instance only
input := &ec2.DescribeInstancesInput{
Filters: []*ec2.Filter{
Expand All @@ -33,12 +50,12 @@ func DescribeRunningEC2Instances(svc ec2iface.EC2API) ([]EC2Instance, error) {
},
}

res, err := svc.DescribeInstances(input)
res, err := i.client.DescribeInstances(input)
if err != nil {
return nil, err
}

e := []EC2Instance{}
e := []EC2{}
for _, r := range res.Reservations {
for _, i := range r.Instances {

Expand All @@ -62,7 +79,7 @@ func DescribeRunningEC2Instances(svc ec2iface.EC2API) ([]EC2Instance, error) {
privateIPAddress = *i.PrivateIpAddress
}

e = append(e, EC2Instance{
e = append(e, EC2{
InstanceID: *i.InstanceId,
InstanceType: *i.InstanceType,
PublicIPAddress: publicIPAddress,
Expand All @@ -76,8 +93,8 @@ func DescribeRunningEC2Instances(svc ec2iface.EC2API) ([]EC2Instance, error) {
return e, nil
}

// FinderEC2Instance : find information of ec2 instance through fuzzyfinder
func FinderEC2Instance(ec2List []EC2Instance) (EC2Instance EC2Instance, err error) {
// FinderEC2 : find information of ec2 instance through fuzzyfinder
func FinderEC2(ec2List []EC2) (ec2 EC2, err error) {
idx, err := fuzzyfinder.FindMulti(
ec2List,
func(i int) string {
Expand All @@ -103,14 +120,14 @@ func FinderEC2Instance(ec2List []EC2Instance) (EC2Instance EC2Instance, err erro
)

if err != nil {
return EC2Instance, err
return ec2, err
}

for _, i := range idx {
EC2Instance = ec2List[i]
ec2 = ec2List[i]
}

return EC2Instance, nil
return ec2, nil
}

// FinderUsername : find ssh username through fuzzyfinder
Expand Down
33 changes: 20 additions & 13 deletions pkg/awsapi/ec2_instance_connect.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
package awsapi

import (
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2instanceconnect"
"github.com/aws/aws-sdk-go/service/ec2instanceconnect/ec2instanceconnectiface"
)

// SendSSHPubKey : send ssh public key to using ec2 instance api
func SendSSHPubKey(svc ec2instanceconnectiface.EC2InstanceConnectAPI, user, instanceID, publicKey, availabilityZone string) (bool, error) {
input := &ec2instanceconnect.SendSSHPublicKeyInput{
AvailabilityZone: aws.String(availabilityZone),
InstanceId: aws.String(instanceID),
InstanceOSUser: aws.String(user),
SSHPublicKey: aws.String(publicKey),
// EC2InstanceConnectIface : ec2 instance connect interface
type EC2InstanceConnectIface interface {
SendSSHPubKey(ec2instanceconnect.SendSSHPublicKeyInput) (bool, error)
}

// EC2InstanceConnectInstance : ec2 instance connect instance
type EC2InstanceConnectInstance struct {
client ec2instanceconnectiface.EC2InstanceConnectAPI
}

// NewEC2InstanceConnectClient : new ec2 instance connect client
func NewEC2InstanceConnectClient(svc ec2instanceconnectiface.EC2InstanceConnectAPI) EC2InstanceConnectIface {
return &EC2InstanceConnectInstance{
client: svc,
}
r, err := svc.SendSSHPublicKey(input)
}

// SendSSHPubKey : send ssh public key to using ec2 instance api
func (i *EC2InstanceConnectInstance) SendSSHPubKey(p ec2instanceconnect.SendSSHPublicKeyInput) (bool, error) {
r, err := i.client.SendSSHPublicKey(&p)
if err != nil {
log.Fatal(err)
return *r.Success, err
return false, err
}
return *r.Success, nil
}
45 changes: 34 additions & 11 deletions pkg/awsapi/ec2_instance_connect_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package awsapi

import (
"errors"
"testing"

"github.com/aws/aws-sdk-go/aws"
Expand All @@ -9,25 +10,47 @@ import (
"github.com/google/go-cmp/cmp"
)

type mockEC2InstanceConnectClient struct {
type mockEC2InstanceConnectiface struct {
ec2instanceconnectiface.EC2InstanceConnectAPI

Resp ec2instanceconnect.SendSSHPublicKeyOutput
Error error
}

func (m *mockEC2InstanceConnectClient) SendSSHPublicKey(input *ec2instanceconnect.SendSSHPublicKeyInput) (*ec2instanceconnect.SendSSHPublicKeyOutput, error) {
return &ec2instanceconnect.SendSSHPublicKeyOutput{
RequestId: aws.String("123456789"),
Success: aws.Bool(true),
}, nil
func (m *mockEC2InstanceConnectiface) SendSSHPublicKey(p *ec2instanceconnect.SendSSHPublicKeyInput) (*ec2instanceconnect.SendSSHPublicKeyOutput, error) {
return &m.Resp, m.Error
}

// SendSSHPubKey : send ssh public key to using ec2 instance api
func TestSendSSHPubKey(t *testing.T) {
mockSvc := &mockEC2InstanceConnectClient{}
r, err := SendSSHPubKey(mockSvc, "ec2-user", "t3.micro", "", "ap-northeast-1a")
m := NewEC2InstanceConnectClient(&mockEC2InstanceConnectiface{
Resp: ec2instanceconnect.SendSSHPublicKeyOutput{
RequestId: aws.String("1234567890"),
Success: aws.Bool(true),
},
Error: nil,
})
r, err := m.SendSSHPubKey(ec2instanceconnect.SendSSHPublicKeyInput{})
if err != nil {
t.Error(err)
t.Error("wrong result \n err is not nil")
}
if diff := cmp.Diff(r, true); diff != "" {
t.Errorf("wrong result: \n%s", diff)
t.Errorf("wrong result \n%s", diff)
}
}

func TestSendSSHPubKeyWithError(t *testing.T) {
m := NewEC2InstanceConnectClient(&mockEC2InstanceConnectiface{
Resp: ec2instanceconnect.SendSSHPublicKeyOutput{
RequestId: aws.String("1234567890"),
Success: aws.Bool(false),
},
Error: errors.New("error occured"),
})
r, err := m.SendSSHPubKey(ec2instanceconnect.SendSSHPublicKeyInput{})
if err == nil {
t.Error("wrong result \n err is nil")
}
if diff := cmp.Diff(r, false); diff != "" {
t.Errorf("wrong result \n%s", diff)
}
}
Loading

0 comments on commit f946ed4

Please sign in to comment.