Commit 3d1ff5d5 authored by Ilias Tsitsimpis's avatar Ilias Tsitsimpis Committed by Christos Stavrakakis
Browse files

snf-cyclades-app: Use astakosclient

parent 3e4e2d65
......@@ -76,6 +76,7 @@ INSTALL_REQUIRES = [
'setproctitle>=1.0.1',
'bitarray>=0.8',
'objpool>=0.2',
'astakosclient',
'snf-django-lib'
]
......
......@@ -51,6 +51,24 @@ USERS_DISPLAYNAMES = dict(map(lambda k: (k[1]['displayname'], {'uuid': k[0]}),
from synnefo.db import models_factory as mfactory
class AstakosClientMock():
def __init__(*args, **kwargs):
pass
def get_username(self, token, uuid):
try:
return USERS_UUIDS.get(uuid)['displayname']
except TypeError:
return None
def get_uuid(self, token, display_name):
try:
return USERS_DISPLAYNAMES.get(display_name)['uuid']
except TypeError:
return None
class AuthClient(Client):
def request(self, **request):
......@@ -79,23 +97,11 @@ class HelpdeskTests(TestCase):
'helpdesk'],
'auth_token': '0001'}
def get_uuid_mock(token, displayname, url):
try:
return USERS_DISPLAYNAMES.get(displayname)['uuid']
except TypeError:
return None
def get_displayname_mock(token, uuid, url):
try:
return USERS_UUIDS.get(uuid)['displayname']
except TypeError:
return None
# mock the astakos authentication function
from snf_django.lib import astakos
astakos.get_user = get_user_mock
astakos.get_displayname = get_displayname_mock
astakos.get_user_uuid = get_uuid_mock
import astakosclient
astakosclient.AstakosClient = AstakosClientMock
settings.SKIP_SSH_VALIDATION = True
settings.HELPDESK_ENABLED = True
......
......@@ -45,7 +45,7 @@ from urllib import unquote
from snf_django.lib.astakos import get_user
from synnefo.db.models import VirtualMachine, NetworkInterface, Network
from snf_django.lib import astakos
from astakosclient import AstakosClient
# server actions specific imports
from synnefo.api import servers
......@@ -58,7 +58,6 @@ UUID_SEARCH_REGEX = re.compile('([0-9a-z]{8}-([0-9a-z]{4}-){3}[0-9a-z]{12})')
VM_SEARCH_REGEX = re.compile('vm(-){0,}(?P<vmid>[0-9]+)')
def get_token_from_cookie(request, cookiename):
"""
Extract token from the cookie name provided. Cookie should be in the same
......@@ -81,11 +80,6 @@ AUTH_COOKIE_NAME = getattr(settings, 'HELPDESK_AUTH_COOKIE_NAME',
PERMITTED_GROUPS = getattr(settings, 'HELPDESK_PERMITTED_GROUPS', ['helpdesk'])
SHOW_DELETED_VMS = getattr(settings, 'HELPDESK_SHOW_DELETED_VMS', False)
# guess cyclades setting too
USER_CATALOG_URL = getattr(settings, 'CYCLADES_USER_CATALOG_URL', None)
USER_CATALOG_URL = getattr(settings, 'HELPDESK_USER_CATALOG_URL',
USER_CATALOG_URL)
def token_check(func):
"""
......@@ -115,7 +109,8 @@ def helpdesk_user_required(func, permitted_groups=PERMITTED_GROUPS):
raise Http404
token = get_token_from_cookie(request, AUTH_COOKIE_NAME)
get_user(request, settings.ASTAKOS_URL, fallback_token=token)
get_user(request, settings.ASTAKOS_URL,
fallback_token=token, logger=logger)
if hasattr(request, 'user') and request.user:
groups = request.user.get('groups', [])
......@@ -199,15 +194,16 @@ def account(request, search_query):
account = None
search_query = vmid
astakos = AstakosClient(settings.ASTAKOS_URL, retry=2,
use_pool=True, logger=logger)
if is_uuid:
account = search_query
account_name = astakos.get_displayname(auth_token, account,
USER_CATALOG_URL)
account_name = astakos.get_username(auth_token, account)
if account_exists and not is_uuid:
account_name = search_query
account = astakos.get_user_uuid(auth_token, account_name,
USER_CATALOG_URL)
account = astakos.get_uuid(auth_token, account_name)
if not account:
account_exists = False
......
......@@ -256,11 +256,7 @@ def machines_console(request):
def user_quota(request):
try:
get_user(request, settings.ASTAKOS_URL, usage=True)
except TypeError:
# astakos client backwards compatibility
get_user(request, settings.ASTAKOS_URL)
get_user(request, settings.ASTAKOS_URL, usage=True)
if request.user and 'usage' in request.user:
response = json.dumps(request.user['usage'])
......
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