Revision 865849d7
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): |
b/snf-astakos-app/astakos/im/management/commands/_common.py | ||
---|---|---|
66 | 66 |
else: |
67 | 67 |
return 'in ' + timeuntil(d) |
68 | 68 |
|
69 |
|
|
69 | 70 |
def format_dict(d, level=1, ident=22): |
70 | 71 |
iteritems = d.iteritems() |
71 | 72 |
if not isinstance(d, OrderedDict): |
... | ... | |
76 | 77 |
l.insert(0, '\n') |
77 | 78 |
return ''.join(l) |
78 | 79 |
|
80 |
|
|
79 | 81 |
def format_set(s): |
80 | 82 |
return list(s) |
81 | 83 |
|
84 |
|
|
82 | 85 |
def format(obj, level=1, ident=22): |
83 | 86 |
if isinstance(obj, bool): |
84 | 87 |
return format_bool(obj) |
... | ... | |
91 | 94 |
else: |
92 | 95 |
return obj |
93 | 96 |
|
97 |
|
|
94 | 98 |
def get_astakosuser_content_type(): |
95 | 99 |
try: |
96 | 100 |
return ContentType.objects.get(app_label='im', |
... | ... | |
148 | 152 |
except Permission.DoesNotExist: |
149 | 153 |
return -1 |
150 | 154 |
|
155 |
|
|
151 | 156 |
def shortened(s, limit, suffix=True): |
152 | 157 |
length = len(s) |
153 | 158 |
if length <= limit: |
b/snf-astakos-app/astakos/im/management/commands/project-list.py | ||
---|---|---|
106 | 106 |
dest='csv', |
107 | 107 |
default=False, |
108 | 108 |
help="Use pipes to separate values"), |
109 |
)
|
|
109 |
) |
|
110 | 110 |
|
111 | 111 |
def handle_noargs(self, **options): |
112 | 112 |
allow_shorten = not options['full'] |
... | ... | |
151 | 151 |
(info['email'], True), |
152 | 152 |
(info['status'], False), |
153 | 153 |
(info['appid'], False), |
154 |
]
|
|
154 |
] |
|
155 | 155 |
|
156 | 156 |
fields = [(format(elem), flag) for (elem, flag) in fields] |
157 | 157 |
|
... | ... | |
169 | 169 |
|
170 | 170 |
self.stdout.write(line + '\n') |
171 | 171 |
|
172 |
|
|
172 | 173 |
def filter_by_state(chain_dict, states): |
173 | 174 |
d = {} |
174 | 175 |
for chain, (state, project, app) in chain_dict.iteritems(): |
... | ... | |
176 | 177 |
d[chain] = (state, project, app) |
177 | 178 |
return d |
178 | 179 |
|
180 |
|
|
179 | 181 |
def chain_info(chain_dict): |
180 | 182 |
l = [] |
181 | 183 |
for chain, (state, project, app) in chain_dict.iteritems(): |
... | ... | |
186 | 188 |
appid = "" |
187 | 189 |
|
188 | 190 |
d = { |
189 |
'projectid' : str(chain),
|
|
190 |
'name' : project.application.name if project else app.name,
|
|
191 |
'applicant' : app.applicant.realname,
|
|
192 |
'email' : app.applicant.email,
|
|
191 |
'projectid': str(chain), |
|
192 |
'name': project.application.name if project else app.name, |
|
193 |
'applicant': app.applicant.realname, |
|
194 |
'email': app.applicant.email, |
|
193 | 195 |
'status': status, |
194 |
'appid' : appid,
|
|
195 |
}
|
|
196 |
'appid': appid, |
|
197 |
} |
|
196 | 198 |
l.append(d) |
197 | 199 |
return l |
b/snf-astakos-app/astakos/im/management/commands/user-show.py | ||
---|---|---|
41 | 41 |
|
42 | 42 |
import uuid |
43 | 43 |
|
44 |
|
|
44 | 45 |
class Command(BaseCommand): |
45 | 46 |
args = "<user ID or email>" |
46 | 47 |
help = "Show user info" |
Also available in: Unified diff