From e009bec2208d2f38896160a1ea677a3014f851e5 Mon Sep 17 00:00:00 2001 From: Dave Wilde Date: Sun, 28 Apr 2013 07:37:33 -0500 Subject: [PATCH] Adds extended status fields to nova list The nova list command now includes 'Task State' and 'Power State' fields to bring parity with the dashboard. * Add helper function _translate_extended_states() to convert extended states to human Fixes: bug #954750 Change-Id: I564b7f88e9e2524d8e4ffe21a51608c5e3b23d2d --- novaclient/v1_1/shell.py | 36 +++++++++++++++++++++++++++++++++++- tests/test_shell.py | 8 ++++---- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/novaclient/v1_1/shell.py b/novaclient/v1_1/shell.py index cb638896a..138b5f7eb 100644 --- a/novaclient/v1_1/shell.py +++ b/novaclient/v1_1/shell.py @@ -380,6 +380,31 @@ def _translate_keys(collection, convert): setattr(item, to_key, item._info[from_key]) +def _translate_extended_states(collection): + power_states = [ + 'NOSTATE', # 0x00 + 'Running', # 0x01 + '', # 0x02 + 'Paused', # 0x03 + 'Shutdown', # 0x04 + '', # 0x05 + 'Crashed', # 0x06 + 'Suspended' # 0x07 + ] + + for item in collection: + try: + setattr(item, 'power_state', + power_states[getattr(item, 'power_state')] + ) + except AttributeError: + setattr(item, 'power_state', "N/A") + try: + getattr(item, 'task_state') + except AttributeError: + setattr(item, 'task_state', "N/A") + + def _translate_flavor_keys(collection): _translate_keys(collection, [('ram', 'memory_mb')]) @@ -964,12 +989,21 @@ def do_list(cs, args): convert = [('OS-EXT-SRV-ATTR:host', 'host'), ('OS-EXT-STS:task_state', 'task_state'), ('OS-EXT-SRV-ATTR:instance_name', 'instance_name'), + ('OS-EXT-STS:power_state', 'power_state'), ('hostId', 'host_id')] _translate_keys(servers, convert) + _translate_extended_states(servers) if field_titles: columns = [id_col] + field_titles else: - columns = [id_col, 'Name', 'Status', 'Networks'] + columns = [ + id_col, + 'Name', + 'Status', + 'Task State', + 'Power State', + 'Networks' + ] formatters['Networks'] = utils._format_servers_list_networks utils.print_list(servers, columns, formatters, sortby_index=1) diff --git a/tests/test_shell.py b/tests/test_shell.py index ee9a5d7cc..ae78815d2 100644 --- a/tests/test_shell.py +++ b/tests/test_shell.py @@ -151,10 +151,10 @@ class ShellTest(utils.TestCase): self.make_env(exclude='OS_PASSWORD') stdout, stderr = self.shell('list') self.assertEqual((stdout + stderr), - '+----+------+--------+----------+\n' - '| ID | Name | Status | Networks |\n' - '+----+------+--------+----------+\n' - '+----+------+--------+----------+\n') + '+----+------+--------+------------+-------------+----------+\n' + '| ID | Name | Status | Task State | Power State | Networks |\n' + '+----+------+--------+------------+-------------+----------+\n' + '+----+------+--------+------------+-------------+----------+\n') @mock.patch('sys.stdin', side_effect=mock.MagicMock) @mock.patch('getpass.getpass', side_effect=EOFError)