From ecc6aeeede68db65cb888390dd9299a4cfad6630 Mon Sep 17 00:00:00 2001 From: Harsh Mutha Date: Tue, 6 Dec 2022 21:44:23 -0500 Subject: [PATCH] Update 'host list' and 'host show' command to use sdk Change-Id: I3813ff604ba46112ebd358509ea4f28ee38ca3ee --- openstackclient/compute/v2/host.py | 60 +++++++--- .../tests/unit/compute/v2/test_host.py | 105 +++++++++++------- ...ost-list-show-to-sdk-9b80cd9b4196ab01.yaml | 4 + 3 files changed, 111 insertions(+), 58 deletions(-) create mode 100644 releasenotes/notes/migrate-host-list-show-to-sdk-9b80cd9b4196ab01.yaml diff --git a/openstackclient/compute/v2/host.py b/openstackclient/compute/v2/host.py index 07c92a8c16..e6dd3a6f82 100644 --- a/openstackclient/compute/v2/host.py +++ b/openstackclient/compute/v2/host.py @@ -22,10 +22,10 @@ from openstackclient.i18n import _ class ListHost(command.Lister): - _description = _("List hosts") + _description = _("DEPRECATED: List hosts") def get_parser(self, prog_name): - parser = super(ListHost, self).get_parser(prog_name) + parser = super().get_parser(prog_name) parser.add_argument( "--zone", metavar="", @@ -34,17 +34,33 @@ class ListHost(command.Lister): return parser def take_action(self, parsed_args): - compute_client = self.app.client_manager.compute + compute_client = self.app.client_manager.sdk_connection.compute columns = ( "Host Name", "Service", "Zone" ) - data = compute_client.api.host_list(parsed_args.zone) - return (columns, - (utils.get_dict_properties( - s, columns, - ) for s in data)) + + self.log.warning( + "API has been deprecated. " + "Please consider using 'hypervisor list' instead." + ) + + # 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): @@ -102,10 +118,10 @@ class SetHost(command.Command): class ShowHost(command.Lister): - _description = _("Display host details") + _description = _("DEPRECATED: Display host details") def get_parser(self, prog_name): - parser = super(ShowHost, self).get_parser(prog_name) + parser = super().get_parser(prog_name) parser.add_argument( "host", metavar="", @@ -114,7 +130,7 @@ class ShowHost(command.Lister): return parser def take_action(self, parsed_args): - compute_client = self.app.client_manager.compute + compute_client = self.app.client_manager.sdk_connection.compute columns = ( "Host", "Project", @@ -123,9 +139,21 @@ class ShowHost(command.Lister): "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, - (utils.get_dict_properties( - s, columns, - ) for s in data)) + # doing this since openstacksdk has decided not to support this + # deprecated command + resources = compute_client.get( + '/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) diff --git a/openstackclient/tests/unit/compute/v2/test_host.py b/openstackclient/tests/unit/compute/v2/test_host.py index 4e1b5ad15b..ec91b37ab8 100644 --- a/openstackclient/tests/unit/compute/v2/test_host.py +++ b/openstackclient/tests/unit/compute/v2/test_host.py @@ -17,6 +17,7 @@ from unittest import mock from openstackclient.compute.v2 import host 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 @@ -26,7 +27,10 @@ class TestHost(compute_fakes.TestComputev2): super(TestHost, self).setUp() # 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( @@ -34,27 +38,29 @@ class TestHost(compute_fakes.TestComputev2): ) class TestHostList(TestHost): - host = compute_fakes.FakeHost.create_one_host() - - columns = ( - 'Host Name', - 'Service', - 'Zone', - ) - - data = [( - host['host_name'], - host['service'], - host['zone'], - )] + _host = compute_fakes.FakeHost.create_one_host() def setUp(self): 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) def test_host_list_no_option(self, h_mock): - h_mock.return_value = [self.host] + h_mock.return_value = [self._host] arglist = [] verifylist = [] @@ -62,24 +68,24 @@ class TestHostList(TestHost): 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.data, list(data)) def test_host_list_with_option(self, h_mock): - h_mock.return_value = [self.host] + h_mock.return_value = [self._host] arglist = [ - '--zone', self.host['zone'], + '--zone', self._host['zone'], ] verifylist = [ - ('zone', self.host['zone']), + ('zone', self._host['zone']), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) 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.data, list(data)) @@ -141,31 +147,43 @@ class TestHostSet(TestHost): ) class TestHostShow(TestHost): - 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'], - )] + _host = compute_fakes.FakeHost.create_one_host() def setUp(self): 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) 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 = [] verifylist = [] @@ -174,18 +192,21 @@ class TestHostShow(TestHost): self.cmd, arglist, verifylist) def test_host_show_with_option(self, h_mock): - h_mock.return_value = [self.host] + h_mock.return_value = [self._host] arglist = [ - self.host['host_name'], + self._host['host_name'], ] verifylist = [ - ('host', self.host['host_name']), + ('host', self._host['host_name']), ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) 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.data, list(data)) diff --git a/releasenotes/notes/migrate-host-list-show-to-sdk-9b80cd9b4196ab01.yaml b/releasenotes/notes/migrate-host-list-show-to-sdk-9b80cd9b4196ab01.yaml new file mode 100644 index 0000000000..085670c930 --- /dev/null +++ b/releasenotes/notes/migrate-host-list-show-to-sdk-9b80cd9b4196ab01.yaml @@ -0,0 +1,4 @@ +--- +features: + - | + The ``host list`` and ``host show`` commands have been migrated to SDK.