From f598fd23e11d31b547a5b7b9281cb8250fd30269 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Mon, 22 Jan 2018 20:32:02 -0800 Subject: [PATCH] Refactored make_client There were two code paths that did the same thing, and while the code paths did the same thing, one of them supported region_name and the other one did not. Moving all this functionality into a single function to prevent this from happening in the future. - Re-factored make_client and create_connection. - Remove now unused sdk.py. - Renamed test_sdk to test_plugin. Change-Id: Ibb477290c90e9af51be3537d11cf5142041db37f --- senlinclient/common/sdk.py | 37 ------------------- senlinclient/plugin.py | 30 +++++++++++---- .../unit/{test_sdk.py => test_plugin.py} | 8 ++-- senlinclient/tests/unit/v1/test_client.py | 4 +- senlinclient/v1/client.py | 15 ++++++-- 5 files changed, 39 insertions(+), 55 deletions(-) delete mode 100644 senlinclient/common/sdk.py rename senlinclient/tests/unit/{test_sdk.py => test_plugin.py} (93%) diff --git a/senlinclient/common/sdk.py b/senlinclient/common/sdk.py deleted file mode 100644 index 876831ac..00000000 --- a/senlinclient/common/sdk.py +++ /dev/null @@ -1,37 +0,0 @@ -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from openstack import connection -from openstack import exceptions -from openstack import profile - -from senlinclient.common import exc - - -def create_connection(prof=None, user_agent=None, **kwargs): - if not prof: - prof = profile.Profile() - interface = kwargs.pop('interface', None) - region_name = kwargs.pop('region_name', None) - if interface: - prof.set_interface('clustering', interface) - if region_name: - prof.set_region('clustering', region_name) - - prof.set_api_version('clustering', '1.7') - try: - conn = connection.Connection(profile=prof, user_agent=user_agent, - **kwargs) - except exceptions.HttpException as ex: - exc.parse_exception(ex.details) - - return conn diff --git a/senlinclient/plugin.py b/senlinclient/plugin.py index bd7ded9f..203b76f2 100644 --- a/senlinclient/plugin.py +++ b/senlinclient/plugin.py @@ -26,17 +26,31 @@ API_NAME = 'clustering' CURRENT_API_VERSION = '1.8' +def create_connection(prof=None, **kwargs): + interface = kwargs.pop('interface', None) + region_name = kwargs.pop('region_name', None) + user_agent = kwargs.pop('user_agent', None) + + if not prof: + prof = profile.Profile() + prof.set_api_version(API_NAME, CURRENT_API_VERSION) + + if interface: + prof.set_interface(API_NAME, interface) + if region_name: + prof.set_region(API_NAME, region_name) + + return connection.Connection(profile=prof, user_agent=user_agent, **kwargs) + + def make_client(instance): """Returns a clustering proxy""" - prof = profile.Profile() - prof.set_api_version(API_NAME, CURRENT_API_VERSION) - if instance.region_name: - prof.set_region('clustering', instance.region_name) - if instance.interface: - prof.set_interface('clustering', instance.interface) + conn = create_connection( + region_name=instance.region_name, + interface=instance.interface, + authenticator=instance.session.auth + ) - conn = connection.Connection(profile=prof, - authenticator=instance.session.auth) LOG.debug('Connection: %s', conn) LOG.debug('Clustering client initialized using OpenStackSDK: %s', conn.cluster) diff --git a/senlinclient/tests/unit/test_sdk.py b/senlinclient/tests/unit/test_plugin.py similarity index 93% rename from senlinclient/tests/unit/test_sdk.py rename to senlinclient/tests/unit/test_plugin.py index 535ddfc3..0750a6be 100644 --- a/senlinclient/tests/unit/test_sdk.py +++ b/senlinclient/tests/unit/test_plugin.py @@ -15,10 +15,10 @@ from openstack import connection as sdk_connection from openstack import profile as sdk_profile import testtools -from senlinclient.common import sdk +from senlinclient import plugin -class TestSdk(testtools.TestCase): +class TestPlugin(testtools.TestCase): @mock.patch.object(sdk_connection, 'Connection') def test_create_connection_with_profile(self, mock_connection): @@ -30,7 +30,7 @@ class TestSdk(testtools.TestCase): 'password': 'abc', 'auth_url': 'test_url' } - res = sdk.create_connection(mock_prof, **kwargs) + res = plugin.create_connection(mock_prof, **kwargs) mock_connection.assert_called_once_with(profile=mock_prof, user_agent=None, user_id='123', @@ -53,7 +53,7 @@ class TestSdk(testtools.TestCase): 'password': 'abc', 'auth_url': 'test_url' } - res = sdk.create_connection(**kwargs) + res = plugin.create_connection(**kwargs) mock_prof.set_interface.assert_called_once_with('clustering', 'public') mock_prof.set_region.assert_called_once_with('clustering', 'RegionOne') diff --git a/senlinclient/tests/unit/v1/test_client.py b/senlinclient/tests/unit/v1/test_client.py index 99da1dc9..11b6f80b 100644 --- a/senlinclient/tests/unit/v1/test_client.py +++ b/senlinclient/tests/unit/v1/test_client.py @@ -13,11 +13,11 @@ import mock import testtools -from senlinclient.common import sdk +from senlinclient import plugin from senlinclient.v1 import client -@mock.patch.object(sdk, 'create_connection') +@mock.patch.object(plugin, 'create_connection') class ClientTest(testtools.TestCase): def setUp(self): diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index b3b7b86c..29858319 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -9,15 +9,22 @@ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. +from openstack import exceptions -from senlinclient.common import sdk +from senlinclient.common import exc +from senlinclient import plugin class Client(object): - def __init__(self, prof=None, user_agent=None, **kwargs): - self.conn = sdk.create_connection(prof=prof, user_agent=user_agent, - **kwargs) + try: + conn = plugin.create_connection(prof=prof, + user_agent=user_agent, + **kwargs) + except exceptions.HttpException as ex: + exc.parse_exception(ex.details) + + self.conn = conn self.service = self.conn.cluster ######################################################################