/
ec2-vpc-peering-connections.go
64 lines (51 loc) · 1.34 KB
/
ec2-vpc-peering-connections.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 resources
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
type EC2VPCPeeringConnection struct {
svc *ec2.EC2
id *string
status *string
}
func init() {
register("EC2VPCPeeringConnection", ListEC2VPCPeeringConnections)
}
func ListEC2VPCPeeringConnections(sess *session.Session) ([]Resource, error) {
svc := ec2.New(sess)
resources := make([]Resource, 0)
// filter should be set as deleted vpc connetions are returned
params := &ec2.DescribeVpcPeeringConnectionsInput{}
resp, err := svc.DescribeVpcPeeringConnections(params)
if err != nil {
return nil, err
}
for _, peeringConfig := range resp.VpcPeeringConnections {
resources = append(resources, &EC2VPCPeeringConnection{
svc: svc,
id: peeringConfig.VpcPeeringConnectionId,
status: peeringConfig.Status.Code,
})
}
return resources, nil
}
func (p *EC2VPCPeeringConnection) Filter() error {
if *p.status == "deleting" || *p.status == "deleted" {
return fmt.Errorf("already deleted")
}
return nil
}
func (p *EC2VPCPeeringConnection) Remove() error {
params := &ec2.DeleteVpcPeeringConnectionInput{
VpcPeeringConnectionId: p.id,
}
_, err := p.svc.DeleteVpcPeeringConnection(params)
if err != nil {
return err
}
return nil
}
func (p *EC2VPCPeeringConnection) String() string {
return *p.id
}