Revision 4944a1f8 snf-cyclades-app/synnefo/volume/views.py
b/snf-cyclades-app/synnefo/volume/views.py | ||
---|---|---|
101 | 101 |
|
102 | 102 |
req = utils.get_request_dict(request) |
103 | 103 |
log.debug("create_volume %s", req) |
104 |
|
|
105 | 104 |
user_id = request.user_uniq |
105 |
|
|
106 |
new_volume = req.get("volume") |
|
107 |
if new_volume is None: |
|
108 |
raise faults.BadRequest("Missing 'volume' attribute.") |
|
109 |
|
|
106 | 110 |
# Get and validate 'name' parameter |
107 | 111 |
# TODO: auto generate name |
108 |
name = req.get("name", None)
|
|
112 |
name = new_volume.get("name", None)
|
|
109 | 113 |
if name is None: |
110 | 114 |
raise faults.BadRequest("Volume 'name' is needed.") |
111 | 115 |
# Get and validate 'size' parameter |
112 |
size = req.get("size")
|
|
116 |
size = new_volume.get("size")
|
|
113 | 117 |
if size is None: |
114 | 118 |
raise faults.BadRequest("Volume 'size' is needed.") |
115 | 119 |
try: |
... | ... | |
121 | 125 |
" value. '%s' cannot be accepted." % size) |
122 | 126 |
|
123 | 127 |
# TODO: Fix volume type, validate, etc.. |
124 |
volume_type = req.get("volume_type", None)
|
|
128 |
volume_type = new_volume.get("volume_type", None)
|
|
125 | 129 |
|
126 | 130 |
# Optional parameters |
127 |
description = req.get("description", "")
|
|
128 |
metadata = req.get("metadata", {})
|
|
131 |
description = new_volume.get("description", "")
|
|
132 |
metadata = new_volume.get("metadata", {})
|
|
129 | 133 |
if not isinstance(metadata, dict): |
130 | 134 |
msg = "Volume 'metadata' needs to be a dictionary of key-value pairs."\ |
131 | 135 |
" '%s' can not be accepted." % metadata |
132 | 136 |
raise faults.BadRequest(msg) |
133 | 137 |
|
134 | 138 |
# Id of the volume to clone from |
135 |
source_volume_id = req.get("source_volid")
|
|
139 |
source_volume_id = new_volume.get("source_volid")
|
|
136 | 140 |
# Id of the snapshot to create the volume from |
137 |
source_snapshot_id = req.get("snapshot_id")
|
|
141 |
source_snapshot_id = new_volume.get("snapshot_id")
|
|
138 | 142 |
# Reference to an Image stored in Glance |
139 |
source_image_id = req.get("imageRef")
|
|
143 |
source_image_id = new_volume.get("imageRef")
|
|
140 | 144 |
# TODO: Check that not all of them are used |
141 | 145 |
|
142 |
server_id = req.get("server_id")
|
|
146 |
server_id = new_volume.get("server_id")
|
|
143 | 147 |
if server_id is None: |
144 | 148 |
raise faults.BadRequest("Attribute 'server_id' is mandatory") |
145 | 149 |
|
... | ... | |
217 | 221 |
|
218 | 222 |
|
219 | 223 |
def snapshot_to_dict(snapshot, detail=True): |
220 |
owner = snapshot["owner"] |
|
221 |
status = snapshot["status"] |
|
222 |
progress = snapshot["progress"] |
|
224 |
owner = snapshot['owner'] |
|
225 |
status = snapshot['status'] |
|
226 |
progress = "%s%%" % 100 if status == "ACTIVE" else 0 |
|
227 |
|
|
223 | 228 |
data = { |
224 | 229 |
"id": snapshot["uuid"], |
225 | 230 |
"size": int(snapshot["size"]) >> 30, # gigabytes |
... | ... | |
244 | 249 |
|
245 | 250 |
req = utils.get_request_dict(request) |
246 | 251 |
log.debug("create_snapshot %s", req) |
247 |
|
|
248 | 252 |
user_id = request.user_uniq |
253 |
|
|
254 |
new_snapshot = req.get("snapshot") |
|
255 |
if new_snapshot is None: |
|
256 |
raise faults.BadRequest("Missing 'snapshot' attribute.") |
|
257 |
|
|
249 | 258 |
# Get and validate 'name' parameter |
250 | 259 |
# TODO: auto generate name |
251 |
metadata = req.get("metadata", {})
|
|
260 |
metadata = new_snapshot.get("metadata", {})
|
|
252 | 261 |
if not isinstance(metadata, dict): |
253 | 262 |
msg = "Snapshot 'metadata' needs to be a dictionary of key-value"\ |
254 | 263 |
" pairs. '%s' can not be accepted." % metadata |
255 | 264 |
raise faults.BadRequest(msg) |
256 | 265 |
|
257 |
volume_id = req.get("volume_id", None)
|
|
266 |
volume_id = new_snapshot.get("volume_id", None)
|
|
258 | 267 |
if volume_id is None: |
259 | 268 |
raise faults.BadRequest("'volume_id' attribute is missing.") |
260 | 269 |
volume = util.get_volume(user_id, volume_id, for_update=True, |
261 | 270 |
exception=faults.BadRequest) |
262 | 271 |
|
263 |
name = req.get("name", None)
|
|
272 |
name = new_snapshot.get("name", None)
|
|
264 | 273 |
if name is None: |
265 | 274 |
name = "snapshot_volume_%s_%s" %\ |
266 | 275 |
(volume.id, str(datetime.datetime.now())) |
267 |
description = req.get("description", "")
|
|
276 |
description = new_snapshot.get("description", "")
|
|
268 | 277 |
|
269 | 278 |
# TODO: What to do with force ? |
270 |
force = req.get("force", False)
|
|
279 |
force = new_snapshot.get("force", False)
|
|
271 | 280 |
if not isinstance(force, bool): |
272 | 281 |
raise faults.BadRequest("Invalid value for 'force' attribute.") |
273 | 282 |
|
... | ... | |
277 | 286 |
|
278 | 287 |
# Render response |
279 | 288 |
data = json.dumps(dict(snapshot=snapshot_to_dict(snapshot, detail=False))) |
280 |
return HttpResponse(data, status=200) # TOO: Maybe 202 ?
|
|
289 |
return HttpResponse(data, status=202)
|
|
281 | 290 |
|
282 | 291 |
|
283 | 292 |
@api.api_method(http_method="GET", user_required=True, logger=log) |
Also available in: Unified diff