Revision f23fbacf
b/snf-cyclades-app/synnefo/logic/subnets.py | ||
---|---|---|
93 | 93 |
|
94 | 94 |
check_number_of_subnets(network, ipversion) |
95 | 95 |
|
96 |
# Returns the first available IP in the subnet |
|
97 | 96 |
try: |
98 | 97 |
cidr_ip = ipaddr.IPNetwork(cidr) |
99 | 98 |
except ValueError: |
100 | 99 |
raise api.faults.BadRequest("Malformed CIDR") |
101 | 100 |
|
102 | 101 |
if ipversion == 6: |
103 |
validate_subnet_params(None, None, cidr, gateway)
|
|
102 |
validate_subnet_params(subnet6=cidr, gateway6=gateway)
|
|
104 | 103 |
else: |
105 |
validate_subnet_params(cidr, gateway)
|
|
104 |
validate_subnet_params(subnet=cidr, gateway=gateway)
|
|
106 | 105 |
|
107 | 106 |
name = check_name_length(name) |
108 | 107 |
sub = Subnet.objects.create(name=name, network=network, cidr=cidr, |
... | ... | |
113 | 112 |
gateway_ip = ipaddr.IPAddress(gateway) if gateway else None |
114 | 113 |
|
115 | 114 |
if allocation_pools is not None: |
116 |
# If the user specified IP allocation pools, validate them and use them |
|
117 | 115 |
if ipversion == 6: |
118 | 116 |
raise api.faults.Conflict("Can't allocate an IP Pool in IPv6") |
119 |
validate_subpools(allocation_pools, cidr_ip, gateway_ip) |
|
120 |
if allocation_pools is None and ipversion == 4: |
|
117 |
elif ipversion == 4: |
|
121 | 118 |
# Check if the gateway is the first IP of the subnet, in this case |
122 | 119 |
# create a single ip pool |
123 | 120 |
if gateway_ip: |
124 | 121 |
if int(gateway_ip) - int(cidr_ip) == 1: |
125 |
allocation_pools = [[gateway_ip + 1, cidr_ip.broadcast - 1]]
|
|
122 |
allocation_pools = [(gateway_ip + 1, cidr_ip.broadcast - 1)]
|
|
126 | 123 |
else: |
127 | 124 |
# If the gateway isn't the first available ip, create two |
128 | 125 |
# different ip pools adjacent to said ip |
129 |
allocation_pools = (([cidr_ip.network + 1, gateway_ip - 1]),
|
|
130 |
([gateway_ip + 1, cidr_ip.broadcast - 1]))
|
|
126 |
allocation_pools = [(cidr_ip.network + 1, gateway_ip - 1),
|
|
127 |
(gateway_ip + 1, cidr_ip.broadcast - 1)]
|
|
131 | 128 |
else: |
132 |
allocation_pools = [[cidr_ip.network + 1, cidr_ip.broadcast - 1]]
|
|
129 |
allocation_pools = [(cidr_ip.network + 1, cidr_ip.broadcast - 1)]
|
|
133 | 130 |
|
134 | 131 |
if allocation_pools: |
132 |
# Validate the allocation pools |
|
133 |
validate_pools(allocation_pools, cidr_ip, gateway_ip) |
|
135 | 134 |
create_ip_pools(allocation_pools, cidr_ip, sub) |
136 | 135 |
|
137 | 136 |
return sub |
... | ... | |
183 | 182 |
#Utility functions |
184 | 183 |
def create_ip_pools(pools, cidr, subnet): |
185 | 184 |
"""Create IP Pools in the database""" |
186 |
ip_pools = []
|
|
187 |
for pool in pools: |
|
188 |
size = int(pool[1]) - int(pool[0]) + 1 |
|
189 |
base = str(cidr)
|
|
190 |
offset = int(pool[0]) - int(cidr.network)
|
|
191 |
ip_pool = IPPoolTable.objects.create(size=size, offset=offset,
|
|
192 |
base=base, subnet=subnet)
|
|
193 |
ip_pools.append(ip_pool)
|
|
194 |
return ip_pools
|
|
185 |
return [_create_ip_pool(pool, cidr, subnet) for pool in pools]
|
|
186 |
|
|
187 |
|
|
188 |
def _create_ip_pool(pool, cidr, subnet):
|
|
189 |
size = int(pool[1]) - int(pool[0]) + 1
|
|
190 |
base = str(cidr)
|
|
191 |
offset = int(pool[0]) - int(cidr.network)
|
|
192 |
return IPPoolTable.objects.create(size=size, offset=offset,
|
|
193 |
base=base, subnet=subnet)
|
|
195 | 194 |
|
196 | 195 |
|
197 | 196 |
def check_number_of_subnets(network, version): |
... | ... | |
208 | 207 |
return name |
209 | 208 |
|
210 | 209 |
|
211 |
def validate_subpools(pool_list, cidr, gateway):
|
|
210 |
def validate_pools(pool_list, cidr, gateway): |
|
212 | 211 |
"""Validate IP Pools |
213 | 212 |
|
214 | 213 |
Validate the given IP pools are inside the cidr range |
Also available in: Unified diff