-
Notifications
You must be signed in to change notification settings - Fork 22
/
vpcs.go
65 lines (54 loc) · 1.51 KB
/
vpcs.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
package ec2
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
awsec2 "github.com/aws/aws-sdk-go/service/ec2"
"github.com/genevieve/leftovers/aws/common"
)
type vpcsClient interface {
DescribeVpcs(*awsec2.DescribeVpcsInput) (*awsec2.DescribeVpcsOutput, error)
DeleteVpc(*awsec2.DeleteVpcInput) (*awsec2.DeleteVpcOutput, error)
}
type Vpcs struct {
client vpcsClient
logger logger
routes routeTables
subnets subnets
gateways internetGateways
resourceTags resourceTags
}
func NewVpcs(client vpcsClient, logger logger, routes routeTables, subnets subnets, gateways internetGateways, resourceTags resourceTags) Vpcs {
return Vpcs{
client: client,
logger: logger,
routes: routes,
subnets: subnets,
gateways: gateways,
resourceTags: resourceTags,
}
}
func (v Vpcs) List(filter string) ([]common.Deletable, error) {
output, err := v.client.DescribeVpcs(&awsec2.DescribeVpcsInput{
Filters: []*awsec2.Filter{{
Name: aws.String("isDefault"),
Values: []*string{aws.String("false")},
}},
})
if err != nil {
return nil, fmt.Errorf("Describe EC2 VPCs: %s", err)
}
var resources []common.Deletable
for _, vpc := range output.Vpcs {
r := NewVpc(v.client, v.routes, v.subnets, v.gateways, v.resourceTags, vpc.VpcId, vpc.Tags)
if !strings.Contains(r.Name(), filter) {
continue
}
proceed := v.logger.PromptWithDetails(r.Type(), r.Name())
if !proceed {
continue
}
resources = append(resources, r)
}
return resources, nil
}