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>
This commit is contained in:
Stephen Finucane 2020-11-17 12:33:40 +00:00
parent e2a9a9607c
commit bf834f6d75
3 changed files with 64 additions and 8 deletions

View File

@ -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)

View File

@ -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,9 @@ 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,
]
@ -292,9 +298,38 @@ 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_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,
]
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_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)

View File

@ -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.