Skip to content

Commit

Permalink
new: feat: add keypair validation
Browse files Browse the repository at this point in the history
  • Loading branch information
felipemarinho97 committed Oct 18, 2023
1 parent 88cf001 commit e280b9f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions core/helpers/keypair.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit e280b9f

Please sign in to comment.