Commit cd42f8d8 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Wrap AstakosClient for backward compatiblity

parent 609471e9
......@@ -35,7 +35,7 @@ from json import load, loads
from os.path import abspath
from kamaki.cli import command
from kamaki.clients.astakos import SynnefoAstakosClient
from kamaki.clients.astakos import LoggedAstakosClient
from kamaki.cli.commands import (
_command_init, errors, _optional_json, addLogSettings)
from kamaki.cli.command_tree import CommandTree
......@@ -100,8 +100,7 @@ class _init_synnefo_astakosclient(_command_init):
'astakos') or self.config.get_cloud(
self.cloud, 'token')
token = token.split()[0] if ' ' in token else token
self.client = SynnefoAstakosClient(
auth_url=base_url, token=token)
self.client = LoggedAstakosClient(base_url, token)
return
else:
self.cloud = 'default'
......
......@@ -32,12 +32,25 @@
# or implied, of GRNET S.A.
from logging import getLogger
from astakosclient import *
# astakosclient contains: AstakosCLient, AstakosClientException
from astakosclient import AstakosClient as OriginalAstakosClient
from astakosclient import AstakosClientException, parse_endpoints
from kamaki.clients import Client, ClientError, RequestManager, recvlog
class AstakosClient(OriginalAstakosClient):
"""Wrap Original AstakosClient to ensure compatibility in kamaki clients"""
def __init__(self, *args, **kwargs):
if args:
args = list(args)
url = args.pop(0)
token = args.pop(0) if args else kwargs.pop('token', None)
args = tuple([token, url] + args)
elif 'base_url' in kwargs:
kwargs['auth_url'] = kwargs.get('auth_url', kwargs['base_url'])
super(AstakosClient, self).__init__(*args, **kwargs)
def _astakos_error(foo):
def wrap(self, *args, **kwargs):
......@@ -48,8 +61,12 @@ def _astakos_error(foo):
return wrap
class SynnefoAstakosClient(AstakosClient):
"""An astakosclient.AstakosClient wrapper, that logs the way of kamaki"""
class LoggedAstakosClient(AstakosClient):
"""An AstakosClient wrapper with modified logging
Logs are adjusted to appear similar to the ones of kamaki clients.
No other changes are made to the parent class.
"""
LOG_TOKEN = False
LOG_DATA = False
......@@ -66,7 +83,7 @@ class SynnefoAstakosClient(AstakosClient):
recvlog.info('- - - - - - -')
def _call_astakos(self, *args, **kwargs):
r = super(SynnefoAstakosClient, self)._call_astakos(*args, **kwargs)
r = super(LoggedAstakosClient, self)._call_astakos(*args, **kwargs)
try:
log_request = getattr(self, 'log_request', None)
if log_request:
......@@ -129,8 +146,8 @@ class CachedAstakosClient(Client):
:param token: (str) custom token to authenticate
"""
token = self._resolve_token(token)
astakos = SynnefoAstakosClient(
token, self.base_url, logger=getLogger('astakosclient'))
astakos = LoggedAstakosClient(
self.base_url, token, logger=getLogger('astakosclient'))
astakos.LOG_TOKEN = getattr(self, 'LOG_TOKEN', False)
astakos.LOG_DATA = getattr(self, 'LOG_DATA', False)
r = astakos.authenticate()
......
......@@ -98,10 +98,9 @@ class AstakosClient(TestCase):
def tearDown(self):
FR.json = example
@patch('%s.LoggedAstakosClient.__init__' % astakos_pkg, return_value=None)
@patch(
'%s.SynnefoAstakosClient.__init__' % astakos_pkg, return_value=None)
@patch(
'%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
'%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
return_value=example)
def _authenticate(self, get_endpoints, sac):
r = self.client.authenticate()
......@@ -187,17 +186,17 @@ class AstakosClient(TestCase):
self.assertEqual(r[0]['auth_token'], self.token)
@patch(
'%s.SynnefoAstakosClient.get_usernames' % astakos_pkg,
'%s.LoggedAstakosClient.get_usernames' % astakos_pkg,
return_value={42: 'username42', 43: 'username43'})
def test_uuids2usernames(self, get_usernames):
from astakosclient import AstakosClientException
self.assertRaises(
AstakosClientException, self.client.uuids2usernames, [42, 43])
with patch(
'%s.SynnefoAstakosClient.__init__' % astakos_pkg,
'%s.LoggedAstakosClient.__init__' % astakos_pkg,
return_value=None) as sac:
with patch(
'%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
'%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
return_value=example) as get_endpoints:
r = self.client.uuids2usernames([42, 43])
self.assert_dicts_are_equal(
......@@ -208,17 +207,17 @@ class AstakosClient(TestCase):
self.assertEqual(get_usernames.mock_calls[-1], call([42, 43]))
@patch(
'%s.SynnefoAstakosClient.get_uuids' % astakos_pkg,
'%s.LoggedAstakosClient.get_uuids' % astakos_pkg,
return_value={'username42': 42, 'username43': 43})
def test_usernames2uuids(self, get_uuids):
from astakosclient import AstakosClientException
self.assertRaises(
AstakosClientException, self.client.usernames2uuids, ['u1', 'u2'])
with patch(
'%s.SynnefoAstakosClient.__init__' % astakos_pkg,
'%s.LoggedAstakosClient.__init__' % astakos_pkg,
return_value=None) as sac:
with patch(
'%s.SynnefoAstakosClient.get_endpoints' % astakos_pkg,
'%s.LoggedAstakosClient.get_endpoints' % astakos_pkg,
return_value=example) as get_endpoints:
r = self.client.usernames2uuids(['u1', 'u2'])
self.assert_dicts_are_equal(
......
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