-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubnet.go
More file actions
112 lines (89 loc) · 3.1 KB
/
subnet.go
File metadata and controls
112 lines (89 loc) · 3.1 KB
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
package window
import (
"net"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
)
type (
Subnet struct {
// The Availability Zone of the subnet.
AvailabilityZoneName string
// The number of unused IP addresses in the subnet. Note that the IP addresses
// for any stopped instances are considered unavailable.
AvailableIpAddressCount int64
// The CIDR block assigned to the subnet.
CidrBlock string
// Indicates whether this is the default subnet for the Availability Zone.
DefaultForAz bool
// Indicates whether instances launched in this subnet receive a public IP address.
MapPublicIpOnLaunch bool
// The current state of the subnet.
State string
// The ID of the subnet.
SubnetId string
// Any tags assigned to the subnet.
Tags []*ec2.Tag
// The ID of the VPC the subnet is in.
VpcId string
// new props
Name string
Id string
CIDR *net.IPNet
VPC *VPC
AvailabilityZone *AvailabilityZone
Instances []*Instance
LambdaFunctions []*LambdaFunction
RouteTables []*RouteTable
ACLs []*ACL
InternetGateway *InternetGateway
NATInstance *Instance
NATGateway *NATGateway
VPNConnections []*VPNConnection
VPCEndpoints []*VPCEndpoint
VPCPeeringConnections []*VPCPeeringConnection
}
SubnetByNameAsc []*Subnet
SubnetByCIDRAsc []*Subnet
)
func (a SubnetByNameAsc) Len() int { return len(a) }
func (a SubnetByNameAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SubnetByNameAsc) Less(i, j int) bool {
return string_less_than(a[i].Name, a[j].Name)
}
func (a SubnetByCIDRAsc) Len() int { return len(a) }
func (a SubnetByCIDRAsc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a SubnetByCIDRAsc) Less(i, j int) bool {
return cidr_less_than(a[i].CIDR, a[j].CIDR)
}
func LoadSubnets(input *ec2.DescribeSubnetsInput) (map[string]*Subnet, error) {
resp, err := EC2Client.DescribeSubnets(input)
if err != nil {
return nil, err
}
subnets := make(map[string]*Subnet, len(resp.Subnets))
for _, ec2subnet := range resp.Subnets {
subnet := &Subnet{
AvailabilityZoneName: aws.StringValue(ec2subnet.AvailabilityZone),
AvailableIpAddressCount: aws.Int64Value(ec2subnet.AvailableIpAddressCount),
CidrBlock: aws.StringValue(ec2subnet.CidrBlock),
DefaultForAz: aws.BoolValue(ec2subnet.DefaultForAz),
MapPublicIpOnLaunch: aws.BoolValue(ec2subnet.MapPublicIpOnLaunch),
State: aws.StringValue(ec2subnet.State),
SubnetId: aws.StringValue(ec2subnet.SubnetId),
Tags: ec2subnet.Tags,
VpcId: aws.StringValue(ec2subnet.VpcId),
}
subnet.Name = TagOrDefault(subnet.Tags, "Name", subnet.SubnetId)
subnet.Id = "subnet:" + subnet.SubnetId
_, subnet.CIDR, _ = net.ParseCIDR(subnet.CidrBlock)
subnets[subnet.SubnetId] = subnet
}
return subnets, nil
}
func (subnet *Subnet) Inactive() bool {
return false
}
func (subnet *Subnet) TotalIPs() int {
bits, size := subnet.CIDR.Mask.Size()
return 1 << uint(size-bits)
}