Commit 15309172 authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

Import resource from no source

Since quantity (initial supply) has been removed from Policy,
there is no direct way to set the initial available resource,
for example the total available VMs for holding cyclades.

This is now achieved by issuing (and accepting) a commission
from a source set to None.
parent 79bed174
......@@ -306,8 +306,10 @@ class QuotaholderDjangoDBCallpoint(object):
# Source
try:
h = db_get_holding(holder=holder, resource=resource,
for_update=True)
h = (db_get_holding(holder=holder, resource=resource,
for_update=True)
if holder is not None
else None)
except Holding.DoesNotExist:
m = ("%s has no stock of %s." % (holder, resource))
raise NoStockError(m,
......@@ -332,13 +334,15 @@ class QuotaholderDjangoDBCallpoint(object):
limit=0)
if quantity >= 0:
operations.prepare(Export, h, quantity)
if h is not None:
operations.prepare(Export, h, quantity)
operations.prepare(Import, th, quantity)
else: # release
abs_quantity = -quantity
operations.prepare(Reclaim, h, abs_quantity)
if h is not None:
operations.prepare(Reclaim, h, abs_quantity)
operations.prepare(Release, th, abs_quantity)
Provision.objects.create(serial=commission,
......@@ -356,20 +360,32 @@ class QuotaholderDjangoDBCallpoint(object):
commission, s_holding, t_holding,
provision, log_time, reason):
s_holder = s_holding.holder
t_holder = t_holding.holder
if s_holding is not None:
s_holder = s_holding.holder
s_capacity = s_holding.capacity
s_imported_min = s_holding.imported_min
s_imported_max = s_holding.imported_max
s_stock_min = s_holding.stock_min
s_stock_max = s_holding.stock_max
else:
s_holder = None
s_capacity = None
s_imported_min = None
s_imported_max = None
s_stock_min = None
s_stock_max = None
kwargs = {
'serial': commission.serial,
'name': commission.name,
'source': s_holder,
'target': t_holder,
'target': t_holding.holder,
'resource': provision.resource,
'source_capacity': s_holding.capacity,
'source_imported_min': s_holding.imported_min,
'source_imported_max': s_holding.imported_max,
'source_stock_min': s_holding.stock_min,
'source_stock_max': s_holding.stock_max,
'source_capacity': s_capacity,
'source_imported_min': s_imported_min,
'source_imported_max': s_imported_max,
'source_stock_min': s_stock_min,
'source_stock_max': s_stock_max,
'target_capacity': t_holding.capacity,
'target_imported_min': t_holding.imported_min,
'target_imported_max': t_holding.imported_max,
......@@ -402,8 +418,10 @@ class QuotaholderDjangoDBCallpoint(object):
provisions = db_filter_provision(serial=serial, for_update=True)
for pv in provisions:
try:
h = db_get_holding(holder=pv.holder,
resource=pv.resource, for_update=True)
h = (db_get_holding(holder=pv.holder,
resource=pv.resource, for_update=True)
if pv.holder is not None
else None)
th = db_get_holding(holder=t, resource=pv.resource,
for_update=True)
except Holding.DoesNotExist:
......@@ -413,12 +431,14 @@ class QuotaholderDjangoDBCallpoint(object):
quantity = pv.quantity
if quantity >= 0:
operations.finalize(Export, h, quantity)
if h is not None:
operations.finalize(Export, h, quantity)
operations.finalize(Import, th, quantity)
else: # release
abs_quantity = -quantity
operations.finalize(Reclaim, h, abs_quantity)
if h is not None:
operations.finalize(Reclaim, h, abs_quantity)
operations.finalize(Release, th, abs_quantity)
reason = 'ACCEPT:' + reason[-121:]
......@@ -447,8 +467,10 @@ class QuotaholderDjangoDBCallpoint(object):
provisions = db_filter_provision(serial=serial, for_update=True)
for pv in provisions:
try:
h = db_get_holding(holder=pv.holder,
resource=pv.resource, for_update=True)
h = (db_get_holding(holder=pv.holder,
resource=pv.resource, for_update=True)
if pv.holder is not None
else None)
th = db_get_holding(holder=t, resource=pv.resource,
for_update=True)
except Holding.DoesNotExist:
......@@ -458,12 +480,14 @@ class QuotaholderDjangoDBCallpoint(object):
quantity = pv.quantity
if quantity >= 0:
operations.undo(Export, h, quantity)
if h is not None:
operations.undo(Export, h, quantity)
operations.undo(Import, th, quantity)
else: # release
abs_quantity = -quantity
operations.undo(Reclaim, h, abs_quantity)
if h is not None:
operations.undo(Reclaim, h, abs_quantity)
operations.undo(Release, th, abs_quantity)
reason = 'REJECT:' + reason[-121:]
......
......@@ -80,7 +80,7 @@ class Provision(Model):
to_field='serial',
related_name='provisions' )
holder = CharField(max_length=4096, db_index=True)
holder = CharField(max_length=4096, db_index=True, null=True)
resource = CharField(max_length=4096, null=False)
quantity = intDecimalField()
......@@ -89,17 +89,17 @@ class Provision(Model):
class ProvisionLog(Model):
serial = BigIntegerField()
source = CharField(max_length=4096)
source = CharField(max_length=4096, null=True)
target = CharField(max_length=4096)
name = CharField(max_length=4096)
issue_time = CharField(max_length=4096)
log_time = CharField(max_length=4096)
resource = CharField(max_length=4096)
source_capacity = intDecimalField()
source_imported_min = intDecimalField()
source_imported_max = intDecimalField()
source_stock_min = intDecimalField()
source_stock_max = intDecimalField()
source_capacity = intDecimalField(null=True)
source_imported_min = intDecimalField(null=True)
source_imported_max = intDecimalField(null=True)
source_stock_min = intDecimalField(null=True)
source_stock_max = intDecimalField(null=True)
target_capacity = intDecimalField()
target_imported_min = intDecimalField()
target_imported_max = intDecimalField()
......
......@@ -177,6 +177,19 @@ class QHAPITest(QHTestCase):
self.assertEqual(r, [(e0, resource0, 2*QH_PRACTICALLY_INFINITE)
+ DEFAULT_HOLDING + (0,)])
@transaction.commit_on_success
def initialize_holding(self, holder, resource, quantity):
s = self.qh.issue_commission(clientkey=self.client, target=holder,
name='initialize',
provisions=[(None, resource, quantity)])
self.qh.accept_commission(clientkey=self.client, serials=[s])
@transaction.commit_on_success
def issue_commission(self, target, provisions):
return self.qh.issue_commission(clientkey=self.client, target=target,
name='something',
provisions=provisions)
@transaction.commit_on_success
def test_0090_commissions(self):
e0 = self.rand_holder()
......@@ -185,36 +198,24 @@ class QHAPITest(QHTestCase):
c0, = self.new_quota(e0, resource)
c1, = self.new_quota(e1, resource)
@transaction.commit_on_success
def f():
self.qh.reset_holding(
reset_holding=[(e1, resource, c1, c1, c1, c1)])
most = min(c0, c1)
if most < 0:
raise AssertionError("%s <= 0" % most)
self.initialize_holding(e1, resource, c1)
return self.qh.issue_commission(clientkey=self.client, target=e0,
name='something',
provisions=[(e1, resource, most)])
most = min(c0, c1)
if most < 0:
raise AssertionError("%s <= 0" % most)
r = f()
self.assertEqual(r, 1)
@transaction.commit_on_success
def f():
self.qh.issue_commission(clientkey=self.client, target=e0,
name='something',
provisions=[(e1, resource, 1)])
s1 = self.issue_commission(target=e0,
provisions=[(e1, resource, most)])
self.assertGreater(s1, 0)
with self.assertRaises(CommissionValueException):
f()
self.issue_commission(target=e0,
provisions=[(e1, resource, 1)])
r = self.qh.get_pending_commissions(clientkey=self.client)
self.assertEqual(list(r), [1])
self.assertEqual(list(r), [s1])
r = self.qh.resolve_pending_commissions(clientkey=self.client,
max_serial=1, accept_set=[1])
max_serial=s1, accept_set=[s1])
r = self.qh.get_pending_commissions(clientkey=self.client)
self.assertEqual(list(r), [])
......@@ -230,9 +231,8 @@ class QHAPITest(QHTestCase):
self.new_quota(et1, resource, (15,))
self.new_quota(et2, resource, (15,))
self.qh.reset_holding(
reset_holding=[(es1, resource, 10, 10, 10, 10),
(es2, resource, 10, 10, 10, 10)])
self.initialize_holding(es1, resource, 10)
self.initialize_holding(es2, resource, 10)
with self.assertRaises(NoStockError) as cm:
self.qh.issue_commission(clientkey=self.client, target=et1,
......@@ -280,8 +280,7 @@ class QHAPITest(QHTestCase):
(e0, resource, 10, 0),
(e1, resource, 10, 0)])
self.qh.reset_holding(
reset_holding=[(sys, resource, 10, 10, 10, 10)])
self.initialize_holding(sys, resource, 10)
s0 = self.qh.issue_commission(clientkey=self.client, target=e0,
name='a commission',
......@@ -313,8 +312,8 @@ class QHAPITest(QHTestCase):
e = self.rand_holder()
resource = self.rand_resource()
limits = self.new_quota(e, resource, (2,))
self.qh.reset_holding(
reset_holding=[(e, resource, 1, 1, 1, 1)])
self.initialize_holding(e, resource, 1)
with self.assertRaises(QuotaholderError) as cm:
self.qh.release_holding(release_holding=[(e, resource)])
......@@ -337,8 +336,7 @@ class QHAPITest(QHTestCase):
es = self.rand_holder()
limits_s = self.new_quota(es, resource, (3,))
self.qh.reset_holding(
reset_holding=[(es, resource, 3, 3, 3, 3)])
self.initialize_holding(es, resource, 3)
e = self.rand_holder()
limits = self.new_quota(e, resource, (2,))
......@@ -385,7 +383,9 @@ class QHAPITest(QHTestCase):
target = "test_015_release_nocapacity_target"
flags = 0
qh.init_holding(init_holding=[(source, resource, 6, 6, 6, 6, 6, 0)])
qh.set_quota(set_quota=[(source, resource, 6, 0)])
self.initialize_holding(source, resource, 6)
qh.set_quota(set_quota=[(target, resource, 5, 0)])
serial = qh.issue_commission(clientkey=self.client, target=target,
......
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