root / astakosclient / astakosclient / __init__.py @ db9f7a2b
History | View | Annotate | Download (18.9 kB)
1 |
# Copyright (C) 2012, 2013 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 |
import logging |
35 |
import urlparse |
36 |
import urllib |
37 |
import hashlib |
38 |
from copy import copy |
39 |
|
40 |
import simplejson |
41 |
from astakosclient.utils import \ |
42 |
retry, scheme_to_class, parse_request, check_input |
43 |
from astakosclient.errors import \ |
44 |
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \ |
45 |
NoUserName, NoUUID, BadValue, QuotaLimit, InvalidResponse |
46 |
|
47 |
|
48 |
# --------------------------------------------------------------------
|
49 |
# Astakos Client Class
|
50 |
|
51 |
def get_token_from_cookie(request, cookie_name): |
52 |
"""Extract token from the cookie name provided
|
53 |
|
54 |
Cookie should be in the same form as astakos
|
55 |
service sets its cookie contents:
|
56 |
<user_uniq>|<user_token>
|
57 |
|
58 |
"""
|
59 |
try:
|
60 |
cookie_content = urllib.unquote(request.COOKIE.get(cookie_name, None))
|
61 |
return cookie_content.split("|")[1] |
62 |
except:
|
63 |
return None |
64 |
|
65 |
|
66 |
class AstakosClient(): |
67 |
"""AstakosClient Class Implementation"""
|
68 |
|
69 |
# ----------------------------------
|
70 |
def __init__(self, astakos_url, retry=0, |
71 |
use_pool=False, pool_size=8, logger=None): |
72 |
"""Initialize AstakosClient Class
|
73 |
|
74 |
Keyword arguments:
|
75 |
astakos_url -- i.e https://accounts.example.com (string)
|
76 |
use_pool -- use objpool for http requests (boolean)
|
77 |
retry -- how many time to retry (integer)
|
78 |
logger -- pass a different logger
|
79 |
|
80 |
"""
|
81 |
if logger is None: |
82 |
logging.basicConfig( |
83 |
format='%(asctime)s [%(levelname)s] %(name)s %(message)s',
|
84 |
datefmt='%Y-%m-%d %H:%M:%S',
|
85 |
level=logging.INFO) |
86 |
logger = logging.getLogger("astakosclient")
|
87 |
logger.debug("Intialize AstakosClient: astakos_url = %s, "
|
88 |
"use_pool = %s" % (astakos_url, use_pool))
|
89 |
|
90 |
check_input("__init__", logger, astakos_url=astakos_url)
|
91 |
|
92 |
# Check for supported scheme
|
93 |
p = urlparse.urlparse(astakos_url) |
94 |
conn_class = scheme_to_class(p.scheme, use_pool, pool_size) |
95 |
if conn_class is None: |
96 |
m = "Unsupported scheme: %s" % p.scheme
|
97 |
logger.error(m) |
98 |
raise BadValue(m)
|
99 |
|
100 |
# Save astakos_url etc. in our class
|
101 |
self.retry = retry
|
102 |
self.logger = logger
|
103 |
self.netloc = p.netloc
|
104 |
self.scheme = p.scheme
|
105 |
self.conn_class = conn_class
|
106 |
|
107 |
# ----------------------------------
|
108 |
@retry
|
109 |
def _call_astakos(self, token, request_path, |
110 |
headers=None, body=None, method="GET"): |
111 |
"""Make the actual call to Astakos Service"""
|
112 |
if token is not None: |
113 |
hashed_token = hashlib.sha1() |
114 |
hashed_token.update(token) |
115 |
using_token = "using token %s" % (hashed_token.hexdigest())
|
116 |
else:
|
117 |
using_token = "without using token"
|
118 |
self.logger.debug(
|
119 |
"Make a %s request to %s %s with headers %s and body %s"
|
120 |
% (method, request_path, using_token, headers, body)) |
121 |
|
122 |
# Check Input
|
123 |
if headers is None: |
124 |
headers = {} |
125 |
if body is None: |
126 |
body = {} |
127 |
if request_path[0] != '/': |
128 |
request_path = "/" + request_path
|
129 |
|
130 |
# Build request's header and body
|
131 |
kwargs = {} |
132 |
kwargs['headers'] = copy(headers)
|
133 |
if token is not None: |
134 |
kwargs['headers']['X-Auth-Token'] = token |
135 |
if body:
|
136 |
kwargs['body'] = copy(body)
|
137 |
kwargs['headers'].setdefault(
|
138 |
'content-type', 'application/octet-stream') |
139 |
kwargs['headers'].setdefault('content-length', |
140 |
len(body) if body else 0) |
141 |
|
142 |
try:
|
143 |
# Get the connection object
|
144 |
with self.conn_class(self.netloc) as conn: |
145 |
# Send request
|
146 |
(message, data, status) = \ |
147 |
_do_request(conn, method, request_path, **kwargs) |
148 |
except Exception as err: |
149 |
self.logger.error("Failed to send request: %s" % repr(err)) |
150 |
raise AstakosClientException(str(err)) |
151 |
|
152 |
# Return
|
153 |
self.logger.debug("Request returned with status %s" % status) |
154 |
if status == 400: |
155 |
raise BadRequest(message, data)
|
156 |
elif status == 401: |
157 |
raise Unauthorized(message, data)
|
158 |
elif status == 403: |
159 |
raise Forbidden(message, data)
|
160 |
elif status == 404: |
161 |
raise NotFound(message, data)
|
162 |
elif status < 200 or status >= 300: |
163 |
raise AstakosClientException(message, data, status)
|
164 |
|
165 |
try:
|
166 |
if data:
|
167 |
return simplejson.loads(unicode(data)) |
168 |
else:
|
169 |
return None |
170 |
except Exception as err: |
171 |
self.logger.error("Cannot parse response \"%s\" with simplejson: %s" |
172 |
% (data, str(err)))
|
173 |
raise InvalidResponse(str(err), data) |
174 |
|
175 |
# ------------------------
|
176 |
# GET /im/authenticate
|
177 |
def get_user_info(self, token, usage=False): |
178 |
"""Authenticate user and get user's info as a dictionary
|
179 |
|
180 |
Keyword arguments:
|
181 |
token -- user's token (string)
|
182 |
usage -- return usage information for user (boolean)
|
183 |
|
184 |
In case of success return user information (json parsed format).
|
185 |
Otherwise raise an AstakosClientException.
|
186 |
|
187 |
"""
|
188 |
# Send request
|
189 |
auth_path = "/im/authenticate"
|
190 |
if usage:
|
191 |
auth_path += "?usage=1"
|
192 |
return self._call_astakos(token, auth_path) |
193 |
|
194 |
# ----------------------------------
|
195 |
# POST /user_catalogs (or /service/api/user_catalogs)
|
196 |
# with {'uuids': uuids}
|
197 |
def _uuid_catalog(self, token, uuids, req_path): |
198 |
req_headers = {'content-type': 'application/json'} |
199 |
req_body = parse_request({'uuids': uuids}, self.logger) |
200 |
data = self._call_astakos(
|
201 |
token, req_path, req_headers, req_body, "POST")
|
202 |
if "uuid_catalog" in data: |
203 |
return data.get("uuid_catalog") |
204 |
else:
|
205 |
m = "_uuid_catalog request returned %s. No uuid_catalog found" \
|
206 |
% data |
207 |
self.logger.error(m)
|
208 |
raise AstakosClientException(m)
|
209 |
|
210 |
def get_usernames(self, token, uuids): |
211 |
"""Return a uuid_catalog dictionary for the given uuids
|
212 |
|
213 |
Keyword arguments:
|
214 |
token -- user's token (string)
|
215 |
uuids -- list of user ids (list of strings)
|
216 |
|
217 |
The returned uuid_catalog is a dictionary with uuids as
|
218 |
keys and the corresponding user names as values
|
219 |
|
220 |
"""
|
221 |
req_path = "/user_catalogs"
|
222 |
return self._uuid_catalog(token, uuids, req_path) |
223 |
|
224 |
def get_username(self, token, uuid): |
225 |
"""Return the user name of a uuid (see get_usernames)"""
|
226 |
check_input("get_username", self.logger, uuid=uuid) |
227 |
uuid_dict = self.get_usernames(token, [uuid])
|
228 |
if uuid in uuid_dict: |
229 |
return uuid_dict.get(uuid)
|
230 |
else:
|
231 |
raise NoUserName(uuid)
|
232 |
|
233 |
def service_get_usernames(self, token, uuids): |
234 |
"""Return a uuid_catalog dict using a service's token"""
|
235 |
req_path = "/service/api/user_catalogs"
|
236 |
return self._uuid_catalog(token, uuids, req_path) |
237 |
|
238 |
def service_get_username(self, token, uuid): |
239 |
"""Return the displayName of a uuid using a service's token"""
|
240 |
check_input("service_get_username", self.logger, uuid=uuid) |
241 |
uuid_dict = self.service_get_usernames(token, [uuid])
|
242 |
if uuid in uuid_dict: |
243 |
return uuid_dict.get(uuid)
|
244 |
else:
|
245 |
raise NoUserName(uuid)
|
246 |
|
247 |
# ----------------------------------
|
248 |
# POST /user_catalogs (or /service/api/user_catalogs)
|
249 |
# with {'displaynames': display_names}
|
250 |
def _displayname_catalog(self, token, display_names, req_path): |
251 |
req_headers = {'content-type': 'application/json'} |
252 |
req_body = parse_request({'displaynames': display_names}, self.logger) |
253 |
data = self._call_astakos(
|
254 |
token, req_path, req_headers, req_body, "POST")
|
255 |
if "displayname_catalog" in data: |
256 |
return data.get("displayname_catalog") |
257 |
else:
|
258 |
m = "_displayname_catalog request returned %s. " \
|
259 |
"No displayname_catalog found" % data
|
260 |
self.logger.error(m)
|
261 |
raise AstakosClientException(m)
|
262 |
|
263 |
def get_uuids(self, token, display_names): |
264 |
"""Return a displayname_catalog for the given names
|
265 |
|
266 |
Keyword arguments:
|
267 |
token -- user's token (string)
|
268 |
display_names -- list of user names (list of strings)
|
269 |
|
270 |
The returned displayname_catalog is a dictionary with
|
271 |
the names as keys and the corresponding uuids as values
|
272 |
|
273 |
"""
|
274 |
req_path = "/user_catalogs"
|
275 |
return self._displayname_catalog(token, display_names, req_path) |
276 |
|
277 |
def get_uuid(self, token, display_name): |
278 |
"""Return the uuid of a name (see getUUIDs)"""
|
279 |
check_input("get_uuid", self.logger, display_name=display_name) |
280 |
name_dict = self.get_uuids(token, [display_name])
|
281 |
if display_name in name_dict: |
282 |
return name_dict.get(display_name)
|
283 |
else:
|
284 |
raise NoUUID(display_name)
|
285 |
|
286 |
def service_get_uuids(self, token, display_names): |
287 |
"""Return a display_name catalog using a service's token"""
|
288 |
req_path = "/service/api/user_catalogs"
|
289 |
return self._displayname_catalog(token, display_names, req_path) |
290 |
|
291 |
def service_get_uuid(self, token, display_name): |
292 |
"""Return the uuid of a name using a service's token"""
|
293 |
check_input("service_get_uuid", self.logger, display_name=display_name) |
294 |
name_dict = self.service_get_uuids(token, [display_name])
|
295 |
if display_name in name_dict: |
296 |
return name_dict.get(display_name)
|
297 |
else:
|
298 |
raise NoUUID(display_name)
|
299 |
|
300 |
# ----------------------------------
|
301 |
# GET "/im/get_services"
|
302 |
def get_services(self): |
303 |
"""Return a list of dicts with the registered services"""
|
304 |
return self._call_astakos(None, "/im/get_services") |
305 |
|
306 |
# ----------------------------------
|
307 |
# GET "/astakos/api/resources"
|
308 |
def get_resources(self): |
309 |
"""Return a dict of dicts with the available resources"""
|
310 |
return self._call_astakos(None, "/astakos/api/resources") |
311 |
|
312 |
# ----------------------------------
|
313 |
# GET "/astakos/api/quotas"
|
314 |
def get_quotas(self, token): |
315 |
"""Get user's quotas
|
316 |
|
317 |
Keyword arguments:
|
318 |
token -- user's token (string)
|
319 |
|
320 |
In case of success return a dict of dicts with user's current quotas.
|
321 |
Otherwise raise an AstakosClientException
|
322 |
|
323 |
"""
|
324 |
return self._call_astakos(token, "/astakos/api/quotas") |
325 |
|
326 |
# ----------------------------------
|
327 |
# GET "/astakos/api/service_quotas"
|
328 |
def get_service_quotas(self, token, user=None): |
329 |
"""Get all quotas for resources associated with the service
|
330 |
|
331 |
Keyword arguments:
|
332 |
token -- service's token (string)
|
333 |
user -- optionally, the uuid of a specific user
|
334 |
|
335 |
In case of success return a dict of dicts of dicts with current quotas
|
336 |
for all users, or of a specified user, if user argument is set.
|
337 |
Otherwise raise an AstakosClientException
|
338 |
|
339 |
"""
|
340 |
query = "/astakos/api/service_quotas"
|
341 |
if user is not None: |
342 |
query += "?user=" + user
|
343 |
return self._call_astakos(token, query) |
344 |
|
345 |
# ----------------------------------
|
346 |
# POST "/astakos/api/commisions"
|
347 |
def issue_commission(self, token, request): |
348 |
"""Issue a commission
|
349 |
|
350 |
Keyword arguments:
|
351 |
token -- service's token (string)
|
352 |
request -- commision request (dict)
|
353 |
|
354 |
In case of success return commission's id (int).
|
355 |
Otherwise raise an AstakosClientException.
|
356 |
|
357 |
"""
|
358 |
req_headers = {'content-type': 'application/json'} |
359 |
req_body = parse_request(request, self.logger)
|
360 |
try:
|
361 |
response = self._call_astakos(token, "/astakos/api/commissions", |
362 |
req_headers, req_body, "POST")
|
363 |
except AstakosClientException as err: |
364 |
if err.status == 413: |
365 |
raise QuotaLimit(err.message, err.details)
|
366 |
else:
|
367 |
raise
|
368 |
|
369 |
if "serial" in response: |
370 |
return response['serial'] |
371 |
else:
|
372 |
m = "issue_commission_core request returned %s. No serial found" \
|
373 |
% response |
374 |
self.logger.error(m)
|
375 |
raise AstakosClientException(m)
|
376 |
|
377 |
def issue_one_commission(self, token, holder, source, provisions, |
378 |
force=False, auto_accept=False): |
379 |
"""Issue one commission (with specific holder and source)
|
380 |
|
381 |
keyword arguments:
|
382 |
token -- service's token (string)
|
383 |
holder -- user's id (string)
|
384 |
source -- commission's source (ex system) (string)
|
385 |
provisions -- resources with their quantity (list of (string, int))
|
386 |
force -- force this commission (boolean)
|
387 |
auto_accept -- auto accept this commission (boolean)
|
388 |
|
389 |
In case of success return commission's id (int).
|
390 |
Otherwise raise an AstakosClientException.
|
391 |
(See also issue_commission)
|
392 |
|
393 |
"""
|
394 |
check_input("issue_one_commission", self.logger, |
395 |
holder=holder, source=source, |
396 |
provisions=provisions) |
397 |
|
398 |
request = {} |
399 |
request["force"] = force
|
400 |
request["auto_accept"] = auto_accept
|
401 |
try:
|
402 |
request["provisions"] = []
|
403 |
for p in provisions: |
404 |
resource = p[0]
|
405 |
quantity = p[1]
|
406 |
t = {"holder": holder, "source": source, |
407 |
"resource": resource, "quantity": quantity} |
408 |
request["provisions"].append(t)
|
409 |
except Exception as err: |
410 |
self.logger.error(str(err)) |
411 |
raise BadValue(str(err)) |
412 |
|
413 |
return self.issue_commission(token, request) |
414 |
|
415 |
# ----------------------------------
|
416 |
# GET "/astakos/api/commissions"
|
417 |
def get_pending_commissions(self, token): |
418 |
"""Get Pending Commissions
|
419 |
|
420 |
Keyword arguments:
|
421 |
token -- service's token (string)
|
422 |
|
423 |
In case of success return a list of pending commissions' ids
|
424 |
(list of integers)
|
425 |
|
426 |
"""
|
427 |
return self._call_astakos(token, "/astakos/api/commissions") |
428 |
|
429 |
# ----------------------------------
|
430 |
# GET "/astakos/api/commissions/<serial>
|
431 |
def get_commission_info(self, token, serial): |
432 |
"""Get Description of a Commission
|
433 |
|
434 |
Keyword arguments:
|
435 |
token -- service's token (string)
|
436 |
serial -- commission's id (int)
|
437 |
|
438 |
In case of success return a dict of dicts containing
|
439 |
informations (details) about the requested commission
|
440 |
|
441 |
"""
|
442 |
check_input("get_commission_info", self.logger, serial=serial) |
443 |
|
444 |
path = "/astakos/api/commissions/" + str(serial) |
445 |
return self._call_astakos(token, path) |
446 |
|
447 |
# ----------------------------------
|
448 |
# POST "/astakos/api/commissions/<serial>/action"
|
449 |
def commission_action(self, token, serial, action): |
450 |
"""Perform a commission action
|
451 |
|
452 |
Keyword arguments:
|
453 |
token -- service's token (string)
|
454 |
serial -- commission's id (int)
|
455 |
action -- action to perform, currently accept/reject (string)
|
456 |
|
457 |
In case of success return nothing.
|
458 |
|
459 |
"""
|
460 |
check_input("commission_action", self.logger, |
461 |
serial=serial, action=action) |
462 |
|
463 |
path = "/astakos/api/commissions/" + str(serial) + "/action" |
464 |
req_headers = {'content-type': 'application/json'} |
465 |
req_body = parse_request({str(action): ""}, self.logger) |
466 |
self._call_astakos(token, path, req_headers, req_body, "POST") |
467 |
|
468 |
def accept_commission(self, token, serial): |
469 |
"""Accept a commission (see commission_action)"""
|
470 |
self.commission_action(token, serial, "accept") |
471 |
|
472 |
def reject_commission(self, token, serial): |
473 |
"""Reject a commission (see commission_action)"""
|
474 |
self.commission_action(token, serial, "reject") |
475 |
|
476 |
# ----------------------------------
|
477 |
# POST "/astakos/api/commissions/action"
|
478 |
def resolve_commissions(self, token, accept_serials, reject_serials): |
479 |
"""Resolve multiple commissions at once
|
480 |
|
481 |
Keyword arguments:
|
482 |
token -- service's token (string)
|
483 |
accept_serials -- commissions to accept (list of ints)
|
484 |
reject_serials -- commissions to reject (list of ints)
|
485 |
|
486 |
In case of success return a dict of dicts describing which
|
487 |
commissions accepted, which rejected and which failed to
|
488 |
resolved.
|
489 |
|
490 |
"""
|
491 |
check_input("resolve_commissions", self.logger, |
492 |
accept_serials=accept_serials, |
493 |
reject_serials=reject_serials) |
494 |
|
495 |
path = "/astakos/api/commissions/action"
|
496 |
req_headers = {'content-type': 'application/json'} |
497 |
req_body = parse_request({"accept": accept_serials,
|
498 |
"reject": reject_serials},
|
499 |
self.logger)
|
500 |
return self._call_astakos(token, path, req_headers, req_body, "POST") |
501 |
|
502 |
|
503 |
# --------------------------------------------------------------------
|
504 |
# Private functions
|
505 |
# We want _doRequest to be a distinct function
|
506 |
# so that we can replace it during unit tests.
|
507 |
def _do_request(conn, method, url, **kwargs): |
508 |
"""The actual request. This function can easily be mocked"""
|
509 |
conn.request(method, url, **kwargs) |
510 |
response = conn.getresponse() |
511 |
length = response.getheader('content-length', None) |
512 |
data = response.read(length) |
513 |
status = int(response.status)
|
514 |
message = response.reason |
515 |
return (message, data, status)
|