-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add v1 and v2 client for diff sdks, break out client
- Loading branch information
1 parent
c22a259
commit 4ac20bf
Showing
8 changed files
with
309 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package elasticspot | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
) | ||
|
||
// Client will create a new client for version 1 of the AWS SDK. | ||
type Client struct { | ||
EC2API EC2API | ||
} | ||
|
||
type EC2API interface { | ||
DescribeAddresses(input *ec2.DescribeAddressesInput) (*ec2.DescribeAddressesOutput, error) | ||
AssociateAddress(input *ec2.AssociateAddressInput) (*ec2.AssociateAddressOutput, error) | ||
DescribeInstances(input *ec2.DescribeInstancesInput) (*ec2.DescribeInstancesOutput, error) | ||
} | ||
|
||
func (c *Client) AssociateIP(ctx context.Context, ip, instanceID string) (*AssociateResponse, error) { | ||
instance, err := c.getInstanceById(instanceID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching instance: %w", err) | ||
} | ||
|
||
if *instance.PublicIpAddress == ip { | ||
return &AssociateResponse{AlreadyAssociated: true}, nil | ||
} | ||
|
||
address, err := c.getAddressForIp(ip) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching elastic ip address: %w", err) | ||
} | ||
|
||
assocRes, err := c.EC2API.AssociateAddress(&ec2.AssociateAddressInput{ | ||
AllocationId: address.AllocationId, | ||
InstanceId: aws.String(instanceID), | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error associating elastic ip address with %q %w", instanceID, err) | ||
} | ||
|
||
return &AssociateResponse{ | ||
AllocationID: *address.AllocationId, | ||
AssociationID: *assocRes.AssociationId, | ||
}, nil | ||
} | ||
|
||
func (a *Client) getInstanceById(id string) (*ec2.Instance, error) { | ||
instances, err := a.EC2API.DescribeInstances(&ec2.DescribeInstancesInput{ | ||
Filters: []*ec2.Filter{ | ||
{ | ||
Name: aws.String("instance-id"), | ||
Values: aws.StringSlice([]string{id}), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("DescribeInstances failed: %w", err) | ||
} | ||
|
||
if instances == nil || len(instances.Reservations) == 0 || len(instances.Reservations[0].Instances) == 0 { | ||
return nil, errors.New("no instance found for the given id") | ||
} | ||
|
||
return instances.Reservations[0].Instances[0], nil | ||
} | ||
|
||
func (a *Client) getAddressForIp(ip string) (*ec2.Address, error) { | ||
result, err := a.EC2API.DescribeAddresses(&ec2.DescribeAddressesInput{ | ||
Filters: []*ec2.Filter{ | ||
{ | ||
Name: aws.String("domain"), | ||
Values: aws.StringSlice([]string{"vpc"}), | ||
}, | ||
}, | ||
PublicIps: []*string{aws.String(ip)}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(result.Addresses) == 0 { | ||
return nil, fmt.Errorf("elastic ip address not found in region %q", os.Getenv("AWS_REGION")) | ||
} | ||
|
||
return result.Addresses[0], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package elasticspot | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
ec2v2 "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/aws/aws-sdk-go/aws" | ||
) | ||
|
||
type AssociateResponse struct { | ||
AlreadyAssociated bool | ||
AllocationID string | ||
AssociationID string | ||
} | ||
|
||
// ClientV2 will create a new client for version 2 of the AWS SDK. | ||
type ClientV2 struct { | ||
EC2API EC2APIV2 | ||
} | ||
|
||
type EC2APIV2 interface { | ||
DescribeAddresses(ctx context.Context, params *ec2v2.DescribeAddressesInput, optFns ...func(*ec2v2.Options)) (*ec2v2.DescribeAddressesOutput, error) | ||
AssociateAddress(ctx context.Context, params *ec2v2.AssociateAddressInput, optFns ...func(*ec2v2.Options)) (*ec2v2.AssociateAddressOutput, error) | ||
DescribeInstances(ctx context.Context, params *ec2v2.DescribeInstancesInput, optFns ...func(*ec2v2.Options)) (*ec2v2.DescribeInstancesOutput, error) | ||
} | ||
|
||
func (a *ClientV2) AssociateIP(ctx context.Context, ip string, instanceID string) (*AssociateResponse, error) { | ||
instance, err := a.getInstanceById(ctx, instanceID) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching instance: %w", err) | ||
} | ||
|
||
if *instance.PublicIpAddress == ip { | ||
return &AssociateResponse{AlreadyAssociated: true}, nil | ||
} | ||
|
||
address, err := a.getAddressForIp(ctx, ip) | ||
if err != nil { | ||
return nil, fmt.Errorf("error fetching elastic ip address: %w", err) | ||
} | ||
|
||
assocRes, err := a.EC2API.AssociateAddress(ctx, &ec2v2.AssociateAddressInput{ | ||
AllocationId: address.AllocationId, | ||
InstanceId: aws.String(instanceID), | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("error associating elastic ip address with %q %w", instanceID, err) | ||
} | ||
|
||
return &AssociateResponse{ | ||
AllocationID: *address.AllocationId, | ||
AssociationID: *assocRes.AssociationId, | ||
}, nil | ||
} | ||
|
||
func (a *ClientV2) getInstanceById(ctx context.Context, id string) (*types.Instance, error) { | ||
instances, err := a.EC2API.DescribeInstances(ctx, &ec2v2.DescribeInstancesInput{ | ||
Filters: []types.Filter{ | ||
{ | ||
Name: aws.String("instance-id"), | ||
Values: []string{id}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("DescribeInstances failed: %w", err) | ||
} | ||
|
||
if instances == nil || len(instances.Reservations) == 0 || len(instances.Reservations[0].Instances) == 0 { | ||
return nil, errors.New("no instance found for the given id") | ||
} | ||
|
||
return &instances.Reservations[0].Instances[0], nil | ||
} | ||
|
||
func (c *ClientV2) getAddressForIp(ctx context.Context, ip string) (*types.Address, error) { | ||
result, err := c.EC2API.DescribeAddresses(ctx, &ec2v2.DescribeAddressesInput{ | ||
Filters: []types.Filter{ | ||
{ | ||
Name: aws.String("domain"), | ||
Values: []string{"vpc"}, | ||
}, | ||
}, | ||
PublicIps: []string{ip}, | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(result.Addresses) == 0 { | ||
return nil, fmt.Errorf("elastic ip address not found in region %q", os.Getenv("AWS_REGION")) | ||
} | ||
|
||
return &result.Addresses[0], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/aws/aws-lambda-go/lambda" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
"github.com/nickfiggins/elasticspot" | ||
) | ||
|
||
var handler elasticspot.HandleFunc | ||
|
||
func init() { | ||
ctx := context.Background() | ||
cfg, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
panic(fmt.Sprintf("error loading default config: %v", err)) | ||
} | ||
|
||
client := ec2.NewFromConfig(cfg) | ||
elasticIP := os.Getenv("ELASTIC_IP") | ||
if len(elasticIP) == 0 { | ||
panic("Elastic IP has not been set.") | ||
} | ||
handler = elasticspot.HandleV2(client, elasticIP) | ||
} | ||
|
||
func main() { | ||
lambda.Start(handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.