Revision 9e8be4fb snf-cyclades-app/synnefo/api/util.py
b/snf-cyclades-app/synnefo/api/util.py | ||
---|---|---|
213 | 213 |
return disk_template, provider |
214 | 214 |
|
215 | 215 |
|
216 |
def get_network(network_id, user_id, for_update=False): |
|
216 |
def get_network(network_id, user_id, for_update=False, non_deleted=False):
|
|
217 | 217 |
"""Return a Network instance or raise ItemNotFound.""" |
218 | 218 |
|
219 | 219 |
try: |
... | ... | |
221 | 221 |
objects = Network.objects |
222 | 222 |
if for_update: |
223 | 223 |
objects = objects.select_for_update() |
224 |
return objects.get(Q(userid=user_id) | Q(public=True), id=network_id) |
|
224 |
network = objects.get(Q(userid=user_id) | Q(public=True), |
|
225 |
id=network_id) |
|
226 |
if non_deleted and network.deleted: |
|
227 |
raise faults.BadRequest("Networkhas been deleted.") |
|
228 |
return network |
|
225 | 229 |
except (ValueError, Network.DoesNotExist): |
226 | 230 |
raise faults.ItemNotFound('Network not found.') |
227 | 231 |
|
... | ... | |
336 | 340 |
return address |
337 | 341 |
|
338 | 342 |
|
343 |
def allocate_public_ip(networks=None): |
|
344 |
"""Allocate an IP address from public networks.""" |
|
345 |
if networks is None: |
|
346 |
networks = Network.objects.select_for_update().filter(public=True, |
|
347 |
deleted=False) |
|
348 |
for network in networks: |
|
349 |
try: |
|
350 |
address = get_network_free_address(network) |
|
351 |
except: |
|
352 |
pass |
|
353 |
else: |
|
354 |
return network, address |
|
355 |
msg = "Can not allocate public IP. Public networks are full." |
|
356 |
log.error(msg) |
|
357 |
raise faults.OverLimit(msg) |
|
358 |
|
|
359 |
|
|
339 | 360 |
def get_nic(machine, network): |
340 | 361 |
try: |
341 | 362 |
return NetworkInterface.objects.get(machine=machine, network=network) |
Also available in: Unified diff