root / snf-quotaholder-app / quotaholder_django / quotaholder_app / callpoint.py @ 32464501
History | View | Annotate | Download (27.3 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 |
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, now) |
49 |
|
50 |
|
51 |
class QuotaholderDjangoDBCallpoint(Callpoint): |
52 |
|
53 |
api_spec = QuotaholderAPI() |
54 |
|
55 |
http_exc_lookup = { |
56 |
CorruptedError: 550,
|
57 |
InvalidDataError: 400,
|
58 |
InvalidKeyError: 401,
|
59 |
NoEntityError: 404,
|
60 |
NoQuantityError: 413,
|
61 |
NoCapacityError: 413,
|
62 |
} |
63 |
|
64 |
def init_connection(self, connection): |
65 |
if connection is not None: |
66 |
raise ValueError("Cannot specify connection args with %s" % |
67 |
type(self).__name__) |
68 |
pass
|
69 |
|
70 |
def commit(self): |
71 |
transaction.commit() |
72 |
|
73 |
def rollback(self): |
74 |
transaction.rollback() |
75 |
|
76 |
def do_make_call(self, call_name, data): |
77 |
call_fn = getattr(self, call_name, None) |
78 |
if not call_fn: |
79 |
m = "cannot find call '%s'" % (call_name,)
|
80 |
raise CorruptedError(m)
|
81 |
|
82 |
return call_fn(**data)
|
83 |
|
84 |
def create_entity(self, context={}, create_entity=()): |
85 |
rejected = [] |
86 |
append = rejected.append |
87 |
|
88 |
for idx, (entity, owner, key, ownerkey) in enumerate(create_entity): |
89 |
try:
|
90 |
owner = Entity.objects.get(entity=owner, key=ownerkey) |
91 |
except Entity.DoesNotExist:
|
92 |
append(idx) |
93 |
continue
|
94 |
|
95 |
try:
|
96 |
e = Entity.objects.get(entity=entity) |
97 |
append(idx) |
98 |
except Entity.DoesNotExist:
|
99 |
e = Entity.objects.create(entity=entity, |
100 |
owner=owner, |
101 |
key=key) |
102 |
return rejected
|
103 |
|
104 |
def set_entity_key(self, context={}, set_entity_key=()): |
105 |
rejected = [] |
106 |
append = rejected.append |
107 |
|
108 |
for entity, key, newkey in set_entity_key: |
109 |
try:
|
110 |
e = Entity.objects.get(entity=entity, key=key) |
111 |
except Entity.DoesNotExist:
|
112 |
append(entity) |
113 |
continue
|
114 |
|
115 |
e.key = newkey |
116 |
e.save() |
117 |
|
118 |
return rejected
|
119 |
|
120 |
def list_entities(self, context={}, entity=None, key=None): |
121 |
try:
|
122 |
e = Entity.objects.get(entity=entity, key=key) |
123 |
except Entity.DoesNotExist:
|
124 |
m = "Entity '%s' does not exist" % (entity,)
|
125 |
raise NoEntityError(m)
|
126 |
|
127 |
children = e.entities.all() |
128 |
entities = [e.entity for e in children] |
129 |
return entities
|
130 |
|
131 |
def get_entity(self, context={}, get_entity=()): |
132 |
entities = [] |
133 |
append = entities.append |
134 |
|
135 |
for entity, key in get_entity: |
136 |
try:
|
137 |
e = Entity.objects.get(entity=entity, key=key) |
138 |
except Entity.DoesNotExist:
|
139 |
continue
|
140 |
|
141 |
append((entity, e.owner.entity)) |
142 |
|
143 |
return entities
|
144 |
|
145 |
def get_limits(self, context={}, get_limits=()): |
146 |
limits = [] |
147 |
append = limits.append |
148 |
|
149 |
for policy in get_limits: |
150 |
try:
|
151 |
p = Policy.objects.get(policy=policy) |
152 |
except Policy.DoesNotExist:
|
153 |
continue
|
154 |
|
155 |
append((policy, p.quantity, p.capacity, |
156 |
p.import_limit, p.export_limit)) |
157 |
|
158 |
return limits
|
159 |
|
160 |
def set_limits(self, context={}, set_limits=()): |
161 |
|
162 |
for ( policy, quantity, capacity,
|
163 |
import_limit, export_limit ) in set_limits:
|
164 |
|
165 |
try:
|
166 |
policy = Policy.objects.get(policy=policy) |
167 |
except Policy.DoesNotExist:
|
168 |
Policy.objects.create( policy=policy, |
169 |
quantity=quantity, |
170 |
capacity=capacity, |
171 |
import_limit=import_limit, |
172 |
export_limit=export_limit ) |
173 |
else:
|
174 |
policy.quantity = quantity |
175 |
policy.capacity = capacity |
176 |
policy.export_limit = export_limit |
177 |
policy.import_limit = import_limit |
178 |
policy.save() |
179 |
|
180 |
return ()
|
181 |
|
182 |
def get_holding(self, context={}, get_holding=()): |
183 |
holdings = [] |
184 |
append = holdings.append |
185 |
|
186 |
for entity, resource, key in get_holding: |
187 |
try:
|
188 |
h = Holding.objects.get(entity=entity, resource=resource) |
189 |
except Holding.DoesNotExist:
|
190 |
continue
|
191 |
|
192 |
if h.entity.key != key:
|
193 |
continue
|
194 |
|
195 |
append((h.entity.entity, h.resource, h.policy.policy, |
196 |
h.imported, h.exported, |
197 |
h.returned, h.released, h.flags)) |
198 |
|
199 |
return holdings
|
200 |
|
201 |
def _set_holding(self, entity, resource, policy, flags): |
202 |
try:
|
203 |
h = Holding.objects.get(entity=entity, resource=resource) |
204 |
h.policy = p |
205 |
h.flags = flags |
206 |
h.save() |
207 |
except Holding.DoesNotExist:
|
208 |
h = Holding.objects.create( entity=e, resource=resource, |
209 |
policy=p, flags=flags ) |
210 |
return h
|
211 |
|
212 |
def set_holding(self, context={}, set_holding=()): |
213 |
rejected = [] |
214 |
append = rejected.append |
215 |
|
216 |
for entity, resource, key, policy, flags in set_holding: |
217 |
try:
|
218 |
e = Entity.objects.get(entity=entity, key=key) |
219 |
except Entity.DoesNotExist:
|
220 |
append((entity, resource, policy)) |
221 |
continue
|
222 |
|
223 |
if e.key != key:
|
224 |
append((entity, resource, policy)) |
225 |
continue
|
226 |
|
227 |
try:
|
228 |
p = Policy.objects.get(policy=policy) |
229 |
except Policy.DoesNotExist:
|
230 |
append((entity, resource, policy)) |
231 |
continue
|
232 |
|
233 |
try:
|
234 |
h = Holding.objects.get(entity=entity, resource=resource) |
235 |
h.policy = p |
236 |
h.flags = flags |
237 |
h.save() |
238 |
except Holding.DoesNotExist:
|
239 |
h = Holding.objects.create( entity=e, resource=resource, |
240 |
policy=p, flags=flags ) |
241 |
|
242 |
return rejected
|
243 |
|
244 |
def _init_holding(self, entity, resource, policy, |
245 |
imported, exported, returned, released, |
246 |
flags): |
247 |
try:
|
248 |
h = Holding.objects.get(entity=entity, resource=resource) |
249 |
except Holding.DoesNotExist:
|
250 |
h = Holding(entity=entity, resource=resource) |
251 |
|
252 |
h.policy = policy |
253 |
h.flags = flags |
254 |
h.imported=imported |
255 |
h.importing=imported |
256 |
h.exported=exported |
257 |
h.exporting=exported |
258 |
h.returned=returned |
259 |
h.returning=returned |
260 |
h.released=released |
261 |
h.releasing=released |
262 |
h.save() |
263 |
|
264 |
def init_holding(self, context={}, init_holding=()): |
265 |
rejected = [] |
266 |
append = rejected.append |
267 |
|
268 |
for idx, sfh in enumerate(init_holding): |
269 |
(entity, resource, key, policy, |
270 |
imported, exported, returned, released, |
271 |
flags) = sfh |
272 |
try:
|
273 |
e = Entity.objects.get(entity=entity, key=key) |
274 |
except Entity.DoesNotExist:
|
275 |
append(idx) |
276 |
continue
|
277 |
|
278 |
if e.key != key:
|
279 |
append(idx) |
280 |
continue
|
281 |
|
282 |
try:
|
283 |
p = Policy.objects.get(policy=policy) |
284 |
except Policy.DoesNotExist:
|
285 |
append(idx) |
286 |
continue
|
287 |
|
288 |
self._init_holding(e, resource, p,
|
289 |
imported, exported, |
290 |
returned, released, |
291 |
flags) |
292 |
return rejected
|
293 |
|
294 |
def _check_pending(self, entity, resource): |
295 |
cs = Commission.objects.filter(entity=entity) |
296 |
cs = [c for c in cs if c.provisions.filter(resource=resource)] |
297 |
as_target = [c.serial for c in cs] |
298 |
|
299 |
ps = Provision.objects.filter(entity=entity, resource=resource) |
300 |
as_source = [p.serial.serial for p in ps] |
301 |
|
302 |
return as_target + as_source
|
303 |
|
304 |
def _actual_quantity(self, holding): |
305 |
hp = holding.policy |
306 |
return hp.quantity + (holding.imported + holding.returned -
|
307 |
holding.exported - holding.released) |
308 |
|
309 |
def _new_policy_name(self): |
310 |
return newname('policy_') |
311 |
|
312 |
def _increase_resource(self, entity, resource, amount): |
313 |
try:
|
314 |
h = Holding.objects.get(entity=entity, resource=resource) |
315 |
except Holding.DoesNotExist:
|
316 |
h = Holding(entity=entity, resource=resource) |
317 |
p = Policy.objects.create(policy=self._new_policy_name(),
|
318 |
quantity=0)
|
319 |
h.policy = p |
320 |
h.imported += amount |
321 |
h.save() |
322 |
|
323 |
def release_holding(self, context={}, release_holding=()): |
324 |
rejected = [] |
325 |
append = rejected.append |
326 |
|
327 |
for idx, (entity, resource, key) in enumerate(release_holding): |
328 |
try:
|
329 |
h = Holding.objects.get(entity=entity, resource=resource) |
330 |
except Holding.DoesNotExist:
|
331 |
append(idx) |
332 |
continue
|
333 |
|
334 |
if h.entity.key != key:
|
335 |
append(idx) |
336 |
continue
|
337 |
|
338 |
if self._check_pending(entity, resource): |
339 |
append(idx) |
340 |
continue
|
341 |
|
342 |
q = self._actual_quantity(h)
|
343 |
if q > 0: |
344 |
owner = h.entity.owner |
345 |
self._increase_resource(owner, resource, q)
|
346 |
|
347 |
h.delete() |
348 |
|
349 |
return rejected
|
350 |
|
351 |
def list_resources(self, context={}, entity=None, key=None): |
352 |
try:
|
353 |
e = Entity.objects.get(entity=entity) |
354 |
except Entity.DoesNotExist:
|
355 |
m = "No such entity '%s'" % (entity,)
|
356 |
raise NoEntityError(m)
|
357 |
|
358 |
if e.key != key:
|
359 |
m = "Invalid key for entity '%s'" % (entity,)
|
360 |
raise InvalidKeyError(m)
|
361 |
|
362 |
holdings = e.holding_set.filter(entity=entity) |
363 |
resources = [h.resource for h in holdings] |
364 |
return resources
|
365 |
|
366 |
def get_quota(self, context={}, get_quota=()): |
367 |
quotas = [] |
368 |
append = quotas.append |
369 |
|
370 |
for entity, resource, key in get_quota: |
371 |
try:
|
372 |
h = Holding.objects.get(entity=entity, resource=resource) |
373 |
except Holding.DoesNotExist:
|
374 |
continue
|
375 |
|
376 |
if h.entity.key != key:
|
377 |
continue
|
378 |
|
379 |
p = h.policy |
380 |
|
381 |
append((h.entity.entity, h.resource, p.quantity, p.capacity, |
382 |
p.import_limit, p.export_limit, |
383 |
h.imported, h.exported, |
384 |
h.returned, h.released, |
385 |
h.flags)) |
386 |
|
387 |
return quotas
|
388 |
|
389 |
def set_quota(self, context={}, set_quota=()): |
390 |
rejected = [] |
391 |
append = rejected.append |
392 |
|
393 |
for ( entity, resource, key,
|
394 |
quantity, capacity, |
395 |
import_limit, export_limit, flags ) in set_quota:
|
396 |
|
397 |
p = None
|
398 |
|
399 |
try:
|
400 |
h = Holding.objects.get(entity=entity, resource=resource) |
401 |
if h.entity.key != key:
|
402 |
append((entity, resource)) |
403 |
continue
|
404 |
p = h.policy |
405 |
|
406 |
except Holding.DoesNotExist:
|
407 |
try:
|
408 |
e = Entity.objects.get(entity=entity) |
409 |
except Entity.DoesNotExist:
|
410 |
append((entity, resource)) |
411 |
continue
|
412 |
|
413 |
if e.key != key:
|
414 |
append((entity, resource)) |
415 |
continue
|
416 |
|
417 |
h = None
|
418 |
|
419 |
policy = newname('policy_')
|
420 |
newp = Policy ( |
421 |
policy=policy, |
422 |
quantity=quantity, |
423 |
capacity=capacity, |
424 |
import_limit=import_limit, |
425 |
export_limit=export_limit |
426 |
) |
427 |
|
428 |
if h is None: |
429 |
h = Holding(entity=e, resource=resource, |
430 |
policy=newp, flags=flags) |
431 |
else:
|
432 |
h.policy = newp |
433 |
h.flags = flags |
434 |
|
435 |
h.save() |
436 |
newp.save() |
437 |
|
438 |
if p is not None and p.holding_set.count() == 0: |
439 |
p.delete() |
440 |
|
441 |
return rejected
|
442 |
|
443 |
def issue_commission(self, context = {}, |
444 |
clientkey = None,
|
445 |
target = None,
|
446 |
key = None,
|
447 |
name = None,
|
448 |
provisions = () ): |
449 |
|
450 |
try:
|
451 |
t = Entity.objects.get(entity=target) |
452 |
except Entity.DoesNotExist:
|
453 |
m = "No target entity '%s'" % (target,)
|
454 |
raise NoEntityError(m)
|
455 |
else:
|
456 |
if t.key != key:
|
457 |
m = "Invalid key for target entity '%s'" % (target,)
|
458 |
raise InvalidKeyError(m)
|
459 |
|
460 |
create = Commission.objects.create |
461 |
commission = create(entity_id=target, clientkey=clientkey, name=name) |
462 |
serial = commission.serial |
463 |
|
464 |
checked = [] |
465 |
for entity, resource, quantity in provisions: |
466 |
|
467 |
ent_res = entity, resource |
468 |
if ent_res in checked: |
469 |
m = "Duplicate provision for %s.%s" % ent_res
|
470 |
raise DuplicateError(m)
|
471 |
checked.append(ent_res) |
472 |
|
473 |
try:
|
474 |
e = Entity.objects.get(entity=entity) |
475 |
except Entity.DoesNotExist:
|
476 |
m = "No source entity '%s'" % (entity,)
|
477 |
raise NoEntityError(m)
|
478 |
|
479 |
release = 0
|
480 |
if quantity < 0: |
481 |
release = 1
|
482 |
|
483 |
try:
|
484 |
h = Holding.objects.get(entity=entity, resource=resource) |
485 |
except Holding.DoesNotExist:
|
486 |
m = ("There is not enough quantity "
|
487 |
"to allocate from in %s.%s" % (entity, resource))
|
488 |
raise NoQuantityError(m)
|
489 |
|
490 |
hp = h.policy |
491 |
|
492 |
if (hp.export_limit is not None and |
493 |
h.exporting + quantity > hp.export_limit): |
494 |
m = ("Export limit reached for %s.%s" % (entity, resource))
|
495 |
raise ExportLimitError(m)
|
496 |
|
497 |
if hp.quantity is not None: |
498 |
available = (+ hp.quantity + h.imported + h.returned |
499 |
- h.exporting - h.releasing) |
500 |
|
501 |
if available - quantity < 0: |
502 |
m = ("There is not enough quantity "
|
503 |
"to allocate from in %s.%s" % (entity, resource))
|
504 |
raise NoQuantityError(m)
|
505 |
|
506 |
try:
|
507 |
th = Holding.objects.get(entity=target, resource=resource) |
508 |
except Holding.DoesNotExist:
|
509 |
m = ("There is not enough capacity "
|
510 |
"to allocate into in %s.%s" % (target, resource))
|
511 |
raise NoCapacityError(m)
|
512 |
|
513 |
tp = th.policy |
514 |
|
515 |
if (tp.import_limit is not None and |
516 |
th.importing + quantity > tp.import_limit): |
517 |
m = ("Import limit reached for %s.%s" % (target, resource))
|
518 |
raise ImportLimitError(m)
|
519 |
|
520 |
if tp.capacity is not None: |
521 |
capacity = (+ tp.capacity + th.exported + th.released |
522 |
- th.importing - th.returning) |
523 |
|
524 |
if capacity - quantity < 0: |
525 |
m = ("There is not enough capacity "
|
526 |
"to allocate into in %s.%s" % (target, resource))
|
527 |
raise NoCapacityError(m)
|
528 |
|
529 |
Provision.objects.create( serial = commission, |
530 |
entity = e, |
531 |
resource = resource, |
532 |
quantity = quantity ) |
533 |
if release:
|
534 |
h.returning -= quantity |
535 |
th.releasing -= quantity |
536 |
else:
|
537 |
h.exporting += quantity |
538 |
th.importing += quantity |
539 |
|
540 |
h.save() |
541 |
th.save() |
542 |
|
543 |
return serial
|
544 |
|
545 |
def _log_provision(self, commission, s_holding, t_holding, |
546 |
provision, log_time, reason): |
547 |
|
548 |
s_entity = s_holding.entity |
549 |
s_policy = s_holding.policy |
550 |
t_entity = t_holding.entity |
551 |
t_policy = t_holding.policy |
552 |
|
553 |
ProvisionLog.objects.create( |
554 |
serial = commission.serial, |
555 |
name = commission.name, |
556 |
source = s_entity.entity, |
557 |
target = t_entity.entity, |
558 |
resource = provision.resource, |
559 |
source_quantity = s_policy.quantity, |
560 |
source_capacity = s_policy.capacity, |
561 |
source_import_limit = s_policy.import_limit, |
562 |
source_export_limit = s_policy.export_limit, |
563 |
source_imported = s_holding.imported, |
564 |
source_exported = s_holding.exported, |
565 |
source_returned = s_holding.returned, |
566 |
source_released = s_holding.released, |
567 |
target_quantity = t_policy.quantity, |
568 |
target_capacity = t_policy.capacity, |
569 |
target_import_limit = t_policy.import_limit, |
570 |
target_export_limit = t_policy.export_limit, |
571 |
target_imported = t_holding.imported, |
572 |
target_exported = t_holding.exported, |
573 |
target_returned = t_holding.returned, |
574 |
target_released = t_holding.released, |
575 |
delta_quantity = provision.quantity, |
576 |
issue_time = commission.issue_time, |
577 |
log_time = log_time, |
578 |
reason = reason) |
579 |
|
580 |
def accept_commission(self, context={}, clientkey=None, |
581 |
serials=(), reason=''):
|
582 |
log_time = now() |
583 |
|
584 |
for serial in serials: |
585 |
try:
|
586 |
c = Commission.objects.get(clientkey=clientkey, serial=serial) |
587 |
except Commission.DoesNotExist:
|
588 |
return
|
589 |
|
590 |
t = c.entity |
591 |
|
592 |
provisions = Provision.objects.filter(serial=serial) |
593 |
for pv in provisions: |
594 |
try:
|
595 |
h = Holding.objects.get(entity=pv.entity.entity, |
596 |
resource=pv.resource ) |
597 |
th = Holding.objects.get(entity=t, resource=pv.resource) |
598 |
except Holding.DoesNotExist:
|
599 |
m = "Corrupted provision"
|
600 |
raise CorruptedError(m)
|
601 |
|
602 |
quantity = pv.quantity |
603 |
release = 0
|
604 |
if quantity < 0: |
605 |
release = 1
|
606 |
|
607 |
if release:
|
608 |
h.returned -= quantity |
609 |
th.released -= quantity |
610 |
else:
|
611 |
h.exported += quantity |
612 |
th.imported += quantity |
613 |
|
614 |
reason = 'ACCEPT:' + reason[-121:] |
615 |
self._log_provision(c, h, th, pv, log_time, reason)
|
616 |
h.save() |
617 |
th.save() |
618 |
pv.delete() |
619 |
c.delete() |
620 |
|
621 |
return
|
622 |
|
623 |
def reject_commission(self, context={}, clientkey=None, |
624 |
serials=(), reason=''):
|
625 |
log_time = now() |
626 |
|
627 |
for serial in serials: |
628 |
try:
|
629 |
c = Commission.objects.get(clientkey=clientkey, serial=serial) |
630 |
except Commission.DoesNotExist:
|
631 |
return
|
632 |
|
633 |
t = c.entity |
634 |
|
635 |
provisions = Provision.objects.filter(serial=serial) |
636 |
for pv in provisions: |
637 |
try:
|
638 |
h = Holding.objects.get(entity=pv.entity.entity, |
639 |
resource=pv.resource) |
640 |
th = Holding.objects.get(entity=t, resource=pv.resource) |
641 |
except Holding.DoesNotExist:
|
642 |
m = "Corrupted provision"
|
643 |
raise CorruptedError(m)
|
644 |
|
645 |
quantity = pv.quantity |
646 |
release = 0
|
647 |
if quantity < 0: |
648 |
release = 1
|
649 |
|
650 |
if release:
|
651 |
h.returning += quantity |
652 |
th.releasing += quantity |
653 |
else:
|
654 |
h.exporting -= quantity |
655 |
th.importing -= quantity |
656 |
|
657 |
reason = 'REJECT:' + reason[-121:] |
658 |
self._log_provision(c, h, th, pv, log_time, reason)
|
659 |
h.save() |
660 |
th.save() |
661 |
pv.delete() |
662 |
c.delete() |
663 |
|
664 |
return
|
665 |
|
666 |
def get_pending_commissions(self, context={}, clientkey=None): |
667 |
pending = Commission.objects.filter(clientkey=clientkey)\ |
668 |
.values_list('serial', flat=True) |
669 |
return pending
|
670 |
|
671 |
def resolve_pending_commissions(self, context={}, clientkey=None, |
672 |
max_serial=None, accept_set=() ):
|
673 |
accept_set = set(accept_set)
|
674 |
pending = self.get_pending_commissions(context=context, clientkey=clientkey)
|
675 |
pending = sorted(pending)
|
676 |
|
677 |
accept = self.accept_commission
|
678 |
reject = self.reject_commission
|
679 |
|
680 |
for serial in pending: |
681 |
if serial > max_serial:
|
682 |
break
|
683 |
|
684 |
if serial in accept_set: |
685 |
accept(context=context, clientkey=clientkey, serials=[serial]) |
686 |
else:
|
687 |
reject(context=context, clientkey=clientkey, serials=[serial]) |
688 |
|
689 |
return
|
690 |
|
691 |
def release_entity(self, context={}, release_entity=()): |
692 |
rejected = [] |
693 |
append = rejected.append |
694 |
for entity, key in release_entity: |
695 |
try:
|
696 |
e = Entity.objects.get(entity=entity, key=key) |
697 |
except Entity.DoesNotExist:
|
698 |
append(entity) |
699 |
continue
|
700 |
|
701 |
if e.entities.count() != 0: |
702 |
append(entity) |
703 |
continue
|
704 |
|
705 |
if e.holding_set.count() != 0: |
706 |
append(entity) |
707 |
continue
|
708 |
|
709 |
e.delete() |
710 |
|
711 |
return rejected
|
712 |
|
713 |
def get_timeline(self, context={}, after="", before="Z", get_timeline=()): |
714 |
entity_set = set()
|
715 |
e_add = entity_set.add |
716 |
resource_set = set()
|
717 |
r_add = resource_set.add |
718 |
|
719 |
for entity, resource, key in get_timeline: |
720 |
if entity not in entity_set: |
721 |
try:
|
722 |
e = Entity.objects.get(entity=entity, key=key) |
723 |
e_add(entity) |
724 |
except Entity.DoesNotExist:
|
725 |
continue
|
726 |
|
727 |
r_add((entity, resource)) |
728 |
|
729 |
chunk_size = 65536
|
730 |
nr = 0
|
731 |
timeline = [] |
732 |
append = timeline.append |
733 |
filterlogs = ProvisionLog.objects.filter |
734 |
if entity_set:
|
735 |
q_entity = Q(source__in = entity_set) | Q(target__in = entity_set) |
736 |
else:
|
737 |
q_entity = Q() |
738 |
|
739 |
while 1: |
740 |
logs = filterlogs( q_entity, |
741 |
issue_time__gt = after, |
742 |
issue_time__lte = before, |
743 |
reason__startswith = 'ACCEPT:' )
|
744 |
|
745 |
logs = logs.order_by('issue_time')
|
746 |
#logs = logs.values()
|
747 |
logs = logs[:chunk_size] |
748 |
nr += len(logs)
|
749 |
if not logs: |
750 |
break
|
751 |
for g in logs: |
752 |
if ((g.source, g.resource) not in resource_set |
753 |
or (g.target, g.resource) not in resource_set): |
754 |
continue
|
755 |
|
756 |
o = { |
757 |
'serial' : g.serial,
|
758 |
'source' : g.source,
|
759 |
'target' : g.target,
|
760 |
'resource' : g.resource,
|
761 |
'name' : g.name,
|
762 |
'quantity' : g.delta_quantity,
|
763 |
'source_allocated' : g.source_allocated(),
|
764 |
'source_allocated_through' : g.source_allocated_through(),
|
765 |
'source_inbound' : g.source_inbound(),
|
766 |
'source_inbound_through' : g.source_inbound_through(),
|
767 |
'source_outbound' : g.source_outbound(),
|
768 |
'source_outbound_through' : g.source_outbound_through(),
|
769 |
'target_allocated' : g.target_allocated(),
|
770 |
'target_allocated_through' : g.target_allocated_through(),
|
771 |
'target_inbound' : g.target_inbound(),
|
772 |
'target_inbound_through' : g.target_inbound_through(),
|
773 |
'target_outbound' : g.target_outbound(),
|
774 |
'target_outbound_through' : g.target_outbound_through(),
|
775 |
'issue_time' : g.issue_time,
|
776 |
'log_time' : g.log_time,
|
777 |
'reason' : g.reason,
|
778 |
} |
779 |
|
780 |
append(o) |
781 |
|
782 |
after = g.issue_time |
783 |
if after >= before:
|
784 |
break
|
785 |
|
786 |
return timeline
|
787 |
|
788 |
|
789 |
API_Callpoint = QuotaholderDjangoDBCallpoint |
790 |
|