Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / volume / volumes.py @ 19b2c29d

History | View | Annotate | Download (3.1 kB)

1 79a1e9bd Christos Stavrakakis
import logging
2 79a1e9bd Christos Stavrakakis
3 79a1e9bd Christos Stavrakakis
from django.db import transaction
4 79a1e9bd Christos Stavrakakis
from synnefo.db.models import Volume
5 79a1e9bd Christos Stavrakakis
from snf_django.lib.api import faults
6 79a1e9bd Christos Stavrakakis
from synnefo.volume import util
7 79a1e9bd Christos Stavrakakis
from synnefo.logic import backend
8 79a1e9bd Christos Stavrakakis
9 79a1e9bd Christos Stavrakakis
log = logging.getLogger(__name__)
10 79a1e9bd Christos Stavrakakis
11 79a1e9bd Christos Stavrakakis
12 79a1e9bd Christos Stavrakakis
@transaction.commit_on_success
13 79a1e9bd Christos Stavrakakis
def create(user_id, size, server_id, name=None, description=None,
14 79a1e9bd Christos Stavrakakis
           source_volume_id=None, source_snapshot_id=None,
15 79a1e9bd Christos Stavrakakis
           source_image_id=None, metadata=None):
16 79a1e9bd Christos Stavrakakis
17 79a1e9bd Christos Stavrakakis
    if server_id is None:
18 79a1e9bd Christos Stavrakakis
        raise faults.BadRequest("Volume must be attached to server")
19 79a1e9bd Christos Stavrakakis
    server = util.get_server(user_id, server_id, for_update=True,
20 79a1e9bd Christos Stavrakakis
                             exception=faults.BadRequest)
21 79a1e9bd Christos Stavrakakis
22 79a1e9bd Christos Stavrakakis
    # Assert that not more than one source are used
23 79a1e9bd Christos Stavrakakis
    sources = filter(lambda x: x is not None,
24 79a1e9bd Christos Stavrakakis
                     [source_volume_id, source_snapshot_id, source_image_id])
25 79a1e9bd Christos Stavrakakis
    if len(sources) > 1:
26 79a1e9bd Christos Stavrakakis
        raise faults.BadRequest("Volume can not have more than one source!")
27 79a1e9bd Christos Stavrakakis
28 79a1e9bd Christos Stavrakakis
    source_volume = None
29 79a1e9bd Christos Stavrakakis
    if source_volume_id is not None:
30 79a1e9bd Christos Stavrakakis
        source_volume = util.get_volume(user_id, source_volume_id,
31 79a1e9bd Christos Stavrakakis
                                        for_update=True,
32 79a1e9bd Christos Stavrakakis
                                        exception=faults.BadRequest)
33 79a1e9bd Christos Stavrakakis
    source_snapshot = None
34 79a1e9bd Christos Stavrakakis
    if source_snapshot_id is not None:
35 79a1e9bd Christos Stavrakakis
        source_snapshot = util.get_snapshot(user_id, source_snapshot_id,
36 79a1e9bd Christos Stavrakakis
                                            exception=faults.BadRequest)
37 79a1e9bd Christos Stavrakakis
    source_image = None
38 79a1e9bd Christos Stavrakakis
    if source_image_id is not None:
39 79a1e9bd Christos Stavrakakis
        source_image = util.get_image(user_id, source_image_id,
40 79a1e9bd Christos Stavrakakis
                                      exception=faults.BadRequest)
41 79a1e9bd Christos Stavrakakis
42 79a1e9bd Christos Stavrakakis
    volume = Volume.objects.create(userid=user_id,
43 79a1e9bd Christos Stavrakakis
                                   size=size,
44 79a1e9bd Christos Stavrakakis
                                   name=name,
45 79a1e9bd Christos Stavrakakis
                                   machine=server,
46 79a1e9bd Christos Stavrakakis
                                   description=description,
47 79a1e9bd Christos Stavrakakis
                                   source_volume=source_volume,
48 79a1e9bd Christos Stavrakakis
                                   source_image_id=source_image_id,
49 79a1e9bd Christos Stavrakakis
                                   source_snapshot_id=source_snapshot_id,
50 79a1e9bd Christos Stavrakakis
                                   #volume_type=volume_type,
51 79a1e9bd Christos Stavrakakis
                                   status="CREATING")
52 79a1e9bd Christos Stavrakakis
53 79a1e9bd Christos Stavrakakis
    if metadata is not None:
54 79a1e9bd Christos Stavrakakis
        for meta_key, meta_val in metadata.items():
55 79a1e9bd Christos Stavrakakis
            volume.metadata.create(key=meta_key, value=meta_val)
56 79a1e9bd Christos Stavrakakis
57 79a1e9bd Christos Stavrakakis
    # Annote volume with snapshot/image information
58 79a1e9bd Christos Stavrakakis
    volume.source_snapshot = source_snapshot
59 79a1e9bd Christos Stavrakakis
    volume.source_image = source_image
60 79a1e9bd Christos Stavrakakis
61 79a1e9bd Christos Stavrakakis
    # Create the disk in the backend
62 79a1e9bd Christos Stavrakakis
    volume.backendjobid = backend.attach_volume(server, volume)
63 79a1e9bd Christos Stavrakakis
    volume.save()
64 79a1e9bd Christos Stavrakakis
65 79a1e9bd Christos Stavrakakis
    return volume
66 79a1e9bd Christos Stavrakakis
67 79a1e9bd Christos Stavrakakis
68 79a1e9bd Christos Stavrakakis
@transaction.commit_on_success
69 79a1e9bd Christos Stavrakakis
def delete(volume):
70 79a1e9bd Christos Stavrakakis
    if volume.machine_id is not None:
71 79a1e9bd Christos Stavrakakis
        raise faults.BadRequest("Volume %s is still in use by server %s"
72 79a1e9bd Christos Stavrakakis
                                % (volume.id, volume.machine_id))
73 79a1e9bd Christos Stavrakakis
    volume.deleted = True
74 79a1e9bd Christos Stavrakakis
    volume.save()
75 79a1e9bd Christos Stavrakakis
76 79a1e9bd Christos Stavrakakis
    log.info("Deleted volume %s", volume)
77 79a1e9bd Christos Stavrakakis
78 79a1e9bd Christos Stavrakakis
    return volume
79 79a1e9bd Christos Stavrakakis
80 79a1e9bd Christos Stavrakakis
81 79a1e9bd Christos Stavrakakis
@transaction.commit_on_success
82 79a1e9bd Christos Stavrakakis
def rename(volume, new_name):
83 79a1e9bd Christos Stavrakakis
    volume.name = new_name
84 79a1e9bd Christos Stavrakakis
    volume.save()
85 79a1e9bd Christos Stavrakakis
    return volume
86 79a1e9bd Christos Stavrakakis
87 79a1e9bd Christos Stavrakakis
88 79a1e9bd Christos Stavrakakis
@transaction.commit_on_success
89 79a1e9bd Christos Stavrakakis
def update_description(volume, new_description):
90 79a1e9bd Christos Stavrakakis
    volume.description = new_description
91 79a1e9bd Christos Stavrakakis
    volume.save()
92 79a1e9bd Christos Stavrakakis
    return volume