root / snf-cyclades-app / synnefo / volume / util.py @ dd339992
History | View | Annotate | Download (2.8 kB)
1 |
from synnefo.db import models |
---|---|
2 |
from snf_django.lib.api import faults |
3 |
from synnefo.api.util import get_image_dict, get_vm, image_backend |
4 |
from synnefo.cyclades_settings import cyclades_services, BASE_HOST |
5 |
from synnefo.lib import join_urls |
6 |
from synnefo.lib.services import get_service_path |
7 |
|
8 |
|
9 |
def get_volume(user_id, volume_id, for_update=False, |
10 |
exception=faults.ItemNotFound): |
11 |
volumes = models.Volume.objects |
12 |
if for_update:
|
13 |
volumes = volumes.select_for_update() |
14 |
try:
|
15 |
volume_id = int(volume_id)
|
16 |
except (TypeError, ValueError): |
17 |
raise faults.BadRequest("Invalid volume id: %s" % volume_id) |
18 |
try:
|
19 |
return volumes.get(id=volume_id, userid=user_id)
|
20 |
except models.Volume.DoesNotExist:
|
21 |
raise exception("Volume %s not found" % volume_id) |
22 |
|
23 |
|
24 |
def get_snapshot(user_id, snapshot_id, exception=faults.ItemNotFound): |
25 |
try:
|
26 |
with image_backend(user_id) as b: |
27 |
return b.get_snapshot(user_id, snapshot_id)
|
28 |
except faults.ItemNotFound:
|
29 |
raise exception("Snapshot %s not found" % snapshot_id) |
30 |
|
31 |
|
32 |
def get_image(user_id, image_id, exception=faults.ItemNotFound): |
33 |
try:
|
34 |
return get_image_dict(image_id, user_id)
|
35 |
except faults.ItemNotFound:
|
36 |
raise exception("Image %s not found" % image_id) |
37 |
|
38 |
|
39 |
def get_server(user_id, server_id, for_update=False, |
40 |
exception=faults.ItemNotFound): |
41 |
try:
|
42 |
server_id = int(server_id)
|
43 |
except (TypeError, ValueError): |
44 |
raise faults.BadRequest("Invalid server id: %s" % server_id) |
45 |
try:
|
46 |
return get_vm(server_id, user_id, for_update=for_update,
|
47 |
non_deleted=True, non_suspended=True) |
48 |
except faults.ItemNotFound:
|
49 |
raise exception("Server %s not found" % server_id) |
50 |
|
51 |
|
52 |
VOLUME_URL = \ |
53 |
join_urls(BASE_HOST, |
54 |
get_service_path(cyclades_services, "volume", version="v2.0")) |
55 |
|
56 |
VOLUMES_URL = join_urls(VOLUME_URL, "volumes/")
|
57 |
SNAPSHOTS_URL = join_urls(VOLUME_URL, "snapshots/")
|
58 |
|
59 |
|
60 |
def volume_to_links(volume_id): |
61 |
href = join_urls(VOLUMES_URL, str(volume_id))
|
62 |
return [{"rel": rel, "href": href} for rel in ("self", "bookmark")] |
63 |
|
64 |
|
65 |
def snapshot_to_links(snapshot_id): |
66 |
href = join_urls(SNAPSHOTS_URL, str(snapshot_id))
|
67 |
return [{"rel": rel, "href": href} for rel in ("self", "bookmark")] |
68 |
|
69 |
|
70 |
def get_disk_template_provider(disk_template): |
71 |
"""Extract provider from disk template.
|
72 |
|
73 |
Provider for `ext` disk_template is encoded in the disk template
|
74 |
name, which is formed `ext_<provider_name>`. Provider is None
|
75 |
for all other disk templates.
|
76 |
|
77 |
"""
|
78 |
provider = None
|
79 |
if disk_template.startswith("ext") and "_" in disk_template: |
80 |
disk_template, provider = disk_template.split("_", 1) |
81 |
return disk_template, provider
|
82 |
|
83 |
|
84 |
def update_snapshot_status(snapshot_id, user_id, status): |
85 |
with image_backend(user_id) as b: |
86 |
return b.update_status(snapshot_id, status=status)
|