diff --git a/kamaki/cli/commands/astakos.py b/kamaki/cli/commands/astakos.py index 811c97a1460dfbe5728a3c7bae92219466366429..d2cd4daa1d0d02dc6ea7429d6bd17c82dec2181f 100644 --- a/kamaki/cli/commands/astakos.py +++ b/kamaki/cli/commands/astakos.py @@ -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' diff --git a/kamaki/clients/astakos/__init__.py b/kamaki/clients/astakos/__init__.py index 9a79ecdfc1c8d449a4c1b204d0f680a44784c1b2..14aed7a542e3a087ce1c7084b405a430580979e0 100644 --- a/kamaki/clients/astakos/__init__.py +++ b/kamaki/clients/astakos/__init__.py @@ -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() diff --git a/kamaki/clients/astakos/test.py b/kamaki/clients/astakos/test.py index a72ac43a6bf86f8dd65fbdbcaccafc6fc6357679..e16e2e260733afb0e4f4f7d14333ce4a7e5c5fce 100644 --- a/kamaki/clients/astakos/test.py +++ b/kamaki/clients/astakos/test.py @@ -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(