Support to get webmks console url for server

Support 'webmks' console type when get console url
for server.
Change-Id: I1e46a42ad65bba703015ef7c57aefdc67606e87c
Closes-Bug: #1646678
This commit is contained in:
huangtianhua 2017-02-14 17:37:57 +08:00
parent edae9f0c45
commit 7f4a64e0aa
5 changed files with 51 additions and 19 deletions

View File

@ -56,12 +56,16 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
exceptions_module = exceptions exceptions_module = exceptions
supported_versions = [ NOVA_API_VERSION = '2.1'
NOVA_API_VERSION, V2_2, V2_15, V2_26
validate_versions = [
V2_2, V2_15, V2_26, V2_8
] = [ ] = [
'2.1', '2.2', '2.15', '2.26' '2.2', '2.15', '2.26', '2.8'
] ]
supported_versions = [NOVA_API_VERSION] + validate_versions
service_types = [COMPUTE] = ['compute'] service_types = [COMPUTE] = ['compute']
def _get_service_name(self): def _get_service_name(self):
@ -87,7 +91,7 @@ class NovaClientPlugin(client_plugin.ClientPlugin):
client = nc.Client(version, **args) client = nc.Client(version, **args)
# NOTE: check for microversion availability # NOTE: check for microversion availability
if version in [self.V2_2, self.V2_15, self.V2_26]: if version in self.validate_versions:
try: try:
client.versions.get_current() client.versions.get_current()
except exceptions.NotAcceptable: except exceptions.NotAcceptable:
@ -590,18 +594,24 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
The actual console url is lazily resolved on access. The actual console url is lazily resolved on access.
""" """
nc = self.client
mks_version = self.V2_8
class ConsoleUrls(collections.Mapping): class ConsoleUrls(collections.Mapping):
def __init__(self, server): def __init__(self, server):
self.console_method = server.get_console_url self.console_method = server.get_console_url
self.support_console_types = ['novnc', 'xvpvnc', self.support_console_types = ['novnc', 'xvpvnc',
'spice-html5', 'rdp-html5', 'spice-html5', 'rdp-html5',
'serial'] 'serial', 'webmks']
def __getitem__(self, key): def __getitem__(self, key):
try: try:
if key not in self.support_console_types: if key not in self.support_console_types:
raise exceptions.UnsupportedConsoleType(key) raise exceptions.UnsupportedConsoleType(key)
if key == 'webmks':
data = nc(mks_version).servers.get_console_url(
server, key)
else:
data = self.console_method(key) data = self.console_method(key)
console_data = data.get( console_data = data.get(
'remote_console', data.get('console')) 'remote_console', data.get('console'))
@ -614,6 +624,9 @@ echo -e '%s\tALL=(ALL)\tNOPASSWD: ALL' >> /etc/sudoers
url = e.message url = e.message
else: else:
raise raise
except exception.InvalidServiceVersion:
url = _('Nova service version %s is '
'unavailable') % mks_version
return url return url
def __len__(self): def __len__(self):

View File

@ -605,7 +605,7 @@ class Server(server_base.BaseServer, sh.SchedulerHintsMixin,
"can be specified as parameter to the get_attr function, " "can be specified as parameter to the get_attr function, "
"e.g. get_attr: [ <server>, console_urls, novnc ]. " "e.g. get_attr: [ <server>, console_urls, novnc ]. "
"Currently supported types are " "Currently supported types are "
"novnc, xvpvnc, spice-html5, rdp-html5, serial."), "novnc, xvpvnc, spice-html5, rdp-html5, serial and webmks."),
support_status=support.SupportStatus(version='2015.1'), support_status=support.SupportStatus(version='2015.1'),
type=attributes.Schema.MAP type=attributes.Schema.MAP
), ),

View File

@ -637,11 +637,12 @@ class KeypairConstraintTest(common.HeatTestCase):
class ConsoleUrlsTest(common.HeatTestCase): class ConsoleUrlsTest(common.HeatTestCase):
scenarios = [ scenarios = [
('novnc', dict(console_type='novnc')), ('novnc', dict(console_type='novnc', res_obj=True)),
('xvpvnc', dict(console_type='xvpvnc')), ('xvpvnc', dict(console_type='xvpvnc', res_obj=True)),
('spice', dict(console_type='spice-html5')), ('spice', dict(console_type='spice-html5', res_obj=True)),
('rdp', dict(console_type='rdp-html5')), ('rdp', dict(console_type='rdp-html5', res_obj=True)),
('serial', dict(console_type='serial')), ('serial', dict(console_type='serial', res_obj=True)),
('mks', dict(console_type='webmks', res_obj=False)),
] ]
def setUp(self): def setUp(self):
@ -650,9 +651,14 @@ class ConsoleUrlsTest(common.HeatTestCase):
con = utils.dummy_context() con = utils.dummy_context()
c = con.clients c = con.clients
self.nova_plugin = c.client_plugin('nova') self.nova_plugin = c.client_plugin('nova')
self.nova_plugin.client = lambda: self.nova_client self.patchobject(self.nova_plugin, 'client',
return_value=self.nova_client)
self.server = mock.Mock() self.server = mock.Mock()
if self.res_obj:
self.console_method = getattr(self.server, 'get_console_url') self.console_method = getattr(self.server, 'get_console_url')
else:
self.console_method = getattr(self.nova_client.servers,
'get_console_url')
def test_get_console_url(self): def test_get_console_url(self):
console = { console = {
@ -667,13 +673,20 @@ class ConsoleUrlsTest(common.HeatTestCase):
self.console_type] self.console_type]
self.assertEqual(console['console']['url'], console_url) self.assertEqual(console['console']['url'], console_url)
self._assert_console_method_called()
def _assert_console_method_called(self):
if self.console_type == 'webmks':
self.console_method.assert_called_once_with(self.server,
self.console_type)
else:
self.console_method.assert_called_once_with(self.console_type) self.console_method.assert_called_once_with(self.console_type)
def _test_get_console_url_tolerate_exception(self, msg): def _test_get_console_url_tolerate_exception(self, msg):
console_url = self.nova_plugin.get_console_urls(self.server)[ console_url = self.nova_plugin.get_console_urls(self.server)[
self.console_type] self.console_type]
self.console_method.assert_called_once_with(self.console_type) self._assert_console_method_called()
self.assertEqual(msg, console_url) self.assertEqual(msg, console_url)
def test_get_console_url_tolerate_unavailable(self): def test_get_console_url_tolerate_unavailable(self):
@ -698,7 +711,7 @@ class ConsoleUrlsTest(common.HeatTestCase):
urls = self.nova_plugin.get_console_urls(self.server) urls = self.nova_plugin.get_console_urls(self.server)
e = self.assertRaises(exc, urls.__getitem__, self.console_type) e = self.assertRaises(exc, urls.__getitem__, self.console_type)
self.assertIn('spam', encodeutils.exception_to_unicode(e)) self.assertIn('spam', encodeutils.exception_to_unicode(e))
self.console_method.assert_called_once_with(self.console_type) self._assert_console_method_called()
def test_get_console_urls_reraises_other(self): def test_get_console_urls_reraises_other(self):
exc = Exception exc = Exception
@ -706,8 +719,9 @@ class ConsoleUrlsTest(common.HeatTestCase):
urls = self.nova_plugin.get_console_urls(self.server) urls = self.nova_plugin.get_console_urls(self.server)
e = self.assertRaises(exc, urls.__getitem__, self.console_type) e = self.assertRaises(exc, urls.__getitem__, self.console_type)
self.assertIn('spam', e.args) self.assertIn('spam', e.args)
self.console_method.assert_called_once_with(self.console_type) self._assert_console_method_called()
class NovaClientPluginExtensionsTest(NovaClientPluginTestCase): class NovaClientPluginExtensionsTest(NovaClientPluginTestCase):

View File

@ -3097,7 +3097,7 @@ class ServersTest(common.HeatTestCase):
console_urls = ws._resolve_all_attributes('console_urls') console_urls = ws._resolve_all_attributes('console_urls')
self.assertIsInstance(console_urls, collections.Mapping) self.assertIsInstance(console_urls, collections.Mapping)
supported_consoles = ('novnc', 'xvpvnc', 'spice-html5', 'rdp-html5', supported_consoles = ('novnc', 'xvpvnc', 'spice-html5', 'rdp-html5',
'serial') 'serial', 'webmks')
self.assertEqual(set(supported_consoles), self.assertEqual(set(supported_consoles),
set(console_urls)) set(console_urls))

View File

@ -0,0 +1,5 @@
---
features:
- Supports to get the webmks console url for OS::Nova::Server
resource. And this requires nova api version equal or greater
than 2.8.