forked from gruntwork-io/terratest
/
vpc.go
191 lines (162 loc) · 4.74 KB
/
vpc.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package aws
import (
"fmt"
"strconv"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/gruntwork-io/terratest/modules/random"
)
// Vpc is an Amazon Virtual Private Cloud.
type Vpc struct {
Id string // The ID of the VPC
Name string // The name of the VPC
Subnets []Subnet // A list of subnets in the VPC
}
// Subnet is a subnet in an availability zone.
type Subnet struct {
Id string // The ID of the Subnet
AvailabilityZone string // The Availability Zone the subnet is in
}
var vpcIDFilterName = "vpc-id"
var isDefaultFilterName = "isDefault"
var isDefaultFilterValue = "true"
// GetDefaultVpc fetches information about the default VPC in the given region.
func GetDefaultVpc(t *testing.T, region string) *Vpc {
vpc, err := GetDefaultVpcE(t, region)
if err != nil {
t.Fatal(err)
}
return vpc
}
// GetDefaultVpcE fetches information about the default VPC in the given region.
func GetDefaultVpcE(t *testing.T, region string) (*Vpc, error) {
client, err := NewEc2ClientE(t, region)
if err != nil {
return nil, err
}
defaultVpcFilter := ec2.Filter{Name: &isDefaultFilterName, Values: []*string{&isDefaultFilterValue}}
vpcs, err := client.DescribeVpcs(&ec2.DescribeVpcsInput{Filters: []*ec2.Filter{&defaultVpcFilter}})
if err != nil {
return nil, err
}
numVpcs := len(vpcs.Vpcs)
if numVpcs != 1 {
return nil, fmt.Errorf("Expected to find one default VPC in region %s but found %s", region, strconv.Itoa(numVpcs))
}
defaultVpc := vpcs.Vpcs[0]
subnets, err := GetSubnetsForVpcE(t, aws.StringValue(defaultVpc.VpcId), region)
if err != nil {
return nil, err
}
return &Vpc{Id: aws.StringValue(defaultVpc.VpcId), Name: FindVpcName(defaultVpc), Subnets: subnets}, nil
}
// FindVpcName extracts the VPC name from its tags (if any). Fall back to "Default" if it's the default VPC or empty string
// otherwise.
func FindVpcName(vpc *ec2.Vpc) string {
for _, tag := range vpc.Tags {
if *tag.Key == "Name" {
return *tag.Value
}
}
if *vpc.IsDefault {
return "Default"
}
return ""
}
// GetSubnetsForVpc gets the subnets in the specified VPC.
func GetSubnetsForVpc(t *testing.T, vpcID string, region string) []Subnet {
subnets, err := GetSubnetsForVpcE(t, vpcID, region)
if err != nil {
t.Fatal(err)
}
return subnets
}
// GetSubnetsForVpcE gets the subnets in the specified VPC.
func GetSubnetsForVpcE(t *testing.T, vpcID string, region string) ([]Subnet, error) {
client, err := NewEc2ClientE(t, region)
if err != nil {
return nil, err
}
vpcIDFilter := ec2.Filter{Name: &vpcIDFilterName, Values: []*string{&vpcID}}
subnetOutput, err := client.DescribeSubnets(&ec2.DescribeSubnetsInput{Filters: []*ec2.Filter{&vpcIDFilter}})
if err != nil {
return nil, err
}
subnets := []Subnet{}
for _, ec2Subnet := range subnetOutput.Subnets {
subnet := Subnet{Id: aws.StringValue(ec2Subnet.SubnetId), AvailabilityZone: aws.StringValue(ec2Subnet.AvailabilityZone)}
subnets = append(subnets, subnet)
}
return subnets, nil
}
// GetRandomPrivateCidrBlock gets a random CIDR block from the range of acceptable private IP addresses per RFC 1918
// (https://tools.ietf.org/html/rfc1918#section-3)
// The routingPrefix refers to the "/28" in 1.2.3.4/28.
// Note that, as written, this function will return a subset of all valid ranges. Since we will probably use this function
// mostly for generating random CIDR ranges for VPCs and Subnets, having comprehensive set coverage is not essential.
func GetRandomPrivateCidrBlock(routingPrefix int) string {
var o1, o2, o3, o4 int
switch routingPrefix {
case 32:
o1 = random.RandomInt([]int{10, 172, 192})
switch o1 {
case 10:
o2 = random.Random(0, 255)
o3 = random.Random(0, 255)
o4 = random.Random(0, 255)
case 172:
o2 = random.Random(16, 31)
o3 = random.Random(0, 255)
o4 = random.Random(0, 255)
case 192:
o2 = 168
o3 = random.Random(0, 255)
o4 = random.Random(0, 255)
}
case 31, 30, 29, 28, 27, 26, 25:
fallthrough
case 24:
o1 = random.RandomInt([]int{10, 172, 192})
switch o1 {
case 10:
o2 = random.Random(0, 255)
o3 = random.Random(0, 255)
o4 = 0
case 172:
o2 = 16
o3 = 0
o4 = 0
case 192:
o2 = 168
o3 = 0
o4 = 0
}
case 23, 22, 21, 20, 19:
fallthrough
case 18:
o1 = random.RandomInt([]int{10, 172, 192})
switch o1 {
case 10:
o2 = 0
o3 = 0
o4 = 0
case 172:
o2 = 16
o3 = 0
o4 = 0
case 192:
o2 = 168
o3 = 0
o4 = 0
}
}
return fmt.Sprintf("%d.%d.%d.%d/%d", o1, o2, o3, o4, routingPrefix)
}
// GetFirstTwoOctets gets the first two octets from a CIDR block.
func GetFirstTwoOctets(cidrBlock string) string {
ipAddr := strings.Split(cidrBlock, "/")[0]
octets := strings.Split(ipAddr, ".")
return octets[0] + "." + octets[1]
}