root / snf-quotaholder-app / quotaholder_django / quotaholder_app / callpoint.py @ 2fd11a01
History | View | Annotate | Download (34 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from synnefo.lib.quotaholder.api import ( |
35 |
QuotaholderAPI, |
36 |
InvalidKeyError, NoEntityError, |
37 |
NoQuantityError, NoCapacityError, |
38 |
ExportLimitError, ImportLimitError, |
39 |
DuplicateError) |
40 |
|
41 |
from synnefo.lib.commissioning import \ |
42 |
Callpoint, CorruptedError, InvalidDataError, ReturnButFail |
43 |
from synnefo.lib.commissioning.utils.newname import newname |
44 |
|
45 |
from django.db.models import Q |
46 |
from django.db import transaction, IntegrityError |
47 |
from .models import (Holder, Entity, Policy, Holding, |
48 |
Commission, Provision, ProvisionLog, CallSerial, |
49 |
now, |
50 |
db_get_entity, db_get_holding, db_get_policy, |
51 |
db_get_commission, db_filter_provision, db_get_callserial) |
52 |
|
53 |
class QuotaholderDjangoDBCallpoint(Callpoint): |
54 |
|
55 |
api_spec = QuotaholderAPI() |
56 |
|
57 |
http_exc_lookup = { |
58 |
CorruptedError: 550,
|
59 |
InvalidDataError: 400,
|
60 |
InvalidKeyError: 401,
|
61 |
NoEntityError: 404,
|
62 |
NoQuantityError: 413,
|
63 |
NoCapacityError: 413,
|
64 |
} |
65 |
|
66 |
def init_connection(self, connection): |
67 |
if connection is not None: |
68 |
raise ValueError("Cannot specify connection args with %s" % |
69 |
type(self).__name__) |
70 |
pass
|
71 |
|
72 |
def commit(self): |
73 |
transaction.commit() |
74 |
|
75 |
def rollback(self): |
76 |
transaction.rollback() |
77 |
|
78 |
def do_make_call(self, call_name, data): |
79 |
call_fn = getattr(self, call_name, None) |
80 |
if not call_fn: |
81 |
m = "cannot find call '%s'" % (call_name,)
|
82 |
raise CorruptedError(m)
|
83 |
|
84 |
return call_fn(**data)
|
85 |
|
86 |
def create_entity(self, context={}, create_entity=()): |
87 |
rejected = [] |
88 |
append = rejected.append |
89 |
|
90 |
for idx, (entity, owner, key, ownerkey) in enumerate(create_entity): |
91 |
try:
|
92 |
owner = Entity.objects.get(entity=owner, key=ownerkey) |
93 |
except Entity.DoesNotExist:
|
94 |
append(idx) |
95 |
continue
|
96 |
|
97 |
try:
|
98 |
e = Entity.objects.get(entity=entity) |
99 |
append(idx) |
100 |
except Entity.DoesNotExist:
|
101 |
e = Entity.objects.create(entity=entity, |
102 |
owner=owner, |
103 |
key=key) |
104 |
|
105 |
if rejected:
|
106 |
raise ReturnButFail(rejected)
|
107 |
return rejected
|
108 |
|
109 |
def set_entity_key(self, context={}, set_entity_key=()): |
110 |
rejected = [] |
111 |
append = rejected.append |
112 |
|
113 |
for entity, key, newkey in set_entity_key: |
114 |
try:
|
115 |
e = db_get_entity(entity=entity, key=key, for_update=True)
|
116 |
except Entity.DoesNotExist:
|
117 |
append(entity) |
118 |
continue
|
119 |
|
120 |
e.key = newkey |
121 |
e.save() |
122 |
|
123 |
if rejected:
|
124 |
raise ReturnButFail(rejected)
|
125 |
return rejected
|
126 |
|
127 |
def list_entities(self, context={}, entity=None, key=None): |
128 |
try:
|
129 |
e = Entity.objects.get(entity=entity, key=key) |
130 |
except Entity.DoesNotExist:
|
131 |
m = "Entity '%s' does not exist" % (entity,)
|
132 |
raise NoEntityError(m)
|
133 |
|
134 |
children = e.entities.all() |
135 |
entities = [e.entity for e in children] |
136 |
return entities
|
137 |
|
138 |
def get_entity(self, context={}, get_entity=()): |
139 |
entities = [] |
140 |
append = entities.append |
141 |
|
142 |
for entity, key in get_entity: |
143 |
try:
|
144 |
e = Entity.objects.get(entity=entity, key=key) |
145 |
except Entity.DoesNotExist:
|
146 |
continue
|
147 |
|
148 |
append((entity, e.owner.entity)) |
149 |
|
150 |
return entities
|
151 |
|
152 |
def get_limits(self, context={}, get_limits=()): |
153 |
limits = [] |
154 |
append = limits.append |
155 |
|
156 |
for policy in get_limits: |
157 |
try:
|
158 |
p = Policy.objects.get(policy=policy) |
159 |
except Policy.DoesNotExist:
|
160 |
continue
|
161 |
|
162 |
append((policy, p.quantity, p.capacity, |
163 |
p.import_limit, p.export_limit)) |
164 |
|
165 |
return limits
|
166 |
|
167 |
def set_limits(self, context={}, set_limits=()): |
168 |
|
169 |
for ( policy, quantity, capacity,
|
170 |
import_limit, export_limit ) in set_limits:
|
171 |
|
172 |
try:
|
173 |
policy = db_get_policy(policy=policy, for_update=True)
|
174 |
except Policy.DoesNotExist:
|
175 |
Policy.objects.create( policy=policy, |
176 |
quantity=quantity, |
177 |
capacity=capacity, |
178 |
import_limit=import_limit, |
179 |
export_limit=export_limit ) |
180 |
else:
|
181 |
policy.quantity = quantity |
182 |
policy.capacity = capacity |
183 |
policy.export_limit = export_limit |
184 |
policy.import_limit = import_limit |
185 |
policy.save() |
186 |
|
187 |
return ()
|
188 |
|
189 |
def get_holding(self, context={}, get_holding=()): |
190 |
holdings = [] |
191 |
append = holdings.append |
192 |
|
193 |
for entity, resource, key in get_holding: |
194 |
try:
|
195 |
h = Holding.objects.get(entity=entity, resource=resource) |
196 |
except Holding.DoesNotExist:
|
197 |
continue
|
198 |
|
199 |
if h.entity.key != key:
|
200 |
continue
|
201 |
|
202 |
append((h.entity.entity, h.resource, h.policy.policy, |
203 |
h.imported, h.exported, |
204 |
h.returned, h.released, h.flags)) |
205 |
|
206 |
return holdings
|
207 |
|
208 |
def _set_holding(self, entity, resource, policy, flags): |
209 |
try:
|
210 |
h = db_get_holding(entity=entity, resource=resource, |
211 |
for_update=True)
|
212 |
h.policy = p |
213 |
h.flags = flags |
214 |
h.save() |
215 |
except Holding.DoesNotExist:
|
216 |
h = Holding.objects.create( entity=e, resource=resource, |
217 |
policy=p, flags=flags ) |
218 |
return h
|
219 |
|
220 |
def set_holding(self, context={}, set_holding=()): |
221 |
rejected = [] |
222 |
append = rejected.append |
223 |
|
224 |
for entity, resource, key, policy, flags in set_holding: |
225 |
try:
|
226 |
e = Entity.objects.get(entity=entity, key=key) |
227 |
except Entity.DoesNotExist:
|
228 |
append((entity, resource, policy)) |
229 |
continue
|
230 |
|
231 |
if e.key != key:
|
232 |
append((entity, resource, policy)) |
233 |
continue
|
234 |
|
235 |
try:
|
236 |
p = Policy.objects.get(policy=policy) |
237 |
except Policy.DoesNotExist:
|
238 |
append((entity, resource, policy)) |
239 |
continue
|
240 |
|
241 |
try:
|
242 |
h = db_get_holding(entity=entity, resource=resource, |
243 |
for_update=True)
|
244 |
h.policy = p |
245 |
h.flags = flags |
246 |
h.save() |
247 |
except Holding.DoesNotExist:
|
248 |
h = Holding.objects.create( entity=e, resource=resource, |
249 |
policy=p, flags=flags ) |
250 |
|
251 |
if rejected:
|
252 |
raise ReturnButFail(rejected)
|
253 |
return rejected
|
254 |
|
255 |
def _init_holding(self, entity, resource, policy, |
256 |
imported, exported, returned, released, |
257 |
flags): |
258 |
try:
|
259 |
h = db_get_holding(entity=entity, resource=resource, |
260 |
for_update=True)
|
261 |
except Holding.DoesNotExist:
|
262 |
h = Holding(entity=entity, resource=resource) |
263 |
|
264 |
h.policy = policy |
265 |
h.flags = flags |
266 |
h.imported=imported |
267 |
h.importing=imported |
268 |
h.exported=exported |
269 |
h.exporting=exported |
270 |
h.returned=returned |
271 |
h.returning=returned |
272 |
h.released=released |
273 |
h.releasing=released |
274 |
h.save() |
275 |
|
276 |
def init_holding(self, context={}, init_holding=()): |
277 |
rejected = [] |
278 |
append = rejected.append |
279 |
|
280 |
for idx, sfh in enumerate(init_holding): |
281 |
(entity, resource, key, policy, |
282 |
imported, exported, returned, released, |
283 |
flags) = sfh |
284 |
try:
|
285 |
e = Entity.objects.get(entity=entity, key=key) |
286 |
except Entity.DoesNotExist:
|
287 |
append(idx) |
288 |
continue
|
289 |
|
290 |
if e.key != key:
|
291 |
append(idx) |
292 |
continue
|
293 |
|
294 |
try:
|
295 |
p = Policy.objects.get(policy=policy) |
296 |
except Policy.DoesNotExist:
|
297 |
append(idx) |
298 |
continue
|
299 |
|
300 |
self._init_holding(e, resource, p,
|
301 |
imported, exported, |
302 |
returned, released, |
303 |
flags) |
304 |
if rejected:
|
305 |
raise ReturnButFail(rejected)
|
306 |
return rejected
|
307 |
|
308 |
def reset_holding(self, context={}, reset_holding=()): |
309 |
rejected = [] |
310 |
append = rejected.append |
311 |
|
312 |
for idx, tpl in enumerate(reset_holding): |
313 |
(entity, resource, key, |
314 |
imported, exported, returned, released) = tpl |
315 |
try:
|
316 |
e = Entity.objects.get(entity=entity, key=key) |
317 |
except Entity.DoesNotExist:
|
318 |
append(idx) |
319 |
continue
|
320 |
|
321 |
try:
|
322 |
h = db_get_holding(entity=entity, resource=resource, |
323 |
for_update=True)
|
324 |
h.imported=imported |
325 |
h.importing=imported |
326 |
h.exported=exported |
327 |
h.exporting=exported |
328 |
h.returned=returned |
329 |
h.returning=returned |
330 |
h.released=released |
331 |
h.releasing=released |
332 |
h.save() |
333 |
except Holding.DoesNotExist:
|
334 |
append(idx) |
335 |
continue
|
336 |
|
337 |
if rejected:
|
338 |
raise ReturnButFail(rejected)
|
339 |
return rejected
|
340 |
|
341 |
def _check_pending(self, entity, resource): |
342 |
cs = Commission.objects.filter(entity=entity) |
343 |
cs = [c for c in cs if c.provisions.filter(resource=resource)] |
344 |
as_target = [c.serial for c in cs] |
345 |
|
346 |
ps = Provision.objects.filter(entity=entity, resource=resource) |
347 |
as_source = [p.serial.serial for p in ps] |
348 |
|
349 |
return as_target + as_source
|
350 |
|
351 |
def _actual_quantity(self, holding): |
352 |
hp = holding.policy |
353 |
return hp.quantity + (holding.imported + holding.returned -
|
354 |
holding.exported - holding.released) |
355 |
|
356 |
def _new_policy_name(self): |
357 |
return newname('policy_') |
358 |
|
359 |
def _increase_resource(self, entity, resource, amount): |
360 |
try:
|
361 |
h = db_get_holding(entity=entity, resource=resource, |
362 |
for_update=True)
|
363 |
except Holding.DoesNotExist:
|
364 |
h = Holding(entity=entity, resource=resource) |
365 |
p = Policy.objects.create(policy=self._new_policy_name(),
|
366 |
quantity=0)
|
367 |
h.policy = p |
368 |
h.imported += amount |
369 |
h.save() |
370 |
|
371 |
def release_holding(self, context={}, release_holding=()): |
372 |
rejected = [] |
373 |
append = rejected.append |
374 |
|
375 |
for idx, (entity, resource, key) in enumerate(release_holding): |
376 |
try:
|
377 |
h = db_get_holding(entity=entity, resource=resource, |
378 |
for_update=True)
|
379 |
except Holding.DoesNotExist:
|
380 |
append(idx) |
381 |
continue
|
382 |
|
383 |
if h.entity.key != key:
|
384 |
append(idx) |
385 |
continue
|
386 |
|
387 |
if self._check_pending(entity, resource): |
388 |
append(idx) |
389 |
continue
|
390 |
|
391 |
q = self._actual_quantity(h)
|
392 |
if q > 0: |
393 |
owner = h.entity.owner |
394 |
self._increase_resource(owner, resource, q)
|
395 |
|
396 |
h.delete() |
397 |
|
398 |
if rejected:
|
399 |
raise ReturnButFail(rejected)
|
400 |
return rejected
|
401 |
|
402 |
def list_resources(self, context={}, entity=None, key=None): |
403 |
try:
|
404 |
e = Entity.objects.get(entity=entity) |
405 |
except Entity.DoesNotExist:
|
406 |
m = "No such entity '%s'" % (entity,)
|
407 |
raise NoEntityError(m)
|
408 |
|
409 |
if e.key != key:
|
410 |
m = "Invalid key for entity '%s'" % (entity,)
|
411 |
raise InvalidKeyError(m)
|
412 |
|
413 |
holdings = e.holding_set.filter(entity=entity) |
414 |
resources = [h.resource for h in holdings] |
415 |
return resources
|
416 |
|
417 |
def list_holdings(self, context={}, list_holdings=()): |
418 |
rejected = [] |
419 |
reject = rejected.append |
420 |
holdings_list = [] |
421 |
append = holdings_list.append |
422 |
|
423 |
for entity, key in list_holdings: |
424 |
try:
|
425 |
e = Entity.objects.get(entity=entity) |
426 |
if e.key != key:
|
427 |
raise Entity.DoesNotExist("wrong key") |
428 |
except Entity.DoesNotExist:
|
429 |
reject(entity) |
430 |
continue
|
431 |
|
432 |
holdings = e.holding_set.filter(entity=entity) |
433 |
append([[entity, h.resource, |
434 |
h.imported, h.exported, h.returned, h.released] |
435 |
for h in holdings]) |
436 |
|
437 |
return holdings_list, rejected
|
438 |
|
439 |
def get_quota(self, context={}, get_quota=()): |
440 |
quotas = [] |
441 |
append = quotas.append |
442 |
|
443 |
for entity, resource, key in get_quota: |
444 |
try:
|
445 |
h = Holding.objects.get(entity=entity, resource=resource) |
446 |
except Holding.DoesNotExist:
|
447 |
continue
|
448 |
|
449 |
if h.entity.key != key:
|
450 |
continue
|
451 |
|
452 |
p = h.policy |
453 |
|
454 |
append((h.entity.entity, h.resource, p.quantity, p.capacity, |
455 |
p.import_limit, p.export_limit, |
456 |
h.imported, h.exported, |
457 |
h.returned, h.released, |
458 |
h.flags)) |
459 |
|
460 |
return quotas
|
461 |
|
462 |
def set_quota(self, context={}, set_quota=()): |
463 |
rejected = [] |
464 |
append = rejected.append |
465 |
|
466 |
for ( entity, resource, key,
|
467 |
quantity, capacity, |
468 |
import_limit, export_limit, flags ) in set_quota:
|
469 |
|
470 |
try:
|
471 |
e = Entity.objects.get(entity=entity, key=key) |
472 |
except Entity.DoesNotExist:
|
473 |
append((entity, resource)) |
474 |
continue
|
475 |
|
476 |
policy = newname('policy_')
|
477 |
newp = Policy ( |
478 |
policy=policy, |
479 |
quantity=quantity, |
480 |
capacity=capacity, |
481 |
import_limit=import_limit, |
482 |
export_limit=export_limit |
483 |
) |
484 |
|
485 |
try:
|
486 |
h = db_get_holding(entity=entity, resource=resource, |
487 |
for_update=True)
|
488 |
p = h.policy |
489 |
h.policy = newp |
490 |
h.flags = flags |
491 |
except Holding.DoesNotExist:
|
492 |
h = Holding(entity=e, resource=resource, |
493 |
policy=newp, flags=flags) |
494 |
p = None
|
495 |
|
496 |
# the order is intentionally reversed so that it
|
497 |
# would break if we are not within a transaction.
|
498 |
# Has helped before.
|
499 |
h.save() |
500 |
newp.save() |
501 |
|
502 |
if p is not None and p.holding_set.count() == 0: |
503 |
p.delete() |
504 |
|
505 |
if rejected:
|
506 |
raise ReturnButFail(rejected)
|
507 |
return rejected
|
508 |
|
509 |
def add_quota(self, context={}, clientkey=None, serial=None, add_quota=()): |
510 |
rejected = [] |
511 |
append = rejected.append |
512 |
all_pairs = [(q[0], q[1]) for q in add_quota] |
513 |
|
514 |
if serial is not None: |
515 |
if clientkey is None: |
516 |
raise ReturnButFail(all_pairs)
|
517 |
try:
|
518 |
cs = CallSerial.objects.get(serial=serial, clientkey=clientkey) |
519 |
raise ReturnButFail(all_pairs)
|
520 |
except CallSerial.DoesNotExist:
|
521 |
pass
|
522 |
|
523 |
for ( entity, resource, key,
|
524 |
quantity, capacity, |
525 |
import_limit, export_limit ) in add_quota:
|
526 |
|
527 |
try:
|
528 |
e = Entity.objects.get(entity=entity, key=key) |
529 |
except Entity.DoesNotExist:
|
530 |
append((entity, resource)) |
531 |
continue
|
532 |
|
533 |
try:
|
534 |
h = db_get_holding(entity=entity, resource=resource, |
535 |
for_update=True)
|
536 |
p = h.policy |
537 |
except Holding.DoesNotExist:
|
538 |
h = Holding(entity=e, resource=resource, flags=0)
|
539 |
p = None
|
540 |
|
541 |
policy = newname('policy_')
|
542 |
newp = Policy(policy=policy) |
543 |
|
544 |
newp.quantity = _add(p.quantity if p else 0, quantity) |
545 |
newp.capacity = _add(p.capacity if p else 0, capacity) |
546 |
newp.import_limit = _add(p.import_limit if p else 0, |
547 |
import_limit) |
548 |
newp.export_limit = _add(p.export_limit if p else 0, |
549 |
export_limit) |
550 |
|
551 |
new_values = [newp.capacity, |
552 |
newp.import_limit, newp.export_limit] |
553 |
if any(map(_isneg, new_values)): |
554 |
append((entity, resource)) |
555 |
continue
|
556 |
|
557 |
h.policy = newp |
558 |
|
559 |
# the order is intentionally reversed so that it
|
560 |
# would break if we are not within a transaction.
|
561 |
# Has helped before.
|
562 |
h.save() |
563 |
newp.save() |
564 |
|
565 |
if p is not None and p.holding_set.count() == 0: |
566 |
p.delete() |
567 |
|
568 |
if rejected:
|
569 |
raise ReturnButFail(rejected)
|
570 |
|
571 |
if serial is not None and clientkey is not None: |
572 |
CallSerial.objects.create(serial=serial, clientkey=clientkey) |
573 |
return rejected
|
574 |
|
575 |
def ack_serials(self, context={}, clientkey=None, serials=()): |
576 |
if clientkey is None: |
577 |
return
|
578 |
|
579 |
for serial in serials: |
580 |
try:
|
581 |
c = db_get_callserial(clientkey=clientkey, |
582 |
serial=serial, |
583 |
for_update=True)
|
584 |
c.delete() |
585 |
except CallSerial.DoesNotExist:
|
586 |
pass
|
587 |
return
|
588 |
|
589 |
def query_serials(self, context={}, clientkey=None, serials=()): |
590 |
result = [] |
591 |
append = result.append |
592 |
|
593 |
if clientkey is None: |
594 |
return result
|
595 |
|
596 |
if not serials: |
597 |
cs = CallSerial.objects.filter(clientkey=clientkey) |
598 |
return [c.serial for c in cs] |
599 |
|
600 |
for serial in serials: |
601 |
try:
|
602 |
db_get_callserial(clientkey=clientkey, serial=serial) |
603 |
append(serial) |
604 |
except CallSerial.DoesNotExist:
|
605 |
pass
|
606 |
|
607 |
return result
|
608 |
|
609 |
def issue_commission(self, context = {}, |
610 |
clientkey = None,
|
611 |
target = None,
|
612 |
key = None,
|
613 |
name = None,
|
614 |
provisions = () ): |
615 |
|
616 |
try:
|
617 |
t = Entity.objects.get(entity=target) |
618 |
except Entity.DoesNotExist:
|
619 |
m = "No target entity '%s'" % (target,)
|
620 |
raise NoEntityError(m)
|
621 |
else:
|
622 |
if t.key != key:
|
623 |
m = "Invalid key for target entity '%s'" % (target,)
|
624 |
raise InvalidKeyError(m)
|
625 |
|
626 |
create = Commission.objects.create |
627 |
commission = create(entity_id=target, clientkey=clientkey, name=name) |
628 |
serial = commission.serial |
629 |
|
630 |
checked = [] |
631 |
for entity, resource, quantity in provisions: |
632 |
|
633 |
if entity == target:
|
634 |
m = "Cannot issue commission from an entity to itself (%s)" % (
|
635 |
entity,) |
636 |
raise InvalidDataError(m)
|
637 |
|
638 |
ent_res = entity, resource |
639 |
if ent_res in checked: |
640 |
m = "Duplicate provision for %s.%s" % ent_res
|
641 |
raise DuplicateError(m)
|
642 |
checked.append(ent_res) |
643 |
|
644 |
try:
|
645 |
e = Entity.objects.get(entity=entity) |
646 |
except Entity.DoesNotExist:
|
647 |
m = "No source entity '%s'" % (entity,)
|
648 |
raise NoEntityError(m)
|
649 |
|
650 |
release = 0
|
651 |
if quantity < 0: |
652 |
release = 1
|
653 |
|
654 |
try:
|
655 |
h = db_get_holding(entity=entity, resource=resource, |
656 |
for_update=True)
|
657 |
except Holding.DoesNotExist:
|
658 |
m = ("There is not enough quantity "
|
659 |
"to allocate from in %s.%s" % (entity, resource))
|
660 |
raise NoQuantityError(m)
|
661 |
|
662 |
hp = h.policy |
663 |
|
664 |
if (hp.export_limit is not None and |
665 |
h.exporting + quantity > hp.export_limit): |
666 |
m = ("Export limit reached for %s.%s" % (entity, resource))
|
667 |
raise ExportLimitError(m)
|
668 |
|
669 |
if hp.quantity is not None: |
670 |
available = (+ hp.quantity + h.imported + h.returned |
671 |
- h.exporting - h.releasing) |
672 |
|
673 |
if available - quantity < 0: |
674 |
m = ("There is not enough quantity "
|
675 |
"to allocate from in %s.%s" % (entity, resource))
|
676 |
raise NoQuantityError(m)
|
677 |
|
678 |
try:
|
679 |
th = db_get_holding(entity=target, resource=resource, |
680 |
for_update=True)
|
681 |
except Holding.DoesNotExist:
|
682 |
m = ("There is not enough capacity "
|
683 |
"to allocate into in %s.%s" % (target, resource))
|
684 |
raise NoCapacityError(m)
|
685 |
|
686 |
tp = th.policy |
687 |
|
688 |
if (tp.import_limit is not None and |
689 |
th.importing + quantity > tp.import_limit): |
690 |
m = ("Import limit reached for %s.%s" % (target, resource))
|
691 |
raise ImportLimitError(m)
|
692 |
|
693 |
if tp.capacity is not None: |
694 |
capacity = (+ tp.capacity + th.exported + th.released |
695 |
- th.importing - th.returning) |
696 |
|
697 |
if capacity - quantity < 0: |
698 |
m = ("There is not enough capacity "
|
699 |
"to allocate into in %s.%s" % (target, resource))
|
700 |
raise NoCapacityError(m)
|
701 |
|
702 |
Provision.objects.create( serial = commission, |
703 |
entity = e, |
704 |
resource = resource, |
705 |
quantity = quantity ) |
706 |
if release:
|
707 |
h.returning -= quantity |
708 |
th.releasing -= quantity |
709 |
else:
|
710 |
h.exporting += quantity |
711 |
th.importing += quantity |
712 |
|
713 |
h.save() |
714 |
th.save() |
715 |
|
716 |
return serial
|
717 |
|
718 |
def _log_provision(self, commission, s_holding, t_holding, |
719 |
provision, log_time, reason): |
720 |
|
721 |
s_entity = s_holding.entity |
722 |
s_policy = s_holding.policy |
723 |
t_entity = t_holding.entity |
724 |
t_policy = t_holding.policy |
725 |
|
726 |
ProvisionLog.objects.create( |
727 |
serial = commission.serial, |
728 |
name = commission.name, |
729 |
source = s_entity.entity, |
730 |
target = t_entity.entity, |
731 |
resource = provision.resource, |
732 |
source_quantity = s_policy.quantity, |
733 |
source_capacity = s_policy.capacity, |
734 |
source_import_limit = s_policy.import_limit, |
735 |
source_export_limit = s_policy.export_limit, |
736 |
source_imported = s_holding.imported, |
737 |
source_exported = s_holding.exported, |
738 |
source_returned = s_holding.returned, |
739 |
source_released = s_holding.released, |
740 |
target_quantity = t_policy.quantity, |
741 |
target_capacity = t_policy.capacity, |
742 |
target_import_limit = t_policy.import_limit, |
743 |
target_export_limit = t_policy.export_limit, |
744 |
target_imported = t_holding.imported, |
745 |
target_exported = t_holding.exported, |
746 |
target_returned = t_holding.returned, |
747 |
target_released = t_holding.released, |
748 |
delta_quantity = provision.quantity, |
749 |
issue_time = commission.issue_time, |
750 |
log_time = log_time, |
751 |
reason = reason) |
752 |
|
753 |
def accept_commission(self, context={}, clientkey=None, |
754 |
serials=(), reason=''):
|
755 |
log_time = now() |
756 |
|
757 |
for serial in serials: |
758 |
try:
|
759 |
c = db_get_commission(clientkey=clientkey, serial=serial, |
760 |
for_update=True)
|
761 |
except Commission.DoesNotExist:
|
762 |
return
|
763 |
|
764 |
t = c.entity |
765 |
|
766 |
provisions = db_filter_provision(serial=serial, for_update=True)
|
767 |
for pv in provisions: |
768 |
try:
|
769 |
h = db_get_holding(entity=pv.entity.entity, |
770 |
resource=pv.resource, for_update=True)
|
771 |
th = db_get_holding(entity=t, resource=pv.resource, |
772 |
for_update=True)
|
773 |
except Holding.DoesNotExist:
|
774 |
m = "Corrupted provision"
|
775 |
raise CorruptedError(m)
|
776 |
|
777 |
quantity = pv.quantity |
778 |
release = 0
|
779 |
if quantity < 0: |
780 |
release = 1
|
781 |
|
782 |
if release:
|
783 |
h.returned -= quantity |
784 |
th.released -= quantity |
785 |
else:
|
786 |
h.exported += quantity |
787 |
th.imported += quantity |
788 |
|
789 |
reason = 'ACCEPT:' + reason[-121:] |
790 |
self._log_provision(c, h, th, pv, log_time, reason)
|
791 |
h.save() |
792 |
th.save() |
793 |
pv.delete() |
794 |
c.delete() |
795 |
|
796 |
return
|
797 |
|
798 |
def reject_commission(self, context={}, clientkey=None, |
799 |
serials=(), reason=''):
|
800 |
log_time = now() |
801 |
|
802 |
for serial in serials: |
803 |
try:
|
804 |
c = db_get_commission(clientkey=clientkey, serial=serial, |
805 |
for_update=True)
|
806 |
except Commission.DoesNotExist:
|
807 |
return
|
808 |
|
809 |
t = c.entity |
810 |
|
811 |
provisions = db_filter_provision(serial=serial, for_update=True)
|
812 |
for pv in provisions: |
813 |
try:
|
814 |
h = db_get_holding(entity=pv.entity.entity, |
815 |
resource=pv.resource, for_update=True)
|
816 |
th = db_get_holding(entity=t, resource=pv.resource, |
817 |
for_update=True)
|
818 |
except Holding.DoesNotExist:
|
819 |
m = "Corrupted provision"
|
820 |
raise CorruptedError(m)
|
821 |
|
822 |
quantity = pv.quantity |
823 |
release = 0
|
824 |
if quantity < 0: |
825 |
release = 1
|
826 |
|
827 |
if release:
|
828 |
h.returning += quantity |
829 |
th.releasing += quantity |
830 |
else:
|
831 |
h.exporting -= quantity |
832 |
th.importing -= quantity |
833 |
|
834 |
reason = 'REJECT:' + reason[-121:] |
835 |
self._log_provision(c, h, th, pv, log_time, reason)
|
836 |
h.save() |
837 |
th.save() |
838 |
pv.delete() |
839 |
c.delete() |
840 |
|
841 |
return
|
842 |
|
843 |
def get_pending_commissions(self, context={}, clientkey=None): |
844 |
pending = Commission.objects.filter(clientkey=clientkey)\ |
845 |
.values_list('serial', flat=True) |
846 |
return pending
|
847 |
|
848 |
def resolve_pending_commissions(self, context={}, clientkey=None, |
849 |
max_serial=None, accept_set=() ):
|
850 |
accept_set = set(accept_set)
|
851 |
pending = self.get_pending_commissions(context=context, clientkey=clientkey)
|
852 |
pending = sorted(pending)
|
853 |
|
854 |
accept = self.accept_commission
|
855 |
reject = self.reject_commission
|
856 |
|
857 |
for serial in pending: |
858 |
if serial > max_serial:
|
859 |
break
|
860 |
|
861 |
if serial in accept_set: |
862 |
accept(context=context, clientkey=clientkey, serials=[serial]) |
863 |
else:
|
864 |
reject(context=context, clientkey=clientkey, serials=[serial]) |
865 |
|
866 |
return
|
867 |
|
868 |
def release_entity(self, context={}, release_entity=()): |
869 |
rejected = [] |
870 |
append = rejected.append |
871 |
for entity, key in release_entity: |
872 |
try:
|
873 |
e = db_get_entity(entity=entity, key=key, for_update=True)
|
874 |
except Entity.DoesNotExist:
|
875 |
append(entity) |
876 |
continue
|
877 |
|
878 |
if e.entities.count() != 0: |
879 |
append(entity) |
880 |
continue
|
881 |
|
882 |
if e.holding_set.count() != 0: |
883 |
append(entity) |
884 |
continue
|
885 |
|
886 |
e.delete() |
887 |
|
888 |
if rejected:
|
889 |
raise ReturnButFail(rejected)
|
890 |
return rejected
|
891 |
|
892 |
def get_timeline(self, context={}, after="", before="Z", get_timeline=()): |
893 |
entity_set = set()
|
894 |
e_add = entity_set.add |
895 |
resource_set = set()
|
896 |
r_add = resource_set.add |
897 |
|
898 |
for entity, resource, key in get_timeline: |
899 |
if entity not in entity_set: |
900 |
try:
|
901 |
e = Entity.objects.get(entity=entity, key=key) |
902 |
e_add(entity) |
903 |
except Entity.DoesNotExist:
|
904 |
continue
|
905 |
|
906 |
r_add((entity, resource)) |
907 |
|
908 |
chunk_size = 65536
|
909 |
nr = 0
|
910 |
timeline = [] |
911 |
append = timeline.append |
912 |
filterlogs = ProvisionLog.objects.filter |
913 |
if entity_set:
|
914 |
q_entity = Q(source__in = entity_set) | Q(target__in = entity_set) |
915 |
else:
|
916 |
q_entity = Q() |
917 |
|
918 |
while 1: |
919 |
logs = filterlogs( q_entity, |
920 |
issue_time__gt = after, |
921 |
issue_time__lte = before, |
922 |
reason__startswith = 'ACCEPT:' )
|
923 |
|
924 |
logs = logs.order_by('issue_time')
|
925 |
#logs = logs.values()
|
926 |
logs = logs[:chunk_size] |
927 |
nr += len(logs)
|
928 |
if not logs: |
929 |
break
|
930 |
for g in logs: |
931 |
if ((g.source, g.resource) not in resource_set |
932 |
or (g.target, g.resource) not in resource_set): |
933 |
continue
|
934 |
|
935 |
o = { |
936 |
'serial' : g.serial,
|
937 |
'source' : g.source,
|
938 |
'target' : g.target,
|
939 |
'resource' : g.resource,
|
940 |
'name' : g.name,
|
941 |
'quantity' : g.delta_quantity,
|
942 |
'source_allocated' : g.source_allocated(),
|
943 |
'source_allocated_through' : g.source_allocated_through(),
|
944 |
'source_inbound' : g.source_inbound(),
|
945 |
'source_inbound_through' : g.source_inbound_through(),
|
946 |
'source_outbound' : g.source_outbound(),
|
947 |
'source_outbound_through' : g.source_outbound_through(),
|
948 |
'target_allocated' : g.target_allocated(),
|
949 |
'target_allocated_through' : g.target_allocated_through(),
|
950 |
'target_inbound' : g.target_inbound(),
|
951 |
'target_inbound_through' : g.target_inbound_through(),
|
952 |
'target_outbound' : g.target_outbound(),
|
953 |
'target_outbound_through' : g.target_outbound_through(),
|
954 |
'issue_time' : g.issue_time,
|
955 |
'log_time' : g.log_time,
|
956 |
'reason' : g.reason,
|
957 |
} |
958 |
|
959 |
append(o) |
960 |
|
961 |
after = g.issue_time |
962 |
if after >= before:
|
963 |
break
|
964 |
|
965 |
return timeline
|
966 |
|
967 |
def _add(x, y): |
968 |
if x is None or y is None: |
969 |
return None |
970 |
return x + y
|
971 |
|
972 |
def _update(dest, source, attr, delta): |
973 |
dest_attr = getattr(dest, attr)
|
974 |
dest_attr = _add(getattr(source, attr, 0), delta) |
975 |
|
976 |
def _isneg(x): |
977 |
if x is None: |
978 |
return False |
979 |
return x < 0 |
980 |
|
981 |
API_Callpoint = QuotaholderDjangoDBCallpoint |
982 |
|