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
This commit is contained in:
Abhishek Raut 2016-11-19 04:52:19 -08:00
parent 2cb5cf7d54
commit 7832ea357c
3 changed files with 35 additions and 24 deletions

View File

@ -25,6 +25,7 @@ from osc_lib import utils
from openstackclient.i18n import _ from openstackclient.i18n import _
from openstackclient.identity import common as identity_common from openstackclient.identity import common as identity_common
from openstackclient.network import sdk_utils
LOG = logging.getLogger(__name__) LOG = logging.getLogger(__name__)
@ -36,34 +37,33 @@ def _format_admin_state(state):
_formatters = { _formatters = {
'admin_state_up': _format_admin_state, 'admin_state_up': _format_admin_state,
'is_admin_state_up': _format_admin_state,
'allowed_address_pairs': utils.format_list_of_dicts, 'allowed_address_pairs': utils.format_list_of_dicts,
'binding_profile': utils.format_dict, 'binding_profile': utils.format_dict,
'binding_vif_details': 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, 'dns_assignment': utils.format_list_of_dicts,
'extra_dhcp_opts': utils.format_list_of_dicts, 'extra_dhcp_opts': utils.format_list_of_dicts,
'fixed_ips': utils.format_list_of_dicts, 'fixed_ips': utils.format_list_of_dicts,
'security_group_ids': utils.format_list,
'security_groups': utils.format_list, 'security_groups': utils.format_list,
} }
def _get_columns(item): def _get_columns(item):
columns = list(item.keys()) column_map = {
if 'tenant_id' in columns: 'binding:host_id': 'binding_host_id',
columns.remove('tenant_id') 'binding:profile': 'binding_profile',
if 'project_id' not in columns: 'binding:vif_details': 'binding_vif_details',
columns.append('project_id') 'binding:vif_type': 'binding_vif_type',
binding_columns = [ 'binding:vnic_type': 'binding_vnic_type',
'binding:host_id', 'is_admin_state_up': 'admin_state_up',
'binding:profile', 'is_port_security_enabled': 'port_security_enabled',
'binding:vif_details', 'security_group_ids': 'security_groups',
'binding:vif_type', 'tenant_id': 'project_id',
'binding:vnic_type', }
] return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
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))
class JSONKeyValueAction(argparse.Action): 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): class CreatePort(command.ShowOne):
_description = _("Create a new port") _description = _("Create a new port")
@ -349,10 +351,10 @@ class CreatePort(command.ShowOne):
attrs['security_groups'] = [] attrs['security_groups'] = []
obj = client.create_port(**attrs) obj = client.create_port(**attrs)
columns = _get_columns(obj) display_columns, columns = _get_columns(obj)
data = utils.get_item_properties(obj, columns, formatters=_formatters) data = utils.get_item_properties(obj, columns, formatters=_formatters)
return (columns, data) return (display_columns, data)
class DeletePort(command.Command): class DeletePort(command.Command):
@ -389,6 +391,8 @@ class DeletePort(command.Command):
raise exceptions.CommandError(msg) 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): class ListPort(command.Lister):
_description = _("List ports") _description = _("List ports")
@ -451,7 +455,7 @@ class ListPort(command.Lister):
filters = {} filters = {}
if parsed_args.long: if parsed_args.long:
columns += ('security_groups', 'device_owner',) columns += ('security_group_ids', 'device_owner',)
column_headers += ('Security Groups', 'Device Owner',) column_headers += ('Security Groups', 'Device Owner',)
if parsed_args.device_owner is not None: if parsed_args.device_owner is not None:
filters['device_owner'] = parsed_args.device_owner filters['device_owner'] = parsed_args.device_owner
@ -479,6 +483,8 @@ class ListPort(command.Lister):
) for s in data)) ) 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): class SetPort(command.Command):
_description = _("Set port properties") _description = _("Set port properties")
@ -621,11 +627,13 @@ class ShowPort(command.ShowOne):
def take_action(self, parsed_args): def take_action(self, parsed_args):
client = self.app.client_manager.network client = self.app.client_manager.network
obj = client.find_port(parsed_args.port, ignore_missing=False) 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) 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): class UnsetPort(command.Command):
_description = _("Unset port properties") _description = _("Unset port properties")

View File

@ -461,12 +461,15 @@ class FakePort(object):
loaded=True) loaded=True)
# Set attributes with special mappings in OpenStack SDK. # 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_host_id = port_attrs['binding:host_id']
port.binding_profile = port_attrs['binding:profile'] port.binding_profile = port_attrs['binding:profile']
port.binding_vif_details = port_attrs['binding:vif_details'] port.binding_vif_details = port_attrs['binding:vif_details']
port.binding_vif_type = port_attrs['binding:vif_type'] port.binding_vif_type = port_attrs['binding:vif_type']
port.binding_vnic_type = port_attrs['binding:vnic_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 return port

View File

@ -324,7 +324,7 @@ class TestCreatePort(TestPort):
self.assertEqual(ref_columns, columns) self.assertEqual(ref_columns, columns)
self.assertEqual(ref_data, data) self.assertEqual(ref_data, data)
def test_create_with_no_secuirty_groups(self): def test_create_with_no_security_groups(self):
arglist = [ arglist = [
'--network', self._port.network_id, '--network', self._port.network_id,
'--no-security-group', '--no-security-group',