forked from isovalent/aws-delete-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpngateway.go
77 lines (69 loc) · 2.05 KB
/
vpngateway.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
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 deleteVpnGateways(ctx context.Context, client *ec2.Client, vpcId string, vpnGateways []types.VpnGateway) (errs error) {
for _, vpnGateway := range vpnGateways {
if vpnGateway.VpnGatewayId == nil {
continue
}
var vpcAttachmentErrs error
for _, vpcAttachment := range vpnGateway.VpcAttachments {
state := vpcAttachment.State
if state == types.AttachmentStatusDetached || state == types.AttachmentStatusDetaching {
continue
}
if vpcAttachment.VpcId == nil || *vpcAttachment.VpcId != vpcId {
continue
}
_, err := client.DetachVpnGateway(ctx, &ec2.DetachVpnGatewayInput{
VpcId: vpcAttachment.VpcId,
VpnGatewayId: vpnGateway.VpnGatewayId,
})
log.Err(err).
Str("VpcId", *vpcAttachment.VpcId).
Str("VpnGatewayId", *vpnGateway.VpnGatewayId).
Msg("DetachVpnGateway")
vpcAttachmentErrs = multierr.Append(vpcAttachmentErrs, err)
}
if vpcAttachmentErrs != nil {
continue
}
_, err := client.DeleteVpnGateway(ctx, &ec2.DeleteVpnGatewayInput{
VpnGatewayId: vpnGateway.VpnGatewayId,
})
log.Err(err).
Str("VpnGatewayId", *vpnGateway.VpnGatewayId).
Msg("DeleteVpnGateway")
errs = multierr.Append(errs, err)
}
return
}
func listVpnGateways(ctx context.Context, client *ec2.Client, vpcId string) ([]types.VpnGateway, error) {
output, err := client.DescribeVpnGateways(ctx, &ec2.DescribeVpnGatewaysInput{
Filters: []types.Filter{
{
Name: aws.String("attachment.vpc-id"),
Values: []string{vpcId},
},
},
})
if err != nil {
return nil, err
}
return output.VpnGateways, nil
}
func vpnGatewayIds(vpnGateways []types.VpnGateway) []string {
vpnGatewayIds := make([]string, 0, len(vpnGateways))
for _, vpnGateway := range vpnGateways {
if vpnGateway.VpnGatewayId != nil {
vpnGatewayIds = append(vpnGatewayIds, *vpnGateway.VpnGatewayId)
}
}
return vpnGatewayIds
}