Commit 7bdec18f authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

add_quota API call

parent c8d9e13c
......@@ -76,6 +76,10 @@ Quantity = Integer(classname='Quantity', null=True)
Capacity = Nonnegative(classname='Capacity', null=True)
ImportLimit = Nonnegative(classname='ImportLimit', null=True)
ExportLimit = Nonnegative(classname='ExportLimit', null=True)
QuantityDelta = Integer(classname='QuantityDelta', null=True)
CapacityDelta = Integer(classname='CapacityDelta', null=True)
ImportLimitDelta = Integer(classname='ImportLimitDelta', null=True)
ExportLimitDelta = Integer(classname='ExportLimitDelta', null=True)
Imported = Nonnegative(classname='Imported')
Exported = Nonnegative(classname='Exported')
Returned = Nonnegative(classname='Returned')
......@@ -230,6 +234,16 @@ class QuotaholderAPI(Specificator):
rejected = ListOf(Entity, Resource)
return rejected
def add_quota (
self,
context = Context,
add_quota = ListOf( Entity, Resource, Key,
QuantityDelta, CapacityDelta,
ImportLimitDelta, ExportLimitDelta )
):
rejected = ListOf(Entity, Resource)
return rejected
def issue_commission (
self,
context = Context,
......
......@@ -506,6 +506,59 @@ class QuotaholderDjangoDBCallpoint(Callpoint):
raise ReturnButFail(rejected)
return rejected
def add_quota(self, context={}, add_quota=()):
rejected = []
append = rejected.append
for ( entity, resource, key,
quantity, capacity,
import_limit, export_limit ) in add_quota:
try:
e = Entity.objects.get(entity=entity, key=key)
except Entity.DoesNotExist:
append((entity, resource))
continue
try:
h = db_get_holding(entity=entity, resource=resource,
for_update=True)
p = h.policy
except Holding.DoesNotExist:
h = Holding(entity=e, resource=resource, flags=0)
p = None
policy = newname('policy_')
newp = Policy(policy=policy)
newp.quantity = _add(p.quantity if p else 0, quantity)
newp.capacity = _add(p.capacity if p else 0, capacity)
newp.import_limit = _add(p.import_limit if p else 0,
import_limit)
newp.export_limit = _add(p.export_limit if p else 0,
export_limit)
new_values = [newp.capacity,
newp.import_limit, newp.export_limit]
if any(map(_isneg, new_values)):
append((entity, resource))
continue
h.policy = newp
# the order is intentionally reversed so that it
# would break if we are not within a transaction.
# Has helped before.
h.save()
newp.save()
if p is not None and p.holding_set.count() == 0:
p.delete()
if rejected:
raise ReturnButFail(rejected)
return rejected
def issue_commission(self, context = {},
clientkey = None,
target = None,
......@@ -864,6 +917,19 @@ class QuotaholderDjangoDBCallpoint(Callpoint):
return timeline
def _add(x, y):
if x is None or y is None:
return None
return x + y
def _update(dest, source, attr, delta):
dest_attr = getattr(dest, attr)
dest_attr = _add(getattr(source, attr, 0), delta)
def _isneg(x):
if x is None:
return False
return x < 0
API_Callpoint = QuotaholderDjangoDBCallpoint
......@@ -223,6 +223,40 @@ class QHAPITest(QHTestCase):
self.assertEqual(r, [])
return limits
def test_0081_add_quota(self):
e0, k0 = self.new_entity()
e1, k1 = self.new_entity()
resource0 = self.rand_resource()
resource1 = self.rand_resource()
r = self.qh.set_quota(
set_quota=[(e0, resource0, k0) + (5, 5, 5, 5) + (0,),
(e1, resource0, k1) + (5, 5, 5, 5) + (0,)])
self.assertEqual(r, [])
r = self.qh.add_quota(add_quota=[(e0, resource0, k0, 0, (-2), None, 0),
(e0, resource1, k0, 0, None, 5, 5)])
self.assertEqual(r, [])
r = self.qh.get_quota(get_quota=[(e0, resource0, k0),
(e0, resource1, k0)])
self.assertEqual(r, [(e0, resource0, 5, 5 - 2, None, 5)
+ DEFAULT_HOLDING + (0,),
(e0, resource1, 0, None, 5, 5)
+ DEFAULT_HOLDING + (0,)])
# none is committed
r = self.qh.add_quota(add_quota=[(e1, resource0, k1, 0, (-10), None, 0),
(e0, resource1, k0, 1, 0, 0, 0)])
self.assertEqual(r, [(e1, resource0)])
r = self.qh.get_quota(get_quota=[(e1, resource0, k1),
(e0, resource1, k0)])
self.assertEqual(r, [(e1, resource0, 5, 5 , 5, 5)
+ DEFAULT_HOLDING + (0,),
(e0, resource1, 0, None, 5, 5)
+ DEFAULT_HOLDING + (0,)])
def test_009_commissions(self):
e0, k0 = self.new_entity()
e1, k1 = self.new_entity()
......
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