From 7832ea357c29fcf8b1e1ad0d29cede3637893ac6 Mon Sep 17 00:00:00 2001 From: Abhishek Raut Date: Sat, 19 Nov 2016 04:52:19 -0800 Subject: [PATCH] SDK Refactor: Prepare port commands Prepare the OSC "port" commands for the SDK refactor. See [1] for details. Also fixed a typo in the UT. [1] https://etherpad.openstack.org/p/osc-network-command-sdk-support Change-Id: I0e37d6c04f3d8e81fdfd50ac26eea9b5a5fb2ff9 Partially-Implements: blueprint network-command-sdk-support --- openstackclient/network/v2/port.py | 52 +++++++++++-------- .../tests/unit/network/v2/fakes.py | 5 +- .../tests/unit/network/v2/test_port.py | 2 +- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index bce3e2d303..2ffea50148 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -25,6 +25,7 @@ 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__) @@ -36,34 +37,33 @@ def _format_admin_state(state): _formatters = { 'admin_state_up': _format_admin_state, + 'is_admin_state_up': _format_admin_state, 'allowed_address_pairs': utils.format_list_of_dicts, 'binding_profile': utils.format_dict, 'binding_vif_details': utils.format_dict, + 'binding:profile': utils.format_dict, + 'binding:vif_details': utils.format_dict, 'dns_assignment': utils.format_list_of_dicts, 'extra_dhcp_opts': utils.format_list_of_dicts, 'fixed_ips': utils.format_list_of_dicts, + 'security_group_ids': utils.format_list, 'security_groups': utils.format_list, } def _get_columns(item): - columns = list(item.keys()) - if 'tenant_id' in columns: - columns.remove('tenant_id') - if 'project_id' not in columns: - columns.append('project_id') - binding_columns = [ - 'binding:host_id', - 'binding:profile', - 'binding:vif_details', - 'binding:vif_type', - 'binding:vnic_type', - ] - for binding_column in binding_columns: - if binding_column in columns: - columns.remove(binding_column) - columns.append(binding_column.replace('binding:', 'binding_', 1)) - return tuple(sorted(columns)) + column_map = { + 'binding:host_id': 'binding_host_id', + 'binding:profile': 'binding_profile', + 'binding:vif_details': 'binding_vif_details', + 'binding:vif_type': 'binding_vif_type', + 'binding:vnic_type': 'binding_vnic_type', + 'is_admin_state_up': 'admin_state_up', + 'is_port_security_enabled': 'port_security_enabled', + 'security_group_ids': 'security_groups', + 'tenant_id': 'project_id', + } + return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map) class JSONKeyValueAction(argparse.Action): @@ -244,6 +244,8 @@ def _add_updatable_args(parser): ) +# TODO(abhiraut): Use the SDK resource mapped attribute names once the +# OSC minimum requirements include SDK 1.0. class CreatePort(command.ShowOne): _description = _("Create a new port") @@ -349,10 +351,10 @@ class CreatePort(command.ShowOne): attrs['security_groups'] = [] obj = client.create_port(**attrs) - columns = _get_columns(obj) + display_columns, columns = _get_columns(obj) data = utils.get_item_properties(obj, columns, formatters=_formatters) - return (columns, data) + return (display_columns, data) class DeletePort(command.Command): @@ -389,6 +391,8 @@ class DeletePort(command.Command): raise exceptions.CommandError(msg) +# TODO(abhiraut): Use only the SDK resource mapped attribute names once the +# OSC minimum requirements include SDK 1.0. class ListPort(command.Lister): _description = _("List ports") @@ -451,7 +455,7 @@ class ListPort(command.Lister): filters = {} if parsed_args.long: - columns += ('security_groups', 'device_owner',) + columns += ('security_group_ids', 'device_owner',) column_headers += ('Security Groups', 'Device Owner',) if parsed_args.device_owner is not None: filters['device_owner'] = parsed_args.device_owner @@ -479,6 +483,8 @@ class ListPort(command.Lister): ) for s in data)) +# TODO(abhiraut): Use the SDK resource mapped attribute names once the +# OSC minimum requirements include SDK 1.0. class SetPort(command.Command): _description = _("Set port properties") @@ -621,11 +627,13 @@ class ShowPort(command.ShowOne): def take_action(self, parsed_args): client = self.app.client_manager.network obj = client.find_port(parsed_args.port, ignore_missing=False) - columns = _get_columns(obj) + display_columns, columns = _get_columns(obj) data = utils.get_item_properties(obj, columns, formatters=_formatters) - return (columns, data) + return (display_columns, data) +# TODO(abhiraut): Use the SDK resource mapped attribute names once the +# OSC minimum requirements include SDK 1.0. class UnsetPort(command.Command): _description = _("Unset port properties") diff --git a/openstackclient/tests/unit/network/v2/fakes.py b/openstackclient/tests/unit/network/v2/fakes.py index 97d0707636..ece6369afd 100644 --- a/openstackclient/tests/unit/network/v2/fakes.py +++ b/openstackclient/tests/unit/network/v2/fakes.py @@ -461,12 +461,15 @@ class FakePort(object): loaded=True) # Set attributes with special mappings in OpenStack SDK. - port.project_id = port_attrs['tenant_id'] port.binding_host_id = port_attrs['binding:host_id'] port.binding_profile = port_attrs['binding:profile'] port.binding_vif_details = port_attrs['binding:vif_details'] port.binding_vif_type = port_attrs['binding:vif_type'] port.binding_vnic_type = port_attrs['binding:vnic_type'] + port.is_admin_state_up = port_attrs['admin_state_up'] + port.is_port_security_enabled = port_attrs['port_security_enabled'] + port.project_id = port_attrs['tenant_id'] + port.security_group_ids = port_attrs['security_groups'] return port diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index aeb9884a42..255e81166e 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -324,7 +324,7 @@ class TestCreatePort(TestPort): self.assertEqual(ref_columns, columns) self.assertEqual(ref_data, data) - def test_create_with_no_secuirty_groups(self): + def test_create_with_no_security_groups(self): arglist = [ '--network', self._port.network_id, '--no-security-group',