Revision dca7553e snf-cyclades-app/synnefo/api/util.py
b/snf-cyclades-app/synnefo/api/util.py | ||
---|---|---|
33 | 33 |
|
34 | 34 |
import datetime |
35 | 35 |
|
36 |
from base64 import b64encode |
|
36 |
from base64 import b64encode, b64decode
|
|
37 | 37 |
from datetime import timedelta, tzinfo |
38 | 38 |
from functools import wraps |
39 | 39 |
from hashlib import sha256 |
... | ... | |
57 | 57 |
|
58 | 58 |
from synnefo.api.faults import (Fault, BadRequest, BuildInProgress, |
59 | 59 |
ItemNotFound, ServiceUnavailable, Unauthorized, |
60 |
BadMediaType, Forbidden) |
|
60 |
BadMediaType, Forbidden, OverLimit)
|
|
61 | 61 |
from synnefo.db.models import (Flavor, VirtualMachine, VirtualMachineMetadata, |
62 | 62 |
Network, BackendNetwork, NetworkInterface, |
63 | 63 |
BridgePoolTable, MacPrefixPoolTable) |
... | ... | |
200 | 200 |
backend.close() |
201 | 201 |
|
202 | 202 |
|
203 |
def get_image_dict(image_id, user_id): |
|
204 |
image = {} |
|
205 |
img = get_image(image_id, user_id) |
|
206 |
properties = img.get('properties', {}) |
|
207 |
image['backend_id'] = img['location'] |
|
208 |
image['format'] = img['disk_format'] |
|
209 |
image['metadata'] = dict((key.upper(), val) \ |
|
210 |
for key, val in properties.items()) |
|
211 |
return image |
|
212 |
|
|
213 |
|
|
203 | 214 |
def get_flavor(flavor_id, include_deleted=False): |
204 | 215 |
"""Return a Flavor instance or raise ItemNotFound.""" |
205 | 216 |
|
... | ... | |
242 | 253 |
return (None, None) |
243 | 254 |
|
244 | 255 |
|
256 |
def get_public_ip(backend): |
|
257 |
"""Reserve an IP from a public network. |
|
258 |
|
|
259 |
This method should run inside a transaction. |
|
260 |
|
|
261 |
""" |
|
262 |
address = None |
|
263 |
if settings.PUBLIC_ROUTED_USE_POOL: |
|
264 |
(network, address) = allocate_public_address(backend) |
|
265 |
else: |
|
266 |
for net in list(backend_public_networks(backend)): |
|
267 |
pool = net.get_pool() |
|
268 |
if not pool.empty(): |
|
269 |
address = 'pool' |
|
270 |
network = net |
|
271 |
break |
|
272 |
if address is None: |
|
273 |
log.error("Public networks of backend %s are full", backend) |
|
274 |
raise OverLimit("Can not allocate IP for new machine." |
|
275 |
" Public networks are full.") |
|
276 |
return (network, address) |
|
277 |
|
|
278 |
|
|
245 | 279 |
def backend_public_networks(backend): |
246 | 280 |
"""Return available public networks of the backend. |
247 | 281 |
|
... | ... | |
445 | 479 |
raise BadRequest('Unknown network type') |
446 | 480 |
|
447 | 481 |
return link, mac_prefix |
482 |
|
|
483 |
|
|
484 |
def verify_personality(personality): |
|
485 |
for p in personality: |
|
486 |
# Verify that personalities are well-formed |
|
487 |
try: |
|
488 |
assert isinstance(p, dict) |
|
489 |
keys = set(p.keys()) |
|
490 |
allowed = set(['contents', 'group', 'mode', 'owner', 'path']) |
|
491 |
assert keys.issubset(allowed) |
|
492 |
contents = p['contents'] |
|
493 |
if len(contents) > settings.MAX_PERSONALITY_SIZE: |
|
494 |
# No need to decode if contents already exceed limit |
|
495 |
raise OverLimit("Maximum size of personality exceeded") |
|
496 |
if len(b64decode(contents)) > settings.MAX_PERSONALITY_SIZE: |
|
497 |
raise OverLimit("Maximum size of personality exceeded") |
|
498 |
except AssertionError: |
|
499 |
raise BadRequest("Malformed personality in request") |
Also available in: Unified diff