Merge "Abstract Client building into novaclient.client"
This commit is contained in:
commit
4c61a89457
@ -12,8 +12,6 @@ import logging
|
||||
import os
|
||||
import urlparse
|
||||
|
||||
from novaclient import service_catalog
|
||||
|
||||
try:
|
||||
import json
|
||||
except ImportError:
|
||||
@ -24,8 +22,9 @@ if not hasattr(urlparse, 'parse_qsl'):
|
||||
import cgi
|
||||
urlparse.parse_qsl = cgi.parse_qsl
|
||||
|
||||
|
||||
from novaclient import exceptions
|
||||
from novaclient import service_catalog
|
||||
from novaclient import utils
|
||||
|
||||
|
||||
_logger = logging.getLogger(__name__)
|
||||
@ -293,3 +292,21 @@ class HTTPClient(httplib2.Http):
|
||||
self.follow_all_redirects = tmp_follow_all_redirects
|
||||
|
||||
return self._extract_service_catalog(url, resp, body)
|
||||
|
||||
|
||||
def get_client_class(version):
|
||||
try:
|
||||
version = str(version)
|
||||
client_path = {
|
||||
'1.1': 'novaclient.v1_1.client.Client',
|
||||
'2': 'novaclient.v1_1.client.Client',
|
||||
}[version]
|
||||
except (KeyError, ValueError):
|
||||
raise exceptions.UnsupportedVersion()
|
||||
|
||||
return utils.import_class(client_path)
|
||||
|
||||
|
||||
def Client(version, *args, **kwargs):
|
||||
client_class = get_client_class(version)
|
||||
return client_class(*args, **kwargs)
|
||||
|
@ -4,6 +4,12 @@ Exception definitions.
|
||||
"""
|
||||
|
||||
|
||||
class UnsupportedVersion(Exception):
|
||||
"""Indicates that the user is trying to use an unsupported
|
||||
version of the API"""
|
||||
pass
|
||||
|
||||
|
||||
class CommandError(Exception):
|
||||
pass
|
||||
|
||||
|
@ -25,12 +25,12 @@ import imp
|
||||
import os
|
||||
import sys
|
||||
|
||||
from novaclient import base
|
||||
from novaclient import client
|
||||
from novaclient import exceptions as exc
|
||||
import novaclient.extension
|
||||
from novaclient.keystone import shell as shell_keystone
|
||||
from novaclient import utils
|
||||
from novaclient.v1_1 import shell as shell_v1_1
|
||||
from novaclient.keystone import shell as shell_keystone
|
||||
|
||||
|
||||
def env(*vars):
|
||||
@ -272,11 +272,11 @@ class OpenStackComputeShell(object):
|
||||
" either via --url or via "
|
||||
"env[NOVA_URL]")
|
||||
|
||||
self.cs = self.get_api_class(options.version)(user, password,
|
||||
projectid, url, insecure,
|
||||
region_name=region_name,
|
||||
endpoint_name=endpoint_name,
|
||||
extensions=self.extensions)
|
||||
self.cs = client.Client(options.version, user, password,
|
||||
projectid, url, insecure,
|
||||
region_name=region_name,
|
||||
endpoint_name=endpoint_name,
|
||||
extensions=self.extensions)
|
||||
|
||||
try:
|
||||
if not utils.isunauthenticated(args.func):
|
||||
@ -293,15 +293,6 @@ class OpenStackComputeShell(object):
|
||||
for extension in self.extensions:
|
||||
extension.run_hooks(hook_type, *args, **kwargs)
|
||||
|
||||
def get_api_class(self, version):
|
||||
try:
|
||||
return {
|
||||
"1.1": shell_v1_1.CLIENT_CLASS,
|
||||
"2": shell_v1_1.CLIENT_CLASS,
|
||||
}[version]
|
||||
except KeyError:
|
||||
return shell_v1_1.CLIENT_CLASS
|
||||
|
||||
def do_bash_completion(self, args):
|
||||
"""
|
||||
Prints all of the commands and options to stdout so that the
|
||||
|
@ -1,6 +1,6 @@
|
||||
import uuid
|
||||
|
||||
import prettytable
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
from novaclient import exceptions
|
||||
|
||||
@ -185,3 +185,10 @@ def safe_issubclass(*args):
|
||||
pass
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def import_class(import_str):
|
||||
"""Returns a class from a string including module and class."""
|
||||
mod_str, _sep, class_str = import_str.rpartition('.')
|
||||
__import__(mod_str)
|
||||
return getattr(sys.modules[mod_str], class_str)
|
||||
|
@ -20,13 +20,9 @@ import os
|
||||
|
||||
from novaclient import exceptions
|
||||
from novaclient import utils
|
||||
from novaclient.v1_1 import client
|
||||
from novaclient.v1_1 import servers
|
||||
|
||||
|
||||
CLIENT_CLASS = client.Client
|
||||
|
||||
|
||||
AUTO_KEY = object()
|
||||
|
||||
|
||||
|
26
tests/test_client.py
Normal file
26
tests/test_client.py
Normal file
@ -0,0 +1,26 @@
|
||||
|
||||
import novaclient.client
|
||||
import novaclient.v1_1.client
|
||||
from tests import utils
|
||||
|
||||
|
||||
class ClientTest(utils.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
def test_get_client_class_v2(self):
|
||||
output = novaclient.client.get_client_class('2')
|
||||
self.assertEqual(output, novaclient.v1_1.client.Client)
|
||||
|
||||
def test_get_client_class_v2_int(self):
|
||||
output = novaclient.client.get_client_class(2)
|
||||
self.assertEqual(output, novaclient.v1_1.client.Client)
|
||||
|
||||
def test_get_client_class_v1_1(self):
|
||||
output = novaclient.client.get_client_class('1.1')
|
||||
self.assertEqual(output, novaclient.v1_1.client.Client)
|
||||
|
||||
def test_get_client_class_unknown(self):
|
||||
self.assertRaises(novaclient.exceptions.UnsupportedVersion,
|
||||
novaclient.client.get_client_class, '0')
|
@ -4,6 +4,7 @@ import sys
|
||||
import tempfile
|
||||
|
||||
import novaclient.shell
|
||||
import novaclient.client
|
||||
from novaclient import exceptions
|
||||
from tests.v1_1 import fakes
|
||||
from tests import utils
|
||||
@ -24,7 +25,10 @@ class ShellTest(utils.TestCase):
|
||||
}
|
||||
|
||||
self.shell = novaclient.shell.OpenStackComputeShell()
|
||||
self.shell.get_api_class = lambda *_: fakes.FakeClient
|
||||
|
||||
#HACK(bcwaldon): replace this when we start using stubs
|
||||
self.old_get_client_class = novaclient.client.get_client_class
|
||||
novaclient.client.get_client_class = lambda *_: fakes.FakeClient
|
||||
|
||||
def tearDown(self):
|
||||
os.environ = self.old_environment
|
||||
@ -35,6 +39,9 @@ class ShellTest(utils.TestCase):
|
||||
if hasattr(self.shell, 'cs'):
|
||||
self.shell.cs.clear_callstack()
|
||||
|
||||
#HACK(bcwaldon): replace this when we start using stubs
|
||||
novaclient.client.get_client_class = self.old_get_client_class
|
||||
|
||||
def run_command(self, cmd):
|
||||
self.shell.main(cmd.split())
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user