Revision f82dfec6 snf-cyclades-app/synnefo/logic/networks.py
b/snf-cyclades-app/synnefo/logic/networks.py | ||
---|---|---|
30 | 30 |
# documentation are those of the authors and should not be |
31 | 31 |
# interpreted as representing official policies, either expressed |
32 | 32 |
# or implied, of GRNET S.A. |
33 |
import ipaddr |
|
34 | 33 |
|
35 | 34 |
from functools import wraps |
36 | 35 |
from django.db import transaction |
37 | 36 |
|
38 |
from django.conf import settings |
|
39 | 37 |
from snf_django.lib.api import faults |
40 | 38 |
from synnefo.api import util |
41 | 39 |
from synnefo import quotas |
42 |
from synnefo.db.models import Network, Backend, Subnet
|
|
40 |
from synnefo.db.models import Network, Backend |
|
43 | 41 |
from synnefo.db.utils import validate_mac |
44 | 42 |
from synnefo.db.pools import EmptyPool |
45 |
from synnefo.logic import backend as backend_mod |
|
43 |
from synnefo.logic import backend as backend_mod, subnets
|
|
46 | 44 |
|
47 | 45 |
from logging import getLogger |
48 | 46 |
log = getLogger(__name__) |
... | ... | |
83 | 81 |
raise faults.BadRequest("IPv6 only networks can not be floating" |
84 | 82 |
" pools.") |
85 | 83 |
# Check that network parameters are valid |
86 |
validate_network_params(subnet, gateway, subnet6, gateway6)
|
|
84 |
subnets.validate_subnet_params(subnet, gateway, subnet6, gateway6)
|
|
87 | 85 |
|
88 | 86 |
try: |
89 | 87 |
fmode, flink, fmac_prefix, ftags = util.values_from_flavor(flavor) |
... | ... | |
120 | 118 |
state='ACTIVE') |
121 | 119 |
|
122 | 120 |
if subnet: |
123 |
s = Subnet.objects.create(network=network, |
|
124 |
ipversion=4, |
|
125 |
cidr=subnet, |
|
126 |
gateway=gateway, |
|
127 |
dhcp=dhcp) |
|
128 |
s.ip_pools.create(size=0) |
|
129 |
|
|
121 |
subnets._create_subnet(network.id, cidr=subnet, name="", ipversion=4, |
|
122 |
gateway=gateway, dhcp=dhcp, user_id=userid) |
|
130 | 123 |
if subnet6: |
131 |
Subnet.objects.create(network=network, |
|
132 |
ipversion=6, |
|
133 |
cidr=subnet6, |
|
134 |
gateway=gateway6, |
|
135 |
dhcp=dhcp) |
|
124 |
subnets._create_subnet(network.id, cidr=subnet6, name="", ipversion=6, |
|
125 |
gateway=gateway6, dhcp=dhcp, user_id=userid) |
|
136 | 126 |
|
137 | 127 |
# Issue commission to Quotaholder and accept it since at the end of |
138 | 128 |
# this transaction the Network object will be created in the DB. |
... | ... | |
179 | 169 |
# If network does not exist in any backend, update the network state |
180 | 170 |
backend_mod.update_network_state(network) |
181 | 171 |
return network |
182 |
|
|
183 |
|
|
184 |
def validate_network_params(subnet=None, gateway=None, subnet6=None, |
|
185 |
gateway6=None): |
|
186 |
if subnet: |
|
187 |
try: |
|
188 |
# Use strict option to not all subnets with host bits set |
|
189 |
network = ipaddr.IPv4Network(subnet, strict=True) |
|
190 |
except ValueError: |
|
191 |
raise faults.BadRequest("Invalid network IPv4 subnet") |
|
192 |
|
|
193 |
# Check that network size is allowed! |
|
194 |
prefixlen = network.prefixlen |
|
195 |
if prefixlen > 29 or prefixlen <= settings.MAX_CIDR_BLOCK: |
|
196 |
raise faults.OverLimit( |
|
197 |
message="Unsupported network size", |
|
198 |
details="Netmask must be in range: (%s, 29]" % |
|
199 |
settings.MAX_CIDR_BLOCK) |
|
200 |
if gateway: # Check that gateway belongs to network |
|
201 |
try: |
|
202 |
gateway = ipaddr.IPv4Address(gateway) |
|
203 |
except ValueError: |
|
204 |
raise faults.BadRequest("Invalid network IPv4 gateway") |
|
205 |
if not gateway in network: |
|
206 |
raise faults.BadRequest("Invalid network IPv4 gateway") |
|
207 |
|
|
208 |
if subnet6: |
|
209 |
try: |
|
210 |
# Use strict option to not all subnets with host bits set |
|
211 |
network6 = ipaddr.IPv6Network(subnet6, strict=True) |
|
212 |
except ValueError: |
|
213 |
raise faults.BadRequest("Invalid network IPv6 subnet") |
|
214 |
# Check that network6 is an /64 subnet, because this is imposed by |
|
215 |
# 'mac2eui64' utiity. |
|
216 |
if network6.prefixlen != 64: |
|
217 |
msg = ("Unsupported IPv6 subnet size. Network netmask must be" |
|
218 |
" /64") |
|
219 |
raise faults.BadRequest(msg) |
|
220 |
if gateway6: |
|
221 |
try: |
|
222 |
gateway6 = ipaddr.IPv6Address(gateway6) |
|
223 |
except ValueError: |
|
224 |
raise faults.BadRequest("Invalid network IPv6 gateway") |
|
225 |
if not gateway6 in network6: |
|
226 |
raise faults.BadRequest("Invalid network IPv6 gateway") |
Also available in: Unified diff