-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_v2.go
99 lines (83 loc) · 2.83 KB
/
client_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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
}