Commit 755aa5fe authored by Giorgos Korfiatis's avatar Giorgos Korfiatis Committed by Christos Stavrakakis
Browse files

cyclades: Resolve undecided serial in force mode

Reject a serial with pending=True only when we need to force a new
commission on a given resource.

Also improve handling of serial state: make sure pending is set to False
when we decide whether to accept or reject a serial. Mark a serial to
reject by setting accept=False.
parent 7c0efb7b
......@@ -32,6 +32,7 @@
from django.test import TransactionTestCase
#from snf_django.utils.testing import mocked_quotaholder
from synnefo.logic import servers
from synnefo import quotas
from synnefo.db import models_factory as mfactory, models
from mock import patch
......@@ -167,6 +168,8 @@ class ServerCommandTest(TransactionTestCase):
vm.task = None
vm.task_job_id = None
vm.save()
with mocked_quotaholder():
quotas.accept_serial(vm.serial)
mrapi().RebootInstance.return_value = 1
with mocked_quotaholder():
servers.reboot(vm, "HARD")
......@@ -180,10 +183,8 @@ class ServerCommandTest(TransactionTestCase):
serial = vm.serial
mrapi().StartupInstance.return_value = 1
with mocked_quotaholder() as m:
servers.start(vm)
m.resolve_commissions.assert_called_once_with([],
[serial.serial])
self.assertTrue(m.issue_one_commission.called)
with self.assertRaises(quotas.ResolveError):
servers.start(vm)
# Not pending, rejct
vm.task = None
vm.serial = mfactory.QuotaHolderSerialFactory(serial=400,
......
......@@ -106,6 +106,7 @@ def issue_commission(user, source, provisions, name="",
if serial:
serial_info = {"serial": serial}
if auto_accept:
serial_info["pending"] = False
serial_info["accept"] = True
serial_info["resolved"] = True
return QuotaHolderSerial.objects.create(**serial_info)
......@@ -114,7 +115,9 @@ def issue_commission(user, source, provisions, name="",
def accept_serial(serial, strict=True):
assert serial.pending or serial.accept
response = resolve_commissions(accept=[serial.serial], strict=strict)
serial.pending = False
serial.accept = True
serial.resolved = True
serial.save()
......@@ -122,8 +125,10 @@ def accept_serial(serial, strict=True):
def reject_serial(serial, strict=True):
assert serial.pending or not serial.accept
response = resolve_commissions(reject=[serial.serial], strict=strict)
serial.reject = True
serial.pending = False
serial.accept = False
serial.resolved = True
serial.save()
return response
......@@ -238,16 +243,7 @@ def issue_and_accept_commission(resource, delete=False):
7) COMMIT!
"""
previous_serial = resource.serial
if previous_serial is not None and not previous_serial.resolved:
if previous_serial.pending:
msg = "Issuing commission for resource '%s' while previous serial"\
" '%s' is still pending." % (resource, previous_serial)
raise Exception(msg)
elif previous_serial.accept:
accept_serial(previous_serial, strict=False)
else:
reject_serial(previous_serial, strict=False)
resolve_commission(resource.serial)
try:
# Convert resources in the format expected by Quotaholder
......@@ -363,7 +359,7 @@ def handle_resource_commission(resource, action, commission_name,
"""
# Try to resolve previous serial
resolve_commission(resource.serial)
resolve_commission(resource.serial, force=force)
# Check if action is quotable and issue the corresponding commission
serial = None
......@@ -382,9 +378,16 @@ def handle_resource_commission(resource, action, commission_name,
resource.serial = serial
def resolve_commission(serial):
class ResolveError(Exception):
pass
def resolve_commission(serial, force=False):
if serial is None or serial.resolved:
return
if serial.pending and not force:
m = "Could not resolve commission: serial %s is undecided" % serial
raise ResolveError(m)
log.warning("Resolving pending commission: %s", serial)
if not serial.pending and serial.accept:
accept_serial(serial)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment