Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.1 kB)

1
import logging
2

    
3
from django.db import transaction
4
from synnefo.db.models import Volume
5
from snf_django.lib.api import faults
6
from synnefo.volume import util
7
from synnefo.logic import backend
8

    
9
log = logging.getLogger(__name__)
10

    
11

    
12
@transaction.commit_on_success
13
def create(user_id, size, server_id, name=None, description=None,
14
           source_volume_id=None, source_snapshot_id=None,
15
           source_image_id=None, metadata=None):
16

    
17
    if server_id is None:
18
        raise faults.BadRequest("Volume must be attached to server")
19
    server = util.get_server(user_id, server_id, for_update=True,
20
                             exception=faults.BadRequest)
21

    
22
    # Assert that not more than one source are used
23
    sources = filter(lambda x: x is not None,
24
                     [source_volume_id, source_snapshot_id, source_image_id])
25
    if len(sources) > 1:
26
        raise faults.BadRequest("Volume can not have more than one source!")
27

    
28
    source_volume = None
29
    if source_volume_id is not None:
30
        source_volume = util.get_volume(user_id, source_volume_id,
31
                                        for_update=True,
32
                                        exception=faults.BadRequest)
33
    source_snapshot = None
34
    if source_snapshot_id is not None:
35
        source_snapshot = util.get_snapshot(user_id, source_snapshot_id,
36
                                            exception=faults.BadRequest)
37
    source_image = None
38
    if source_image_id is not None:
39
        source_image = util.get_image(user_id, source_image_id,
40
                                      exception=faults.BadRequest)
41

    
42
    volume = Volume.objects.create(userid=user_id,
43
                                   size=size,
44
                                   name=name,
45
                                   machine=server,
46
                                   description=description,
47
                                   source_volume=source_volume,
48
                                   source_image_id=source_image_id,
49
                                   source_snapshot_id=source_snapshot_id,
50
                                   #volume_type=volume_type,
51
                                   status="CREATING")
52

    
53
    if metadata is not None:
54
        for meta_key, meta_val in metadata.items():
55
            volume.metadata.create(key=meta_key, value=meta_val)
56

    
57
    # Annote volume with snapshot/image information
58
    volume.source_snapshot = source_snapshot
59
    volume.source_image = source_image
60

    
61
    # Create the disk in the backend
62
    volume.backendjobid = backend.attach_volume(server, volume)
63
    volume.save()
64

    
65
    return volume
66

    
67

    
68
@transaction.commit_on_success
69
def delete(volume):
70
    if volume.machine_id is not None:
71
        raise faults.BadRequest("Volume %s is still in use by server %s"
72
                                % (volume.id, volume.machine_id))
73
    volume.deleted = True
74
    volume.save()
75

    
76
    log.info("Deleted volume %s", volume)
77

    
78
    return volume
79

    
80

    
81
@transaction.commit_on_success
82
def rename(volume, new_name):
83
    volume.name = new_name
84
    volume.save()
85
    return volume
86

    
87

    
88
@transaction.commit_on_success
89
def update_description(volume, new_description):
90
    volume.description = new_description
91
    volume.save()
92
    return volume