Revision 5f90e24c snf-cyclades-app/synnefo/volume/volumes.py
b/snf-cyclades-app/synnefo/volume/volumes.py | ||
---|---|---|
25 | 25 |
if len(sources) > 1: |
26 | 26 |
raise faults.BadRequest("Volume can not have more than one source!") |
27 | 27 |
|
28 |
source_volume = None |
|
28 |
# Only ext_ disk template supports cloning from another source |
|
29 |
disk_template = server.flavor.disk_template |
|
30 |
if not disk_template.startswith("ext_") and sources: |
|
31 |
msg = ("Volumes of '%s' disk template cannot have a source" % |
|
32 |
disk_template) |
|
33 |
raise faults.BadRequest(msg) |
|
34 |
|
|
35 |
origin = None |
|
36 |
source = None |
|
29 | 37 |
if source_volume_id is not None: |
30 | 38 |
source_volume = util.get_volume(user_id, source_volume_id, |
31 | 39 |
for_update=True, |
32 | 40 |
exception=faults.BadRequest) |
33 |
source_snapshot = None |
|
34 |
if source_snapshot_id is not None: |
|
41 |
# Check that volume is ready to be snapshotted |
|
42 |
if source_volume.status != "AVAILABLE": |
|
43 |
msg = ("Cannot take a snapshot while snapshot is in '%s' state" |
|
44 |
% source_volume.status) |
|
45 |
raise faults.BadRequest(msg) |
|
46 |
source = Volume.SOURCE_VOLUME_PREFIX + str(source_volume_id) |
|
47 |
origin = source_volume.backend_volume_uuid |
|
48 |
elif source_snapshot_id is not None: |
|
35 | 49 |
source_snapshot = util.get_snapshot(user_id, source_snapshot_id, |
36 | 50 |
exception=faults.BadRequest) |
37 |
source_image = None |
|
38 |
if source_image_id is not None: |
|
51 |
# TODO: Check the state of the snapshot!! |
|
52 |
origin = source_snapshot["checksum"] |
|
53 |
source = Volume.SOURCE_SNAPSHOT_PREFIX + str(source_snapshot_id) |
|
54 |
elif source_image_id is not None: |
|
39 | 55 |
source_image = util.get_image(user_id, source_image_id, |
40 | 56 |
exception=faults.BadRequest) |
57 |
origin = source_image["checksum"] |
|
58 |
source = Volume.SOURCE_IMAGE_PREFIX + str(source_image_id) |
|
41 | 59 |
|
42 | 60 |
volume = Volume.objects.create(userid=user_id, |
43 | 61 |
size=size, |
44 | 62 |
name=name, |
45 | 63 |
machine=server, |
46 | 64 |
description=description, |
47 |
source_volume=source_volume, |
|
48 |
source_image_id=source_image_id, |
|
49 |
source_snapshot_id=source_snapshot_id, |
|
65 |
source=source, |
|
66 |
origin=origin, |
|
50 | 67 |
#volume_type=volume_type, |
51 | 68 |
status="CREATING") |
52 | 69 |
|
... | ... | |
54 | 71 |
for meta_key, meta_val in metadata.items(): |
55 | 72 |
volume.metadata.create(key=meta_key, value=meta_val) |
56 | 73 |
|
57 |
# Annote volume with snapshot/image information |
|
58 |
volume.source_snapshot = source_snapshot |
|
59 |
volume.source_image = source_image |
|
60 |
|
|
61 | 74 |
# Create the disk in the backend |
62 | 75 |
volume.backendjobid = backend.attach_volume(server, volume) |
63 | 76 |
volume.save() |
... | ... | |
67 | 80 |
|
68 | 81 |
@transaction.commit_on_success |
69 | 82 |
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()
|
|
83 |
"""Delete a Volume"""
|
|
84 |
# A volume is deleted by detaching it from the server that is attached.
|
|
85 |
# Deleting a detached volume is not implemented.
|
|
86 |
if volume.index == 0:
|
|
87 |
raise faults.BadRequest("Cannot detach the root volume of a server")
|
|
75 | 88 |
|
76 |
log.info("Deleted volume %s", volume) |
|
89 |
if volume.machine_id is not None: |
|
90 |
volume.backendjobid = backend.detach_volume(volume.machine, volume) |
|
91 |
log.info("Detach volume '%s' from server '%s', job: %s", |
|
92 |
volume.id, volume.machine_id, volume.backendjobid) |
|
93 |
else: |
|
94 |
raise faults.BadRequest("Cannot delete a detached volume") |
|
77 | 95 |
|
78 | 96 |
return volume |
79 | 97 |
|
Also available in: Unified diff