/
aws_network.go
91 lines (74 loc) · 2.76 KB
/
aws_network.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
package cloudprovider
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/patrickmn/go-cache"
"net"
"time"
)
var _ CloudNetwork = &AwsNetwork{}
// The AwsNetwork is the AWS implementation of the CloudNetwork
type AwsNetwork struct {
provider *AwsCloudProvider
name string // Name of the AWS Subnet
arn string // ID of the AWS Subnet
availabilityZone string // AWS availability zone this subnet is attached to
cidr *net.IPNet // CIDR of the subnet
instancesInSubnet *cache.Cache // Cache containing all instances with interfaces in this subnet
availableIPCount int64 // Number of free IP addresses of this subnet
defaultForAvailabilityZone bool // This is the default subnet for the availability zone
}
// CreateNetwork - creates an AWS subnet into an AwsNetwork
func CreateNetwork(provider *AwsCloudProvider, subnet *ec2.Subnet) (*AwsNetwork, error) {
var err error
result := &AwsNetwork{
provider: provider,
name: *subnet.SubnetId,
arn: *subnet.SubnetArn,
availabilityZone: *subnet.AvailabilityZone,
availableIPCount: *subnet.AvailableIpAddressCount,
defaultForAvailabilityZone: *subnet.DefaultForAz,
}
result.cidr, err = result.convertToIPNet(subnet.CidrBlock)
if err != nil {
return nil, err
}
result.instancesInSubnet = cache.New(time.Minute, 5*time.Minute)
return result, nil
}
// converts a string into IPNet. Could have been directly inside the method but so it's more expressive.
func (n *AwsNetwork) convertToIPNet(cidr *string) (*net.IPNet, error) {
_, result, err := net.ParseCIDR(*cidr)
if err != nil {
return nil, err
}
return result, nil
}
// IsIPInNetwork checks if the given IP is from the network.
func (n *AwsNetwork) IsIPInNetwork(ip *net.IP) bool {
return n.cidr.Contains(*ip)
}
// Name - the name of the network.
func (n *AwsNetwork) Name() string {
return n.name
}
// URI - the cloud provider URI for referencing this network
func (n *AwsNetwork) URI() string {
return n.arn
}
// FailureZone - The failure zone this network is configured into
func (n *AwsNetwork) FailureZone() string {
return n.availabilityZone
}
// DefaultForFailureZone - This network is the default network for all instances within this failure zone
func (n *AwsNetwork) DefaultForFailureZone() bool {
return n.defaultForAvailabilityZone
}
// Cidr - the cidr of the network
func (n *AwsNetwork) Cidr() *net.IPNet {
return n.cidr
}
// AvailableIPCount - the available IPs. ATTENTION: since the data is cached for some minutes, the value may not be
// correct. Use with care.
func (n *AwsNetwork) AvailableIPCount() int64 {
return n.availableIPCount
}