summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZuul <zuul@review.opendev.org>2023-02-22 12:32:18 +0000
committerGerrit Code Review <review@openstack.org>2023-02-22 12:32:18 +0000
commita7e091c32967a6d61723f5a89e24171ce28588ce (patch)
treee5e097da98b5a85426c563fc6c11e6b95f9090ee
parent14dff075ff19bb950157825d743a8052dd1f98cb (diff)
parentecc6aeeede68db65cb888390dd9299a4cfad6630 (diff)
downloadpython-openstackclient-a7e091c32967a6d61723f5a89e24171ce28588ce.tar.gz
Merge "Update 'host list' and 'host show' command to use sdk"
-rw-r--r--openstackclient/compute/v2/host.py60
-rw-r--r--openstackclient/tests/unit/compute/v2/test_host.py105
-rw-r--r--releasenotes/notes/migrate-host-list-show-to-sdk-9b80cd9b4196ab01.yaml4
3 files changed, 111 insertions, 58 deletions
diff --git a/openstackclient/compute/v2/host.py b/openstackclient/compute/v2/host.py
index 07c92a8c..e6dd3a6f 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="<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."
+ )
+
+ # 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))
+ 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 4e1b5ad1..ec91b37a 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 00000000..085670c9
--- /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.