diff --git a/bilean/drivers/openstack/sdk.py b/bilean/drivers/openstack/sdk.py index 2de6148..7726f1b 100644 --- a/bilean/drivers/openstack/sdk.py +++ b/bilean/drivers/openstack/sdk.py @@ -19,13 +19,13 @@ import six from openstack import connection from openstack import exceptions as sdk_exc -from openstack import profile from oslo_serialization import jsonutils from requests import exceptions as req_exc from bilean.common import exception as bilean_exc +from bilean import version -USER_AGENT = 'bilean' +APP_NAME = 'bilean' exc = sdk_exc LOG = logging.getLogger(__name__) @@ -35,9 +35,7 @@ def parse_exception(ex): code = 500 if isinstance(ex, sdk_exc.HttpException): - # some exceptions don't contain status_code - if ex.http_status is not None: - code = ex.http_status + code = ex.status_code message = ex.message data = {} try: @@ -96,18 +94,15 @@ def create_connection(params=None): params = {} if params.get('token'): - auth_plugin = 'token' + auth_type = 'token' else: - auth_plugin = 'password' + auth_type = 'password' - prof = profile.Profile() - prof.set_version('identity', 'v3') - if 'region_name' in params: - prof.set_region(prof.ALL, params['region_name']) - params.pop('region_name') try: - conn = connection.Connection(profile=prof, user_agent=USER_AGENT, - auth_plugin=auth_plugin, **params) + conn = connection.Connection( + app_name=APP_NAME, + app_version=version.version_info.version_string(), + identity_api_version='3', auth_type=auth_type, **params) except Exception as ex: raise parse_exception(ex) diff --git a/bilean/notification/notification.py b/bilean/notification/notification.py index fcbac78..4fc0aba 100644 --- a/bilean/notification/notification.py +++ b/bilean/notification/notification.py @@ -15,6 +15,7 @@ from oslo_config import cfg from oslo_log import log as logging import oslo_messaging from oslo_service import service +import six from bilean.common.i18n import _LE from bilean.common import messaging as bilean_messaging @@ -53,7 +54,7 @@ class NotificationService(service.Service): try: topic_exchanges = plugin.get_notification_topics_exchanges() for plugin_topic in topic_exchanges: - if isinstance(plugin_topic, basestring): + if isinstance(plugin_topic, six.string_types): raise Exception( _LE("Plugin %s should return a list of topic " "exchange pairs") % plugin.__class__.__name__) diff --git a/bilean/tests/drivers/test_sdk.py b/bilean/tests/drivers/test_sdk.py index 584fbde..460a3bd 100644 --- a/bilean/tests/drivers/test_sdk.py +++ b/bilean/tests/drivers/test_sdk.py @@ -12,7 +12,6 @@ import mock from openstack import connection -from openstack import profile from oslo_serialization import jsonutils from requests import exceptions as req_exc import six @@ -20,11 +19,13 @@ import six from bilean.common import exception as bilean_exc from bilean.drivers.openstack import sdk from bilean.tests.common import base +from bilean import version class OpenStackSDKTest(base.BileanTestCase): def setUp(self): + self.app_version = version.version_info.version_string() super(OpenStackSDKTest, self).setUp() def test_parse_exception_http_exception_with_details(self): @@ -34,7 +35,8 @@ class OpenStackSDKTest(base.BileanTestCase): 'message': 'Resource BAR is not found.' } }) - raw = sdk.exc.ResourceNotFound('A message', details, http_status=404) + raw = sdk.exc.ResourceNotFound( + 'A message', details=details, http_status=404) ex = self.assertRaises(bilean_exc.InternalError, sdk.parse_exception, raw) @@ -47,7 +49,8 @@ class OpenStackSDKTest(base.BileanTestCase): 'message': 'Quota exceeded for instances.' } }) - raw = sdk.exc.ResourceNotFound('A message', details, 403) + raw = sdk.exc.ResourceNotFound('A message', details=details, + http_status=403) ex = self.assertRaises(bilean_exc.InternalError, sdk.parse_exception, raw) @@ -57,7 +60,8 @@ class OpenStackSDKTest(base.BileanTestCase): def test_parse_exception_http_exception_no_details(self): details = "An error message" - raw = sdk.exc.ResourceNotFound('A message.', details, http_status=404) + raw = sdk.exc.ResourceNotFound( + 'A message.', details=details, http_status=404) ex = self.assertRaises(bilean_exc.InternalError, sdk.parse_exception, raw) @@ -129,30 +133,24 @@ class OpenStackSDKTest(base.BileanTestCase): self.assertEqual(500, ex.code) self.assertEqual('BOOM', ex.message) - @mock.patch.object(profile, 'Profile') @mock.patch.object(connection, 'Connection') - def test_create_connection_token(self, mock_conn, mock_profile): - x_profile = mock.Mock() - mock_profile.return_value = x_profile + def test_create_connection_token(self, mock_conn): x_conn = mock.Mock() mock_conn.return_value = x_conn res = sdk.create_connection({'token': 'TOKEN', 'foo': 'bar'}) self.assertEqual(x_conn, res) - mock_profile.assert_called_once_with() - x_profile.set_version.assert_called_once_with('identity', 'v3') - mock_conn.assert_called_once_with(profile=x_profile, - user_agent=sdk.USER_AGENT, - auth_plugin='token', - token='TOKEN', - foo='bar') + mock_conn.assert_called_once_with( + app_name=sdk.APP_NAME, + app_version=self.app_version, + identity_api_version='3', + auth_type='token', + token='TOKEN', + foo='bar') - @mock.patch.object(profile, 'Profile') @mock.patch.object(connection, 'Connection') - def test_create_connection_password(self, mock_conn, mock_profile): - x_profile = mock.Mock() - mock_profile.return_value = x_profile + def test_create_connection_password(self, mock_conn): x_conn = mock.Mock() mock_conn.return_value = x_conn @@ -160,40 +158,33 @@ class OpenStackSDKTest(base.BileanTestCase): 'foo': 'bar'}) self.assertEqual(x_conn, res) - mock_profile.assert_called_once_with() - x_profile.set_version.assert_called_once_with('identity', 'v3') - mock_conn.assert_called_once_with(profile=x_profile, - user_agent=sdk.USER_AGENT, - auth_plugin='password', - user_id='123', - password='abc', - foo='bar') + mock_conn.assert_called_once_with( + app_name=sdk.APP_NAME, + app_version=self.app_version, + identity_api_version='3', + auth_type='password', + user_id='123', + password='abc', + foo='bar') - @mock.patch.object(profile, 'Profile') @mock.patch.object(connection, 'Connection') - def test_create_connection_with_region(self, mock_conn, mock_profile): - x_profile = mock.Mock() - mock_profile.return_value = x_profile + def test_create_connection_with_region(self, mock_conn): x_conn = mock.Mock() mock_conn.return_value = x_conn res = sdk.create_connection({'region_name': 'REGION_ONE'}) self.assertEqual(x_conn, res) - mock_profile.assert_called_once_with() - x_profile.set_region.assert_called_once_with(x_profile.ALL, - 'REGION_ONE') - mock_conn.assert_called_once_with(profile=x_profile, - user_agent=sdk.USER_AGENT, - auth_plugin='password') + mock_conn.assert_called_once_with( + app_name=sdk.APP_NAME, + app_version=self.app_version, + identity_api_version='3', + auth_type='password', + region_name='REGION_ONE') - @mock.patch.object(profile, 'Profile') @mock.patch.object(connection, 'Connection') @mock.patch.object(sdk, 'parse_exception') - def test_create_connection_with_exception(self, mock_parse, mock_conn, - mock_profile): - x_profile = mock.Mock() - mock_profile.return_value = x_profile + def test_create_connection_with_exception(self, mock_parse, mock_conn): ex_raw = Exception('Whatever') mock_conn.side_effect = ex_raw mock_parse.side_effect = bilean_exc.InternalError(code=123, @@ -202,10 +193,11 @@ class OpenStackSDKTest(base.BileanTestCase): ex = self.assertRaises(bilean_exc.InternalError, sdk.create_connection) - mock_profile.assert_called_once_with() - mock_conn.assert_called_once_with(profile=x_profile, - user_agent=sdk.USER_AGENT, - auth_plugin='password') + mock_conn.assert_called_once_with( + app_name=sdk.APP_NAME, + app_version=self.app_version, + identity_api_version='3', + auth_type='password') mock_parse.assert_called_once_with(ex_raw) self.assertEqual(123, ex.code) self.assertEqual('BOOM', ex.message)