Revision 7fede91e snf-cyclades-app/synnefo/api/util.py
b/snf-cyclades-app/synnefo/api/util.py | ||
---|---|---|
56 | 56 |
|
57 | 57 |
from synnefo.api.faults import (Fault, BadRequest, BuildInProgress, |
58 | 58 |
ItemNotFound, ServiceUnavailable, Unauthorized, |
59 |
BadMediaType, OverLimit)
|
|
59 |
BadMediaType) |
|
60 | 60 |
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata, |
61 |
Network, NetworkInterface, BridgePool,
|
|
62 |
MacPrefixPool, Pool)
|
|
61 |
Network, BackendNetwork, NetworkInterface,
|
|
62 |
BridgePool)
|
|
63 | 63 |
|
64 | 64 |
from synnefo.lib.astakos import get_user |
65 | 65 |
from synnefo.plankton.backend import ImageBackend |
66 |
from synnefo.logic import ippool |
|
66 | 67 |
|
67 | 68 |
|
68 | 69 |
log = getLogger('synnefo.api') |
... | ... | |
202 | 203 |
"""Return a Network instance or raise ItemNotFound.""" |
203 | 204 |
|
204 | 205 |
try: |
205 |
if network_id == 'public': |
|
206 |
return Network.objects.get(public=True) |
|
207 |
else: |
|
208 |
network_id = int(network_id) |
|
209 |
return Network.objects.get(id=network_id, userid=user_id) |
|
206 |
network_id = int(network_id) |
|
207 |
return Network.objects.get(id=network_id, userid=user_id) |
|
210 | 208 |
except (ValueError, Network.DoesNotExist): |
211 | 209 |
raise ItemNotFound('Network not found.') |
212 | 210 |
|
213 | 211 |
|
212 |
def backend_public_networks(backend): |
|
213 |
"""Return available public networks of the backend. |
|
214 |
|
|
215 |
Iterator for non-deleted public networks that are available |
|
216 |
to the specified backend. |
|
217 |
|
|
218 |
""" |
|
219 |
for network in Network.objects.filter(public=True, deleted=False): |
|
220 |
if BackendNetwork.objects.filter(network=network, |
|
221 |
backend=backend).exists(): |
|
222 |
yield network |
|
223 |
|
|
224 |
|
|
225 |
def get_network_free_address(network): |
|
226 |
"""Reserve an IP address from the IP Pool of the network. |
|
227 |
|
|
228 |
Raises Network.DoesNotExist , ippool.IPPool.IPPoolExhausted |
|
229 |
|
|
230 |
""" |
|
231 |
|
|
232 |
# Get the Network object in exclusive mode in order to |
|
233 |
# safely (isolated) reserve an IP address |
|
234 |
network = Network.objects.select_for_update().get(id=network.id) |
|
235 |
pool = ippool.IPPool(network) |
|
236 |
address = pool.get_free_address() |
|
237 |
pool.save() |
|
238 |
return address |
|
239 |
|
|
240 |
|
|
214 | 241 |
def get_nic(machine, network): |
215 | 242 |
try: |
216 | 243 |
return NetworkInterface.objects.get(machine=machine, network=network) |
217 | 244 |
except NetworkInterface.DoesNotExist: |
218 | 245 |
raise ItemNotFound('Server not connected to this network.') |
219 | 246 |
|
247 |
|
|
220 | 248 |
def get_nic_from_index(vm, nic_index): |
221 | 249 |
"""Returns the nic_index-th nic of a vm |
222 | 250 |
Error Response Codes: itemNotFound (404), badMediaType (415) |
... | ... | |
230 | 258 |
nic = matching_nics[0] |
231 | 259 |
return nic |
232 | 260 |
|
261 |
|
|
233 | 262 |
def get_request_dict(request): |
234 | 263 |
"""Returns data sent by the client as a python dict.""" |
235 | 264 |
|
... | ... | |
336 | 365 |
raise Unauthorized('No user found.') |
337 | 366 |
if http_method and request.method != http_method: |
338 | 367 |
raise BadRequest('Method not allowed.') |
339 |
|
|
368 |
|
|
340 | 369 |
resp = func(request, *args, **kwargs) |
341 | 370 |
update_response_headers(request, resp) |
342 | 371 |
return resp |
... | ... | |
348 | 377 |
return render_fault(request, fault) |
349 | 378 |
except Fault, fault: |
350 | 379 |
return render_fault(request, fault) |
351 |
except BaseException, e:
|
|
380 |
except BaseException: |
|
352 | 381 |
log.exception('Unexpected error') |
353 | 382 |
fault = ServiceUnavailable('Unexpected error.') |
354 | 383 |
return render_fault(request, fault) |
Also available in: Unified diff