root / kamaki / clients / astakos / __init__.py @ 54b6be76
History | View | Annotate | Download (5.3 kB)
1 | 43ca98ee | Giorgos Verigakis | # Copyright 2012 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 | 54b6be76 | Stavros Sachtouris | body = dict(auth=dict(token=dict(id=self.token))) |
57 | 54b6be76 | Stavros Sachtouris | self.set_headers('content-type', 'application/json') |
58 | 54b6be76 | Stavros Sachtouris | self._cache[self.token] = self.post('/tokens', data=body).json |
59 | 409371de | Stavros Sachtouris | return self._cache[self.token] |
60 | 5207c784 | Stavros Sachtouris | |
61 | 528550d9 | Stavros Sachtouris | def get_services(self, token=None): |
62 | 528550d9 | Stavros Sachtouris | """
|
63 | 528550d9 | Stavros Sachtouris | :returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
|
64 | 528550d9 | Stavros Sachtouris | """
|
65 | 528550d9 | Stavros Sachtouris | token_bu = self.token or token |
66 | 528550d9 | Stavros Sachtouris | token = token or self.token |
67 | 528550d9 | Stavros Sachtouris | try:
|
68 | 528550d9 | Stavros Sachtouris | r = self._cache[token]
|
69 | 528550d9 | Stavros Sachtouris | except KeyError: |
70 | 528550d9 | Stavros Sachtouris | r = self.authenticate(token)
|
71 | 528550d9 | Stavros Sachtouris | finally:
|
72 | 528550d9 | Stavros Sachtouris | self.token = token_bu
|
73 | 528550d9 | Stavros Sachtouris | return r['serviceCatalog'] |
74 | 528550d9 | Stavros Sachtouris | |
75 | 528550d9 | Stavros Sachtouris | def get_service_details(self, service_type, token=None): |
76 | 528550d9 | Stavros Sachtouris | """
|
77 | 528550d9 | Stavros Sachtouris | :param service_type: (str) compute, object-store, image, account, etc.
|
78 | 528550d9 | Stavros Sachtouris |
|
79 | 528550d9 | Stavros Sachtouris | :returns: (dict) {name:..., type:..., endpoints:[...]}
|
80 | 528550d9 | Stavros Sachtouris |
|
81 | 528550d9 | Stavros Sachtouris | :raises ClientError: (600) if service_type not in service catalog
|
82 | 528550d9 | Stavros Sachtouris | """
|
83 | 528550d9 | Stavros Sachtouris | services = self.get_services(token)
|
84 | 528550d9 | Stavros Sachtouris | for service in services: |
85 | 528550d9 | Stavros Sachtouris | try:
|
86 | 528550d9 | Stavros Sachtouris | if service['type'].lower() == service_type.lower(): |
87 | 528550d9 | Stavros Sachtouris | return service
|
88 | 528550d9 | Stavros Sachtouris | except KeyError: |
89 | 528550d9 | Stavros Sachtouris | self.log.warning('Misformated service %s' % service) |
90 | 528550d9 | Stavros Sachtouris | raise ClientError(
|
91 | 528550d9 | Stavros Sachtouris | 'Service type "%s" not in service catalog' % service_type, 600) |
92 | 528550d9 | Stavros Sachtouris | |
93 | 528550d9 | Stavros Sachtouris | def get_service_endpoints(self, service_type, version=None, token=None): |
94 | 528550d9 | Stavros Sachtouris | """
|
95 | 528550d9 | Stavros Sachtouris | :param service_type: (str) can be compute, object-store, etc.
|
96 | 528550d9 | Stavros Sachtouris |
|
97 | 528550d9 | Stavros Sachtouris | :param version: (str) the version id of the service
|
98 | 528550d9 | Stavros Sachtouris |
|
99 | 528550d9 | Stavros Sachtouris | :returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
|
100 | 528550d9 | Stavros Sachtouris |
|
101 | 528550d9 | Stavros Sachtouris | :raises ClientError: (600) if service_type not in service catalog
|
102 | 528550d9 | Stavros Sachtouris |
|
103 | 528550d9 | Stavros Sachtouris | :raises ClientError: (601) if #matching endpoints != 1
|
104 | 528550d9 | Stavros Sachtouris | """
|
105 | 528550d9 | Stavros Sachtouris | service = self.get_service_details(service_type, token)
|
106 | 528550d9 | Stavros Sachtouris | matches = [] |
107 | 528550d9 | Stavros Sachtouris | for endpoint in service['endpoints']: |
108 | 528550d9 | Stavros Sachtouris | |
109 | 528550d9 | Stavros Sachtouris | if (not version) or ( |
110 | 528550d9 | Stavros Sachtouris | endpoint['version_id'].lower() == version.lower()):
|
111 | 528550d9 | Stavros Sachtouris | matches.append(endpoint) |
112 | 528550d9 | Stavros Sachtouris | if len(matches) != 1: |
113 | 528550d9 | Stavros Sachtouris | raise ClientError(
|
114 | 528550d9 | Stavros Sachtouris | '%s endpoints match type %s %s' % (
|
115 | 528550d9 | Stavros Sachtouris | len(matches), service_type,
|
116 | 528550d9 | Stavros Sachtouris | ('and version_id %s' % version) if version else ''), |
117 | 528550d9 | Stavros Sachtouris | 601)
|
118 | 528550d9 | Stavros Sachtouris | return matches[0] |
119 | 528550d9 | Stavros Sachtouris | |
120 | 528550d9 | Stavros Sachtouris | def list_users(self): |
121 | 528550d9 | Stavros Sachtouris | """list cached users information"""
|
122 | 2182231b | Stavros Sachtouris | r = [] |
123 | 2182231b | Stavros Sachtouris | for k, v in self._cache.items(): |
124 | 528550d9 | Stavros Sachtouris | r.append(dict(v['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 | 528550d9 | Stavros Sachtouris | return r['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) |