1 # Copyright 2012-2013 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
34 from logging import getLogger
35 from astakosclient import AstakosClient as SynnefoAstakosClient
37 from kamaki.clients import Client, ClientError
40 class AstakosClient(Client):
41 """Synnefo Astakos cached client wraper"""
43 def __init__(self, base_url, token=None):
44 super(AstakosClient, self).__init__(base_url, token)
45 self._astakos = dict()
48 self._uuids2usernames = dict()
49 self._usernames2uuids = dict()
51 def _resolve_token(self, token):
53 :returns: (str) a single token
55 :raises AssertionError: if no token exists (either param or member)
57 token = token or self.token or self.tokenlist[0]
58 assert token, 'No token provided'
60 isinstance(token, list) or isinstance(token, tuple)) else token
62 def authenticate(self, token=None):
63 """Get authentication information and store it in this client
64 As long as the AstakosClient instance is alive, the latest
65 authentication information for this token will be available
67 :param token: (str) custom token to authenticate
69 token = self._resolve_token(token)
70 astakos = SynnefoAstakosClient(
71 token, self.base_url, logger=getLogger('_my_.astakosclient'))
72 r = astakos.get_endpoints()
73 uuid = r['access']['user']['id']
74 self._uuids[token] = uuid
76 self._astakos[uuid] = astakos
77 self._uuids2usernames[token] = dict()
78 self._usernames2uuids[token] = dict()
80 def get_token(self, uuid):
81 return self._cache[uuid]['access']['token']['id']
83 def _validate_token(self, token):
84 if (token not in self._uuids) or (
85 self.get_token(self._uuids[token]) != token):
86 self._uuids.pop(token, None)
87 self.authenticate(token)
89 def get_services(self, token=None):
91 :returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
93 token = self._resolve_token(token)
94 self._validate_token(token)
95 r = self._cache[self._uuids[token]]
96 return r['access']['serviceCatalog']
98 def get_service_details(self, service_type, token=None):
100 :param service_type: (str) compute, object-store, image, account, etc.
102 :returns: (dict) {name:..., type:..., endpoints:[...]}
104 :raises ClientError: (600) if service_type not in service catalog
106 services = self.get_services(token)
107 for service in services:
109 if service['type'].lower() == service_type.lower():
112 self.log.warning('Misformated service %s' % service)
114 'Service type "%s" not in service catalog' % service_type, 600)
116 def get_service_endpoints(self, service_type, version=None, token=None):
118 :param service_type: (str) can be compute, object-store, etc.
120 :param version: (str) the version id of the service
122 :returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
124 :raises ClientError: (600) if service_type not in service catalog
126 :raises ClientError: (601) if #matching endpoints != 1
128 service = self.get_service_details(service_type, token)
130 for endpoint in service['endpoints']:
131 if (not version) or (
132 endpoint['versionId'].lower() == version.lower()):
133 matches.append(endpoint)
134 if len(matches) != 1:
136 '%s endpoints match type %s %s' % (
137 len(matches), service_type,
138 ('and versionId %s' % version) if version else ''),
142 def list_users(self):
143 """list cached users information"""
147 for k, v in self._cache.items():
148 r.append(dict(v['access']['user']))
149 r[-1].update(dict(auth_token=self.get_token(k)))
152 def user_info(self, token=None):
153 """Get (cached) user information"""
154 token = self._resolve_token(token)
155 self._validate_token(token)
156 r = self._cache[self._uuids[token]]
157 return r['access']['user']
159 def term(self, key, token=None):
160 """Get (cached) term, from user credentials"""
161 return self.user_term(key, token)
163 def user_term(self, key, token=None):
164 """Get (cached) term, from user credentials"""
165 return self.user_info(token).get(key, None)
167 def post_user_catalogs(self, uuids=None, displaynames=None, token=None):
168 """POST base_url/user_catalogs
170 :param uuids: (list or tuple) user uuids
172 :param displaynames: (list or tuple) usernames (mut. excl. to uuids)
174 :returns: (dict) {uuid1: name1, uuid2: name2, ...} or oposite
176 return self.uuids2usernames(uuids, token) if (
177 uuids) else self.usernnames2uuids(displaynames, token)
179 def uuids2usernames(self, uuids, token=None):
180 token = self._resolve_token(token)
181 self._validate_token(token)
182 astakos = self._astakos[self._uuids[token]]
183 if set(uuids).difference(self._uuids2usernames[token]):
184 self._uuids2usernames[token].update(astakos.get_usernames(uuids))
185 return self._uuids2usernames[token]
187 def usernames2uuids(self, usernames, token=None):
188 token = self._resolve_token(token)
189 self._validate_token(token)
190 astakos = self._astakos[self._uuids[token]]
191 if set(usernames).difference(self._usernames2uuids[token]):
192 self._usernames2uuids[token].update(astakos.get_uuids(usernames))
193 return self._usernames2uuids