Revision 865849d7 snf-astakos-app/astakos/im/endpoints/qh.py
b/snf-astakos-app/astakos/im/endpoints/qh.py | ||
---|---|---|
40 | 40 |
from django.utils.translation import ugettext as _ |
41 | 41 |
|
42 | 42 |
from astakos.im.settings import ( |
43 |
QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL)
|
|
43 |
QUOTAHOLDER_URL, QUOTAHOLDER_TOKEN, LOGGING_LEVEL) |
|
44 | 44 |
|
45 | 45 |
if QUOTAHOLDER_URL: |
46 | 46 |
from kamaki.clients.quotaholder import QuotaholderClient |
... | ... | |
58 | 58 |
clientkey = 'astakos' |
59 | 59 |
|
60 | 60 |
_client = None |
61 |
|
|
62 |
|
|
61 | 63 |
def get_client(): |
62 | 64 |
global _client |
63 | 65 |
if _client: |
... | ... | |
67 | 69 |
_client = QuotaholderClient(QUOTAHOLDER_URL, token=QUOTAHOLDER_TOKEN) |
68 | 70 |
return _client |
69 | 71 |
|
72 |
|
|
70 | 73 |
def set_quota(payload): |
71 | 74 |
c = get_client() |
72 | 75 |
if not c: |
... | ... | |
75 | 78 |
logger.debug('set_quota: %s rejected: %s' % (payload, result)) |
76 | 79 |
return result |
77 | 80 |
|
81 |
|
|
78 | 82 |
def get_entity(payload): |
79 | 83 |
c = get_client() |
80 | 84 |
if not c: |
... | ... | |
83 | 87 |
logger.debug('get_entity: %s reply: %s' % (payload, result)) |
84 | 88 |
return result |
85 | 89 |
|
90 |
|
|
86 | 91 |
def get_holding(payload): |
87 | 92 |
c = get_client() |
88 | 93 |
if not c: |
... | ... | |
91 | 96 |
logger.debug('get_holding: %s reply: %s' % (payload, result)) |
92 | 97 |
return result |
93 | 98 |
|
99 |
|
|
94 | 100 |
def qh_get_holdings(users, resources): |
95 | 101 |
payload = [] |
96 | 102 |
append = payload.append |
... | ... | |
100 | 106 |
result = get_holding(payload) |
101 | 107 |
return result |
102 | 108 |
|
109 |
|
|
103 | 110 |
def quota_limits_per_user_from_get(lst): |
104 | 111 |
quotas = {} |
105 | 112 |
for holder, resource, q, c, il, el, imp, exp, ret, rel, flags in lst: |
... | ... | |
109 | 116 |
quotas[holder] = userquotas |
110 | 117 |
return quotas |
111 | 118 |
|
119 |
|
|
112 | 120 |
def quotas_per_user_from_get(lst): |
113 | 121 |
limits = {} |
114 | 122 |
counters = {} |
... | ... | |
139 | 147 |
logger.debug('get_quota: %s rejected: %s' % (payload, result)) |
140 | 148 |
return result |
141 | 149 |
|
150 |
|
|
142 | 151 |
def qh_get_quota_limits(users, resources): |
143 | 152 |
result = qh_get_quota(users, resources) |
144 | 153 |
return quota_limits_per_user_from_get(result) |
145 | 154 |
|
155 |
|
|
146 | 156 |
def qh_get_quotas(users, resources): |
147 | 157 |
result = qh_get_quota(users, resources) |
148 | 158 |
return quotas_per_user_from_get(result) |
149 | 159 |
|
160 |
|
|
150 | 161 |
def create_entity(payload): |
151 | 162 |
c = get_client() |
152 | 163 |
if not c: |
... | ... | |
190 | 201 |
'import_limit', |
191 | 202 |
'export_limit'))): |
192 | 203 |
__slots__ = () |
204 |
|
|
193 | 205 |
def __dir__(self): |
194 | 206 |
return ['quantity', 'capacity', 'import_limit', 'export_limit'] |
195 | 207 |
|
196 | 208 |
def __str__(self): |
197 |
return '\t'.join(['%s=%s' % (f, strbigdec(getattr(self, f))) for f in dir(self)]) |
|
209 |
return '\t'.join(['%s=%s' % (f, strbigdec(getattr(self, f))) |
|
210 |
for f in dir(self)]) |
|
211 |
|
|
198 | 212 |
|
199 | 213 |
def add_quota_values(q1, q2): |
200 | 214 |
return QuotaValues( |
... | ... | |
203 | 217 |
import_limit = q1.import_limit + q2.import_limit, |
204 | 218 |
export_limit = q1.export_limit + q2.export_limit) |
205 | 219 |
|
220 |
|
|
206 | 221 |
def qh_register_user_with_quotas(user): |
207 | 222 |
return register_users_with_quotas([user]) |
208 | 223 |
|
224 |
|
|
209 | 225 |
def register_users_with_quotas(users): |
210 | 226 |
rejected = register_users(users) |
211 | 227 |
if not rejected: |
212 | 228 |
register_quotas(users) |
213 | 229 |
|
230 |
|
|
214 | 231 |
def register_users(users): |
215 | 232 |
if not users: |
216 | 233 |
return |
217 | 234 |
|
218 | 235 |
payload = list(CreateEntityPayload( |
219 |
entity=u.uuid,
|
|
220 |
owner='system',
|
|
221 |
key=ENTITY_KEY,
|
|
222 |
ownerkey='') for u in users)
|
|
236 |
entity=u.uuid, |
|
237 |
owner='system', |
|
238 |
key=ENTITY_KEY, |
|
239 |
ownerkey='') for u in users) |
|
223 | 240 |
return create_entity(payload) |
224 | 241 |
|
242 |
|
|
225 | 243 |
def register_quotas(users): |
226 | 244 |
if not users: |
227 | 245 |
return |
... | ... | |
230 | 248 |
append = payload.append |
231 | 249 |
for u in users: |
232 | 250 |
for resource, q in u.all_quotas().iteritems(): |
233 |
append( SetQuotaPayload(
|
|
251 |
append(SetQuotaPayload( |
|
234 | 252 |
holder=u.uuid, |
235 | 253 |
resource=resource, |
236 | 254 |
key=ENTITY_KEY, |
... | ... | |
241 | 259 |
flags=0)) |
242 | 260 |
return set_quota(payload) |
243 | 261 |
|
262 |
|
|
244 | 263 |
def send_quotas(userquotas): |
245 | 264 |
if not userquotas: |
246 | 265 |
return |
... | ... | |
249 | 268 |
append = payload.append |
250 | 269 |
for holder, quotas in userquotas.iteritems(): |
251 | 270 |
for resource, q in quotas.iteritems(): |
252 |
append( SetQuotaPayload(
|
|
271 |
append(SetQuotaPayload( |
|
253 | 272 |
holder=holder, |
254 | 273 |
resource=resource, |
255 | 274 |
key=ENTITY_KEY, |
... | ... | |
260 | 279 |
flags=0)) |
261 | 280 |
return set_quota(payload) |
262 | 281 |
|
282 |
|
|
263 | 283 |
def register_services(services): |
264 | 284 |
def payload(services): |
265 | 285 |
return list(CreateEntityPayload( |
... | ... | |
282 | 302 |
m = "Failed to register services: %s" % (failed,) |
283 | 303 |
raise RuntimeError(m) |
284 | 304 |
|
305 |
|
|
285 | 306 |
def register_resources(resources): |
286 | 307 |
try: |
287 | 308 |
QH_PRACTICALLY_INFINITE |
... | ... | |
299 | 320 |
flags=0) for resource in resources) |
300 | 321 |
return set_quota(payload) |
301 | 322 |
|
323 |
|
|
302 | 324 |
def qh_check_users(users): |
303 | 325 |
payload = [(u.uuid, ENTITY_KEY) for u in users] |
304 | 326 |
result = get_entity(payload) |
... | ... | |
313 | 335 |
nonexisting.append(u) |
314 | 336 |
return (existing, nonexisting) |
315 | 337 |
|
338 |
|
|
316 | 339 |
def qh_add_quota(serial, sub_list, add_list): |
317 | 340 |
if not QUOTAHOLDER_URL: |
318 | 341 |
return () |
... | ... | |
343 | 366 |
|
344 | 367 |
return result |
345 | 368 |
|
369 |
|
|
346 | 370 |
def qh_query_serials(serials): |
347 | 371 |
if not QUOTAHOLDER_URL: |
348 | 372 |
return () |
... | ... | |
354 | 378 |
serials=serials) |
355 | 379 |
return result |
356 | 380 |
|
381 |
|
|
357 | 382 |
def qh_ack_serials(serials): |
358 | 383 |
if not QUOTAHOLDER_URL: |
359 | 384 |
return () |
... | ... | |
415 | 440 |
|
416 | 441 |
target = point['target'] |
417 | 442 |
if details: |
418 |
yield (target,
|
|
419 |
point['resource'],
|
|
420 |
point['name'],
|
|
421 |
issue_time,
|
|
422 |
uu_cost,
|
|
423 |
uu_total)
|
|
443 |
yield (target, |
|
444 |
point['resource'], |
|
445 |
point['name'], |
|
446 |
issue_time, |
|
447 |
uu_cost, |
|
448 |
uu_total) |
|
424 | 449 |
|
425 | 450 |
if not t_total: |
426 | 451 |
return |
427 | 452 |
|
428 |
yield (target,
|
|
429 |
'total',
|
|
430 |
point['resource'],
|
|
431 |
issue_time,
|
|
432 |
uu_total / t_total,
|
|
433 |
uu_total)
|
|
453 |
yield (target, |
|
454 |
'total', |
|
455 |
point['resource'], |
|
456 |
issue_time, |
|
457 |
uu_total / t_total, |
|
458 |
uu_total) |
|
434 | 459 |
|
435 | 460 |
|
436 | 461 |
def usage_units(timeline, after, before, details=0): |
... | ... | |
454 | 479 |
tu_total += tu |
455 | 480 |
|
456 | 481 |
if details: |
457 |
yield (target,
|
|
458 |
point['resource'],
|
|
459 |
point['name'],
|
|
460 |
issue_time,
|
|
461 |
tu,
|
|
462 |
tu_total)
|
|
482 |
yield (target, |
|
483 |
point['resource'], |
|
484 |
point['name'], |
|
485 |
issue_time, |
|
486 |
tu, |
|
487 |
tu_total) |
|
463 | 488 |
|
464 | 489 |
if not tu_total: |
465 | 490 |
return |
466 | 491 |
|
467 |
yield (target,
|
|
468 |
'total',
|
|
469 |
point['resource'],
|
|
470 |
issue_time,
|
|
471 |
tu_total // len(timeline),
|
|
472 |
tu_total)
|
|
492 |
yield (target, |
|
493 |
'total', |
|
494 |
point['resource'], |
|
495 |
issue_time, |
|
496 |
tu_total // len(timeline), |
|
497 |
tu_total) |
|
473 | 498 |
|
474 | 499 |
|
475 | 500 |
def timeline_charge(entity, resource, after, before, details, charge_type): |
Also available in: Unified diff