From 3a92c6e0b60321fff869c864fb167381066b5585 Mon Sep 17 00:00:00 2001 From: Sean McGinnis Date: Fri, 24 May 2019 10:26:29 -0500 Subject: [PATCH] Enable JSON results output This takes a lighter weight approach to allowing output to be formatted as JSON than switching over to using cliff. This adds the --json flag argument that will format the printed results in a much more compact JSON format. Change-Id: I54f3f347b0d7da4385ffcbfa5da61c83657f390c Signed-off-by: Sean McGinnis --- oslo_upgradecheck/upgradecheck.py | 58 +++++++++++++------ releasenotes/notes/.placeholder | 0 .../notes/json-output-78a9e19588b7b1e1.yaml | 7 +++ 3 files changed, 48 insertions(+), 17 deletions(-) delete mode 100644 releasenotes/notes/.placeholder create mode 100644 releasenotes/notes/json-output-78a9e19588b7b1e1.yaml diff --git a/oslo_upgradecheck/upgradecheck.py b/oslo_upgradecheck/upgradecheck.py index a9aa56f..f3cfe1b 100644 --- a/oslo_upgradecheck/upgradecheck.py +++ b/oslo_upgradecheck/upgradecheck.py @@ -13,6 +13,7 @@ # License for the specific language governing permissions and limitations # under the License. +import json import sys import textwrap import traceback @@ -24,6 +25,8 @@ import six from oslo_upgradecheck._i18n import _ +CONF = None + class Code(enum.IntEnum): """Status codes for the upgrade check command""" @@ -92,6 +95,7 @@ class UpgradeCommands(object): :returns: Code """ + global CONF return_code = Code.SUCCESS # This is a list if 2-item tuples for the check name and it's results. check_results = [] @@ -121,22 +125,36 @@ class UpgradeCommands(object): # NOTE(bnemec): We use six.text_type on the translated string to # force immediate translation if lazy translation is in use. # See lp1801761 for details. - t = prettytable.PrettyTable([six.text_type(self.display_title)], - hrules=prettytable.ALL) - t.align = 'l' - for name, result in check_results: - cell = ( - _('Check: %(name)s\n' - 'Result: %(result)s\n' - 'Details: %(details)s') % - { - 'name': name, - 'result': UPGRADE_CHECK_MSG_MAP[result.code], - 'details': self._get_details(result), - } - ) - t.add_row([cell]) - print(t) + + # Since registering opts can be overridden by consuming code, we can't + # assume that our locally defined option exists. + if (hasattr(CONF, 'command') and hasattr(CONF.command, 'json') and + CONF.command.json): + output = {'name': six.text_type(self.display_title), 'checks': []} + for name, result in check_results: + output['checks'].append( + {'check': name, + 'result': result.code, + 'details': result.details} + ) + print(json.dumps(output)) + else: + t = prettytable.PrettyTable([six.text_type(self.display_title)], + hrules=prettytable.ALL) + t.align = 'l' + for name, result in check_results: + cell = ( + _('Check: %(name)s\n' + 'Result: %(result)s\n' + 'Details: %(details)s') % + { + 'name': name, + 'result': UPGRADE_CHECK_MSG_MAP[result.code], + 'details': self._get_details(result), + } + ) + t.add_row([cell]) + print(t) return return_code @@ -154,6 +172,11 @@ def register_cli_options(conf, upgrade_command): upgrade_action = subparsers.add_parser('upgrade') upgrade_action.add_argument('check') upgrade_action.set_defaults(action_fn=upgrade_command.check) + upgrade_action.add_argument( + '--json', + action='store_true', + help='Output the results in JSON format. Default is to print ' + 'results in human readable table format.') opt = cfg.SubCommandOpt('command', handler=add_parsers) conf.register_cli_opt(opt) @@ -194,6 +217,7 @@ def main(conf, project, upgrade_command, the search behavior in oslo.config. """ + global CONF register_cli_options(conf, upgrade_command) conf( @@ -201,5 +225,5 @@ def main(conf, project, upgrade_command, project=project, default_config_files=default_config_files, ) - + CONF = conf return run(conf) diff --git a/releasenotes/notes/.placeholder b/releasenotes/notes/.placeholder deleted file mode 100644 index e69de29..0000000 diff --git a/releasenotes/notes/json-output-78a9e19588b7b1e1.yaml b/releasenotes/notes/json-output-78a9e19588b7b1e1.yaml new file mode 100644 index 0000000..fd6bf8f --- /dev/null +++ b/releasenotes/notes/json-output-78a9e19588b7b1e1.yaml @@ -0,0 +1,7 @@ +--- +features: + - | + oslo.upgradecheck now supports the command line flag ``--json`` to have + the upgrade check results output in a compact, machine readable JSON + format. The default output without this flag remains a human readable + table of the results.