Statistics
| Branch: | Tag: | Revision:

root / astakosclient / astakosclient / __init__.py @ 92683993

History | View | Annotate | Download (20.3 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 API urls
50
API_AUTHENTICATE = "/astakos/api/authenticate"
51
API_USERCATALOGS = "/astakos/api/user_catalogs"
52
API_SERVICE_USERCATALOGS = "/astakos/api/service/user_catalogs"
53
API_GETSERVICES = "/astakos/api/get_services"
54
API_RESOURCES = "/astakos/api/resources"
55
API_QUOTAS = "/astakos/api/quotas"
56
API_SERVICE_QUOTAS = "/astakos/api/service_quotas"
57
API_COMMISSIONS = "/astakos/api/commissions"
58
API_COMMISSIONS_ACTION = API_COMMISSIONS + "/action"
59
API_FEEDBACK = "/astakos/api/feedback"
60

    
61

    
62
# --------------------------------------------------------------------
63
# Astakos Client Class
64

    
65
def get_token_from_cookie(request, cookie_name):
66
    """Extract token from the cookie name provided
67

68
    Cookie should be in the same form as astakos
69
    service sets its cookie contents:
70
        <user_uniq>|<user_token>
71

72
    """
73
    try:
74
        cookie_content = urllib.unquote(request.COOKIE.get(cookie_name, None))
75
        return cookie_content.split("|")[1]
76
    except:
77
        return None
78

    
79

    
80
class AstakosClient():
81
    """AstakosClient Class Implementation"""
82

    
83
    # ----------------------------------
84
    def __init__(self, astakos_url, retry=0,
85
                 use_pool=False, pool_size=8, logger=None):
86
        """Initialize AstakosClient Class
87

88
        Keyword arguments:
89
        astakos_url -- i.e https://accounts.example.com (string)
90
        use_pool    -- use objpool for http requests (boolean)
91
        retry       -- how many time to retry (integer)
92
        logger      -- pass a different logger
93

94
        """
95
        if logger is None:
96
            logging.basicConfig(
97
                format='%(asctime)s [%(levelname)s] %(name)s %(message)s',
98
                datefmt='%Y-%m-%d %H:%M:%S',
99
                level=logging.INFO)
100
            logger = logging.getLogger("astakosclient")
101
        logger.debug("Intialize AstakosClient: astakos_url = %s, "
102
                     "use_pool = %s" % (astakos_url, use_pool))
103

    
104
        check_input("__init__", logger, astakos_url=astakos_url)
105

    
106
        # Check for supported scheme
107
        p = urlparse.urlparse(astakos_url)
108
        conn_class = scheme_to_class(p.scheme, use_pool, pool_size)
109
        if conn_class is None:
110
            m = "Unsupported scheme: %s" % p.scheme
111
            logger.error(m)
112
            raise BadValue(m)
113

    
114
        # Save astakos_url etc. in our class
115
        self.retry = retry
116
        self.logger = logger
117
        self.netloc = p.netloc
118
        self.scheme = p.scheme
119
        self.path = p.path.rstrip('/')
120
        self.conn_class = conn_class
121

    
122
    # ----------------------------------
123
    @retry
124
    def _call_astakos(self, token, request_path,
125
                      headers=None, body=None, method="GET"):
126
        """Make the actual call to Astakos Service"""
127
        if token is not None:
128
            hashed_token = hashlib.sha1()
129
            hashed_token.update(token)
130
            using_token = "using token %s" % (hashed_token.hexdigest())
131
        else:
132
            using_token = "without using token"
133
        self.logger.debug(
134
            "Make a %s request to %s %s with headers %s and body %s"
135
            % (method, request_path, using_token, headers, body))
136

    
137
        # Check Input
138
        if headers is None:
139
            headers = {}
140
        if body is None:
141
            body = {}
142
        path = self.path + "/" + request_path.strip('/')
143

    
144
        # Build request's header and body
145
        kwargs = {}
146
        kwargs['headers'] = copy(headers)
147
        if token is not None:
148
            kwargs['headers']['X-Auth-Token'] = token
149
        if body:
150
            kwargs['body'] = copy(body)
151
            kwargs['headers'].setdefault(
152
                'content-type', 'application/octet-stream')
153
        kwargs['headers'].setdefault('content-length',
154
                                     len(body) if body else 0)
155

    
156
        try:
157
            # Get the connection object
158
            with self.conn_class(self.netloc) as conn:
159
                # Send request
160
                (message, data, status) = \
161
                    _do_request(conn, method, path, **kwargs)
162
        except Exception as err:
163
            self.logger.error("Failed to send request: %s" % repr(err))
164
            raise AstakosClientException(str(err))
165

    
166
        # Return
167
        self.logger.debug("Request returned with status %s" % status)
168
        if status == 400:
169
            raise BadRequest(message, data)
170
        elif status == 401:
171
            raise Unauthorized(message, data)
172
        elif status == 403:
173
            raise Forbidden(message, data)
174
        elif status == 404:
175
            raise NotFound(message, data)
176
        elif status < 200 or status >= 300:
177
            raise AstakosClientException(message, data, status)
178

    
179
        try:
180
            if data:
181
                return simplejson.loads(unicode(data))
182
            else:
183
                return None
184
        except Exception as err:
185
            self.logger.error("Cannot parse response \"%s\" with simplejson: %s"
186
                              % (data, str(err)))
187
            raise InvalidResponse(str(err), data)
188

    
189
    # ------------------------
190
    # do a GET to ``API_AUTHENTICATE``
191
    def get_user_info(self, token, usage=False):
192
        """Authenticate user and get user's info as a dictionary
193

194
        Keyword arguments:
195
        token   -- user's token (string)
196
        usage   -- return usage information for user (boolean)
197

198
        In case of success return user information (json parsed format).
199
        Otherwise raise an AstakosClientException.
200

201
        """
202
        # Send request
203
        auth_path = copy(API_AUTHENTICATE)
204
        if usage:
205
            auth_path += "?usage=1"
206
        return self._call_astakos(token, auth_path)
207

    
208
    # ----------------------------------
209
    # do a POST to ``API_USERCATALOGS`` (or ``API_SERVICE_USERCATALOGS``)
210
    #   with {'uuids': uuids}
211
    def _uuid_catalog(self, token, uuids, req_path):
212
        req_headers = {'content-type': 'application/json'}
213
        req_body = parse_request({'uuids': uuids}, self.logger)
214
        data = self._call_astakos(
215
            token, req_path, req_headers, req_body, "POST")
216
        if "uuid_catalog" in data:
217
            return data.get("uuid_catalog")
218
        else:
219
            m = "_uuid_catalog request returned %s. No uuid_catalog found" \
220
                % data
221
            self.logger.error(m)
222
            raise AstakosClientException(m)
223

    
224
    def get_usernames(self, token, uuids):
225
        """Return a uuid_catalog dictionary for the given uuids
226

227
        Keyword arguments:
228
        token   -- user's token (string)
229
        uuids   -- list of user ids (list of strings)
230

231
        The returned uuid_catalog is a dictionary with uuids as
232
        keys and the corresponding user names as values
233

234
        """
235
        req_path = copy(API_USERCATALOGS)
236
        return self._uuid_catalog(token, uuids, req_path)
237

    
238
    def get_username(self, token, uuid):
239
        """Return the user name of a uuid (see get_usernames)"""
240
        check_input("get_username", self.logger, uuid=uuid)
241
        uuid_dict = self.get_usernames(token, [uuid])
242
        if uuid in uuid_dict:
243
            return uuid_dict.get(uuid)
244
        else:
245
            raise NoUserName(uuid)
246

    
247
    def service_get_usernames(self, token, uuids):
248
        """Return a uuid_catalog dict using a service's token"""
249
        req_path = copy(API_SERVICE_USERCATALOGS)
250
        return self._uuid_catalog(token, uuids, req_path)
251

    
252
    def service_get_username(self, token, uuid):
253
        """Return the displayName of a uuid using a service's token"""
254
        check_input("service_get_username", self.logger, uuid=uuid)
255
        uuid_dict = self.service_get_usernames(token, [uuid])
256
        if uuid in uuid_dict:
257
            return uuid_dict.get(uuid)
258
        else:
259
            raise NoUserName(uuid)
260

    
261
    # ----------------------------------
262
    # do a POST to ``API_USERCATALOGS`` (or ``API_SERVICE_USERCATALOGS``)
263
    #   with {'displaynames': display_names}
264
    def _displayname_catalog(self, token, display_names, req_path):
265
        req_headers = {'content-type': 'application/json'}
266
        req_body = parse_request({'displaynames': display_names}, self.logger)
267
        data = self._call_astakos(
268
            token, req_path, req_headers, req_body, "POST")
269
        if "displayname_catalog" in data:
270
            return data.get("displayname_catalog")
271
        else:
272
            m = "_displayname_catalog request returned %s. " \
273
                "No displayname_catalog found" % data
274
            self.logger.error(m)
275
            raise AstakosClientException(m)
276

    
277
    def get_uuids(self, token, display_names):
278
        """Return a displayname_catalog for the given names
279

280
        Keyword arguments:
281
        token           -- user's token (string)
282
        display_names   -- list of user names (list of strings)
283

284
        The returned displayname_catalog is a dictionary with
285
        the names as keys and the corresponding uuids as values
286

287
        """
288
        req_path = copy(API_USERCATALOGS)
289
        return self._displayname_catalog(token, display_names, req_path)
290

    
291
    def get_uuid(self, token, display_name):
292
        """Return the uuid of a name (see getUUIDs)"""
293
        check_input("get_uuid", self.logger, display_name=display_name)
294
        name_dict = self.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
    def service_get_uuids(self, token, display_names):
301
        """Return a display_name catalog using a service's token"""
302
        req_path = copy(API_SERVICE_USERCATALOGS)
303
        return self._displayname_catalog(token, display_names, req_path)
304

    
305
    def service_get_uuid(self, token, display_name):
306
        """Return the uuid of a name using a service's token"""
307
        check_input("service_get_uuid", self.logger, display_name=display_name)
308
        name_dict = self.service_get_uuids(token, [display_name])
309
        if display_name in name_dict:
310
            return name_dict.get(display_name)
311
        else:
312
            raise NoUUID(display_name)
313

    
314
    # ----------------------------------
315
    # do a GET to ``API_GETSERVICES``
316
    def get_services(self):
317
        """Return a list of dicts with the registered services"""
318
        return self._call_astakos(None, copy(API_GETSERVICES))
319

    
320
    # ----------------------------------
321
    # do a GET to ``API_RESOURCES``
322
    def get_resources(self):
323
        """Return a dict of dicts with the available resources"""
324
        return self._call_astakos(None, copy(API_RESOURCES))
325

    
326
    # ----------------------------------
327
    # do a POST to ``API_FEEDBACK``
328
    def send_feedback(self, token, message, data):
329
        """Send feedback to astakos service
330

331
        keyword arguments:
332
        token       -- user's token (string)
333
        message     -- Feedback message
334
        data        -- Additional information about service client status
335

336
        In case of success return nothing.
337
        Otherwise raise an AstakosClientException
338

339
        """
340
        check_input("send_feedback", self.logger, message=message, data=data)
341
        path = copy(API_FEEDBACK)
342
        req_headers = {'content-type': 'application/json'}
343
        req_body = urllib.urlencode(
344
            {'feedback_msg': message, 'feedback_data': data})
345
        self._call_astakos(token, path, req_headers, req_body, "POST")
346

    
347
    # ----------------------------------
348
    # do a GET to ``API_QUOTAS``
349
    def get_quotas(self, token):
350
        """Get user's quotas
351

352
        Keyword arguments:
353
        token   -- user's token (string)
354

355
        In case of success return a dict of dicts with user's current quotas.
356
        Otherwise raise an AstakosClientException
357

358
        """
359
        return self._call_astakos(token, copy(API_QUOTAS))
360

    
361
    # ----------------------------------
362
    # do a GET to ``API_SERVICE_QUOTAS``
363
    def service_get_quotas(self, token, user=None):
364
        """Get all quotas for resources associated with the service
365

366
        Keyword arguments:
367
        token   -- service's token (string)
368
        user    -- optionally, the uuid of a specific user
369

370
        In case of success return a dict of dicts of dicts with current quotas
371
        for all users, or of a specified user, if user argument is set.
372
        Otherwise raise an AstakosClientException
373

374
        """
375
        query = copy(API_SERVICE_QUOTAS)
376
        if user is not None:
377
            query += "?user=" + user
378
        return self._call_astakos(token, query)
379

    
380
    # ----------------------------------
381
    # do a POST to ``API_COMMISSIONS``
382
    def issue_commission(self, token, request):
383
        """Issue a commission
384

385
        Keyword arguments:
386
        token   -- service's token (string)
387
        request -- commision request (dict)
388

389
        In case of success return commission's id (int).
390
        Otherwise raise an AstakosClientException.
391

392
        """
393
        req_headers = {'content-type': 'application/json'}
394
        req_body = parse_request(request, self.logger)
395
        try:
396
            response = self._call_astakos(token, copy(API_COMMISSIONS),
397
                                          req_headers, req_body, "POST")
398
        except AstakosClientException as err:
399
            if err.status == 413:
400
                raise QuotaLimit(err.message, err.details)
401
            else:
402
                raise
403

    
404
        if "serial" in response:
405
            return response['serial']
406
        else:
407
            m = "issue_commission_core request returned %s. No serial found" \
408
                % response
409
            self.logger.error(m)
410
            raise AstakosClientException(m)
411

    
412
    def issue_one_commission(self, token, holder, source, provisions,
413
                             name="", force=False, auto_accept=False):
414
        """Issue one commission (with specific holder and source)
415

416
        keyword arguments:
417
        token       -- service's token (string)
418
        holder      -- user's id (string)
419
        source      -- commission's source (ex system) (string)
420
        provisions  -- resources with their quantity (dict from string to int)
421
        name        -- description of the commission (string)
422
        force       -- force this commission (boolean)
423
        auto_accept -- auto accept this commission (boolean)
424

425
        In case of success return commission's id (int).
426
        Otherwise raise an AstakosClientException.
427
        (See also issue_commission)
428

429
        """
430
        check_input("issue_one_commission", self.logger,
431
                    holder=holder, source=source,
432
                    provisions=provisions)
433

    
434
        request = {}
435
        request["force"] = force
436
        request["auto_accept"] = auto_accept
437
        request["name"] = name
438
        try:
439
            request["provisions"] = []
440
            for resource, quantity in provisions.iteritems():
441
                t = {"holder": holder, "source": source,
442
                     "resource": resource, "quantity": quantity}
443
                request["provisions"].append(t)
444
        except Exception as err:
445
            self.logger.error(str(err))
446
            raise BadValue(str(err))
447

    
448
        return self.issue_commission(token, request)
449

    
450
    # ----------------------------------
451
    # do a GET to ``API_COMMISSIONS``
452
    def get_pending_commissions(self, token):
453
        """Get Pending Commissions
454

455
        Keyword arguments:
456
        token   -- service's token (string)
457

458
        In case of success return a list of pending commissions' ids
459
        (list of integers)
460

461
        """
462
        return self._call_astakos(token, copy(API_COMMISSIONS))
463

    
464
    # ----------------------------------
465
    # do a GET to ``API_COMMISSIONS``/<serial>
466
    def get_commission_info(self, token, serial):
467
        """Get Description of a Commission
468

469
        Keyword arguments:
470
        token   -- service's token (string)
471
        serial  -- commission's id (int)
472

473
        In case of success return a dict of dicts containing
474
        informations (details) about the requested commission
475

476
        """
477
        check_input("get_commission_info", self.logger, serial=serial)
478

    
479
        path = API_COMMISSIONS + "/" + str(serial)
480
        return self._call_astakos(token, path)
481

    
482
    # ----------------------------------
483
    # do a POST to ``API_COMMISSIONS``/<serial>/action"
484
    def commission_action(self, token, serial, action):
485
        """Perform a commission action
486

487
        Keyword arguments:
488
        token   -- service's token (string)
489
        serial  -- commission's id (int)
490
        action  -- action to perform, currently accept/reject (string)
491

492
        In case of success return nothing.
493

494
        """
495
        check_input("commission_action", self.logger,
496
                    serial=serial, action=action)
497

    
498
        path = API_COMMISSIONS + "/" + str(serial) + "/action"
499
        req_headers = {'content-type': 'application/json'}
500
        req_body = parse_request({str(action): ""}, self.logger)
501
        self._call_astakos(token, path, req_headers, req_body, "POST")
502

    
503
    def accept_commission(self, token, serial):
504
        """Accept a commission (see commission_action)"""
505
        self.commission_action(token, serial, "accept")
506

    
507
    def reject_commission(self, token, serial):
508
        """Reject a commission (see commission_action)"""
509
        self.commission_action(token, serial, "reject")
510

    
511
    # ----------------------------------
512
    # do a POST to ``API_COMMISSIONS_ACTION``
513
    def resolve_commissions(self, token, accept_serials, reject_serials):
514
        """Resolve multiple commissions at once
515

516
        Keyword arguments:
517
        token           -- service's token (string)
518
        accept_serials  -- commissions to accept (list of ints)
519
        reject_serials  -- commissions to reject (list of ints)
520

521
        In case of success return a dict of dicts describing which
522
        commissions accepted, which rejected and which failed to
523
        resolved.
524

525
        """
526
        check_input("resolve_commissions", self.logger,
527
                    accept_serials=accept_serials,
528
                    reject_serials=reject_serials)
529

    
530
        path = copy(API_COMMISSIONS_ACTION)
531
        req_headers = {'content-type': 'application/json'}
532
        req_body = parse_request({"accept": accept_serials,
533
                                  "reject": reject_serials},
534
                                 self.logger)
535
        return self._call_astakos(token, path, req_headers, req_body, "POST")
536

    
537

    
538
# --------------------------------------------------------------------
539
# Private functions
540
# We want _doRequest to be a distinct function
541
# so that we can replace it during unit tests.
542
def _do_request(conn, method, url, **kwargs):
543
    """The actual request. This function can easily be mocked"""
544
    conn.request(method, url, **kwargs)
545
    response = conn.getresponse()
546
    length = response.getheader('content-length', None)
547
    data = response.read(length)
548
    status = int(response.status)
549
    message = response.reason
550
    return (message, data, status)