Merge "Update 'host list' and 'host show' command to use sdk"

This commit is contained in:
Zuul 2023-02-22 12:32:18 +00:00 committed by Gerrit Code Review
commit a7e091c329
3 changed files with 111 additions and 58 deletions
openstackclient
compute/v2
tests/unit/compute/v2
releasenotes/notes

@ -22,10 +22,10 @@ from openstackclient.i18n import _
class ListHost(command.Lister): class ListHost(command.Lister):
_description = _("List hosts") _description = _("DEPRECATED: List hosts")
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ListHost, self).get_parser(prog_name) parser = super().get_parser(prog_name)
parser.add_argument( parser.add_argument(
"--zone", "--zone",
metavar="<zone>", metavar="<zone>",
@ -34,17 +34,33 @@ class ListHost(command.Lister):
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
columns = ( columns = (
"Host Name", "Host Name",
"Service", "Service",
"Zone" "Zone"
) )
data = compute_client.api.host_list(parsed_args.zone)
return (columns, self.log.warning(
(utils.get_dict_properties( "API has been deprecated. "
s, columns, "Please consider using 'hypervisor list' instead."
) for s in data)) )
# doing this since openstacksdk has decided not to support this
# deprecated command
hosts = compute_client.get(
'/os-hosts', microversion='2.1'
).json().get('hosts')
if parsed_args.zone is not None:
filtered_hosts = []
for host in hosts:
if host['zone'] == parsed_args.zone:
filtered_hosts.append(host)
hosts = filtered_hosts
return columns, (utils.get_dict_properties(s, columns) for s in hosts)
class SetHost(command.Command): class SetHost(command.Command):
@ -102,10 +118,10 @@ class SetHost(command.Command):
class ShowHost(command.Lister): class ShowHost(command.Lister):
_description = _("Display host details") _description = _("DEPRECATED: Display host details")
def get_parser(self, prog_name): def get_parser(self, prog_name):
parser = super(ShowHost, self).get_parser(prog_name) parser = super().get_parser(prog_name)
parser.add_argument( parser.add_argument(
"host", "host",
metavar="<host>", metavar="<host>",
@ -114,7 +130,7 @@ class ShowHost(command.Lister):
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
columns = ( columns = (
"Host", "Host",
"Project", "Project",
@ -123,9 +139,21 @@ class ShowHost(command.Lister):
"Disk GB" "Disk GB"
) )
data = compute_client.api.host_show(parsed_args.host) self.log.warning(
"API has been deprecated. "
"Please consider using 'hypervisor show' instead."
)
return (columns, # doing this since openstacksdk has decided not to support this
(utils.get_dict_properties( # deprecated command
s, columns, resources = compute_client.get(
) for s in data)) '/os-hosts/' + parsed_args.host,
microversion='2.1'
).json().get('host')
data = []
if resources is not None:
for resource in resources:
data.append(resource['resource'])
return columns, (utils.get_dict_properties(s, columns) for s in data)

@ -17,6 +17,7 @@ from unittest import mock
from openstackclient.compute.v2 import host from openstackclient.compute.v2 import host
from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes from openstackclient.tests.unit.compute.v2 import fakes as compute_fakes
from openstackclient.tests.unit import fakes
from openstackclient.tests.unit import utils as tests_utils from openstackclient.tests.unit import utils as tests_utils
@ -26,7 +27,10 @@ class TestHost(compute_fakes.TestComputev2):
super(TestHost, self).setUp() super(TestHost, self).setUp()
# Get a shortcut to the compute client # Get a shortcut to the compute client
self.compute = self.app.client_manager.compute self.app.client_manager.sdk_connection = mock.Mock()
self.app.client_manager.sdk_connection.compute = mock.Mock()
self.sdk_client = self.app.client_manager.sdk_connection.compute
self.sdk_client.get = mock.Mock()
@mock.patch( @mock.patch(
@ -34,27 +38,29 @@ class TestHost(compute_fakes.TestComputev2):
) )
class TestHostList(TestHost): class TestHostList(TestHost):
host = compute_fakes.FakeHost.create_one_host() _host = compute_fakes.FakeHost.create_one_host()
columns = (
'Host Name',
'Service',
'Zone',
)
data = [(
host['host_name'],
host['service'],
host['zone'],
)]
def setUp(self): def setUp(self):
super(TestHostList, self).setUp() super(TestHostList, self).setUp()
self.sdk_client.get.return_value = fakes.FakeResponse(
data={'hosts': [self._host]}
)
self.columns = (
'Host Name', 'Service', 'Zone'
)
self.data = [(
self._host['host_name'],
self._host['service'],
self._host['zone'],
)]
self.cmd = host.ListHost(self.app, None) self.cmd = host.ListHost(self.app, None)
def test_host_list_no_option(self, h_mock): def test_host_list_no_option(self, h_mock):
h_mock.return_value = [self.host] h_mock.return_value = [self._host]
arglist = [] arglist = []
verifylist = [] verifylist = []
@ -62,24 +68,24 @@ class TestHostList(TestHost):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
h_mock.assert_called_with(None) self.sdk_client.get.assert_called_with('/os-hosts', microversion='2.1')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
def test_host_list_with_option(self, h_mock): def test_host_list_with_option(self, h_mock):
h_mock.return_value = [self.host] h_mock.return_value = [self._host]
arglist = [ arglist = [
'--zone', self.host['zone'], '--zone', self._host['zone'],
] ]
verifylist = [ verifylist = [
('zone', self.host['zone']), ('zone', self._host['zone']),
] ]
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)
h_mock.assert_called_with(self.host['zone']) self.sdk_client.get.assert_called_with('/os-hosts', microversion='2.1')
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -141,31 +147,43 @@ class TestHostSet(TestHost):
) )
class TestHostShow(TestHost): class TestHostShow(TestHost):
host = compute_fakes.FakeHost.create_one_host() _host = compute_fakes.FakeHost.create_one_host()
columns = (
'Host',
'Project',
'CPU',
'Memory MB',
'Disk GB',
)
data = [(
host['host'],
host['project'],
host['cpu'],
host['memory_mb'],
host['disk_gb'],
)]
def setUp(self): def setUp(self):
super(TestHostShow, self).setUp() super(TestHostShow, self).setUp()
output_data = {"resource": {
"host": self._host['host'],
"project": self._host['project'],
"cpu": self._host['cpu'],
"memory_mb": self._host['memory_mb'],
"disk_gb": self._host['disk_gb']
}}
self.sdk_client.get.return_value = fakes.FakeResponse(
data={'host': [output_data]}
)
self.columns = (
'Host',
'Project',
'CPU',
'Memory MB',
'Disk GB',
)
self.data = [(
self._host['host'],
self._host['project'],
self._host['cpu'],
self._host['memory_mb'],
self._host['disk_gb'],
)]
self.cmd = host.ShowHost(self.app, None) self.cmd = host.ShowHost(self.app, None)
def test_host_show_no_option(self, h_mock): def test_host_show_no_option(self, h_mock):
h_mock.host_show.return_value = [self.host] h_mock.host_show.return_value = [self._host]
arglist = [] arglist = []
verifylist = [] verifylist = []
@ -174,18 +192,21 @@ class TestHostShow(TestHost):
self.cmd, arglist, verifylist) self.cmd, arglist, verifylist)
def test_host_show_with_option(self, h_mock): def test_host_show_with_option(self, h_mock):
h_mock.return_value = [self.host] h_mock.return_value = [self._host]
arglist = [ arglist = [
self.host['host_name'], self._host['host_name'],
] ]
verifylist = [ verifylist = [
('host', self.host['host_name']), ('host', self._host['host_name']),
] ]
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)
h_mock.assert_called_with(self.host['host_name']) self.sdk_client.get.assert_called_with(
'/os-hosts/' + self._host['host_name'],
microversion='2.1'
)
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))

@ -0,0 +1,4 @@
---
features:
- |
The ``host list`` and ``host show`` commands have been migrated to SDK.