forked from isovalent/aws-delete-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
routetable.go
64 lines (58 loc) · 1.59 KB
/
routetable.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
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/rs/zerolog/log"
"go.uber.org/multierr"
)
func deleteRouteTables(ctx context.Context, client *ec2.Client, vpcId string, routeTables []types.RouteTable) (errs error) {
for _, routeTable := range routeTables {
if routeTable.RouteTableId == nil {
continue
}
if routeTable.VpcId == nil || *routeTable.VpcId != vpcId {
continue
}
_, err := client.DeleteRouteTable(ctx, &ec2.DeleteRouteTableInput{
RouteTableId: routeTable.RouteTableId,
})
log.Err(err).
Str("RouteTableId", *routeTable.RouteTableId).
Msg("DeleteRouteTable")
errs = multierr.Append(errs, err)
}
return
}
func listRouteTables(ctx context.Context, client *ec2.Client, vpcId string) ([]types.RouteTable, error) {
input := ec2.DescribeRouteTablesInput{
Filters: []types.Filter{
{
Name: aws.String("vpc-id"),
Values: []string{vpcId},
},
},
}
var routeTables []types.RouteTable
for {
output, err := client.DescribeRouteTables(ctx, &input)
if err != nil {
return nil, err
}
routeTables = append(routeTables, output.RouteTables...)
if output.NextToken == nil {
return routeTables, nil
}
input.NextToken = output.NextToken
}
}
func routeTableIds(routeTables []types.RouteTable) []string {
routeTableIds := make([]string, 0, len(routeTables))
for _, routeTable := range routeTables {
if routeTable.RouteTableId != nil {
routeTableIds = append(routeTableIds, *routeTable.RouteTableId)
}
}
return routeTableIds
}