Get endpoint urls for all CLI operations
[kamaki] / kamaki / clients / astakos / __init__.py
1 # Copyright 2012 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 from kamaki.clients import Client, ClientError
35 from logging import getLogger
36
37
38 class AstakosClient(Client):
39     """Synnefo Astakos API client"""
40
41     def __init__(self, base_url, token=None):
42         super(AstakosClient, self).__init__(base_url, token)
43         self._cache = {}
44         self.log = getLogger('__name__')
45
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
50
51         :param token: (str) custom token to authenticate
52
53         :returns: (dict) authentication information
54         """
55         self.token = token or self.token
56         self._cache[self.token] = self.get('/tokens').json
57         return self._cache[self.token]
58
59     def get_services(self, token=None):
60         """
61         :returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
62         """
63         token_bu = self.token or token
64         token = token or self.token
65         try:
66             r = self._cache[token]
67         except KeyError:
68             r = self.authenticate(token)
69         finally:
70             self.token = token_bu
71         return r['serviceCatalog']
72
73     def get_service_details(self, service_type, token=None):
74         """
75         :param service_type: (str) compute, object-store, image, account, etc.
76
77         :returns: (dict) {name:..., type:..., endpoints:[...]}
78
79         :raises ClientError: (600) if service_type not in service catalog
80         """
81         services = self.get_services(token)
82         for service in services:
83             try:
84                 if service['type'].lower() == service_type.lower():
85                     return service
86             except KeyError:
87                 self.log.warning('Misformated service %s' % service)
88         raise ClientError(
89             'Service type "%s" not in service catalog' % service_type, 600)
90
91     def get_service_endpoints(self, service_type, version=None, token=None):
92         """
93         :param service_type: (str) can be compute, object-store, etc.
94
95         :param version: (str) the version id of the service
96
97         :returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
98
99         :raises ClientError: (600) if service_type not in service catalog
100
101         :raises ClientError: (601) if #matching endpoints != 1
102         """
103         service = self.get_service_details(service_type, token)
104         matches = []
105         for endpoint in service['endpoints']:
106
107             if (not version) or (
108                     endpoint['version_id'].lower() == version.lower()):
109                 matches.append(endpoint)
110         if len(matches) != 1:
111             raise ClientError(
112                 '%s endpoints match type %s %s' % (
113                     len(matches), service_type,
114                     ('and version_id %s' % version) if version else ''),
115                 601)
116         return matches[0]
117
118     def list_users(self):
119         """list cached users information"""
120         r = []
121         for k, v in self._cache.items():
122             r.append(dict(v['user']))
123             r[-1].update(dict(auth_token=k))
124         return r
125
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
130         try:
131             r = self._cache[token]
132         except KeyError:
133             r = self.authenticate(token)
134         finally:
135             self.token = token_bu
136         return r['user']
137
138     def term(self, key, token=None):
139         """Get (cached) term, from user credentials"""
140         return self.user_term(key, token)
141
142     def user_term(self, key, token=None):
143         """Get (cached) term, from user credentials"""
144         return self.user_info(token).get(key, None)