forked from isovalent/aws-delete-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
internetgateway.go
87 lines (80 loc) · 2.69 KB
/
internetgateway.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
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 deleteInternetGateways(ctx context.Context, client *ec2.Client, vpcId string, internetGateways []types.InternetGateway) (errs error) {
for _, internetGateway := range internetGateways {
if internetGateway.InternetGatewayId == nil {
continue
}
// Detach the InternetGateway from the VPC.
var internetGatewayErrs error
for _, internetGatewayAttachment := range internetGateway.Attachments {
state := internetGatewayAttachment.State
if state == types.AttachmentStatusDetaching || state == types.AttachmentStatusDetached {
continue
}
if internetGatewayAttachment.VpcId == nil || *internetGatewayAttachment.VpcId != vpcId {
continue
}
_, err := client.DetachInternetGateway(ctx, &ec2.DetachInternetGatewayInput{
InternetGatewayId: internetGateway.InternetGatewayId,
VpcId: internetGatewayAttachment.VpcId,
})
log.Err(err).
Str("InternetGatewayId", *internetGateway.InternetGatewayId).
Str("VpcId", *internetGatewayAttachment.VpcId).
Msg("DetachInternetGateway")
internetGatewayErrs = multierr.Append(internetGatewayErrs, err)
}
errs = multierr.Append(errs, internetGatewayErrs)
if internetGatewayErrs != nil {
continue
}
// Delete the InternetGateway.
_, err := client.DeleteInternetGateway(ctx, &ec2.DeleteInternetGatewayInput{
InternetGatewayId: internetGateway.InternetGatewayId,
})
log.Err(err).
Str("InternetGatewayId", *internetGateway.InternetGatewayId).
Msg("DeleteInternetGateway")
errs = multierr.Append(errs, err)
}
return
}
func internetGatewayIds(internetGateways []types.InternetGateway) []string {
internetGatewayIds := make([]string, 0, len(internetGateways))
for _, internetGateway := range internetGateways {
if internetGateway.InternetGatewayId != nil {
internetGatewayIds = append(internetGatewayIds, *internetGateway.InternetGatewayId)
}
}
return internetGatewayIds
}
func listInternetGateways(ctx context.Context, client *ec2.Client, vpcId string) ([]types.InternetGateway, error) {
input := ec2.DescribeInternetGatewaysInput{
Filters: []types.Filter{
{
Name: aws.String("attachment.vpc-id"),
Values: []string{vpcId},
},
},
}
var internetGateways []types.InternetGateway
for {
output, err := client.DescribeInternetGateways(ctx, &input)
if err != nil {
return nil, err
}
internetGateways = append(internetGateways, output.InternetGateways...)
if output.NextToken == nil {
return internetGateways, nil
}
input.NextToken = output.NextToken
}
}