Skip to content

Commit fc32b0d

Browse files
committed
Improve "server list" command to have the same output as "nova list".
"nova list" will also output "Task State" and "Power State" by default. This patch improves "server list" command to have the same columns, but not by default. These two columns will be output if --long is added. The power state is an int, so also adds a formatter helper function to translate it to human readable string, just as "Networks" does. Change-Id: I0530a910bec03835839a5ba7687c66d5643338f3
1 parent 6e1ad73 commit fc32b0d

1 file changed

Lines changed: 43 additions & 2 deletions

File tree

openstackclient/compute/v2/server.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,29 @@ def _format_servers_list_networks(networks):
5656
return '; '.join(output)
5757

5858

59+
def _format_servers_list_power_state(state):
60+
"""Return a formatted string of a server's power state
61+
62+
:param state: the power state number of a server
63+
:rtype: a string mapped to the power state number
64+
"""
65+
power_states = [
66+
'NOSTATE', # 0x00
67+
'Running', # 0x01
68+
'', # 0x02
69+
'Paused', # 0x03
70+
'Shutdown', # 0x04
71+
'', # 0x05
72+
'Crashed', # 0x06
73+
'Suspended' # 0x07
74+
]
75+
76+
try:
77+
return power_states[state]
78+
except Exception:
79+
return 'N/A'
80+
81+
5982
def _get_ip_address(addresses, address_type, ip_address_family):
6083
# Old style addresses
6184
if address_type in addresses:
@@ -762,6 +785,8 @@ def take_action(self, parsed_args):
762785
'ID',
763786
'Name',
764787
'Status',
788+
'OS-EXT-STS:task_state',
789+
'OS-EXT-STS:power_state',
765790
'Networks',
766791
'OS-EXT-AZ:availability_zone',
767792
'OS-EXT-SRV-ATTR:host',
@@ -771,25 +796,41 @@ def take_action(self, parsed_args):
771796
'ID',
772797
'Name',
773798
'Status',
799+
'Task State',
800+
'Power State',
774801
'Networks',
775802
'Availability Zone',
776803
'Host',
777804
'Properties',
778805
)
779806
mixed_case_fields = [
807+
'OS-EXT-STS:task_state',
808+
'OS-EXT-STS:power_state',
780809
'OS-EXT-AZ:availability_zone',
781810
'OS-EXT-SRV-ATTR:host',
782811
]
783812
else:
784-
columns = ('ID', 'Name', 'Status', 'Networks')
785-
column_headers = columns
813+
columns = (
814+
'ID',
815+
'Name',
816+
'Status',
817+
'Networks',
818+
)
819+
column_headers = (
820+
'ID',
821+
'Name',
822+
'Status',
823+
'Networks',
824+
)
786825
mixed_case_fields = []
787826
data = compute_client.servers.list(search_opts=search_opts)
788827
return (column_headers,
789828
(utils.get_item_properties(
790829
s, columns,
791830
mixed_case_fields=mixed_case_fields,
792831
formatters={
832+
'OS-EXT-STS:power_state':
833+
_format_servers_list_power_state,
793834
'Networks': _format_servers_list_networks,
794835
'Metadata': utils.format_dict,
795836
},

0 commit comments

Comments
 (0)