Revision 2377e7c2
b/snf-astakos-client/astakosclient/__init__.py | ||
---|---|---|
40 | 40 |
import simplejson |
41 | 41 |
from astakosclient.utils import retry, scheme_to_class |
42 | 42 |
from astakosclient.errors import \ |
43 |
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden |
|
43 |
AstakosClientException, Unauthorized, BadRequest, NotFound, Forbidden, \ |
|
44 |
NoDisplayName, NoUUID |
|
44 | 45 |
|
45 | 46 |
|
46 | 47 |
# -------------------------------------------------------------------- |
... | ... | |
190 | 191 |
req_body = simplejson.dumps({'uuids': uuids}) |
191 | 192 |
data = self._callAstakos( |
192 | 193 |
token, req_path, req_headers, req_body, "POST") |
193 |
# XXX: check if exists |
|
194 |
return data.get("uuid_catalog") |
|
194 |
if "uuid_catalog" in data: |
|
195 |
return data.get("uuid_catalog") |
|
196 |
else: |
|
197 |
m = "_uuidCatalog request returned %s. No uuid_catalog found" \ |
|
198 |
% data |
|
199 |
self.logger.error(m) |
|
200 |
raise AstakosClientException(m) |
|
195 | 201 |
|
196 | 202 |
def getDisplayNames(self, token, uuids): |
197 | 203 |
"""Return a uuid_catalog dictionary for the given uuids |
... | ... | |
214 | 220 |
self.logger.error(m) |
215 | 221 |
raise ValueError(m) |
216 | 222 |
uuid_dict = self.getDisplayNames(token, [uuid]) |
217 |
# XXX: check if exists |
|
218 |
return uuid_dict.get(uuid) |
|
223 |
if uuid in uuid_dict: |
|
224 |
return uuid_dict.get(uuid) |
|
225 |
else: |
|
226 |
raise NoDisplayName(uuid) |
|
219 | 227 |
|
220 | 228 |
def getServiceDisplayNames(self, token, uuids): |
221 | 229 |
"""Return a uuid_catalog dict using a service's token""" |
... | ... | |
229 | 237 |
self.logger.error(m) |
230 | 238 |
raise ValueError(m) |
231 | 239 |
uuid_dict = self.getServiceDisplayNames(token, [uuid]) |
232 |
# XXX: check if exists |
|
233 |
return uuid_dict.get(uuid) |
|
240 |
if uuid in uuid_dict: |
|
241 |
return uuid_dict.get(uuid) |
|
242 |
else: |
|
243 |
raise NoDisplayName(uuid) |
|
234 | 244 |
|
235 | 245 |
# ---------------------------------- |
236 | 246 |
def _displayNameCatalog(self, token, display_names, req_path): |
... | ... | |
238 | 248 |
req_body = simplejson.dumps({'displaynames': display_names}) |
239 | 249 |
data = self._callAstakos( |
240 | 250 |
token, req_path, req_headers, req_body, "POST") |
241 |
# XXX: check if exists |
|
242 |
return data.get("displayname_catalog") |
|
251 |
if "displayname_catalog" in data: |
|
252 |
return data.get("displayname_catalog") |
|
253 |
else: |
|
254 |
m = "_displayNameCatalog request returned %s. " \ |
|
255 |
"No displayname_catalog found" % data |
|
256 |
self.logger.error(m) |
|
257 |
raise AstakosClientException(m) |
|
243 | 258 |
|
244 | 259 |
def getUUIDs(self, token, display_names): |
245 | 260 |
"""Return a displayname_catalog for the given names |
... | ... | |
262 | 277 |
self.logger.error(m) |
263 | 278 |
raise ValueError(m) |
264 | 279 |
name_dict = self.getUUIDs(token, [display_name]) |
265 |
# XXX: check if exists |
|
266 |
return name_dict.get(display_name) |
|
280 |
if display_name in name_dict: |
|
281 |
return name_dict.get(display_name) |
|
282 |
else: |
|
283 |
raise NoUUID(display_name) |
|
267 | 284 |
|
268 | 285 |
def getServiceUUIDs(self, token, display_names): |
269 | 286 |
"""Return a display_name catalog using a service's token""" |
... | ... | |
277 | 294 |
self.logger.error(m) |
278 | 295 |
raise ValueError(m) |
279 | 296 |
name_dict = self.getServiceUUIDs(token, [display_name]) |
280 |
# XXX: check if exists |
|
281 |
return name_dict.get(display_name) |
|
297 |
if display_name in name_dict: |
|
298 |
return name_dict.get(display_name) |
|
299 |
else: |
|
300 |
raise NoUUID(display_name) |
|
282 | 301 |
|
283 | 302 |
# ---------------------------------- |
284 | 303 |
def getServices(self): |
b/snf-astakos-client/astakosclient/errors.py | ||
---|---|---|
63 | 63 |
def __init__(self, message): |
64 | 64 |
"""404 Not Found""" |
65 | 65 |
super(NotFound, self).__init__(message, 404) |
66 |
|
|
67 |
|
|
68 |
class NoDisplayName(AstakosClientException): |
|
69 |
def __init__(self, uuid): |
|
70 |
"""No display name for the given uuid""" |
|
71 |
message = "No display name for the given uuid: %s" % uuid |
|
72 |
super(NoDisplayName, self).__init__(message) |
|
73 |
|
|
74 |
|
|
75 |
class NoUUID(AstakosClientException): |
|
76 |
def __init__(self, display_name): |
|
77 |
"""No uuid for the given display name""" |
|
78 |
message = "No uuid for the given display name: %s" % display_name |
|
79 |
super(NoUUID, self).__init__(message) |
b/snf-astakos-client/astakosclient/tests.py | ||
---|---|---|
47 | 47 |
import astakosclient |
48 | 48 |
from astakosclient import AstakosClient |
49 | 49 |
from astakosclient.errors import \ |
50 |
AstakosClientException, Unauthorized, BadRequest, NotFound |
|
50 |
AstakosClientException, Unauthorized, BadRequest, NotFound, \ |
|
51 |
NoDisplayName, NoUUID |
|
51 | 52 |
|
52 | 53 |
# Use backported unittest functionality if Python < 2.7 |
53 | 54 |
try: |
... | ... | |
575 | 576 |
self.fail("Shouldn't raise an Exception") |
576 | 577 |
self.assertEqual(info, user_1['username']) |
577 | 578 |
|
579 |
# ---------------------------------- |
|
580 |
# Get info with wrong uuid |
|
581 |
def test_NoDisplayName(self): |
|
582 |
global token_1 |
|
583 |
_mockRequest([_requestOk]) |
|
584 |
try: |
|
585 |
client = AstakosClient("https://example.com") |
|
586 |
client.getDisplayName(token_1, "1234") |
|
587 |
except NoDisplayName: |
|
588 |
pass |
|
589 |
except: |
|
590 |
self.fail("Should have raised NoDisplayName exception") |
|
591 |
else: |
|
592 |
self.fail("Should have raised NoDisplayName exception") |
|
593 |
|
|
578 | 594 |
|
579 | 595 |
class TestGetUUIDs(unittest.TestCase): |
580 | 596 |
"""Test cases for functions getUUIDs/getUUID""" |
... | ... | |
624 | 640 |
self.fail("Shouldn't raise an Exception") |
625 | 641 |
self.assertEqual(info, user_1['uuid']) |
626 | 642 |
|
643 |
# ---------------------------------- |
|
644 |
# Get uuid with wrong username |
|
645 |
def test_NoUUID(self): |
|
646 |
global token_1 |
|
647 |
_mockRequest([_requestOk]) |
|
648 |
try: |
|
649 |
client = AstakosClient("https://example.com") |
|
650 |
client.getUUID(token_1, "1234") |
|
651 |
except NoUUID: |
|
652 |
pass |
|
653 |
except: |
|
654 |
self.fail("Should have raised NoUUID exception") |
|
655 |
else: |
|
656 |
self.fail("Should have raised NoUUID exception") |
|
657 |
|
|
627 | 658 |
|
628 | 659 |
# ---------------------------- |
629 | 660 |
# Run tests |
Also available in: Unified diff