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

Change-Id: I3813ff604ba46112ebd358509ea4f28ee38ca3ee
This commit is contained in:
Harsh Mutha 2022-12-06 21:44:23 -05:00 committed by Stephen Finucane
parent b2c9a4cd40
commit ecc6aeeede
3 changed files with 111 additions and 58 deletions

View File

@ -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="<zone>",
@ -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="<host>",
@ -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)

View File

@ -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))

View File

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