root / kamaki / clients / astakos / __init__.py @ 9d3cd179
History | View | Annotate | Download (6.1 kB)
1 |
# Copyright 2012-2013 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 logging import getLogger |
35 |
from json import dumps |
36 |
|
37 |
from kamaki.clients import Client, ClientError |
38 |
from kamaki.clients.utils import path4url |
39 |
|
40 |
|
41 |
class AstakosClient(Client): |
42 |
"""Synnefo Astakos API client"""
|
43 |
|
44 |
def __init__(self, base_url, token=None): |
45 |
super(AstakosClient, self).__init__(base_url, token) |
46 |
self._cache = {}
|
47 |
self._uuids = {}
|
48 |
self.log = getLogger('__name__') |
49 |
|
50 |
def authenticate(self, token=None): |
51 |
"""Get authentication information and store it in this client
|
52 |
As long as the AstakosClient instance is alive, the latest
|
53 |
authentication information for this token will be available
|
54 |
|
55 |
:param token: (str) custom token to authenticate
|
56 |
|
57 |
:returns: (dict) authentication information
|
58 |
"""
|
59 |
self.token = token or self.token |
60 |
body = dict(auth=dict(token=dict(id=self.token))) |
61 |
r = self.post('/tokens', json=body).json |
62 |
uuid = r['access']['user']['id'] |
63 |
self._uuids[self.token] = uuid |
64 |
self._cache[uuid] = r
|
65 |
return self._cache[uuid] |
66 |
|
67 |
def get_token(self, uuid): |
68 |
return self._cache[uuid]['access']['token']['id'] |
69 |
|
70 |
def get_services(self, token=None): |
71 |
"""
|
72 |
:returns: (list) [{name:..., type:..., endpoints:[...]}, ...]
|
73 |
"""
|
74 |
token_bu = self.token or token |
75 |
token = token or self.token |
76 |
try:
|
77 |
r = self._cache[self._uuids[token]] |
78 |
except KeyError: |
79 |
r = self.authenticate(token)
|
80 |
finally:
|
81 |
self.token = token_bu
|
82 |
return r['access']['serviceCatalog'] |
83 |
|
84 |
def get_service_details(self, service_type, token=None): |
85 |
"""
|
86 |
:param service_type: (str) compute, object-store, image, account, etc.
|
87 |
|
88 |
:returns: (dict) {name:..., type:..., endpoints:[...]}
|
89 |
|
90 |
:raises ClientError: (600) if service_type not in service catalog
|
91 |
"""
|
92 |
services = self.get_services(token)
|
93 |
for service in services: |
94 |
try:
|
95 |
if service['type'].lower() == service_type.lower(): |
96 |
return service
|
97 |
except KeyError: |
98 |
self.log.warning('Misformated service %s' % service) |
99 |
raise ClientError(
|
100 |
'Service type "%s" not in service catalog' % service_type, 600) |
101 |
|
102 |
def get_service_endpoints(self, service_type, version=None, token=None): |
103 |
"""
|
104 |
:param service_type: (str) can be compute, object-store, etc.
|
105 |
|
106 |
:param version: (str) the version id of the service
|
107 |
|
108 |
:returns: (dict) {SNF:uiURL, adminURL, internalURL, publicURL, ...}
|
109 |
|
110 |
:raises ClientError: (600) if service_type not in service catalog
|
111 |
|
112 |
:raises ClientError: (601) if #matching endpoints != 1
|
113 |
"""
|
114 |
service = self.get_service_details(service_type, token)
|
115 |
matches = [] |
116 |
for endpoint in service['endpoints']: |
117 |
if (not version) or ( |
118 |
endpoint['versionId'].lower() == version.lower()):
|
119 |
matches.append(endpoint) |
120 |
if len(matches) != 1: |
121 |
raise ClientError(
|
122 |
'%s endpoints match type %s %s' % (
|
123 |
len(matches), service_type,
|
124 |
('and versionId %s' % version) if version else ''), |
125 |
601)
|
126 |
return matches[0] |
127 |
|
128 |
def list_users(self): |
129 |
"""list cached users information"""
|
130 |
if not self._cache: |
131 |
self.authenticate()
|
132 |
r = [] |
133 |
for k, v in self._cache.items(): |
134 |
r.append(dict(v['access']['user'])) |
135 |
r[-1].update(dict(auth_token=self.get_token(k))) |
136 |
return r
|
137 |
|
138 |
def user_info(self, token=None): |
139 |
"""Get (cached) user information"""
|
140 |
token_bu = self.token or token |
141 |
token = token or self.token |
142 |
try:
|
143 |
r = self._cache[self._uuids[token]] |
144 |
except KeyError: |
145 |
r = self.authenticate(token)
|
146 |
finally:
|
147 |
self.token = token_bu
|
148 |
return r['access']['user'] |
149 |
|
150 |
def term(self, key, token=None): |
151 |
"""Get (cached) term, from user credentials"""
|
152 |
return self.user_term(key, token) |
153 |
|
154 |
def user_term(self, key, token=None): |
155 |
"""Get (cached) term, from user credentials"""
|
156 |
return self.user_info(token).get(key, None) |
157 |
|
158 |
def post_user_catalogs(self, uuids): |
159 |
"""POST base_url/user_catalogs
|
160 |
|
161 |
:param uuids: (list or tuple) user uuids
|
162 |
|
163 |
:returns: (dict) {uuid1: name1, uuid2: name2, ...}
|
164 |
"""
|
165 |
account_url = self.get_service_endpoints('account')['publicURL'] |
166 |
account = AstakosClient(account_url, self.token)
|
167 |
json_data = dict(uuids=uuids)
|
168 |
return account.post('user_catalogs', json=json_data) |