/
find_cloud_posse.go
100 lines (77 loc) · 1.83 KB
/
find_cloud_posse.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
88
89
90
91
92
93
94
95
96
97
98
99
100
package ec2
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
// VpcDefault looks up the Default Vpc. When not found, returns nil and potentially an API error.
func FindInternetGatewayForVPC(conn *ec2.EC2, vpcID string) (*ec2.InternetGateway, error) {
filters := []*ec2.Filter{
{
Name: aws.String("attachment.vpc-id"),
Values: []*string{aws.String(vpcID)},
},
}
input := &ec2.DescribeInternetGatewaysInput{
Filters: filters,
}
output, err := conn.DescribeInternetGateways(input)
if err != nil {
return nil, err
}
if output == nil {
return nil, nil
}
for _, ig := range output.InternetGateways {
if ig == nil {
continue
}
return ig, nil
}
return nil, nil
}
// SubnetsForVPC looks up a the Subnets for a VPC. When not found, returns nil and potentially an API error.
func FindSubnetsForVPC(conn *ec2.EC2, vpcID string) ([]*ec2.Subnet, error) {
filters := []*ec2.Filter{
{
Name: aws.String("vpc-id"),
Values: []*string{aws.String(vpcID)},
},
}
input := &ec2.DescribeSubnetsInput{
Filters: filters,
}
output, err := conn.DescribeSubnets(input)
if err != nil {
return nil, err
}
if output == nil || len(output.Subnets) == 0 || output.Subnets[0] == nil {
return nil, nil
}
return output.Subnets, nil
}
// VpcDefault looks up the Default Vpc. When not found, returns nil and potentially an API error.
func FindDefaultVpc(conn *ec2.EC2) (*ec2.Vpc, error) {
filters := []*ec2.Filter{
{
Name: aws.String("isDefault"),
Values: []*string{aws.String("true")},
},
}
input := &ec2.DescribeVpcsInput{
Filters: filters,
}
output, err := conn.DescribeVpcs(input)
if err != nil {
return nil, err
}
if output == nil {
return nil, nil
}
for _, vpc := range output.Vpcs {
if vpc == nil {
continue
}
return vpc, nil
}
return nil, nil
}