Fix error returned by ironic --json node-validate
The option --json used the attribute "_info" to print the JSON response from Ironic API. Some objects do not have this attribute, which results in an error. This patch stops relying on "_info" and uses the fields names to build the JSON array. Change-Id: I6c0ff73a38cda21e147b3216d0c0d0539cc867bb Closes-Bug: #1558688
This commit is contained in:
parent
4c1432afb3
commit
87a072933e
|
@ -153,10 +153,21 @@ def print_list(objs, fields, formatters=None, sortby_index=0,
|
|||
fields.
|
||||
:param json_flag: print the list as JSON instead of table
|
||||
"""
|
||||
if json_flag:
|
||||
print(json.dumps([o._info for o in objs], indent=4,
|
||||
separators=(',', ': ')))
|
||||
return
|
||||
def _get_name_and_data(field):
|
||||
if field in formatters:
|
||||
# The value of the field has to be modified.
|
||||
# For example, it can be used to add extra fields.
|
||||
return (field, formatters[field](o))
|
||||
|
||||
field_name = field.replace(' ', '_')
|
||||
if field not in mixed_case_fields:
|
||||
field_name = field.lower()
|
||||
if isinstance(o, dict):
|
||||
data = o.get(field_name, '')
|
||||
else:
|
||||
data = getattr(o, field_name, '')
|
||||
return (field_name, data)
|
||||
|
||||
formatters = formatters or {}
|
||||
mixed_case_fields = mixed_case_fields or []
|
||||
field_labels = field_labels or fields
|
||||
|
@ -172,24 +183,20 @@ def print_list(objs, fields, formatters=None, sortby_index=0,
|
|||
pt = prettytable.PrettyTable(field_labels)
|
||||
pt.align = 'l'
|
||||
|
||||
json_array = []
|
||||
|
||||
for o in objs:
|
||||
row = []
|
||||
for field in fields:
|
||||
if field in formatters:
|
||||
row.append(formatters[field](o))
|
||||
else:
|
||||
if field in mixed_case_fields:
|
||||
field_name = field.replace(' ', '_')
|
||||
else:
|
||||
field_name = field.lower().replace(' ', '_')
|
||||
if isinstance(o, dict):
|
||||
data = o.get(field_name, '')
|
||||
else:
|
||||
data = getattr(o, field_name, '')
|
||||
row.append(data)
|
||||
pt.add_row(row)
|
||||
row.append(_get_name_and_data(field))
|
||||
if json_flag:
|
||||
json_array.append(dict(row))
|
||||
else:
|
||||
pt.add_row([r[1] for r in row])
|
||||
|
||||
if six.PY3:
|
||||
if json_flag:
|
||||
print(json.dumps(json_array, indent=4, separators=(',', ': ')))
|
||||
elif six.PY3:
|
||||
print(encodeutils.safe_encode(pt.get_string(**kwargs)).decode())
|
||||
else:
|
||||
print(encodeutils.safe_encode(pt.get_string(**kwargs)))
|
||||
|
|
|
@ -435,7 +435,6 @@ class _FakeResult(object):
|
|||
def __init__(self, name, value):
|
||||
self.name = name
|
||||
self.value = value
|
||||
self._info = {"name": name, "value": value}
|
||||
|
||||
|
||||
class PrintResultTestCase(test_base.BaseTestCase):
|
||||
|
@ -598,7 +597,9 @@ class PrintResultStringTestCase(test_base.BaseTestCase):
|
|||
out = sys.stdout.getvalue()
|
||||
sys.stdout.close()
|
||||
sys.stdout = orig
|
||||
self.assertEqual([objs[0]._info], json.loads(out))
|
||||
|
||||
expected = [{"name": "k1", "value": 1}]
|
||||
self.assertEqual(expected, json.loads(out))
|
||||
|
||||
def test_print_dict_string(self):
|
||||
orig = sys.stdout
|
||||
|
|
Loading…
Reference in New Issue