Switch console url show operations to SDK

Switch from using novaclient to SDK for openstack console url show operation.

Depends-On: https://review.opendev.org/756286
Change-Id: Ibe247825148788c549c2c1e991aae92338cdf557
This commit is contained in:
Artem Goncharov 2020-09-11 14:31:18 +02:00
parent 66f5fa18d4
commit e05d39abb5
4 changed files with 53 additions and 74 deletions

View File

@ -5,7 +5,7 @@ asn1crypto==0.23.0
bandit==1.1.0 bandit==1.1.0
cachetools==2.0.0 cachetools==2.0.0
cffi==1.14.0 cffi==1.14.0
cliff==2.8.0 cliff==3.4.0
cmd2==0.8.0 cmd2==0.8.0
contextlib2==0.4.0 contextlib2==0.4.0
coverage==4.0 coverage==4.0
@ -48,7 +48,7 @@ netifaces==0.10.4
openstacksdk==0.48.0 openstacksdk==0.48.0
os-service-types==1.7.0 os-service-types==1.7.0
os-testr==1.0.0 os-testr==1.0.0
osc-lib==2.0.0 osc-lib==2.2.0
osc-placement==1.7.0 osc-placement==1.7.0
oslo.concurrency==3.26.0 oslo.concurrency==3.26.0
oslo.config==5.2.0 oslo.config==5.2.0

View File

@ -22,6 +22,15 @@ from osc_lib import utils
from openstackclient.i18n import _ from openstackclient.i18n import _
def _get_console_columns(item):
# To maintain backwards compatibility we need to rename sdk props to
# whatever OSC was using before
column_map = {}
hidden_columns = ['id', 'links', 'location', 'name']
return utils.get_osc_show_columns_for_sdk_resource(
item, column_map, hidden_columns)
class ShowConsoleLog(command.Command): class ShowConsoleLog(command.Command):
_description = _("Show server's console output") _description = _("Show server's console output")
@ -119,21 +128,15 @@ class ShowConsoleURL(command.ShowOne):
return parser return parser
def take_action(self, parsed_args): def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute compute_client = self.app.client_manager.sdk_connection.compute
server = utils.find_resource( server = compute_client.find_server(
compute_client.servers,
parsed_args.server, parsed_args.server,
) ignore_missing=False)
data = server.get_console_url(parsed_args.url_type) data = compute_client.create_console(server.id,
if not data: console_type=parsed_args.url_type)
return ({}, {})
info = {} display_columns, columns = _get_console_columns(data)
# NOTE(Rui Chen): Return 'remote_console' in compute microversion API data = utils.get_dict_properties(data, columns)
# 2.6 and later, return 'console' in compute
# microversion API from 2.0 to 2.5, do compatibility return (display_columns, data)
# handle for different microversion API.
console_data = data.get('remote_console', data.get('console'))
info.update(console_data)
return zip(*sorted(info.items()))

View File

@ -32,9 +32,6 @@ class TestConsole(compute_fakes.TestComputev2):
self.sdk_client.find_server = mock.Mock() self.sdk_client.find_server = mock.Mock()
self.sdk_client.get_server_console_output = mock.Mock() self.sdk_client.get_server_console_output = mock.Mock()
self.servers_mock = self.app.client_manager.compute.servers
self.servers_mock.reset_mock()
class TestConsoleLog(TestConsole): class TestConsoleLog(TestConsole):
_server = compute_fakes.FakeServer.create_one_server() _server = compute_fakes.FakeServer.create_one_server()
@ -107,18 +104,16 @@ class TestConsoleLog(TestConsole):
class TestConsoleUrlShow(TestConsole): class TestConsoleUrlShow(TestConsole):
_server = compute_fakes.FakeServer.create_one_server()
def setUp(self): def setUp(self):
super(TestConsoleUrlShow, self).setUp() super(TestConsoleUrlShow, self).setUp()
fake_console_data = {'remote_console': {'url': 'http://localhost', self.sdk_client.find_server.return_value = self._server
fake_console_data = {'url': 'http://localhost',
'protocol': 'fake_protocol', 'protocol': 'fake_protocol',
'type': 'fake_type'}} 'type': 'fake_type'}
methods = { self.sdk_client.create_console = mock.Mock(
'get_console_url': fake_console_data return_value=fake_console_data)
}
self.fake_server = compute_fakes.FakeServer.create_one_server(
methods=methods)
self.servers_mock.get.return_value = self.fake_server
self.columns = ( self.columns = (
'protocol', 'protocol',
@ -126,9 +121,9 @@ class TestConsoleUrlShow(TestConsole):
'url', 'url',
) )
self.data = ( self.data = (
fake_console_data['remote_console']['protocol'], fake_console_data['protocol'],
fake_console_data['remote_console']['type'], fake_console_data['type'],
fake_console_data['remote_console']['url'] fake_console_data['url']
) )
self.cmd = console.ShowConsoleURL(self.app, None) self.cmd = console.ShowConsoleURL(self.app, None)
@ -143,7 +138,9 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with('novnc') self.sdk_client.create_console.assert_called_once_with(
self._server.id,
console_type='novnc')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
@ -158,7 +155,9 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with('novnc') self.sdk_client.create_console.assert_called_once_with(
self._server.id,
console_type='novnc')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
@ -173,7 +172,9 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with('xvpvnc') self.sdk_client.create_console.assert_called_once_with(
self._server.id,
console_type='xvpvnc')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
@ -188,41 +189,12 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with( self.sdk_client.create_console.assert_called_once_with(
'spice-html5') self._server.id,
console_type='spice-html5')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
def test_console_url_show_compatible(self):
methods = {
'get_console_url': {'console': {'url': 'http://localhost',
'type': 'fake_type'}},
}
old_fake_server = compute_fakes.FakeServer.create_one_server(
methods=methods)
old_columns = (
'type',
'url',
)
old_data = (
methods['get_console_url']['console']['type'],
methods['get_console_url']['console']['url']
)
arglist = [
'foo_vm',
]
verifylist = [
('url_type', 'novnc'),
('server', 'foo_vm'),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
with mock.patch.object(self.servers_mock, 'get',
return_value=old_fake_server):
columns, data = self.cmd.take_action(parsed_args)
old_fake_server.get_console_url.assert_called_once_with('novnc')
self.assertEqual(old_columns, columns)
self.assertEqual(old_data, data)
def test_console_url_show_with_rdp(self): def test_console_url_show_with_rdp(self):
arglist = [ arglist = [
'--rdp', '--rdp',
@ -234,8 +206,9 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with( self.sdk_client.create_console.assert_called_once_with(
'rdp-html5') self._server.id,
console_type='rdp-html5')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
@ -250,8 +223,9 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with( self.sdk_client.create_console.assert_called_once_with(
'serial') self._server.id,
console_type='serial')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)
@ -266,6 +240,8 @@ class TestConsoleUrlShow(TestConsole):
] ]
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.fake_server.get_console_url.assert_called_once_with('webmks') self.sdk_client.create_console.assert_called_once_with(
self._server.id,
console_type='webmks')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, data) self.assertEqual(self.data, data)

View File

@ -3,10 +3,10 @@
# process, which may cause wedges in the gate later. # process, which may cause wedges in the gate later.
pbr!=2.1.0,>=2.0.0 # Apache-2.0 pbr!=2.1.0,>=2.0.0 # Apache-2.0
cliff!=2.9.0,>=2.8.0 # Apache-2.0 cliff>=3.4.0 # Apache-2.0
iso8601>=0.1.11 # MIT iso8601>=0.1.11 # MIT
openstacksdk>=0.48.0 # Apache-2.0 openstacksdk>=0.48.0 # Apache-2.0
osc-lib>=2.0.0 # Apache-2.0 osc-lib>=2.2.0 # Apache-2.0
oslo.i18n>=3.15.3 # Apache-2.0 oslo.i18n>=3.15.3 # Apache-2.0
python-keystoneclient>=3.22.0 # Apache-2.0 python-keystoneclient>=3.22.0 # Apache-2.0
python-novaclient>=15.1.0 # Apache-2.0 python-novaclient>=15.1.0 # Apache-2.0