From e280b9f225e495341515b6fbadd541b6f88dcb5c Mon Sep 17 00:00:00 2001 From: Felipe Marinho Date: Wed, 18 Oct 2023 18:46:41 +0000 Subject: [PATCH] new: feat: add keypair validation --- core/create.go | 6 ++++++ core/helpers/keypair.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 core/helpers/keypair.go diff --git a/core/create.go b/core/create.go index 8cc1a4f..62cc417 100644 --- a/core/create.go +++ b/core/create.go @@ -84,6 +84,12 @@ func (h *Handler) Create(ctx context.Context, opts CreateOptions) (CreateOutput, startupScript = script } + // validate key pair + _, err = helpers.GetKeyPair(ctx, client, keyName) + if err != nil { + return CreateOutput{}, err + } + // get the image of the dev space machine devSpaceAMI, err := helpers.GetImageFromFilter(ctx, client, helpers.AMIFilter{ ID: opts.DevSpaceAMI.ID, diff --git a/core/helpers/keypair.go b/core/helpers/keypair.go new file mode 100644 index 0000000..495cb6e --- /dev/null +++ b/core/helpers/keypair.go @@ -0,0 +1,25 @@ +package helpers + +import ( + "context" + "fmt" + + "github.com/aws/aws-sdk-go-v2/service/ec2" + "github.com/aws/aws-sdk-go-v2/service/ec2/types" + "github.com/felipemarinho97/invest-path/clients" +) + +func GetKeyPair(ctx context.Context, client clients.IEC2Client, keyName string) (*types.KeyPairInfo, error) { + keyPairs, err := client.DescribeKeyPairs(ctx, &ec2.DescribeKeyPairsInput{ + KeyNames: []string{keyName}, + }) + if err != nil { + return nil, err + } + + if len(keyPairs.KeyPairs) == 0 { + return nil, fmt.Errorf("no key pair found with name %s", keyName) + } + + return &keyPairs.KeyPairs[0], nil +}