root / kamaki / clients / astakos / __init__.py @ b44a5a37
History | View | Annotate | Download (5.6 kB)
1 | e3f01d64 | Stavros Sachtouris | # Copyright 2012-2013 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 | b44a5a37 | Stavros Sachtouris | self._uuids = {}
|
45 | 528550d9 | Stavros Sachtouris | self.log = getLogger('__name__') |
46 | 2f749e6e | Stavros Sachtouris | |
47 | 0238c167 | Stavros Sachtouris | def authenticate(self, token=None): |
48 | 409371de | Stavros Sachtouris | """Get authentication information and store it in this client
|
49 | 409371de | Stavros Sachtouris | As long as the AstakosClient instance is alive, the latest
|
50 | 409371de | Stavros Sachtouris | authentication information for this token will be available
|
51 | 409371de | Stavros Sachtouris |
|
52 | 0d79eb23 | Stavros Sachtouris | :param token: (str) custom token to authenticate
|
53 | 0d79eb23 | Stavros Sachtouris |
|
54 | 0d79eb23 | Stavros Sachtouris | :returns: (dict) authentication information
|
55 | 0d79eb23 | Stavros Sachtouris | """
|
56 | 3f61753d | Stavros Sachtouris | self.token = token or self.token |
57 | 8cec3671 | Stavros Sachtouris | body = dict(auth=dict(token=dict(id=self.token))) |
58 | b44a5a37 | Stavros Sachtouris | r = self.post('/tokens', json=body).json |
59 | b44a5a37 | Stavros Sachtouris | uuid = r['access']['user']['id'] |
60 | b44a5a37 | Stavros Sachtouris | self._uuids[self.token] = uuid |
61 | b44a5a37 | Stavros Sachtouris | self._cache[uuid] = r
|
62 | b44a5a37 | Stavros Sachtouris | return self._cache[uuid] |
63 | b44a5a37 | Stavros Sachtouris | |
64 | b44a5a37 | Stavros Sachtouris | def get_token(self, uuid): |
65 | b44a5a37 | Stavros Sachtouris | return self._cache[uuid]['access']['token']['id'] |
66 | 5207c784 | Stavros Sachtouris | |
67 | 528550d9 | Stavros Sachtouris | def get_services(self, token=None): |
68 | 528550d9 | Stavros Sachtouris | """
|
69 | 528550d9 | Stavros Sachtouris | :returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
|
70 | 528550d9 | Stavros Sachtouris | """
|
71 | 528550d9 | Stavros Sachtouris | token_bu = self.token or token |
72 | 528550d9 | Stavros Sachtouris | token = token or self.token |
73 | 528550d9 | Stavros Sachtouris | try:
|
74 | b44a5a37 | Stavros Sachtouris | r = self._cache[self._uuids[token]] |
75 | 528550d9 | Stavros Sachtouris | except KeyError: |
76 | 528550d9 | Stavros Sachtouris | r = self.authenticate(token)
|
77 | 528550d9 | Stavros Sachtouris | finally:
|
78 | 528550d9 | Stavros Sachtouris | self.token = token_bu
|
79 | 3950a864 | Stavros Sachtouris | return r['access']['serviceCatalog'] |
80 | 528550d9 | Stavros Sachtouris | |
81 | 528550d9 | Stavros Sachtouris | def get_service_details(self, service_type, token=None): |
82 | 528550d9 | Stavros Sachtouris | """
|
83 | 528550d9 | Stavros Sachtouris | :param service_type: (str) compute, object-store, image, account, etc.
|
84 | 528550d9 | Stavros Sachtouris |
|
85 | 528550d9 | Stavros Sachtouris | :returns: (dict) {name:..., type:..., endpoints:[...]}
|
86 | 528550d9 | Stavros Sachtouris |
|
87 | 528550d9 | Stavros Sachtouris | :raises ClientError: (600) if service_type not in service catalog
|
88 | 528550d9 | Stavros Sachtouris | """
|
89 | 528550d9 | Stavros Sachtouris | services = self.get_services(token)
|
90 | 528550d9 | Stavros Sachtouris | for service in services: |
91 | 528550d9 | Stavros Sachtouris | try:
|
92 | 528550d9 | Stavros Sachtouris | if service['type'].lower() == service_type.lower(): |
93 | 528550d9 | Stavros Sachtouris | return service
|
94 | 528550d9 | Stavros Sachtouris | except KeyError: |
95 | 528550d9 | Stavros Sachtouris | self.log.warning('Misformated service %s' % service) |
96 | 528550d9 | Stavros Sachtouris | raise ClientError(
|
97 | 528550d9 | Stavros Sachtouris | 'Service type "%s" not in service catalog' % service_type, 600) |
98 | 528550d9 | Stavros Sachtouris | |
99 | 528550d9 | Stavros Sachtouris | def get_service_endpoints(self, service_type, version=None, token=None): |
100 | 528550d9 | Stavros Sachtouris | """
|
101 | 528550d9 | Stavros Sachtouris | :param service_type: (str) can be compute, object-store, etc.
|
102 | 528550d9 | Stavros Sachtouris |
|
103 | 528550d9 | Stavros Sachtouris | :param version: (str) the version id of the service
|
104 | 528550d9 | Stavros Sachtouris |
|
105 | 528550d9 | Stavros Sachtouris | :returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
|
106 | 528550d9 | Stavros Sachtouris |
|
107 | 528550d9 | Stavros Sachtouris | :raises ClientError: (600) if service_type not in service catalog
|
108 | 528550d9 | Stavros Sachtouris |
|
109 | 528550d9 | Stavros Sachtouris | :raises ClientError: (601) if #matching endpoints != 1
|
110 | 528550d9 | Stavros Sachtouris | """
|
111 | 528550d9 | Stavros Sachtouris | service = self.get_service_details(service_type, token)
|
112 | 528550d9 | Stavros Sachtouris | matches = [] |
113 | 528550d9 | Stavros Sachtouris | for endpoint in service['endpoints']: |
114 | 528550d9 | Stavros Sachtouris | if (not version) or ( |
115 | e7884f25 | Stavros Sachtouris | endpoint['versionId'].lower() == version.lower()):
|
116 | 528550d9 | Stavros Sachtouris | matches.append(endpoint) |
117 | 528550d9 | Stavros Sachtouris | if len(matches) != 1: |
118 | 528550d9 | Stavros Sachtouris | raise ClientError(
|
119 | 528550d9 | Stavros Sachtouris | '%s endpoints match type %s %s' % (
|
120 | 528550d9 | Stavros Sachtouris | len(matches), service_type,
|
121 | e7884f25 | Stavros Sachtouris | ('and versionId %s' % version) if version else ''), |
122 | 528550d9 | Stavros Sachtouris | 601)
|
123 | 528550d9 | Stavros Sachtouris | return matches[0] |
124 | 528550d9 | Stavros Sachtouris | |
125 | 528550d9 | Stavros Sachtouris | def list_users(self): |
126 | 528550d9 | Stavros Sachtouris | """list cached users information"""
|
127 | 9a8861d1 | Stavros Sachtouris | if not self._cache: |
128 | 9a8861d1 | Stavros Sachtouris | self.authenticate()
|
129 | 2182231b | Stavros Sachtouris | r = [] |
130 | 2182231b | Stavros Sachtouris | for k, v in self._cache.items(): |
131 | 3950a864 | Stavros Sachtouris | r.append(dict(v['access']['user'])) |
132 | b44a5a37 | Stavros Sachtouris | r[-1].update(dict(auth_token=self.get_token(k))) |
133 | 2182231b | Stavros Sachtouris | return r
|
134 | 76d3b2d7 | Stavros Sachtouris | |
135 | 528550d9 | Stavros Sachtouris | def user_info(self, token=None): |
136 | 2182231b | Stavros Sachtouris | """Get (cached) user information"""
|
137 | 528550d9 | Stavros Sachtouris | token_bu = self.token or token |
138 | 3f61753d | Stavros Sachtouris | token = token or self.token |
139 | 3f61753d | Stavros Sachtouris | try:
|
140 | b44a5a37 | Stavros Sachtouris | r = self._cache[self._uuids[token]] |
141 | 3f61753d | Stavros Sachtouris | except KeyError: |
142 | 409371de | Stavros Sachtouris | r = self.authenticate(token)
|
143 | 528550d9 | Stavros Sachtouris | finally:
|
144 | 528550d9 | Stavros Sachtouris | self.token = token_bu
|
145 | 3950a864 | Stavros Sachtouris | return r['access']['user'] |
146 | 76d3b2d7 | Stavros Sachtouris | |
147 | 5207c784 | Stavros Sachtouris | def term(self, key, token=None): |
148 | 5207c784 | Stavros Sachtouris | """Get (cached) term, from user credentials"""
|
149 | f724cd35 | Stavros Sachtouris | return self.user_term(key, token) |
150 | f724cd35 | Stavros Sachtouris | |
151 | f724cd35 | Stavros Sachtouris | def user_term(self, key, token=None): |
152 | f724cd35 | Stavros Sachtouris | """Get (cached) term, from user credentials"""
|
153 | 528550d9 | Stavros Sachtouris | return self.user_info(token).get(key, None) |