root / kamaki / clients / astakos / __init__.py @ 9a8861d1
History | View | Annotate | Download (5.4 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 | 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 | 3950a864 | Stavros Sachtouris | return r['access']['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 | if (not version) or ( |
108 | e7884f25 | Stavros Sachtouris | endpoint['versionId'].lower() == version.lower()):
|
109 | 528550d9 | Stavros Sachtouris | matches.append(endpoint) |
110 | 528550d9 | Stavros Sachtouris | if len(matches) != 1: |
111 | 528550d9 | Stavros Sachtouris | raise ClientError(
|
112 | 528550d9 | Stavros Sachtouris | '%s endpoints match type %s %s' % (
|
113 | 528550d9 | Stavros Sachtouris | len(matches), service_type,
|
114 | e7884f25 | Stavros Sachtouris | ('and versionId %s' % version) if version else ''), |
115 | 528550d9 | Stavros Sachtouris | 601)
|
116 | 528550d9 | Stavros Sachtouris | return matches[0] |
117 | 528550d9 | Stavros Sachtouris | |
118 | 528550d9 | Stavros Sachtouris | def list_users(self): |
119 | 528550d9 | Stavros Sachtouris | """list cached users information"""
|
120 | 9a8861d1 | Stavros Sachtouris | if not self._cache: |
121 | 9a8861d1 | Stavros Sachtouris | self.authenticate()
|
122 | 2182231b | Stavros Sachtouris | r = [] |
123 | 2182231b | Stavros Sachtouris | for k, v in self._cache.items(): |
124 | 3950a864 | Stavros Sachtouris | r.append(dict(v['access']['user'])) |
125 | 2182231b | Stavros Sachtouris | r[-1].update(dict(auth_token=k)) |
126 | 2182231b | Stavros Sachtouris | return r
|
127 | 76d3b2d7 | Stavros Sachtouris | |
128 | 528550d9 | Stavros Sachtouris | def user_info(self, token=None): |
129 | 2182231b | Stavros Sachtouris | """Get (cached) user information"""
|
130 | 528550d9 | Stavros Sachtouris | token_bu = self.token or token |
131 | 3f61753d | Stavros Sachtouris | token = token or self.token |
132 | 3f61753d | Stavros Sachtouris | try:
|
133 | 409371de | Stavros Sachtouris | r = self._cache[token]
|
134 | 3f61753d | Stavros Sachtouris | except KeyError: |
135 | 409371de | Stavros Sachtouris | r = self.authenticate(token)
|
136 | 528550d9 | Stavros Sachtouris | finally:
|
137 | 528550d9 | Stavros Sachtouris | self.token = token_bu
|
138 | 3950a864 | Stavros Sachtouris | return r['access']['user'] |
139 | 76d3b2d7 | Stavros Sachtouris | |
140 | 5207c784 | Stavros Sachtouris | def term(self, key, token=None): |
141 | 5207c784 | Stavros Sachtouris | """Get (cached) term, from user credentials"""
|
142 | f724cd35 | Stavros Sachtouris | return self.user_term(key, token) |
143 | f724cd35 | Stavros Sachtouris | |
144 | f724cd35 | Stavros Sachtouris | def user_term(self, key, token=None): |
145 | f724cd35 | Stavros Sachtouris | """Get (cached) term, from user credentials"""
|
146 | 528550d9 | Stavros Sachtouris | return self.user_info(token).get(key, None) |