Commit f041ab50 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis Committed by Giorgos Korfiatis

django-lib: Use the new AstakosClient

parent d70d62f7
......@@ -62,7 +62,7 @@ def get_token(request):
def api_method(http_method=None, token_required=True, user_required=True,
logger=None, format_allowed=True, astakos_url=None,
logger=None, format_allowed=True, astakos_auth_url=None,
serializations=None, strict_serlization=False):
"""Decorator function for views that implement an API method."""
if not logger:
......@@ -104,12 +104,12 @@ def api_method(http_method=None, token_required=True, user_required=True,
# Authenticate
if user_required:
assert(token_required), "Can not get user without token"
astakos = astakos_url or settings.ASTAKOS_BASE_URL
astakos = AstakosClient(astakos,
astakos = astakos_auth_url or settings.ASTAKOS_AUTH_URL
astakos = AstakosClient(token, astakos,
use_pool=True,
retry=2,
logger=logger)
user_info = astakos.get_user_info(token)
user_info = astakos.get_user_info()
request.user_uniq = user_info["uuid"]
request.user = user_info
......
......@@ -57,6 +57,10 @@ def proxy(request, proxy_base=None, target_base=None):
m = "proxy() needs both proxy_base and target_base argument not None"
raise AssertionError(m)
# Get strings from lazy objects
proxy_base = str(proxy_base)
target_base = str(target_base)
parsed = urlparse.urlparse(target_base)
target_base = '/' + parsed.path.strip('/')
proxy_base = proxy_base.strip('/')
......
......@@ -38,30 +38,33 @@ from astakosclient.errors import (Unauthorized, NoUUID, NoUserName,
AstakosClientException)
def user_for_token(client, token, usage=False):
if not token:
def user_for_token(token, astakos_auth_url, usage=False, logger=None):
if token is None:
return None
client = AstakosClient(token, astakos_auth_url,
retry=2, use_pool=True, logger=logger)
try:
return client.get_user_info(token, usage=True)
return client.get_user_info(usage=True)
except Unauthorized:
return None
def get_user(request, astakos_url, fallback_token=None,
def get_user(request, astakos_auth_url, fallback_token=None,
usage=False, logger=None):
request.user = None
request.user_uniq = None
client = AstakosClient(astakos_url, retry=2, use_pool=True, logger=logger)
# Try to find token in a parameter or in a request header.
user = user_for_token(client, request.GET.get('X-Auth-Token'), usage=usage)
user = user_for_token(
request.GET.get('X-Auth-Token'), astakos_auth_url,
usage, logger)
if not user:
user = user_for_token(client,
request.META.get('HTTP_X_AUTH_TOKEN'),
usage=usage)
user = user_for_token(
request.META.get('HTTP_X_AUTH_TOKEN'), astakos_auth_url,
usage, logger)
if not user:
user = user_for_token(client, fallback_token, usage=usage)
user = user_for_token(
fallback_token, astakos_auth_url, usage, logger)
if not user:
return None
......@@ -76,14 +79,14 @@ def get_user(request, astakos_url, fallback_token=None,
class UserCache(object):
"""uuid<->displayname user 'cache'"""
def __init__(self, astakos_url, astakos_token, split=100, logger=None):
def __init__(self, astakos_auth_url, astakos_token,
split=100, logger=None):
if logger is None:
logger = logging.getLogger(__name__)
self.logger = logger
self.astakos = AstakosClient(astakos_url, retry=2,
use_pool=True, logger=logger)
self.astakos_token = astakos_token
self.astakos = AstakosClient(astakos_token, astakos_auth_url,
retry=2, use_pool=True, logger=logger)
self.users = {}
self.split = split
......@@ -97,8 +100,8 @@ class UserCache(object):
for start in range(0, total, split):
end = start + split
try:
names = self.astakos.service_get_usernames(
self.astakos_token, uuid_list[start:end])
names = \
self.astakos.service_get_usernames(uuid_list[start:end])
count += len(names)
self.users.update(names)
......@@ -119,8 +122,7 @@ class UserCache(object):
if not name in self.users:
try:
uuid = self.astakos.service_get_uuid(
self.astakos_token, name)
uuid = self.astakos.service_get_uuid(name)
except NoUUID:
self.logger.debug("Failed to fetch uuid for %s", name)
except AstakosClientException:
......@@ -138,8 +140,7 @@ class UserCache(object):
if not uuid in self.users:
try:
name = self.astakos.service_get_username(
self.astakos_token, uuid)
name = self.astakos.service_get_username(uuid)
except NoUserName:
self.logger.debug("Failed to fetch display name for %s", uuid)
except AstakosClientException:
......
......@@ -108,7 +108,7 @@ class ListCommand(BaseCommand):
order_by = None
# Fields used only with user_user_field
astakos_url = None
astakos_auth_url = None
astakos_token = None
help = "Generic List Command"
......@@ -154,8 +154,9 @@ class ListCommand(BaseCommand):
def __init__(self, *args, **kwargs):
if self.user_uuid_field:
assert(self.astakos_url), "astakos_url attribute is needed when"\
" user_uuid_field is declared"
assert(self.astakos_auth_url), "astakos_auth_url attribute is "\
"needed when user_uuid_field "\
"is declared"
assert(self.astakos_token), "astakos_token attribute is needed"\
" when user_uuid_field is declared"
self.option_list += (
......@@ -218,7 +219,7 @@ class ListCommand(BaseCommand):
user = options.get("user")
if user:
if "@" in user:
ucache = UserCache(self.astakos_url, self.astakos_token)
ucache = UserCache(self.astakos_auth_url, self.astakos_token)
user = ucache.get_uuid(user)
self.filters[self.user_uuid_field] = user
......@@ -255,7 +256,7 @@ class ListCommand(BaseCommand):
self.FIELDS["user.email"] =\
("user_email", "The email of the owner.")
uuids = [getattr(obj, self.user_uuid_field) for obj in objects]
ucache = UserCache(self.astakos_url, self.astakos_token)
ucache = UserCache(self.astakos_auth_url, self.astakos_token)
ucache.fetch_names(list(set(uuids)))
for obj in objects:
uuid = getattr(obj, self.user_uuid_field)
......
......@@ -132,45 +132,59 @@ def astakos_user(user):
"""
with patch("snf_django.lib.api.get_token") as get_token:
get_token.return_value = "DummyToken"
with patch('astakosclient.AstakosClient.get_user_info') as m:
m.return_value = {"uuid": text.udec(user, 'utf8')}
with patch('astakosclient.AstakosClient.get_quotas') as m2:
m2.return_value = {
"system": {
"pithos.diskspace": {
"usage": 0,
"limit": 1073741824, # 1GB
"pending": 0
}
with patch('astakosclient.AstakosClient.get_endpoints') as m1:
m1.return_value = {
'access': {
'serviceCatalog': [{
'endpoints': [{'SNF:uiURL': 'ui_url',
'publicURL': 'accounts_url',
'region': 'default',
'versionId': 'v1.0'}],
'name': 'astakos_account',
'type': 'account'}]
}
}
issue_fun = "astakosclient.AstakosClient.issue_one_commission"
with patch(issue_fun) as m3:
serials = []
append = serials.append
def get_serial(*args, **kwargs):
global serial
serial += 1
append(serial)
return serial
m3.side_effect = get_serial
resolv_fun = \
'astakosclient.AstakosClient.resolve_commissions'
with patch(resolv_fun) as m4:
m4.return_value = {'accepted': serials,
'rejected': [],
'failed': []}
users_fun = 'astakosclient.AstakosClient.get_usernames'
with patch(users_fun) as m5:
def get_usernames(*args, **kwargs):
uuids = args[-1]
return dict((uuid, uuid) for uuid in uuids)
m5.side_effect = get_usernames
yield
with patch('astakosclient.AstakosClient.get_user_info') as m2:
m2.return_value = {"uuid": text.udec(user, 'utf8')}
with patch('astakosclient.AstakosClient.get_quotas') as m3:
m3.return_value = {
"system": {
"pithos.diskspace": {
"usage": 0,
"limit": 1073741824, # 1GB
"pending": 0
}
}
}
issue_fun = \
"astakosclient.AstakosClient.issue_one_commission"
with patch(issue_fun) as m3:
serials = []
append = serials.append
def get_serial(*args, **kwargs):
global serial
serial += 1
append(serial)
return serial
m3.side_effect = get_serial
resolv_fun = \
'astakosclient.AstakosClient.resolve_commissions'
with patch(resolv_fun) as m4:
m4.return_value = {'accepted': serials,
'rejected': [],
'failed': []}
users_fun = \
'astakosclient.AstakosClient.get_usernames'
with patch(users_fun) as m5:
def get_usernames(*args, **kwargs):
uuids = args[-1]
return dict((uuid, uuid) for uuid in uuids)
m5.side_effect = get_usernames
yield
serial = 0
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment