-
Notifications
You must be signed in to change notification settings - Fork 22
/
addresses.go
56 lines (44 loc) · 1.24 KB
/
addresses.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
package ec2
import (
"fmt"
"strings"
awsec2 "github.com/aws/aws-sdk-go/service/ec2"
"github.com/genevieve/leftovers/common"
)
//go:generate faux --interface addressesClient --output fakes/addresses_client.go
type addressesClient interface {
DescribeAddresses(*awsec2.DescribeAddressesInput) (*awsec2.DescribeAddressesOutput, error)
ReleaseAddress(*awsec2.ReleaseAddressInput) (*awsec2.ReleaseAddressOutput, error)
}
type Addresses struct {
client addressesClient
logger logger
}
func NewAddresses(client addressesClient, logger logger) Addresses {
return Addresses{
client: client,
logger: logger,
}
}
func (d Addresses) List(filter string) ([]common.Deletable, error) {
addresses, err := d.client.DescribeAddresses(&awsec2.DescribeAddressesInput{})
if err != nil {
return nil, fmt.Errorf("Describing EC2 Addresses: %s", err)
}
var resources []common.Deletable
for _, a := range addresses.Addresses {
r := NewAddress(d.client, a.PublicIp, a.AllocationId, a.Tags)
if !strings.Contains(r.Name(), filter) {
continue
}
proceed := d.logger.PromptWithDetails(r.Type(), r.Name())
if !proceed {
continue
}
resources = append(resources, r)
}
return resources, nil
}
func (d Addresses) Type() string {
return "ec2-address"
}