[Refactor code] Support dynamic load Client module
- Support dynamic load Client module depend on given version. At this time, Zunclient has only 1 version, but this patch will be useful in the future. - Change related test cases. - Remove hardcode version. Change-Id: I49403d88c6d64577d5a6c1a11e36a6d4c3bf0eba
This commit is contained in:
parent
f6d4dca49d
commit
f4e2e419f0
@ -12,13 +12,29 @@
|
|||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
from zunclient.v1 import client
|
from oslo_utils import importutils
|
||||||
|
|
||||||
|
from zunclient import api_versions
|
||||||
|
from zunclient import exceptions
|
||||||
|
from zunclient.i18n import _
|
||||||
|
|
||||||
|
|
||||||
def Client(version='1', **kwargs):
|
def _get_client_class_and_version(version):
|
||||||
"""Factory function to create a new container service client."""
|
if not isinstance(version, api_versions.APIVersion):
|
||||||
if version != '1':
|
version = api_versions.get_api_version(version)
|
||||||
raise ValueError(
|
else:
|
||||||
"zun only has one API version. Valid values for 'version'"
|
api_versions.check_major_version(version)
|
||||||
" are '1'")
|
if version.is_latest():
|
||||||
return client.Client(**kwargs)
|
raise exceptions.UnsupportedVersion(
|
||||||
|
_('The version should be explicit, not latest.'))
|
||||||
|
return version, importutils.import_class(
|
||||||
|
'zunclient.v%s.client.Client' % version.ver_major)
|
||||||
|
|
||||||
|
|
||||||
|
def Client(version='1', username=None, auth_url=None, **kwargs):
|
||||||
|
"""Initialize client objects based on given version"""
|
||||||
|
api_version, client_class = _get_client_class_and_version(version)
|
||||||
|
return client_class(api_version=api_version,
|
||||||
|
auth_url=auth_url,
|
||||||
|
username=username,
|
||||||
|
**kwargs)
|
||||||
|
@ -15,24 +15,35 @@
|
|||||||
import mock
|
import mock
|
||||||
import testtools
|
import testtools
|
||||||
|
|
||||||
|
from zunclient import api_versions
|
||||||
from zunclient import client
|
from zunclient import client
|
||||||
|
from zunclient import exceptions
|
||||||
|
|
||||||
|
|
||||||
class ClientTest(testtools.TestCase):
|
class ClientTest(testtools.TestCase):
|
||||||
|
|
||||||
@mock.patch('zunclient.v1.client.Client')
|
@mock.patch('zunclient.v1.client.Client')
|
||||||
def test_no_version_argument(self, mock_zun_client):
|
def test_no_version_argument(self, mock_zun_client_v1):
|
||||||
client.Client(input_auth_token='mytoken', zun_url='http://myurl/')
|
client.Client(auth_url='http://example/identity',
|
||||||
mock_zun_client.assert_called_with(
|
username='admin')
|
||||||
input_auth_token='mytoken', zun_url='http://myurl/')
|
api_version = api_versions.get_api_version('1')
|
||||||
|
mock_zun_client_v1.assert_called_with(
|
||||||
|
api_version=api_version,
|
||||||
|
auth_url='http://example/identity',
|
||||||
|
username='admin')
|
||||||
|
|
||||||
@mock.patch('zunclient.v1.client.Client')
|
@mock.patch('zunclient.v1.client.Client')
|
||||||
def test_valid_version_argument(self, mock_zun_client):
|
def test_valid_version_argument(self, mock_zun_client_v1):
|
||||||
client.Client(version='1', zun_url='http://myurl/')
|
client.Client(version='1',
|
||||||
mock_zun_client.assert_called_with(zun_url='http://myurl/')
|
auth_url='http://example/identity',
|
||||||
|
username='admin')
|
||||||
|
api_version = api_versions.get_api_version('1')
|
||||||
|
mock_zun_client_v1.assert_called_with(
|
||||||
|
api_version=api_version,
|
||||||
|
auth_url='http://example/identity',
|
||||||
|
username='admin')
|
||||||
|
|
||||||
@mock.patch('zunclient.v1.client.Client')
|
def test_invalid_version_argument(self):
|
||||||
def test_invalid_version_argument(self, mock_zun_client):
|
|
||||||
self.assertRaises(
|
self.assertRaises(
|
||||||
ValueError,
|
exceptions.UnsupportedVersion,
|
||||||
client.Client, version='2', zun_url='http://myurl/')
|
client.Client, version='2')
|
||||||
|
Loading…
Reference in New Issue
Block a user