forked from isovalent/aws-delete-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subnet.go
64 lines (58 loc) · 1.43 KB
/
subnet.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 deleteSubnets(ctx context.Context, client *ec2.Client, vpcId string, subnets []types.Subnet) (errs error) {
for _, subnet := range subnets {
if subnet.SubnetId == nil {
continue
}
if subnet.VpcId == nil || *subnet.VpcId != vpcId {
continue
}
_, err := client.DeleteSubnet(ctx, &ec2.DeleteSubnetInput{
SubnetId: subnet.SubnetId,
})
log.Err(err).
Str("SubnetId", *subnet.SubnetId).
Msg("DeleteSubnet")
errs = multierr.Append(errs, err)
}
return
}
func listSubnets(ctx context.Context, client *ec2.Client, vpcId string) ([]types.Subnet, error) {
input := ec2.DescribeSubnetsInput{
Filters: []types.Filter{
{
Name: aws.String("vpc-id"),
Values: []string{vpcId},
},
},
}
var subnets []types.Subnet
for {
output, err := client.DescribeSubnets(ctx, &input)
if err != nil {
return nil, err
}
subnets = append(subnets, output.Subnets...)
if output.NextToken == nil {
return subnets, nil
}
input.NextToken = output.NextToken
}
}
func subnetIds(subnets []types.Subnet) []string {
subnetIds := make([]string, 0, len(subnets))
for _, subnet := range subnets {
if subnet.SubnetId != nil {
subnetIds = append(subnetIds, *subnet.SubnetId)
}
}
return subnetIds
}