Statistics
| Branch: | Tag: | Revision:

root / astakosclient / astakosclient / __init__.py @ 5c418e94

History | View | Annotate | Download (18.2 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
    # POST "/astakos/api/commisions"
328
    def issue_commission(self, token, request):
329
        """Issue a commission
330

331
        Keyword arguments:
332
        token   -- service's token (string)
333
        request -- commision request (dict)
334

335
        In case of success return commission's id (int).
336
        Otherwise raise an AstakosClientException.
337

338
        """
339
        req_headers = {'content-type': 'application/json'}
340
        req_body = parse_request(request, self.logger)
341
        try:
342
            response = self._call_astakos(token, "/astakos/api/commissions",
343
                                          req_headers, req_body, "POST")
344
        except AstakosClientException as err:
345
            if err.status == 413:
346
                raise QuotaLimit(err.message, err.details)
347
            else:
348
                raise
349

    
350
        if "serial" in response:
351
            return response['serial']
352
        else:
353
            m = "issue_commission_core request returned %s. No serial found" \
354
                % response
355
            self.logger.error(m)
356
            raise AstakosClientException(m)
357

    
358
    def issue_one_commission(self, token, holder, source, provisions,
359
                             force=False, auto_accept=False):
360
        """Issue one commission (with specific holder and source)
361

362
        keyword arguments:
363
        token       -- service's token (string)
364
        holder      -- user's id (string)
365
        source      -- commission's source (ex system) (string)
366
        provisions  -- resources with their quantity (list of (string, int))
367
        force       -- force this commission (boolean)
368
        auto_accept -- auto accept this commission (boolean)
369

370
        In case of success return commission's id (int).
371
        Otherwise raise an AstakosClientException.
372
        (See also issue_commission)
373

374
        """
375
        check_input("issue_one_commission", self.logger,
376
                    holder=holder, source=source,
377
                    provisions=provisions)
378

    
379
        request = {}
380
        request["force"] = force
381
        request["auto_accept"] = auto_accept
382
        try:
383
            request["provisions"] = []
384
            for p in provisions:
385
                resource = p[0]
386
                quantity = p[1]
387
                t = {"holder": holder, "source": source,
388
                     "resource": resource, "quantity": quantity}
389
                request["provisions"].append(t)
390
        except Exception as err:
391
            self.logger.error(str(err))
392
            raise BadValue(str(err))
393

    
394
        return self.issue_commission(token, request)
395

    
396
    # ----------------------------------
397
    # GET "/astakos/api/commissions"
398
    def get_pending_commissions(self, token):
399
        """Get Pending Commissions
400

401
        Keyword arguments:
402
        token   -- service's token (string)
403

404
        In case of success return a list of pending commissions' ids
405
        (list of integers)
406

407
        """
408
        return self._call_astakos(token, "/astakos/api/commissions")
409

    
410
    # ----------------------------------
411
    # GET "/astakos/api/commissions/<serial>
412
    def get_commission_info(self, token, serial):
413
        """Get Description of a Commission
414

415
        Keyword arguments:
416
        token   -- service's token (string)
417
        serial  -- commission's id (int)
418

419
        In case of success return a dict of dicts containing
420
        informations (details) about the requested commission
421

422
        """
423
        check_input("get_commission_info", self.logger, serial=serial)
424

    
425
        path = "/astakos/api/commissions/" + str(serial)
426
        return self._call_astakos(token, path)
427

    
428
    # ----------------------------------
429
    # POST "/astakos/api/commissions/<serial>/action"
430
    def commission_action(self, token, serial, action):
431
        """Perform a commission action
432

433
        Keyword arguments:
434
        token   -- service's token (string)
435
        serial  -- commission's id (int)
436
        action  -- action to perform, currently accept/reject (string)
437

438
        In case of success return nothing.
439

440
        """
441
        check_input("commission_action", self.logger,
442
                    serial=serial, action=action)
443

    
444
        path = "/astakos/api/commissions/" + str(serial) + "/action"
445
        req_headers = {'content-type': 'application/json'}
446
        req_body = parse_request({str(action): ""}, self.logger)
447
        self._call_astakos(token, path, req_headers, req_body, "POST")
448

    
449
    def accept_commission(self, token, serial):
450
        """Accept a commission (see commission_action)"""
451
        self.commission_action(token, serial, "accept")
452

    
453
    def reject_commission(self, token, serial):
454
        """Reject a commission (see commission_action)"""
455
        self.commission_action(token, serial, "reject")
456

    
457
    # ----------------------------------
458
    # POST "/astakos/api/commissions/action"
459
    def resolve_commissions(self, token, accept_serials, reject_serials):
460
        """Resolve multiple commissions at once
461

462
        Keyword arguments:
463
        token           -- service's token (string)
464
        accept_serials  -- commissions to accept (list of ints)
465
        reject_serials  -- commissions to reject (list of ints)
466

467
        In case of success return a dict of dicts describing which
468
        commissions accepted, which rejected and which failed to
469
        resolved.
470

471
        """
472
        check_input("resolve_commissions", self.logger,
473
                    accept_serials=accept_serials,
474
                    reject_serials=reject_serials)
475

    
476
        path = "/astakos/api/commissions/action"
477
        req_headers = {'content-type': 'application/json'}
478
        req_body = parse_request({"accept": accept_serials,
479
                                  "reject": reject_serials},
480
                                 self.logger)
481
        return self._call_astakos(token, path, req_headers, req_body, "POST")
482

    
483

    
484
# --------------------------------------------------------------------
485
# Private functions
486
# We want _doRequest to be a distinct function
487
# so that we can replace it during unit tests.
488
def _do_request(conn, method, url, **kwargs):
489
    """The actual request. This function can easily be mocked"""
490
    conn.request(method, url, **kwargs)
491
    response = conn.getresponse()
492
    length = response.getheader('content-length', None)
493
    data = response.read(length)
494
    status = int(response.status)
495
    message = response.reason
496
    return (message, data, status)