-
Notifications
You must be signed in to change notification settings - Fork 3
/
delete.go
45 lines (38 loc) · 1.12 KB
/
delete.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
package main
import (
"context"
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/pkg/errors"
)
func (h *handler) delete(ctx context.Context, detail CloudTrailDetail) error {
req := DeleteNetworkInterfaceRequestParameters{}
err := json.Unmarshal(detail.RequestParameters, &req)
if err != nil {
return errors.WithStack(err)
}
describe, err := h.api.DescribeAddressesWithContext(ctx, &ec2.DescribeAddressesInput{
Filters: []*ec2.Filter{
{Name: aws.String("tag:lambdaeip:eni"), Values: aws.StringSlice([]string{req.NetworkInterfaceId})},
},
})
if err != nil {
return errors.WithStack(err)
}
for _, address := range describe.Addresses {
_, _ = h.api.DisassociateAddressWithContext(ctx, &ec2.DisassociateAddressInput{
AssociationId: address.AssociationId,
})
_, err = h.api.ReleaseAddressWithContext(ctx, &ec2.ReleaseAddressInput{
AllocationId: address.AllocationId,
})
if err != nil {
return errors.WithStack(err)
}
}
return nil
}
type DeleteNetworkInterfaceRequestParameters struct {
NetworkInterfaceId string `json:"networkInterfaceId"`
}