formatters: Cast columns before outputting

Libraries like 'novaclient' return non-primitive types for many
operations. These are typically wrappers around primitive types, e.g.
'OrderedDict', but they all break YAML output formatting since PyYAML is
only compatible with primitive types when operating in safe mode.

The solution is rather simple: simply cast the values for these to their
expected format.

Change-Id: Ib7db6a25f2352a013cb2ce603e60ca48b6cc70e6
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Story: #2008320
Task: #41217
This commit is contained in:
Stephen Finucane 2020-11-04 11:18:47 +00:00
parent c710e0cc5b
commit 4bb55ce75d
2 changed files with 22 additions and 0 deletions

View File

@ -26,6 +26,9 @@ class DictColumn(columns.FormattableColumn):
def human_readable(self):
return utils.format_dict(self._value)
def machine_readable(self):
return dict(self._value or {})
class DictListColumn(columns.FormattableColumn):
"""Format column for dict, key is string, value is list"""
@ -33,6 +36,9 @@ class DictListColumn(columns.FormattableColumn):
def human_readable(self):
return utils.format_dict_of_list(self._value)
def machine_readable(self):
return dict(self._value or {})
class ListColumn(columns.FormattableColumn):
"""Format column for list content"""

View File

@ -13,6 +13,8 @@
# under the License.
#
import collections
from osc_lib.cli import format_columns
from osc_lib.tests import utils
@ -28,6 +30,13 @@ class TestDictColumn(utils.TestCase):
self.assertEqual(dict_content, col.machine_readable())
self.assertEqual("key1='value1', key2='value2'", col.human_readable())
def test_complex_object(self):
"""Non-primitive objects should be converted to a dict."""
dict_content = collections.OrderedDict(
[('key1', 'value1'), ('key2', 'value2')])
col = format_columns.DictColumn(dict_content)
self.assertIsInstance(col._value, dict)
class TestDictListColumn(utils.TestCase):
@ -40,6 +49,13 @@ class TestDictListColumn(utils.TestCase):
'public=172.24.4.6, 2001:db8::8',
col.human_readable())
def test_complex_object(self):
"""Non-primitive objects should be converted to a dict."""
dict_content = collections.OrderedDict(
[('key1', ['value1']), ('key2', ['value2'])])
col = format_columns.DictListColumn(dict_content)
self.assertIsInstance(col._value, dict)
class TestListColumn(utils.TestCase):