Remove 'get_osc_show_columns_for_sdk_resource' duplicates

There were a number of 'get_osc_show_columns_for_sdk_resource' defined
in-tree. However, osc-lib has provided this method for some time (since
2.2.0, June 2020 [1] - our minimum version is currently 2.3.0) so
there's no need to provide our own copies. Remove them.

[1] https://github.com/openstack/osc-lib/commit/29a0c5a5

Change-Id: I25695f4f9a379dd691b7eaa1e3247164668ae77e
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2021-10-08 18:06:11 +01:00
parent a797c9d2a3
commit 30612bf622
30 changed files with 37 additions and 256 deletions

View File

@ -1,58 +0,0 @@
# 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.
def get_osc_show_columns_for_sdk_resource(
sdk_resource,
osc_column_map,
invisible_columns=None
):
"""Get and filter the display and attribute columns for an SDK resource.
Common utility function for preparing the output of an OSC show command.
Some of the columns may need to get renamed, others made invisible.
:param sdk_resource: An SDK resource
:param osc_column_map: A hash of mappings for display column names
:param invisible_columns: A list of invisible column names
:returns: Two tuples containing the names of the display and attribute
columns
"""
if getattr(sdk_resource, 'allow_get', None) is not None:
resource_dict = sdk_resource.to_dict(
body=True, headers=False, ignore_none=False)
else:
resource_dict = sdk_resource
# Build the OSC column names to display for the SDK resource.
attr_map = {}
display_columns = list(resource_dict.keys())
invisible_columns = [] if invisible_columns is None else invisible_columns
for col_name in invisible_columns:
if col_name in display_columns:
display_columns.remove(col_name)
for sdk_attr, osc_attr in osc_column_map.items():
if sdk_attr in display_columns:
attr_map[osc_attr] = sdk_attr
display_columns.remove(sdk_attr)
if osc_attr not in display_columns:
display_columns.append(osc_attr)
sorted_display_columns = sorted(display_columns)
# Build the SDK attribute names for the OSC column names.
attr_columns = []
for column in sorted_display_columns:
new_column = attr_map[column] if column in attr_map else column
attr_columns.append(new_column)
return tuple(sorted_display_columns), tuple(attr_columns)

View File

@ -28,7 +28,6 @@ from osc_lib.cli import parseractions
from osc_lib.command import command
from osc_lib import utils
from openstackclient.common import sdk_utils
from openstackclient.i18n import _
if os.name == "nt":
@ -48,15 +47,17 @@ LOG = logging.getLogger(__name__)
def _get_columns(item):
# Trick sdk_utils to return URI attribute
column_map = {
'is_protected': 'protected',
'owner_id': 'owner'
}
hidden_columns = ['location', 'checksum',
'copy_from', 'created_at', 'status', 'updated_at']
return sdk_utils.get_osc_show_columns_for_sdk_resource(
item.to_dict(), column_map, hidden_columns)
hidden_columns = [
'location', 'checksum', 'copy_from', 'created_at', 'status',
'updated_at',
]
return utils.get_osc_show_columns_for_sdk_resource(
item.to_dict(), column_map, hidden_columns,
)
_formatters = {

View File

@ -31,7 +31,6 @@ from osc_lib import exceptions
from osc_lib import utils
from openstackclient.common import progressbar
from openstackclient.common import sdk_utils
from openstackclient.i18n import _
from openstackclient.identity import common
@ -99,13 +98,13 @@ _formatters = {
def _get_member_columns(item):
# Trick sdk_utils to return URI attribute
column_map = {
'image_id': 'image_id'
}
hidden_columns = ['id', 'location', 'name']
return sdk_utils.get_osc_show_columns_for_sdk_resource(
item.to_dict(), column_map, hidden_columns)
return utils.get_osc_show_columns_for_sdk_resource(
item.to_dict(), column_map, hidden_columns,
)
def get_data_file(args):

View File

@ -1,63 +0,0 @@
# 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.
import munch
def get_osc_show_columns_for_sdk_resource(
sdk_resource,
osc_column_map,
invisible_columns=None
):
"""Get and filter the display and attribute columns for an SDK resource.
Common utility function for preparing the output of an OSC show command.
Some of the columns may need to get renamed, others made invisible.
:param sdk_resource: An SDK resource
:param osc_column_map: A hash of mappings for display column names
:param invisible_columns: A list of invisible column names
:returns: Two tuples containing the names of the display and attribute
columns
"""
if getattr(sdk_resource, 'allow_get', None) is not None:
resource_dict = sdk_resource.to_dict(
body=True, headers=False, ignore_none=False)
else:
resource_dict = sdk_resource
# Build the OSC column names to display for the SDK resource.
attr_map = {}
display_columns = list(resource_dict.keys())
for col_name in display_columns:
if isinstance(resource_dict[col_name], munch.Munch):
display_columns.remove(col_name)
invisible_columns = [] if invisible_columns is None else invisible_columns
for col_name in invisible_columns:
if col_name in display_columns:
display_columns.remove(col_name)
for sdk_attr, osc_attr in osc_column_map.items():
if sdk_attr in display_columns:
attr_map[osc_attr] = sdk_attr
display_columns.remove(sdk_attr)
if osc_attr not in display_columns:
display_columns.append(osc_attr)
sorted_display_columns = sorted(display_columns)
# Build the SDK attribute names for the OSC column names.
attr_columns = []
for column in sorted_display_columns:
new_column = attr_map[column] if column in attr_map else column
attr_columns.append(new_column)
return tuple(sorted_display_columns), tuple(attr_columns)

View File

@ -23,8 +23,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -33,7 +31,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _format_addresses(addresses):

View File

@ -22,8 +22,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -33,7 +31,7 @@ def _get_columns(item):
'is_shared': 'shared',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -19,7 +19,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
_formatters = {
@ -31,7 +30,7 @@ def _get_network_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_columns(item):

View File

@ -12,6 +12,7 @@
#
"""Floating IP Port Forwarding action implementations"""
import logging
from osc_lib.command import command
@ -20,8 +21,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -30,7 +29,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
class CreateFloatingIPPortForwarding(command.ShowOne,

View File

@ -19,7 +19,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import sdk_utils
_formatters = {
'subnet_ip_availability': format_columns.ListDictColumn,
@ -30,7 +29,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
# TODO(ankur-gupta-f): Use the SDK resource mapped attribute names once

View File

@ -20,15 +20,13 @@ from osc_lib import exceptions
from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
def _get_columns(item):
column_map = {}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client, parsed_args):

View File

@ -21,7 +21,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
class AdminStateColumn(cliff_columns.FormattableColumn):
@ -62,14 +61,14 @@ def _get_columns_network(item):
'tenant_id': 'project_id',
'tags': 'tags',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_columns_compute(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs_network(client_manager, parsed_args):

View File

@ -22,8 +22,6 @@ from osc_lib import exceptions
from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -52,7 +50,7 @@ def _get_network_columns(item):
'is_admin_state_up': 'admin_state_up',
'is_alive': 'alive',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
class AddNetworkToAgent(command.Command):

View File

@ -20,7 +20,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -29,7 +28,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _format_check_resource_columns():

View File

@ -22,8 +22,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -34,7 +32,7 @@ def _get_columns(item):
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -20,8 +20,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -32,7 +30,7 @@ def _get_columns(item):
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -22,7 +22,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -32,7 +31,7 @@ def _get_columns(item):
'is_shared': 'shared',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -22,7 +22,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -31,7 +30,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -22,8 +22,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -33,7 +31,7 @@ def _get_columns(item):
'is_shared': 'shared',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -21,8 +21,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import common
from openstackclient.network import sdk_utils
RULE_TYPE_BANDWIDTH_LIMIT = 'bandwidth-limit'
RULE_TYPE_DSCP_MARKING = 'dscp-marking'
@ -51,7 +49,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _check_type_parameters(attrs, type, is_create):

View File

@ -17,7 +17,6 @@ from osc_lib.command import command
from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import sdk_utils
def _get_columns(item):
@ -26,7 +25,7 @@ def _get_columns(item):
"drivers": "drivers",
}
invisible_columns = ["id", "name"]
return sdk_utils.get_osc_show_columns_for_sdk_resource(
return utils.get_osc_show_columns_for_sdk_resource(
item, column_map, invisible_columns)

View File

@ -22,8 +22,6 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -33,7 +31,7 @@ def _get_columns(item):
'target_tenant': 'target_project_id',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_attrs(client_manager, parsed_args):

View File

@ -21,14 +21,12 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
def _get_columns(item):
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, {})
return utils.get_osc_show_columns_for_sdk_resource(item, {})
class CreateNetworkSegment(command.ShowOne,

View File

@ -26,14 +26,13 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
def _get_columns(item):
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, {})
return utils.get_osc_show_columns_for_sdk_resource(item, {})
def _get_ranges(item):

View File

@ -29,8 +29,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -67,7 +65,7 @@ def _get_columns(item):
'is_port_security_enabled': 'port_security_enabled',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
class JSONKeyValueAction(argparse.Action):

View File

@ -28,8 +28,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -83,7 +81,7 @@ def _get_columns(item):
if item.is_distributed is None:
invisible_columns.append('is_distributed')
column_map.pop('is_distributed')
return sdk_utils.get_osc_show_columns_for_sdk_resource(
return utils.get_osc_show_columns_for_sdk_resource(
item, column_map, invisible_columns)

View File

@ -23,7 +23,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
from openstackclient.network import utils as network_utils
@ -90,7 +89,7 @@ def _get_columns(item):
'security_group_rules': 'rules',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
# TODO(abhiraut): Use the SDK resource mapped attribute names once the

View File

@ -23,10 +23,8 @@ from osc_lib import utils
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
from openstackclient.network import utils as network_utils
LOG = logging.getLogger(__name__)
@ -76,7 +74,7 @@ def _get_columns(item):
column_map = {
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _convert_to_lowercase(string):

View File

@ -27,8 +27,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -143,7 +141,7 @@ def _get_columns(item):
}
# Do not show this column when displaying a subnet
invisible_columns = ['use_default_subnet_pool', 'prefix_length']
return sdk_utils.get_osc_show_columns_for_sdk_resource(
return utils.get_osc_show_columns_for_sdk_resource(
item,
column_map,
invisible_columns=invisible_columns

View File

@ -25,7 +25,6 @@ from osc_lib.utils import tags as _tag
from openstackclient.i18n import _
from openstackclient.identity import common as identity_common
from openstackclient.network import common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__)
@ -39,7 +38,7 @@ def _get_columns(item):
'minimum_prefix_length': 'min_prefixlen',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
return utils.get_osc_show_columns_for_sdk_resource(item, column_map)
_formatters = {

View File

@ -1,59 +0,0 @@
# 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 openstackclient.network import sdk_utils
from openstackclient.tests.unit import utils as tests_utils
class TestSDKUtils(tests_utils.TestCase):
def setUp(self):
super(TestSDKUtils, self).setUp()
def _test_get_osc_show_columns_for_sdk_resource(
self, sdk_resource, column_map,
expected_display_columns, expected_attr_columns):
display_columns, attr_columns = \
sdk_utils.get_osc_show_columns_for_sdk_resource(
sdk_resource, column_map)
self.assertEqual(expected_display_columns, display_columns)
self.assertEqual(expected_attr_columns, attr_columns)
def test_get_osc_show_columns_for_sdk_resource_empty(self):
self._test_get_osc_show_columns_for_sdk_resource(
{}, {}, tuple(), tuple())
def test_get_osc_show_columns_for_sdk_resource_empty_map(self):
self._test_get_osc_show_columns_for_sdk_resource(
{'foo': 'foo1'}, {},
('foo',), ('foo',))
def test_get_osc_show_columns_for_sdk_resource_empty_data(self):
self._test_get_osc_show_columns_for_sdk_resource(
{}, {'foo': 'foo_map'},
('foo_map',), ('foo_map',))
def test_get_osc_show_columns_for_sdk_resource_map(self):
self._test_get_osc_show_columns_for_sdk_resource(
{'foo': 'foo1'}, {'foo': 'foo_map'},
('foo_map',), ('foo',))
def test_get_osc_show_columns_for_sdk_resource_map_dup(self):
self._test_get_osc_show_columns_for_sdk_resource(
{'foo': 'foo1', 'foo_map': 'foo1'}, {'foo': 'foo_map'},
('foo_map',), ('foo',))
def test_get_osc_show_columns_for_sdk_resource_map_full(self):
self._test_get_osc_show_columns_for_sdk_resource(
{'foo': 'foo1', 'bar': 'bar1'},
{'foo': 'foo_map', 'new': 'bar'},
('bar', 'foo_map'), ('bar', 'foo'))