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
This commit is contained in:
Erik Olof Gunnar Andersson 2018-01-22 20:32:02 -08:00
parent 87f0bd14f3
commit f598fd23e1
5 changed files with 39 additions and 55 deletions

View File

@ -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

View File

@ -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)

View File

@ -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')

View File

@ -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):

View File

@ -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
######################################################################