def retrieve_user(token, astakos_auth_url, logger=None, client_ip=None): """Return user_info retrieved from astakos for the given token""" astakos_url = astakos_auth_url if astakos_url is None: try: astakos_url = settings.ASTAKOS_AUTH_URL except AttributeError: if logger: logger.error("Cannot authenticate without having" " an Astakos Authentication URL") raise if not token: return None headers = None if client_ip: headers = {'X-Client-IP': client_ip} astakos = AstakosClient(token, astakos_url, use_pool=True, retry=2, logger=logger, headers=headers) user_info = astakos.authenticate() return user_info
def retrieve_uuids(token, displaynames, return_dict=False, fail_silently=True): astakos = AstakosClient(token, ASTAKOS_AUTH_URL, retry=2, use_pool=True, logger=logger) catalog = astakos.get_uuids(displaynames) or {} missing = list(set(displaynames) - set(catalog)) if missing and not fail_silently: raise ItemNotExists("Unknown uuids: %s" % ", ".join(missing)) return catalog if return_dict else [catalog.get(i) for i in displaynames]
def test_reject_commission(self): """Test function call of reject_commission""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.reject_commission(57) except Exception as err: self.fail("Shouldn't raise Exception %s" % err)
def _auth_user(self, token, user_info, usage, pool): _mock_request([_request_ok]) try: client = AstakosClient("https://example.com", use_pool=pool) auth_info = client.get_user_info(token, usage=usage) except: self.fail("Shouldn't raise an Exception") self.assertEqual(user_info, auth_info)
def get_pithos_usage(token): """Get Pithos Usage from astakos.""" astakos = AstakosClient(ASTAKOS_BASE_URL, retry=2, use_pool=True, logger=logger) quotas = astakos.get_quotas(token)['system'] pithos_resources = [r['name'] for r in resources] map(quotas.pop, filter(lambda k: k not in pithos_resources, quotas.keys())) return quotas.popitem()[-1] # assume only one resource
def retrieve_displaynames(token, uuids, return_dict=False, fail_silently=True): astakos = AstakosClient(ASTAKOS_BASE_URL, retry=2, use_pool=True, logger=logger) catalog = astakos.get_usernames(token, uuids) or {} missing = list(set(uuids) - set(catalog)) if missing and not fail_silently: raise ItemNotExists('Unknown displaynames: %s' % ', '.join(missing)) return catalog if return_dict else [catalog.get(i) for i in uuids]
def _auth_user(self, pool): global token, endpoints_with_info, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) auth_info = client.authenticate() except Exception as err: self.fail("Shouldn't raise an Exception: %s" % err) self.assertEqual(endpoints_with_info, auth_info)
def _invalid_url(self, pool): global token, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) client._call_astakos("/astakos/api/misspelled") except NotFound: pass except Exception, e: self.fail("Got \"%s\" instead of 404" % e)
def user_for_token(token, astakos_auth_url, logger=None): if token is None: return None client = AstakosClient(token, astakos_auth_url, retry=2, use_pool=True, logger=logger) try: return client.authenticate() except Unauthorized: return None
def test_resolve_commissions(self): """Test function call of resolve_commissions""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) result = client.resolve_commissions([56, 57], [56, 58, 59]) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(result, resolve_commissions_rep)
def retrieve_uuids(token, displaynames, return_dict=False, fail_silently=True): astakos = AstakosClient(token, ASTAKOS_AUTH_URL, retry=2, use_pool=True, logger=logger) catalog = astakos.get_uuids(displaynames) or {} missing = list(set(displaynames) - set(catalog)) if missing and not fail_silently: raise ItemNotExists('Unknown uuids: %s' % ', '.join(missing)) return catalog if return_dict else [catalog.get(i) for i in displaynames]
def test_reject_commission(self): """Test function call of reject_commission""" global token_1 _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") client.reject_commission(token_1, 57) except Exception as err: self.fail("Shouldn't raise Exception %s" % err)
def test_get_quotas(self): """Test function call of get_quotas""" global quotas, token, auth_url try: client = AstakosClient(token['id'], auth_url) result = client.get_quotas() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(quotas, result)
def test_get_uuid(self): """Test get_uuid""" global token, user, auth_url try: client = AstakosClient(token['id'], auth_url, retry=1) catalog = client.get_uuids([user['name']]) except: self.fail("Shouldn't raise an Exception") self.assertEqual(catalog[user['name']], user['id'])
def test_get_pending_commissions(self): """Test function call of get_pending_commissions""" global token, pending_commissions, auth_url try: client = AstakosClient(token['id'], auth_url) response = client.get_pending_commissions() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, pending_commissions)
def test_get_resources(self): """Test function call of get_resources""" global resources, auth_url, token try: client = AstakosClient(token['id'], auth_url, retry=1) result = client.get_resources() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(resources, result)
def test_username(self): """Test get_username""" global token, user, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=False, retry=2) info = client.get_username(user['id']) except Exception, e: self.fail("Shouldn't raise an Exception: %s" % e)
def test_get_commission_info(self): """Test function call of get_commission_info""" global token, commission_description, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=True, pool_size=2) response = client.get_commission_info(57) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, commission_description)
def test_issue_commission(self): """Test function call of issue_commission""" global token, commission_request, commission_successful_reqsponse global auth_url try: client = AstakosClient(token['id'], auth_url) response = client._issue_commission(commission_request) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, commission_successful_response['serial'])
def _offline(self, pool): global token_1 _mock_request([_request_offline]) try: client = AstakosClient("https://example.com", use_pool=pool) client._call_astakos(token_1, astakosclient.API_AUTHENTICATE) except AstakosClientException: pass else: self.fail("Should have raised AstakosClientException")
def test_issue_commission(self): """Test function call of issue_commission""" global token_1, commission_request, commission_successful_reqsponse _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") response = client.issue_commission(token_1, commission_request) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, commission_successful_response['serial'])
def test_get_quotas(self): """Test function call of get_quotas""" global quotas, token_1 _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") result = client.get_quotas(token_1) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(quotas, result)
def test_get_uuid_user_two(self): """Test get_uuid for User Two""" global token_1, user_2 _mock_request([_request_offline, _request_ok]) try: client = AstakosClient("https://example.com", retry=1) info = client.get_uuid(token_2, user_1['username']) except: self.fail("Shouldn't raise an Exception") self.assertEqual(info, user_1['uuid'])
def test_get_resources(self): """Test function call of get_resources""" global resources _mock_request([_request_offline, _request_ok]) try: client = AstakosClient("https://example.com", retry=1) result = client.get_resources() except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(resources, result)
def test_offline_retry(self): """Test retry functionality for getUserInfo""" global token_1, user_1 _mock_request([_request_offline, _request_offline, _request_ok]) try: client = AstakosClient("https://example.com", retry=2) auth_info = client.get_user_info(token_1, usage=True) except: self.fail("Shouldn't raise an Exception") self.assertEqual(user_1, auth_info)
def test_get_endpoints(self): """Test function call of get_endpoints""" global token_1, user_info_endpoints _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") response = client.get_endpoints(token_1) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(response, user_info_endpoints)
def test_resolve_commissions(self): """Test function call of resolve_commissions""" global token_1 _mock_request([_request_ok]) try: client = AstakosClient("https://example.com") result = client.resolve_commissions(token_1, [56, 57], [56, 58, 59]) except Exception as err: self.fail("Shouldn't raise Exception %s" % err) self.assertEqual(result, resolve_commissions_rep)
def retrieve_uuid(token, displayname): if is_uuid(displayname): return displayname astakos = AstakosClient(token, ASTAKOS_AUTH_URL, retry=2, use_pool=True, logger=logger) try: uuid = astakos.get_uuid(displayname) except NoUUID: raise ItemNotExists(displayname) return uuid
def test_no_username(self): global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.get_username("1234") except NoUserName: pass except: self.fail("Should have raised NoDisplayName exception") else: self.fail("Should have raised NoDisplayName exception")
def test_username_user_one(self): """Test get_username for User One""" global token_2, user_1 _mock_request([_request_offline, _request_ok]) try: client = AstakosClient( "https://example.com", use_pool=True, retry=2) info = client.get_username(token_2, user_1['uuid']) except: self.fail("Shouldn't raise an Exception") self.assertEqual(info, user_1['username'])
def _get_user_catalogs(self, pool): global token, auth_url, api_usercatalogs try: client = AstakosClient(token['id'], auth_url, use_pool=pool) client._call_astakos(api_usercatalogs) except BadRequest: pass except Exception: self.fail("Should have returned 400 (Method not allowed)") else: self.fail("Should have returned 400 (Method not allowed)")
def _get_authenticate(self, pool): global token, auth_url try: client = AstakosClient(token['id'], auth_url, use_pool=pool) client._call_astakos(api_tokens, method="GET") except BadRequest: pass except Exception: self.fail("Should have returned 400 (Method not allowed)") else: self.fail("Should have returned 400 (Method not allowed)")
def test_issue_one_commission(self): """Test function call of issue_one_commission""" global token, commission_successful_response, auth_url try: client = AstakosClient(token['id'], auth_url) response = client.issue_one_commission( "c02f315b-7d84-45bc-a383-552a3f97d2ad", "system", {"cyclades.vm": 1, "cyclades.ram": 30000}) except Exception as err: self.fail("Shouldn't have raised Exception %s" % err) self.assertEqual(response, commission_successful_response['serial'])
def _http_scheme(self, pool): global token http_auth_url = "http://example.org/identity/v2.0" try: client = AstakosClient(token['id'], http_auth_url, use_pool=pool) client.authenticate() except AstakosClientException as err: if err.status != 302: self.fail("Should have returned 302 (Found)") else: self.fail("Should have returned 302 (Found)")
def retrieve_displayname(token, uuid, fail_silently=True): astakos = AstakosClient(token, ASTAKOS_AUTH_URL, retry=2, use_pool=True, logger=logger) try: displayname = astakos.get_username(uuid) except NoUserName: if not fail_silently: raise ItemNotExists(uuid) else: # just return the uuid return uuid return displayname
def test_no_uuid(self): global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.get_uuid("1234") except NoUUID: pass except: self.fail("Should have raised NoUUID exception") else: self.fail("Should have raised NoUUID exception")
def test_get_commission_info_not_found(self): """Test function call of get_commission_info with invalid serial""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.get_commission_info("57lala") except NotFound: pass except Exception as err: self.fail("Shouldn't raise Exception %s" % err) else: self.fail("Should have raised NotFound")
def test_commision_action(self): """Test function call of commision_action with wrong action""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.commission_action(57, "lala") except BadRequest: pass except Exception as err: self.fail("Shouldn't raise Exception %s" % err) else: self.fail("Should have raised BadRequest")
def test_get_commission_info_without_serial(self): """Test function call of get_commission_info without serial""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.get_commission_info(None) except BadValue: pass except Exception as err: self.fail("Shouldn't raise Exception %s" % err) else: self.fail("Should have raise BadValue")
def _unsupported_scheme(self, pool): global token, auth_url try: client = AstakosClient( token['id'], "ftp://example.com", use_pool=pool) client.authenticate() except BadValue: pass except Exception: self.fail("Should have raise BadValue Exception") else: self.fail("Should have raise BadValue Exception")
def _invalid_token(self, pool): global auth_url token = "skaksaFlBl+fasFdaf24sx" try: client = AstakosClient(token, auth_url, use_pool=pool) client.authenticate() except Unauthorized: pass except Exception: self.fail("Should have returned 401 (Invalid X-Auth-Token)") else: self.fail("Should have returned 401 (Invalid X-Auth-Token)")
def test_accept_commission_not_found(self): """Test function call of accept_commission with wrong serial""" global token, auth_url try: client = AstakosClient(token['id'], auth_url) client.reject_commission(20) except NotFound: pass except Exception as err: self.fail("Shouldn't raise Exception %s" % err) else: self.fail("Should have raised NotFound")
def test_get_quotas_unauthorized(self): """Test function call of get_quotas with wrong token""" global auth_url token = "buahfhsda" try: client = AstakosClient(token, auth_url) client.get_quotas() except Unauthorized: pass except Exception as err: self.fail("Shouldn't raise Exception %s" % err) else: self.fail("Should have raised Unauthorized Exception")