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