root / snf-quotaholder-app / quotaholder_django / quotaholder_app / callpoint.py @ cceadca8
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 |
owner = h.entity.owner |
344 |
self._increase_resource(owner, resource, q)
|
345 |
h.delete() |
346 |
|
347 |
return rejected
|
348 |
|
349 |
def list_resources(self, context={}, entity=None, key=None): |
350 |
try:
|
351 |
e = Entity.objects.get(entity=entity) |
352 |
except Entity.DoesNotExist:
|
353 |
m = "No such entity '%s'" % (entity,)
|
354 |
raise NoEntityError(m)
|
355 |
|
356 |
if e.key != key:
|
357 |
m = "Invalid key for entity '%s'" % (entity,)
|
358 |
raise InvalidKeyError(m)
|
359 |
|
360 |
holdings = e.holding_set.filter(entity=entity) |
361 |
resources = [h.resource for h in holdings] |
362 |
return resources
|
363 |
|
364 |
def get_quota(self, context={}, get_quota=()): |
365 |
quotas = [] |
366 |
append = quotas.append |
367 |
|
368 |
for entity, resource, key in get_quota: |
369 |
try:
|
370 |
h = Holding.objects.get(entity=entity, resource=resource) |
371 |
except Holding.DoesNotExist:
|
372 |
continue
|
373 |
|
374 |
if h.entity.key != key:
|
375 |
continue
|
376 |
|
377 |
p = h.policy |
378 |
|
379 |
append((h.entity.entity, h.resource, p.quantity, p.capacity, |
380 |
p.import_limit, p.export_limit, |
381 |
h.imported, h.exported, |
382 |
h.returned, h.released, |
383 |
h.flags)) |
384 |
|
385 |
return quotas
|
386 |
|
387 |
def set_quota(self, context={}, set_quota=()): |
388 |
rejected = [] |
389 |
append = rejected.append |
390 |
|
391 |
for ( entity, resource, key,
|
392 |
quantity, capacity, |
393 |
import_limit, export_limit, flags ) in set_quota:
|
394 |
|
395 |
p = None
|
396 |
|
397 |
try:
|
398 |
h = Holding.objects.get(entity=entity, resource=resource) |
399 |
if h.entity.key != key:
|
400 |
append((entity, resource)) |
401 |
continue
|
402 |
p = h.policy |
403 |
|
404 |
except Holding.DoesNotExist:
|
405 |
try:
|
406 |
e = Entity.objects.get(entity=entity) |
407 |
except Entity.DoesNotExist:
|
408 |
append((entity, resource)) |
409 |
continue
|
410 |
|
411 |
if e.key != key:
|
412 |
append((entity, resource)) |
413 |
continue
|
414 |
|
415 |
h = None
|
416 |
|
417 |
policy = newname('policy_')
|
418 |
newp = Policy ( |
419 |
policy=policy, |
420 |
quantity=quantity, |
421 |
capacity=capacity, |
422 |
import_limit=import_limit, |
423 |
export_limit=export_limit |
424 |
) |
425 |
|
426 |
if h is None: |
427 |
h = Holding(entity=e, resource=resource, |
428 |
policy=newp, flags=flags) |
429 |
else:
|
430 |
h.policy = newp |
431 |
h.flags = flags |
432 |
|
433 |
h.save() |
434 |
newp.save() |
435 |
|
436 |
if p is not None and p.holding_set.count() == 0: |
437 |
p.delete() |
438 |
|
439 |
return rejected
|
440 |
|
441 |
def issue_commission(self, context = {}, |
442 |
clientkey = None,
|
443 |
target = None,
|
444 |
key = None,
|
445 |
name = None,
|
446 |
provisions = () ): |
447 |
|
448 |
try:
|
449 |
t = Entity.objects.get(entity=target) |
450 |
except Entity.DoesNotExist:
|
451 |
m = "No target entity '%s'" % (target,)
|
452 |
raise NoEntityError(m)
|
453 |
else:
|
454 |
if t.key != key:
|
455 |
m = "Invalid key for target entity '%s'" % (target,)
|
456 |
raise InvalidKeyError(m)
|
457 |
|
458 |
create = Commission.objects.create |
459 |
commission = create(entity_id=target, clientkey=clientkey, name=name) |
460 |
serial = commission.serial |
461 |
|
462 |
checked = [] |
463 |
for entity, resource, quantity in provisions: |
464 |
|
465 |
ent_res = entity, resource |
466 |
if ent_res in checked: |
467 |
m = "Duplicate provision for %s.%s" % ent_res
|
468 |
raise DuplicateError(m)
|
469 |
checked.append(ent_res) |
470 |
|
471 |
try:
|
472 |
e = Entity.objects.get(entity=entity) |
473 |
except Entity.DoesNotExist:
|
474 |
m = "No source entity '%s'" % (entity,)
|
475 |
raise NoEntityError(m)
|
476 |
|
477 |
release = 0
|
478 |
if quantity < 0: |
479 |
release = 1
|
480 |
|
481 |
try:
|
482 |
h = Holding.objects.get(entity=entity, resource=resource) |
483 |
except Holding.DoesNotExist:
|
484 |
m = ("There is not enough quantity "
|
485 |
"to allocate from in %s.%s" % (entity, resource))
|
486 |
raise NoQuantityError(m)
|
487 |
|
488 |
hp = h.policy |
489 |
|
490 |
if (hp.export_limit is not None and |
491 |
h.exporting + quantity > hp.export_limit): |
492 |
m = ("Export limit reached for %s.%s" % (entity, resource))
|
493 |
raise ExportLimitError(m)
|
494 |
|
495 |
if hp.quantity is not None: |
496 |
available = (+ hp.quantity + h.imported + h.returned |
497 |
- h.exporting - h.releasing) |
498 |
|
499 |
if available - quantity < 0: |
500 |
m = ("There is not enough quantity "
|
501 |
"to allocate from in %s.%s" % (entity, resource))
|
502 |
raise NoQuantityError(m)
|
503 |
|
504 |
try:
|
505 |
th = Holding.objects.get(entity=target, resource=resource) |
506 |
except Holding.DoesNotExist:
|
507 |
m = ("There is not enough capacity "
|
508 |
"to allocate into in %s.%s" % (target, resource))
|
509 |
raise NoCapacityError(m)
|
510 |
|
511 |
tp = th.policy |
512 |
|
513 |
if (tp.import_limit is not None and |
514 |
th.importing + quantity > tp.import_limit): |
515 |
m = ("Import limit reached for %s.%s" % (target, resource))
|
516 |
raise ImportLimitError(m)
|
517 |
|
518 |
if tp.capacity is not None: |
519 |
capacity = (+ tp.capacity + th.exported + th.released |
520 |
- th.importing - th.returning) |
521 |
|
522 |
if capacity - quantity < 0: |
523 |
m = ("There is not enough capacity "
|
524 |
"to allocate into in %s.%s" % (target, resource))
|
525 |
raise NoCapacityError(m)
|
526 |
|
527 |
Provision.objects.create( serial = commission, |
528 |
entity = e, |
529 |
resource = resource, |
530 |
quantity = quantity ) |
531 |
if release:
|
532 |
h.returning -= quantity |
533 |
th.releasing -= quantity |
534 |
else:
|
535 |
h.exporting += quantity |
536 |
th.importing += quantity |
537 |
|
538 |
h.save() |
539 |
th.save() |
540 |
|
541 |
return serial
|
542 |
|
543 |
def _log_provision(self, commission, s_holding, t_holding, |
544 |
provision, log_time, reason): |
545 |
|
546 |
s_entity = s_holding.entity |
547 |
s_policy = s_holding.policy |
548 |
t_entity = t_holding.entity |
549 |
t_policy = t_holding.policy |
550 |
|
551 |
ProvisionLog.objects.create( |
552 |
serial = commission.serial, |
553 |
name = commission.name, |
554 |
source = s_entity.entity, |
555 |
target = t_entity.entity, |
556 |
resource = provision.resource, |
557 |
source_quantity = s_policy.quantity, |
558 |
source_capacity = s_policy.capacity, |
559 |
source_import_limit = s_policy.import_limit, |
560 |
source_export_limit = s_policy.export_limit, |
561 |
source_imported = s_holding.imported, |
562 |
source_exported = s_holding.exported, |
563 |
source_returned = s_holding.returned, |
564 |
source_released = s_holding.released, |
565 |
target_quantity = t_policy.quantity, |
566 |
target_capacity = t_policy.capacity, |
567 |
target_import_limit = t_policy.import_limit, |
568 |
target_export_limit = t_policy.export_limit, |
569 |
target_imported = t_holding.imported, |
570 |
target_exported = t_holding.exported, |
571 |
target_returned = t_holding.returned, |
572 |
target_released = t_holding.released, |
573 |
delta_quantity = provision.quantity, |
574 |
issue_time = commission.issue_time, |
575 |
log_time = log_time, |
576 |
reason = reason) |
577 |
|
578 |
def accept_commission(self, context={}, clientkey=None, |
579 |
serials=(), reason=''):
|
580 |
log_time = now() |
581 |
|
582 |
for serial in serials: |
583 |
try:
|
584 |
c = Commission.objects.get(clientkey=clientkey, serial=serial) |
585 |
except Commission.DoesNotExist:
|
586 |
return
|
587 |
|
588 |
t = c.entity |
589 |
|
590 |
provisions = Provision.objects.filter(serial=serial) |
591 |
for pv in provisions: |
592 |
try:
|
593 |
h = Holding.objects.get(entity=pv.entity.entity, |
594 |
resource=pv.resource ) |
595 |
th = Holding.objects.get(entity=t, resource=pv.resource) |
596 |
except Holding.DoesNotExist:
|
597 |
m = "Corrupted provision"
|
598 |
raise CorruptedError(m)
|
599 |
|
600 |
quantity = pv.quantity |
601 |
release = 0
|
602 |
if quantity < 0: |
603 |
release = 1
|
604 |
|
605 |
if release:
|
606 |
h.returned -= quantity |
607 |
th.released -= quantity |
608 |
else:
|
609 |
h.exported += quantity |
610 |
th.imported += quantity |
611 |
|
612 |
reason = 'ACCEPT:' + reason[-121:] |
613 |
self._log_provision(c, h, th, pv, log_time, reason)
|
614 |
h.save() |
615 |
th.save() |
616 |
pv.delete() |
617 |
c.delete() |
618 |
|
619 |
return
|
620 |
|
621 |
def reject_commission(self, context={}, clientkey=None, |
622 |
serials=(), reason=''):
|
623 |
log_time = now() |
624 |
|
625 |
for serial in serials: |
626 |
try:
|
627 |
c = Commission.objects.get(clientkey=clientkey, serial=serial) |
628 |
except Commission.DoesNotExist:
|
629 |
return
|
630 |
|
631 |
t = c.entity |
632 |
|
633 |
provisions = Provision.objects.filter(serial=serial) |
634 |
for pv in provisions: |
635 |
try:
|
636 |
h = Holding.objects.get(entity=pv.entity.entity, |
637 |
resource=pv.resource) |
638 |
th = Holding.objects.get(entity=t, resource=pv.resource) |
639 |
except Holding.DoesNotExist:
|
640 |
m = "Corrupted provision"
|
641 |
raise CorruptedError(m)
|
642 |
|
643 |
quantity = pv.quantity |
644 |
release = 0
|
645 |
if quantity < 0: |
646 |
release = 1
|
647 |
|
648 |
if release:
|
649 |
h.returning += quantity |
650 |
th.releasing += quantity |
651 |
else:
|
652 |
h.exporting -= quantity |
653 |
th.importing -= quantity |
654 |
|
655 |
reason = 'REJECT:' + reason[-121:] |
656 |
self._log_provision(c, h, th, pv, log_time, reason)
|
657 |
h.save() |
658 |
th.save() |
659 |
pv.delete() |
660 |
c.delete() |
661 |
|
662 |
return
|
663 |
|
664 |
def get_pending_commissions(self, context={}, clientkey=None): |
665 |
pending = Commission.objects.filter(clientkey=clientkey)\ |
666 |
.values_list('serial', flat=True) |
667 |
return pending
|
668 |
|
669 |
def resolve_pending_commissions(self, context={}, clientkey=None, |
670 |
max_serial=None, accept_set=() ):
|
671 |
accept_set = set(accept_set)
|
672 |
pending = self.get_pending_commissions(context=context, clientkey=clientkey)
|
673 |
pending = sorted(pending)
|
674 |
|
675 |
accept = self.accept_commission
|
676 |
reject = self.reject_commission
|
677 |
|
678 |
for serial in pending: |
679 |
if serial > max_serial:
|
680 |
break
|
681 |
|
682 |
if serial in accept_set: |
683 |
accept(context=context, clientkey=clientkey, serials=[serial]) |
684 |
else:
|
685 |
reject(context=context, clientkey=clientkey, serials=[serial]) |
686 |
|
687 |
return
|
688 |
|
689 |
def release_entity(self, context={}, release_entity=()): |
690 |
rejected = [] |
691 |
append = rejected.append |
692 |
for entity, key in release_entity: |
693 |
try:
|
694 |
e = Entity.objects.get(entity=entity, key=key) |
695 |
except Entity.DoesNotExist:
|
696 |
append(entity) |
697 |
continue
|
698 |
|
699 |
if e.entities.count() != 0: |
700 |
append(entity) |
701 |
continue
|
702 |
|
703 |
if e.holding_set.count() != 0: |
704 |
append(entity) |
705 |
continue
|
706 |
|
707 |
e.delete() |
708 |
|
709 |
return rejected
|
710 |
|
711 |
def get_timeline(self, context={}, after="", before="Z", get_timeline=()): |
712 |
entity_set = set()
|
713 |
e_add = entity_set.add |
714 |
resource_set = set()
|
715 |
r_add = resource_set.add |
716 |
|
717 |
for entity, resource, key in get_timeline: |
718 |
if entity not in entity_set: |
719 |
try:
|
720 |
e = Entity.objects.get(entity=entity, key=key) |
721 |
e_add(entity) |
722 |
except Entity.DoesNotExist:
|
723 |
continue
|
724 |
|
725 |
r_add((entity, resource)) |
726 |
|
727 |
chunk_size = 65536
|
728 |
nr = 0
|
729 |
timeline = [] |
730 |
append = timeline.append |
731 |
filterlogs = ProvisionLog.objects.filter |
732 |
if entity_set:
|
733 |
q_entity = Q(source__in = entity_set) | Q(target__in = entity_set) |
734 |
else:
|
735 |
q_entity = Q() |
736 |
|
737 |
while 1: |
738 |
logs = filterlogs( q_entity, |
739 |
issue_time__gt = after, |
740 |
issue_time__lte = before, |
741 |
reason__startswith = 'ACCEPT:' )
|
742 |
|
743 |
logs = logs.order_by('issue_time')
|
744 |
#logs = logs.values()
|
745 |
logs = logs[:chunk_size] |
746 |
nr += len(logs)
|
747 |
if not logs: |
748 |
break
|
749 |
for g in logs: |
750 |
if ((g.source, g.resource) not in resource_set |
751 |
or (g.target, g.resource) not in resource_set): |
752 |
continue
|
753 |
|
754 |
o = { |
755 |
'serial' : g.serial,
|
756 |
'source' : g.source,
|
757 |
'target' : g.target,
|
758 |
'resource' : g.resource,
|
759 |
'name' : g.name,
|
760 |
'quantity' : g.delta_quantity,
|
761 |
'source_allocated' : g.source_allocated(),
|
762 |
'source_allocated_through' : g.source_allocated_through(),
|
763 |
'source_inbound' : g.source_inbound(),
|
764 |
'source_inbound_through' : g.source_inbound_through(),
|
765 |
'source_outbound' : g.source_outbound(),
|
766 |
'source_outbound_through' : g.source_outbound_through(),
|
767 |
'target_allocated' : g.target_allocated(),
|
768 |
'target_allocated_through' : g.target_allocated_through(),
|
769 |
'target_inbound' : g.target_inbound(),
|
770 |
'target_inbound_through' : g.target_inbound_through(),
|
771 |
'target_outbound' : g.target_outbound(),
|
772 |
'target_outbound_through' : g.target_outbound_through(),
|
773 |
'issue_time' : g.issue_time,
|
774 |
'log_time' : g.log_time,
|
775 |
'reason' : g.reason,
|
776 |
} |
777 |
|
778 |
append(o) |
779 |
|
780 |
after = g.issue_time |
781 |
if after >= before:
|
782 |
break
|
783 |
|
784 |
return timeline
|
785 |
|
786 |
|
787 |
API_Callpoint = QuotaholderDjangoDBCallpoint |
788 |
|