[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
|
||||
# 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):
|
||||
"""Factory function to create a new container service client."""
|
||||
if version != '1':
|
||||
raise ValueError(
|
||||
"zun only has one API version. Valid values for 'version'"
|
||||
" are '1'")
|
||||
return client.Client(**kwargs)
|
||||
def _get_client_class_and_version(version):
|
||||
if not isinstance(version, api_versions.APIVersion):
|
||||
version = api_versions.get_api_version(version)
|
||||
else:
|
||||
api_versions.check_major_version(version)
|
||||
if version.is_latest():
|
||||
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 testtools
|
||||
|
||||
from zunclient import api_versions
|
||||
from zunclient import client
|
||||
from zunclient import exceptions
|
||||
|
||||
|
||||
class ClientTest(testtools.TestCase):
|
||||
|
||||
@mock.patch('zunclient.v1.client.Client')
|
||||
def test_no_version_argument(self, mock_zun_client):
|
||||
client.Client(input_auth_token='mytoken', zun_url='http://myurl/')
|
||||
mock_zun_client.assert_called_with(
|
||||
input_auth_token='mytoken', zun_url='http://myurl/')
|
||||
def test_no_version_argument(self, mock_zun_client_v1):
|
||||
client.Client(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_valid_version_argument(self, mock_zun_client):
|
||||
client.Client(version='1', zun_url='http://myurl/')
|
||||
mock_zun_client.assert_called_with(zun_url='http://myurl/')
|
||||
def test_valid_version_argument(self, mock_zun_client_v1):
|
||||
client.Client(version='1',
|
||||
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, mock_zun_client):
|
||||
def test_invalid_version_argument(self):
|
||||
self.assertRaises(
|
||||
ValueError,
|
||||
client.Client, version='2', zun_url='http://myurl/')
|
||||
exceptions.UnsupportedVersion,
|
||||
client.Client, version='2')
|
||||
|
Loading…
Reference in New Issue
Block a user