Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / volume / util.py @ 58194535

History | View | Annotate | Download (2.4 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
        return volumes.get(id=volume_id, userid=user_id)
16
    except models.Volume.DoesNotExist:
17
        raise exception("Volume %s not found" % volume_id)
18

    
19

    
20
def get_snapshot(user_id, snapshot_id, exception=faults.ItemNotFound):
21
    try:
22
        with image_backend(user_id) as b:
23
            return b.get_snapshot(user_id, snapshot_id)
24
    except faults.ItemNotFound:
25
        raise exception("Snapshot %s not found" % snapshot_id)
26

    
27

    
28
def get_image(user_id, image_id, exception=faults.ItemNotFound):
29
    try:
30
        return get_image_dict(image_id, user_id)
31
    except faults.ItemNotFound:
32
        raise exception("Image %s not found" % image_id)
33

    
34

    
35
def get_server(user_id, server_id, for_update=False,
36
               exception=faults.ItemNotFound):
37
    try:
38
        return get_vm(server_id, user_id, for_update=for_update,
39
                      non_deleted=True, non_suspended=True)
40
    except faults.ItemNotFound:
41
        raise exception("Server %s not found" % server_id)
42

    
43

    
44
VOLUME_URL = \
45
    join_urls(BASE_HOST,
46
              get_service_path(cyclades_services, "volume", version="v2.0"))
47

    
48
VOLUMES_URL = join_urls(VOLUME_URL, "volumes/")
49
SNAPSHOTS_URL = join_urls(VOLUME_URL, "snapshots/")
50

    
51

    
52
def volume_to_links(volume_id):
53
    href = join_urls(VOLUMES_URL, str(volume_id))
54
    return [{"rel": rel, "href": href} for rel in ("self", "bookmark")]
55

    
56

    
57
def snapshot_to_links(snapshot_id):
58
    href = join_urls(SNAPSHOTS_URL, str(snapshot_id))
59
    return [{"rel": rel, "href": href} for rel in ("self", "bookmark")]
60

    
61

    
62
def get_disk_template_provider(disk_template):
63
    """Extract provider from disk template.
64

65
    Provider for `ext` disk_template is encoded in the disk template
66
    name, which is formed `ext_<provider_name>`. Provider is None
67
    for all other disk templates.
68

69
    """
70
    provider = None
71
    if disk_template.startswith("ext") and "_" in disk_template:
72
        disk_template, provider = disk_template.split("_", 1)
73
    return disk_template, provider