Add --long option and more columns to the hypervisor list command

Support --long option and more columns in output of hypervisor
list command, including 'Hypervisor Type', 'Host IP', 'State',
and 'vCPU Used', 'vCPUs', 'Memory MB Used', 'Memory MB' with
--long option.

Change-Id: I0c790c7835309dded03e230cf497168e19404537
Closes-Bug: #1637074
This commit is contained in:
Yan Xing'an 2016-10-26 23:48:58 -07:00
parent 0b63d5d586
commit 8ca1cc6370
4 changed files with 87 additions and 2 deletions

View File

@ -14,11 +14,16 @@ List hypervisors
os hypervisor list
[--matching <hostname>]
[--long]
.. option:: --matching <hostname>
Filter hypervisors using <hostname> substring
.. option:: --long
List additional fields in output
hypervisor show
---------------

View File

@ -35,14 +35,24 @@ class ListHypervisor(command.Lister):
metavar="<hostname>",
help=_("Filter hypervisors using <hostname> substring")
)
parser.add_argument(
'--long',
action='store_true',
help=_("List additional fields in output")
)
return parser
def take_action(self, parsed_args):
compute_client = self.app.client_manager.compute
columns = (
"ID",
"Hypervisor Hostname"
"Hypervisor Hostname",
"Hypervisor Type",
"Host IP",
"State"
)
if parsed_args.long:
columns += ("vCPUs Used", "vCPUs", "Memory MB Used", "Memory MB")
if parsed_args.matching:
data = compute_client.hypervisors.search(parsed_args.matching)

View File

@ -48,19 +48,63 @@ class TestHypervisorList(TestHypervisor):
self.columns = (
"ID",
"Hypervisor Hostname"
"Hypervisor Hostname",
"Hypervisor Type",
"Host IP",
"State"
)
self.columns_long = (
"ID",
"Hypervisor Hostname",
"Hypervisor Type",
"Host IP",
"State",
"vCPUs Used",
"vCPUs",
"Memory MB Used",
"Memory MB"
)
self.data = (
(
self.hypervisors[0].id,
self.hypervisors[0].hypervisor_hostname,
self.hypervisors[0].hypervisor_type,
self.hypervisors[0].host_ip,
self.hypervisors[0].state
),
(
self.hypervisors[1].id,
self.hypervisors[1].hypervisor_hostname,
self.hypervisors[1].hypervisor_type,
self.hypervisors[1].host_ip,
self.hypervisors[1].state
),
)
self.data_long = (
(
self.hypervisors[0].id,
self.hypervisors[0].hypervisor_hostname,
self.hypervisors[0].hypervisor_type,
self.hypervisors[0].host_ip,
self.hypervisors[0].state,
self.hypervisors[0].vcpus_used,
self.hypervisors[0].vcpus,
self.hypervisors[0].memory_mb_used,
self.hypervisors[0].memory_mb
),
(
self.hypervisors[1].id,
self.hypervisors[1].hypervisor_hostname,
self.hypervisors[1].hypervisor_type,
self.hypervisors[1].host_ip,
self.hypervisors[1].state,
self.hypervisors[1].vcpus_used,
self.hypervisors[1].vcpus,
self.hypervisors[1].memory_mb_used,
self.hypervisors[1].memory_mb
),
)
# Get the command object to test
self.cmd = hypervisor.ListHypervisor(self.app, None)
@ -93,6 +137,9 @@ class TestHypervisorList(TestHypervisor):
(
self.hypervisors[0].id,
self.hypervisors[0].hypervisor_hostname,
self.hypervisors[1].hypervisor_type,
self.hypervisors[1].host_ip,
self.hypervisors[1].state,
),
)
@ -123,6 +170,24 @@ class TestHypervisorList(TestHypervisor):
self.cmd.take_action,
parsed_args)
def test_hypervisor_list_long_option(self):
arglist = [
'--long',
]
verifylist = [
('long', True),
]
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
# In base command class Lister in cliff, abstract method take_action()
# returns a tuple containing the column names and an iterable
# containing the data to be listed.
columns, data = self.cmd.take_action(parsed_args)
self.hypervisors_mock.list.assert_called_with()
self.assertEqual(self.columns_long, columns)
self.assertEqual(self.data_long, tuple(data))
class TestHypervisorShow(TestHypervisor):

View File

@ -0,0 +1,5 @@
---
features:
- |
Add ``--long`` option and more columns to the ``hypervisor list`` command.
[Bug `1637074 <https://bugs.launchpad.net/bugs/1637074>`_]