From 85713447b1ae4c9e6f5f154e915bfd9488cbcdf1 Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Wed, 7 May 2025 14:29:14 +0900 Subject: [PATCH] osc: Use FormattableColumn for formatter ... because usage of legacy format function was deprecated 8 years ago and was recently removed from osc-lib[1]. [1] 0e7dada02b825b2f97ac3eb68544bdd438a3a099 Change-Id: I62521846b4aaf8036cd83e3f5a38d9eed6c520a7 --- heatclient/osc/v1/build_info.py | 6 +-- heatclient/osc/v1/common.py | 42 +++++++++++++++++++ heatclient/osc/v1/event.py | 5 ++- heatclient/osc/v1/stack.py | 23 +++++----- heatclient/osc/v1/template.py | 11 +++-- .../tests/unit/osc/v1/test_build_info.py | 8 ++-- heatclient/tests/unit/osc/v1/test_template.py | 6 ++- 7 files changed, 76 insertions(+), 25 deletions(-) create mode 100644 heatclient/osc/v1/common.py diff --git a/heatclient/osc/v1/build_info.py b/heatclient/osc/v1/build_info.py index cd4700a2..1ab39dfb 100644 --- a/heatclient/osc/v1/build_info.py +++ b/heatclient/osc/v1/build_info.py @@ -18,7 +18,7 @@ import logging from osc_lib.command import command from osc_lib import utils -from heatclient.common import utils as heat_utils +from heatclient.osc.v1 import common class BuildInfo(command.ShowOne): @@ -37,8 +37,8 @@ class BuildInfo(command.ShowOne): result = heat_client.build_info.build_info() formatters = { - 'api': heat_utils.json_formatter, - 'engine': heat_utils.json_formatter, + 'api': common.JsonColumn, + 'engine': common.JsonColumn, } columns = sorted(list(result.keys())) return columns, utils.get_dict_properties(result, columns, diff --git a/heatclient/osc/v1/common.py b/heatclient/osc/v1/common.py new file mode 100644 index 00000000..1b5ac701 --- /dev/null +++ b/heatclient/osc/v1/common.py @@ -0,0 +1,42 @@ +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +from cliff import columns + +from heatclient.common import utils as heat_utils + + +class LinkColumn(columns.FormattableColumn): + def human_readable(self): + return heat_utils.link_formatter(self._value) + + +class JsonColumn(columns.FormattableColumn): + def human_readable(self): + return heat_utils.json_formatter(self._value) + + +class YamlColumn(columns.FormattableColumn): + def human_readable(self): + return heat_utils.yaml_formatter(self._value) + + +class TextWrapColumn(columns.FormattableColumn): + def human_readable(self): + return heat_utils.text_wrap_formatter(self._value) + + +class NewlineListColumn(columns.FormattableColumn): + def human_readable(self): + return heat_utils.newline_list_formatter(self._value) diff --git a/heatclient/osc/v1/event.py b/heatclient/osc/v1/event.py index 8f25ea34..017803a4 100644 --- a/heatclient/osc/v1/event.py +++ b/heatclient/osc/v1/event.py @@ -23,6 +23,7 @@ from heatclient._i18n import _ from heatclient.common import event_utils from heatclient.common import utils as heat_utils from heatclient import exc +from heatclient.osc.v1 import common class ShowEvent(command.ShowOne): @@ -69,8 +70,8 @@ class ShowEvent(command.ShowOne): raise exc.CommandError(str(ex)) formatters = { - 'links': heat_utils.link_formatter, - 'resource_properties': heat_utils.json_formatter + 'links': common.LinkColumn, + 'resource_properties': common.JsonColumn } columns = [] diff --git a/heatclient/osc/v1/stack.py b/heatclient/osc/v1/stack.py index e12ed55f..61bc9db3 100644 --- a/heatclient/osc/v1/stack.py +++ b/heatclient/osc/v1/stack.py @@ -31,6 +31,7 @@ from heatclient.common import http from heatclient.common import template_utils from heatclient.common import utils as heat_utils from heatclient import exc as heat_exc +from heatclient.osc.v1 import common class CreateStack(command.ShowOne): @@ -180,13 +181,13 @@ class CreateStack(command.ShowOne): stack = client.stacks.preview(**fields) formatters = { - 'description': heat_utils.text_wrap_formatter, - 'template_description': heat_utils.text_wrap_formatter, - 'stack_status_reason': heat_utils.text_wrap_formatter, - 'parameters': heat_utils.json_formatter, - 'outputs': heat_utils.json_formatter, - 'resources': heat_utils.json_formatter, - 'links': heat_utils.link_formatter, + 'description': common.TextWrapColumn, + 'template_description': common.TextWrapColumn, + 'stack_status_reason': common.TextWrapColumn, + 'parameters': common.JsonColumn, + 'outputs': common.JsonColumn, + 'resources': common.JsonColumn, + 'links': common.LinkColumn, } columns = [] @@ -386,7 +387,7 @@ class UpdateStack(command.ShowOne): 'resource_identity'] columns = sorted(changes.get("resource_changes", {}).keys()) - data = [heat_utils.json_formatter(changes["resource_changes"][key]) + data = [common.JsonColumn(changes["resource_changes"][key]) for key in columns] return columns, data @@ -478,9 +479,9 @@ def _show_stack(heat_client, stack_id, format='', short=False, formatters = {} complex_formatter = None if format in 'table': - complex_formatter = heat_utils.yaml_formatter + complex_formatter = common.YamlColumn elif format in ('shell', 'value', 'html'): - complex_formatter = heat_utils.json_formatter + complex_formatter = common.JsonColumn if complex_formatter: formatters['parameters'] = complex_formatter formatters['outputs'] = complex_formatter @@ -1019,7 +1020,7 @@ class OutputShowStack(command.ShowOne): values = [] for output in outputs: columns.append(output['output_key']) - values.append(heat_utils.json_formatter(output)) + values.append(common.JsonColumn(output)) return columns, values diff --git a/heatclient/osc/v1/template.py b/heatclient/osc/v1/template.py index ef29790c..41419a1a 100644 --- a/heatclient/osc/v1/template.py +++ b/heatclient/osc/v1/template.py @@ -14,6 +14,7 @@ import logging +from cliff import columns from osc_lib.command import command from osc_lib import utils @@ -25,6 +26,11 @@ from heatclient.common import utils as heat_utils from heatclient import exc +class ListColumn(columns.FormattableColumn): + def human_readable(self): + return ','.join(self._value) + + class VersionList(command.Lister): """List the available template versions.""" @@ -39,11 +45,8 @@ class VersionList(command.Lister): try: versions[1].aliases - def format_alias(aliases): - return ','.join(aliases) - fields = ['Version', 'Type', 'Aliases'] - formatters = {'Aliases': format_alias} + formatters = {'Aliases': ListColumn} except AttributeError: fields = ['Version', 'Type'] formatters = None diff --git a/heatclient/tests/unit/osc/v1/test_build_info.py b/heatclient/tests/unit/osc/v1/test_build_info.py index 40893d21..a43fd8a1 100644 --- a/heatclient/tests/unit/osc/v1/test_build_info.py +++ b/heatclient/tests/unit/osc/v1/test_build_info.py @@ -14,6 +14,7 @@ from unittest import mock from heatclient.osc.v1 import build_info as osc_build_info +from heatclient.osc.v1 import common from heatclient.tests.unit.osc.v1 import fakes as orchestration_fakes @@ -39,6 +40,7 @@ class TestBuildInfo(orchestration_fakes.TestOrchestrationv1): columns, data = self.cmd.take_action(parsed_args) self.mock_client.build_info.build_info.assert_called_with() self.assertEqual(['api', 'engine'], columns) - self.assertEqual(['{\n "revision": "{api_build_revision}"\n}', - '{\n "revision": "{engine_build_revision}"\n}'], - list(data)) + self.assertEqual([ + common.JsonColumn({"revision": "{api_build_revision}"}), + common.JsonColumn({"revision": "{engine_build_revision}"}) + ], list(data)) diff --git a/heatclient/tests/unit/osc/v1/test_template.py b/heatclient/tests/unit/osc/v1/test_template.py index 0694510a..0d4fb9ab 100644 --- a/heatclient/tests/unit/osc/v1/test_template.py +++ b/heatclient/tests/unit/osc/v1/test_template.py @@ -57,8 +57,10 @@ class TestTemplateVersionList(TestTemplate): columns, data = self.cmd.take_action(parsed_args) self.assertEqual(['Version', 'Type', 'Aliases'], columns) - self.assertEqual([('HOT123', 'hot', 'releasex'), - ('CFN456', 'cfn', 'releasey')], list(data)) + self.assertEqual([ + ('HOT123', 'hot', template.ListColumn(['releasex'])), + ('CFN456', 'cfn', template.ListColumn(['releasey'])) + ], list(data)) class TestTemplateFunctionList(TestTemplate):