1 # Copyright 2012 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 kamaki.clients import Client, ClientError
35 from logging import getLogger
38 class AstakosClient(Client):
39 """Synnefo Astakos API client"""
41 def __init__(self, base_url, token=None):
42 super(AstakosClient, self).__init__(base_url, token)
44 self.log = getLogger('__name__')
46 def authenticate(self, token=None):
47 """Get authentication information and store it in this client
48 As long as the AstakosClient instance is alive, the latest
49 authentication information for this token will be available
51 :param token: (str) custom token to authenticate
53 :returns: (dict) authentication information
55 self.token = token or self.token
56 self._cache[self.token] = self.get('/tokens').json
57 return self._cache[self.token]
59 def get_services(self, token=None):
61 :returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
63 token_bu = self.token or token
64 token = token or self.token
66 r = self._cache[token]
68 r = self.authenticate(token)
71 return r['serviceCatalog']
73 def get_service_details(self, service_type, token=None):
75 :param service_type: (str) compute, object-store, image, account, etc.
77 :returns: (dict) {name:..., type:..., endpoints:[...]}
79 :raises ClientError: (600) if service_type not in service catalog
81 services = self.get_services(token)
82 for service in services:
84 if service['type'].lower() == service_type.lower():
87 self.log.warning('Misformated service %s' % service)
89 'Service type "%s" not in service catalog' % service_type, 600)
91 def get_service_endpoints(self, service_type, version=None, token=None):
93 :param service_type: (str) can be compute, object-store, etc.
95 :param version: (str) the version id of the service
97 :returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
99 :raises ClientError: (600) if service_type not in service catalog
101 :raises ClientError: (601) if #matching endpoints != 1
103 service = self.get_service_details(service_type, token)
105 for endpoint in service['endpoints']:
107 if (not version) or (
108 endpoint['version_id'].lower() == version.lower()):
109 matches.append(endpoint)
110 if len(matches) != 1:
112 '%s endpoints match type %s %s' % (
113 len(matches), service_type,
114 ('and version_id %s' % version) if version else ''),
118 def list_users(self):
119 """list cached users information"""
121 for k, v in self._cache.items():
122 r.append(dict(v['user']))
123 r[-1].update(dict(auth_token=k))
126 def user_info(self, token=None):
127 """Get (cached) user information"""
128 token_bu = self.token or token
129 token = token or self.token
131 r = self._cache[token]
133 r = self.authenticate(token)
135 self.token = token_bu
138 def term(self, key, token=None):
139 """Get (cached) term, from user credentials"""
140 return self.user_term(key, token)
142 def user_term(self, key, token=None):
143 """Get (cached) term, from user credentials"""
144 return self.user_info(token).get(key, None)