Statistics
| Branch: | Tag: | Revision:

root / kamaki / clients / astakos / __init__.py @ 82e32e50

History | View | Annotate | Download (5.3 kB)

1 43ca98ee Giorgos Verigakis
# Copyright 2012 GRNET S.A. All rights reserved.
2 43ca98ee Giorgos Verigakis
#
3 43ca98ee Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 43ca98ee Giorgos Verigakis
# without modification, are permitted provided that the following
5 43ca98ee Giorgos Verigakis
# conditions are met:
6 43ca98ee Giorgos Verigakis
#
7 43ca98ee Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 43ca98ee Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 43ca98ee Giorgos Verigakis
#      disclaimer.
10 43ca98ee Giorgos Verigakis
#
11 43ca98ee Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 43ca98ee Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 43ca98ee Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 43ca98ee Giorgos Verigakis
#      provided with the distribution.
15 43ca98ee Giorgos Verigakis
#
16 43ca98ee Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 43ca98ee Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 43ca98ee Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 43ca98ee Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 43ca98ee Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 43ca98ee Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 43ca98ee Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 43ca98ee Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 43ca98ee Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 43ca98ee Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 43ca98ee Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 43ca98ee Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 43ca98ee Giorgos Verigakis
#
29 43ca98ee Giorgos Verigakis
# The views and conclusions contained in the software and
30 43ca98ee Giorgos Verigakis
# documentation are those of the authors and should not be
31 43ca98ee Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 43ca98ee Giorgos Verigakis
# or implied, of GRNET S.A.
33 43ca98ee Giorgos Verigakis
34 c270fe96 Stavros Sachtouris
from kamaki.clients import Client, ClientError
35 528550d9 Stavros Sachtouris
from logging import getLogger
36 528550d9 Stavros Sachtouris
37 528550d9 Stavros Sachtouris
38 43ca98ee Giorgos Verigakis
class AstakosClient(Client):
39 76e7661e Stavros Sachtouris
    """Synnefo Astakos API client"""
40 44b8928d Giorgos Verigakis
41 528550d9 Stavros Sachtouris
    def __init__(self, base_url, token=None):
42 33dc6317 Stavros Sachtouris
        super(AstakosClient, self).__init__(base_url, token)
43 9d74de5d Stavros Sachtouris
        self._cache = {}
44 528550d9 Stavros Sachtouris
        self.log = getLogger('__name__')
45 2f749e6e Stavros Sachtouris
46 0238c167 Stavros Sachtouris
    def authenticate(self, token=None):
47 409371de Stavros Sachtouris
        """Get authentication information and store it in this client
48 409371de Stavros Sachtouris
        As long as the AstakosClient instance is alive, the latest
49 409371de Stavros Sachtouris
        authentication information for this token will be available
50 409371de Stavros Sachtouris

51 0d79eb23 Stavros Sachtouris
        :param token: (str) custom token to authenticate
52 0d79eb23 Stavros Sachtouris

53 0d79eb23 Stavros Sachtouris
        :returns: (dict) authentication information
54 0d79eb23 Stavros Sachtouris
        """
55 3f61753d Stavros Sachtouris
        self.token = token or self.token
56 8cec3671 Stavros Sachtouris
        body = dict(auth=dict(token=dict(id=self.token)))
57 82e32e50 Stavros Sachtouris
        self._cache[self.token] = self.post('/tokens', json=body).json
58 409371de Stavros Sachtouris
        return self._cache[self.token]
59 5207c784 Stavros Sachtouris
60 528550d9 Stavros Sachtouris
    def get_services(self, token=None):
61 528550d9 Stavros Sachtouris
        """
62 528550d9 Stavros Sachtouris
        :returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
63 528550d9 Stavros Sachtouris
        """
64 528550d9 Stavros Sachtouris
        token_bu = self.token or token
65 528550d9 Stavros Sachtouris
        token = token or self.token
66 528550d9 Stavros Sachtouris
        try:
67 528550d9 Stavros Sachtouris
            r = self._cache[token]
68 528550d9 Stavros Sachtouris
        except KeyError:
69 528550d9 Stavros Sachtouris
            r = self.authenticate(token)
70 528550d9 Stavros Sachtouris
        finally:
71 528550d9 Stavros Sachtouris
            self.token = token_bu
72 528550d9 Stavros Sachtouris
        return r['serviceCatalog']
73 528550d9 Stavros Sachtouris
74 528550d9 Stavros Sachtouris
    def get_service_details(self, service_type, token=None):
75 528550d9 Stavros Sachtouris
        """
76 528550d9 Stavros Sachtouris
        :param service_type: (str) compute, object-store, image, account, etc.
77 528550d9 Stavros Sachtouris

78 528550d9 Stavros Sachtouris
        :returns: (dict) {name:..., type:..., endpoints:[...]}
79 528550d9 Stavros Sachtouris

80 528550d9 Stavros Sachtouris
        :raises ClientError: (600) if service_type not in service catalog
81 528550d9 Stavros Sachtouris
        """
82 528550d9 Stavros Sachtouris
        services = self.get_services(token)
83 528550d9 Stavros Sachtouris
        for service in services:
84 528550d9 Stavros Sachtouris
            try:
85 528550d9 Stavros Sachtouris
                if service['type'].lower() == service_type.lower():
86 528550d9 Stavros Sachtouris
                    return service
87 528550d9 Stavros Sachtouris
            except KeyError:
88 528550d9 Stavros Sachtouris
                self.log.warning('Misformated service %s' % service)
89 528550d9 Stavros Sachtouris
        raise ClientError(
90 528550d9 Stavros Sachtouris
            'Service type "%s" not in service catalog' % service_type, 600)
91 528550d9 Stavros Sachtouris
92 528550d9 Stavros Sachtouris
    def get_service_endpoints(self, service_type, version=None, token=None):
93 528550d9 Stavros Sachtouris
        """
94 528550d9 Stavros Sachtouris
        :param service_type: (str) can be compute, object-store, etc.
95 528550d9 Stavros Sachtouris

96 528550d9 Stavros Sachtouris
        :param version: (str) the version id of the service
97 528550d9 Stavros Sachtouris

98 528550d9 Stavros Sachtouris
        :returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
99 528550d9 Stavros Sachtouris

100 528550d9 Stavros Sachtouris
        :raises ClientError: (600) if service_type not in service catalog
101 528550d9 Stavros Sachtouris

102 528550d9 Stavros Sachtouris
        :raises ClientError: (601) if #matching endpoints != 1
103 528550d9 Stavros Sachtouris
        """
104 528550d9 Stavros Sachtouris
        service = self.get_service_details(service_type, token)
105 528550d9 Stavros Sachtouris
        matches = []
106 528550d9 Stavros Sachtouris
        for endpoint in service['endpoints']:
107 528550d9 Stavros Sachtouris
108 528550d9 Stavros Sachtouris
            if (not version) or (
109 528550d9 Stavros Sachtouris
                    endpoint['version_id'].lower() == version.lower()):
110 528550d9 Stavros Sachtouris
                matches.append(endpoint)
111 528550d9 Stavros Sachtouris
        if len(matches) != 1:
112 528550d9 Stavros Sachtouris
            raise ClientError(
113 528550d9 Stavros Sachtouris
                '%s endpoints match type %s %s' % (
114 528550d9 Stavros Sachtouris
                    len(matches), service_type,
115 528550d9 Stavros Sachtouris
                    ('and version_id %s' % version) if version else ''),
116 528550d9 Stavros Sachtouris
                601)
117 528550d9 Stavros Sachtouris
        return matches[0]
118 528550d9 Stavros Sachtouris
119 528550d9 Stavros Sachtouris
    def list_users(self):
120 528550d9 Stavros Sachtouris
        """list cached users information"""
121 2182231b Stavros Sachtouris
        r = []
122 2182231b Stavros Sachtouris
        for k, v in self._cache.items():
123 528550d9 Stavros Sachtouris
            r.append(dict(v['user']))
124 2182231b Stavros Sachtouris
            r[-1].update(dict(auth_token=k))
125 2182231b Stavros Sachtouris
        return r
126 76d3b2d7 Stavros Sachtouris
127 528550d9 Stavros Sachtouris
    def user_info(self, token=None):
128 2182231b Stavros Sachtouris
        """Get (cached) user information"""
129 528550d9 Stavros Sachtouris
        token_bu = self.token or token
130 3f61753d Stavros Sachtouris
        token = token or self.token
131 3f61753d Stavros Sachtouris
        try:
132 409371de Stavros Sachtouris
            r = self._cache[token]
133 3f61753d Stavros Sachtouris
        except KeyError:
134 409371de Stavros Sachtouris
            r = self.authenticate(token)
135 528550d9 Stavros Sachtouris
        finally:
136 528550d9 Stavros Sachtouris
            self.token = token_bu
137 528550d9 Stavros Sachtouris
        return r['user']
138 76d3b2d7 Stavros Sachtouris
139 5207c784 Stavros Sachtouris
    def term(self, key, token=None):
140 5207c784 Stavros Sachtouris
        """Get (cached) term, from user credentials"""
141 f724cd35 Stavros Sachtouris
        return self.user_term(key, token)
142 f724cd35 Stavros Sachtouris
143 f724cd35 Stavros Sachtouris
    def user_term(self, key, token=None):
144 f724cd35 Stavros Sachtouris
        """Get (cached) term, from user credentials"""
145 528550d9 Stavros Sachtouris
        return self.user_info(token).get(key, None)