Skip to content

Commit 8ca1cc6

Browse files
committed
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
1 parent 0b63d5d commit 8ca1cc6

4 files changed

Lines changed: 87 additions & 2 deletions

File tree

doc/source/command-objects/hypervisor.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@ List hypervisors
1414
1515
os hypervisor list
1616
[--matching <hostname>]
17+
[--long]
1718
1819
.. option:: --matching <hostname>
1920

2021
Filter hypervisors using <hostname> substring
2122

23+
.. option:: --long
24+
25+
List additional fields in output
26+
2227
hypervisor show
2328
---------------
2429

openstackclient/compute/v2/hypervisor.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,24 @@ def get_parser(self, prog_name):
3535
metavar="<hostname>",
3636
help=_("Filter hypervisors using <hostname> substring")
3737
)
38+
parser.add_argument(
39+
'--long',
40+
action='store_true',
41+
help=_("List additional fields in output")
42+
)
3843
return parser
3944

4045
def take_action(self, parsed_args):
4146
compute_client = self.app.client_manager.compute
4247
columns = (
4348
"ID",
44-
"Hypervisor Hostname"
49+
"Hypervisor Hostname",
50+
"Hypervisor Type",
51+
"Host IP",
52+
"State"
4553
)
54+
if parsed_args.long:
55+
columns += ("vCPUs Used", "vCPUs", "Memory MB Used", "Memory MB")
4656

4757
if parsed_args.matching:
4858
data = compute_client.hypervisors.search(parsed_args.matching)

openstackclient/tests/unit/compute/v2/test_hypervisor.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,63 @@ def setUp(self):
4848

4949
self.columns = (
5050
"ID",
51-
"Hypervisor Hostname"
51+
"Hypervisor Hostname",
52+
"Hypervisor Type",
53+
"Host IP",
54+
"State"
55+
)
56+
self.columns_long = (
57+
"ID",
58+
"Hypervisor Hostname",
59+
"Hypervisor Type",
60+
"Host IP",
61+
"State",
62+
"vCPUs Used",
63+
"vCPUs",
64+
"Memory MB Used",
65+
"Memory MB"
5266
)
5367
self.data = (
5468
(
5569
self.hypervisors[0].id,
5670
self.hypervisors[0].hypervisor_hostname,
71+
self.hypervisors[0].hypervisor_type,
72+
self.hypervisors[0].host_ip,
73+
self.hypervisors[0].state
5774
),
5875
(
5976
self.hypervisors[1].id,
6077
self.hypervisors[1].hypervisor_hostname,
78+
self.hypervisors[1].hypervisor_type,
79+
self.hypervisors[1].host_ip,
80+
self.hypervisors[1].state
6181
),
6282
)
6383

84+
self.data_long = (
85+
(
86+
self.hypervisors[0].id,
87+
self.hypervisors[0].hypervisor_hostname,
88+
self.hypervisors[0].hypervisor_type,
89+
self.hypervisors[0].host_ip,
90+
self.hypervisors[0].state,
91+
self.hypervisors[0].vcpus_used,
92+
self.hypervisors[0].vcpus,
93+
self.hypervisors[0].memory_mb_used,
94+
self.hypervisors[0].memory_mb
95+
),
96+
(
97+
self.hypervisors[1].id,
98+
self.hypervisors[1].hypervisor_hostname,
99+
self.hypervisors[1].hypervisor_type,
100+
self.hypervisors[1].host_ip,
101+
self.hypervisors[1].state,
102+
self.hypervisors[1].vcpus_used,
103+
self.hypervisors[1].vcpus,
104+
self.hypervisors[1].memory_mb_used,
105+
self.hypervisors[1].memory_mb
106+
),
107+
)
64108
# Get the command object to test
65109
self.cmd = hypervisor.ListHypervisor(self.app, None)
66110

@@ -93,6 +137,9 @@ def test_hypervisor_list_matching_option_found(self):
93137
(
94138
self.hypervisors[0].id,
95139
self.hypervisors[0].hypervisor_hostname,
140+
self.hypervisors[1].hypervisor_type,
141+
self.hypervisors[1].host_ip,
142+
self.hypervisors[1].state,
96143
),
97144
)
98145

@@ -123,6 +170,24 @@ def test_hypervisor_list_matching_option_not_found(self):
123170
self.cmd.take_action,
124171
parsed_args)
125172

173+
def test_hypervisor_list_long_option(self):
174+
arglist = [
175+
'--long',
176+
]
177+
verifylist = [
178+
('long', True),
179+
]
180+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
181+
182+
# In base command class Lister in cliff, abstract method take_action()
183+
# returns a tuple containing the column names and an iterable
184+
# containing the data to be listed.
185+
columns, data = self.cmd.take_action(parsed_args)
186+
187+
self.hypervisors_mock.list.assert_called_with()
188+
self.assertEqual(self.columns_long, columns)
189+
self.assertEqual(self.data_long, tuple(data))
190+
126191

127192
class TestHypervisorShow(TestHypervisor):
128193

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Add ``--long`` option and more columns to the ``hypervisor list`` command.
5+
[Bug `1637074 <https://bugs.launchpad.net/bugs/1637074>`_]

0 commit comments

Comments
 (0)