forked from isovalent/aws-delete-vpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vpcpeeringconnection.go
77 lines (71 loc) · 2.47 KB
/
vpcpeeringconnection.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 deleteVpcPeeringConnections(ctx context.Context, client *ec2.Client, vpcId string, vpcPeeringConnections []types.VpcPeeringConnection) (errs error) {
for _, vpcPeeringConnection := range vpcPeeringConnections {
if vpcPeeringConnection.VpcPeeringConnectionId == nil {
continue
}
isAccepter := vpcPeeringConnection.AccepterVpcInfo != nil &&
vpcPeeringConnection.AccepterVpcInfo.VpcId != nil &&
*vpcPeeringConnection.AccepterVpcInfo.VpcId == vpcId
isRequester := vpcPeeringConnection.RequesterVpcInfo != nil &&
vpcPeeringConnection.RequesterVpcInfo.VpcId != nil &&
*vpcPeeringConnection.RequesterVpcInfo.VpcId == vpcId
if !isAccepter && !isRequester {
continue
}
_, err := client.DeleteVpcPeeringConnection(ctx, &ec2.DeleteVpcPeeringConnectionInput{
VpcPeeringConnectionId: vpcPeeringConnection.VpcPeeringConnectionId,
})
log.Err(err).
Str("VpcPeeringConnectionId", *vpcPeeringConnection.VpcPeeringConnectionId).
Msg("DeleteVpcPeeringConnection")
errs = multierr.Append(errs, err)
}
return
}
func listVpcPeeringConnections(ctx context.Context, client *ec2.Client, vpcId string) ([]types.VpcPeeringConnection, error) {
var vpcPeeringConnections []types.VpcPeeringConnection
ACCEPTER_REQUESTER:
for _, name := range []string{
"accepter-vpc-info.vpc-id",
"requester-vpc-info.vpc-id",
} {
input := ec2.DescribeVpcPeeringConnectionsInput{
Filters: []types.Filter{
{
Name: aws.String(name),
Values: []string{vpcId},
},
},
}
for {
output, err := client.DescribeVpcPeeringConnections(ctx, &input)
if err != nil {
return nil, err
}
vpcPeeringConnections = append(vpcPeeringConnections, output.VpcPeeringConnections...)
if output.NextToken == nil {
continue ACCEPTER_REQUESTER
}
input.NextToken = output.NextToken
}
}
return vpcPeeringConnections, nil
}
func vpcPeeringConnectionIds(vpcPeeringConnections []types.VpcPeeringConnection) []string {
vpcPeeringConnectionIds := make([]string, 0, len(vpcPeeringConnections))
for _, vpcPeeringConnection := range vpcPeeringConnections {
if vpcPeeringConnection.VpcPeeringConnectionId != nil {
vpcPeeringConnectionIds = append(vpcPeeringConnectionIds, *vpcPeeringConnection.VpcPeeringConnectionId)
}
}
return vpcPeeringConnectionIds
}