summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Finucane <sfinucan@redhat.com>2020-11-17 12:33:40 +0000
committerStephen Finucane <sfinucan@redhat.com>2021-01-06 12:02:25 +0000
commitbf834f6d7582009672fc65aa983f314684dc4380 (patch)
tree13928f1e94e35652b031d98e63eed61e0179b727
parente2a9a9607cc84c0afc2fc5524681a3adebdc68ec (diff)
downloadpython-openstackclient-bf834f6d7582009672fc65aa983f314684dc4380.tar.gz
compute: Fix 'hypervisor show -f yaml' output
The 'cpu_info' field returned by the 'os-hypervisors' API is an object and should be formatted as such. However, this is complicated by the fact that the object in this field is stringified until microversion 2.28 and is only returned as an actual object on later microversions. Handle the conversion from the string for older microversions and display things correctly for all releases. Change-Id: Ide31466cbb9e89c96d6bd542fe039ab5ed1fac1f Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
-rw-r--r--openstackclient/compute/v2/hypervisor.py24
-rw-r--r--openstackclient/tests/unit/compute/v2/test_hypervisor.py45
-rw-r--r--releasenotes/notes/improved-server-output-6965b664f6abda8d.yaml3
3 files changed, 64 insertions, 8 deletions
diff --git a/openstackclient/compute/v2/hypervisor.py b/openstackclient/compute/v2/hypervisor.py
index 7f110028..8fdb6698 100644
--- a/openstackclient/compute/v2/hypervisor.py
+++ b/openstackclient/compute/v2/hypervisor.py
@@ -15,9 +15,12 @@
"""Hypervisor action implementations"""
+import json
import re
+from novaclient import api_versions
from novaclient import exceptions as nova_exceptions
+from osc_lib.cli import format_columns
from osc_lib.command import command
from osc_lib import utils
@@ -86,8 +89,8 @@ class ShowHypervisor(command.ShowOne):
if aggregates:
# Hypervisors in nova cells are prefixed by "<cell>@"
if "@" in hypervisor['service']['host']:
- cell, service_host = hypervisor['service']['host'].split('@',
- 1)
+ cell, service_host = hypervisor['service']['host'].split(
+ '@', 1)
else:
cell = None
service_host = hypervisor['service']['host']
@@ -125,4 +128,19 @@ class ShowHypervisor(command.ShowOne):
hypervisor["service_host"] = hypervisor["service"]["host"]
del hypervisor["service"]
- return zip(*sorted(hypervisor.items()))
+ if compute_client.api_version < api_versions.APIVersion('2.28'):
+ # microversion 2.28 transformed this to a JSON blob rather than a
+ # string; on earlier fields, do this manually
+ if hypervisor['cpu_info']:
+ hypervisor['cpu_info'] = json.loads(hypervisor['cpu_info'])
+ else:
+ hypervisor['cpu_info'] = {}
+
+ columns = tuple(sorted(hypervisor))
+ data = utils.get_dict_properties(
+ hypervisor, columns,
+ formatters={
+ 'cpu_info': format_columns.DictColumn,
+ })
+
+ return (columns, data)
diff --git a/openstackclient/tests/unit/compute/v2/test_hypervisor.py b/openstackclient/tests/unit/compute/v2/test_hypervisor.py
index 7200d04e..518109c6 100644
--- a/openstackclient/tests/unit/compute/v2/test_hypervisor.py
+++ b/openstackclient/tests/unit/compute/v2/test_hypervisor.py
@@ -14,8 +14,11 @@
#
import copy
+import json
+from novaclient import api_versions
from novaclient import exceptions as nova_exceptions
+from osc_lib.cli import format_columns
from osc_lib import exceptions
from openstackclient.compute.v2 import hypervisor
@@ -247,7 +250,7 @@ class TestHypervisorShow(TestHypervisor):
)
self.data = (
[],
- {'aaa': 'aaa'},
+ format_columns.DictColumn({'aaa': 'aaa'}),
0,
50,
50,
@@ -278,6 +281,35 @@ class TestHypervisorShow(TestHypervisor):
self.cmd = hypervisor.ShowHypervisor(self.app, None)
def test_hypervisor_show(self):
+ self.app.client_manager.compute.api_version = \
+ api_versions.APIVersion('2.28')
+
+ arglist = [
+ self.hypervisor.hypervisor_hostname,
+ ]
+ verifylist = [
+ ('hypervisor', self.hypervisor.hypervisor_hostname),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ # In base command class ShowOne in cliff, abstract method take_action()
+ # returns a two-part tuple with a tuple of column names and a tuple of
+ # data to be shown.
+ columns, data = self.cmd.take_action(parsed_args)
+
+ self.assertEqual(self.columns, columns)
+ self.assertItemsEqual(self.data, data)
+
+ def test_hypervisor_show_pre_v228(self):
+ self.app.client_manager.compute.api_version = \
+ api_versions.APIVersion('2.27')
+
+ # before microversion 2.28, nova returned a stringified version of this
+ # field
+ self.hypervisor._info['cpu_info'] = json.dumps(
+ self.hypervisor._info['cpu_info'])
+ self.hypervisors_mock.get.return_value = self.hypervisor
+
arglist = [
self.hypervisor.hypervisor_hostname,
]
@@ -292,9 +324,12 @@ class TestHypervisorShow(TestHypervisor):
columns, data = self.cmd.take_action(parsed_args)
self.assertEqual(self.columns, columns)
- self.assertEqual(self.data, data)
+ self.assertItemsEqual(self.data, data)
+
+ def test_hypervisor_show_uptime_not_implemented(self):
+ self.app.client_manager.compute.api_version = \
+ api_versions.APIVersion('2.28')
- def test_hyprvisor_show_uptime_not_implemented(self):
arglist = [
self.hypervisor.hypervisor_hostname,
]
@@ -337,7 +372,7 @@ class TestHypervisorShow(TestHypervisor):
)
expected_data = (
[],
- {'aaa': 'aaa'},
+ format_columns.DictColumn({'aaa': 'aaa'}),
0,
50,
50,
@@ -361,4 +396,4 @@ class TestHypervisorShow(TestHypervisor):
)
self.assertEqual(expected_columns, columns)
- self.assertEqual(expected_data, data)
+ self.assertItemsEqual(expected_data, data)
diff --git a/releasenotes/notes/improved-server-output-6965b664f6abda8d.yaml b/releasenotes/notes/improved-server-output-6965b664f6abda8d.yaml
index 5dc2980c..305e915e 100644
--- a/releasenotes/notes/improved-server-output-6965b664f6abda8d.yaml
+++ b/releasenotes/notes/improved-server-output-6965b664f6abda8d.yaml
@@ -18,3 +18,6 @@ fixes:
The ``policies`` (or ``policy``, on newer microversions) and ``members``
fields of the ``server group list`` and ``server group show`` commands
will now be rendered correctly as lists.
+ - |
+ The ``cpu_info`` field of the ``hypervisor show`` output is now
+ correctly decoded and output as an object.