Revision 7f2dbcad snf-cyclades-app/synnefo/api/servers.py
b/snf-cyclades-app/synnefo/api/servers.py | ||
---|---|---|
349 | 349 |
backend=backend, |
350 | 350 |
userid=request.user_uniq, |
351 | 351 |
imageid=image_id, |
352 |
flavor=flavor) |
|
352 |
flavor=flavor, |
|
353 |
action="CREATE") |
|
353 | 354 |
|
354 | 355 |
try: |
355 | 356 |
jobID = create_instance(vm, nic, flavor, image, password, personality) |
... | ... | |
419 | 420 |
except (TypeError, KeyError): |
420 | 421 |
raise faults.BadRequest("Malformed request") |
421 | 422 |
|
422 |
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True) |
|
423 |
vm = util.get_vm(server_id, request.user_uniq, for_update=True, |
|
424 |
non_suspended=True) |
|
423 | 425 |
vm.name = name |
424 | 426 |
vm.save() |
425 | 427 |
|
... | ... | |
439 | 441 |
# overLimit (413) |
440 | 442 |
|
441 | 443 |
log.info('delete_server %s', server_id) |
442 |
vm = util.get_vm(server_id, request.user_uniq, non_suspended=True) |
|
444 |
vm = util.get_vm(server_id, request.user_uniq, for_update=True, |
|
445 |
non_suspended=True) |
|
446 |
start_action(vm, 'DESTROY') |
|
443 | 447 |
delete_instance(vm) |
444 | 448 |
return HttpResponse(status=204) |
445 | 449 |
|
... | ... | |
453 | 457 |
raise faults.BadRequest("Malformed request") |
454 | 458 |
|
455 | 459 |
# Do not allow any action on deleted or suspended VMs |
456 |
vm = util.get_vm(server_id, request.user_uniq, non_deleted=True,
|
|
457 |
non_suspended=True) |
|
460 |
vm = util.get_vm(server_id, request.user_uniq, for_update=True,
|
|
461 |
non_deleted=True, non_suspended=True)
|
|
458 | 462 |
|
459 | 463 |
try: |
460 | 464 |
key = req.keys()[0] |
465 |
if key != 'console': |
|
466 |
start_action(vm, key_to_action(key)) |
|
461 | 467 |
val = req[key] |
462 | 468 |
assert isinstance(val, dict) |
463 | 469 |
return server_actions[key](request, vm, val) |
... | ... | |
467 | 473 |
raise faults.BadRequest("Invalid argument") |
468 | 474 |
|
469 | 475 |
|
476 |
def key_to_action(key): |
|
477 |
"""Map HTTP request key to a VM Action""" |
|
478 |
if key == "shutdown": |
|
479 |
return "STOP" |
|
480 |
if key == "delete": |
|
481 |
return "DESTROY" |
|
482 |
if key == "console": |
|
483 |
return None |
|
484 |
else: |
|
485 |
return key.upper() |
|
486 |
|
|
487 |
|
|
488 |
def start_action(vm, action): |
|
489 |
log.debug("Applying action %s to VM %s", action, vm) |
|
490 |
if not action: |
|
491 |
return |
|
492 |
|
|
493 |
if not action in [x[0] for x in VirtualMachine.ACTIONS]: |
|
494 |
raise faults.ServiceUnavailable("Action %s not supported" % action) |
|
495 |
|
|
496 |
# No actions to deleted VMs |
|
497 |
if vm.deleted: |
|
498 |
raise VirtualMachine.DeletedError |
|
499 |
|
|
500 |
# No actions to machines being built. They may be destroyed, however. |
|
501 |
if vm.operstate == 'BUILD' and action != 'DESTROY': |
|
502 |
raise VirtualMachine.BuildingError |
|
503 |
|
|
504 |
vm.action = action |
|
505 |
vm.backendjobid = None |
|
506 |
vm.backendopcode = None |
|
507 |
vm.backendjobstatus = None |
|
508 |
vm.backendlogmsg = None |
|
509 |
|
|
510 |
vm.save() |
|
511 |
|
|
512 |
|
|
470 | 513 |
@util.api_method('GET') |
471 | 514 |
def list_addresses(request, server_id): |
472 | 515 |
# Normal Response Codes: 200, 203 |
Also available in: Unified diff