Merge "SDK refactor: Prepare network commands"

This commit is contained in:
Jenkins 2016-12-16 03:01:12 +00:00 committed by Gerrit Code Review
commit 60370b46f5
3 changed files with 100 additions and 33 deletions

View File

@ -19,6 +19,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 common from openstackclient.network import common
from openstackclient.network import sdk_utils
def _format_admin_state(item): def _format_admin_state(item):
@ -31,13 +32,33 @@ def _format_router_external(item):
_formatters = { _formatters = {
'subnets': utils.format_list, 'subnets': utils.format_list,
'subnet_ids': utils.format_list,
'admin_state_up': _format_admin_state, 'admin_state_up': _format_admin_state,
'is_admin_state_up': _format_admin_state,
'router:external': _format_router_external, 'router:external': _format_router_external,
'is_router_external': _format_router_external,
'availability_zones': utils.format_list, 'availability_zones': utils.format_list,
'availability_zone_hints': utils.format_list, 'availability_zone_hints': utils.format_list,
} }
def _get_network_columns(item):
column_map = {
'subnet_ids': 'subnets',
'is_admin_state_up': 'admin_state_up',
'is_router_external': 'router:external',
'is_port_security_enabled': 'port_security_enabled',
'provider_network_type': 'provider:network_type',
'provider_physical_network': 'provider:physical_network',
'provider_segmentation_id': 'provider:segmentation_id',
'is_shared': 'shared',
'ipv4_address_scope_id': 'ipv4_address_scope',
'ipv6_address_scope_id': 'ipv6_address_scope',
'tenant_id': 'project_id',
}
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
def _get_columns(item): def _get_columns(item):
columns = list(item.keys()) columns = list(item.keys())
if 'tenant_id' in columns: if 'tenant_id' in columns:
@ -162,6 +183,8 @@ def _get_attrs_compute(client_manager, parsed_args):
return attrs return attrs
# TODO(sindhu): Use the SDK resource mapped attribute names once the
# OSC minimum requirements include SDK 1.0.
class CreateNetwork(common.NetworkAndComputeShowOne): class CreateNetwork(common.NetworkAndComputeShowOne):
_description = _("Create new network") _description = _("Create new network")
@ -275,9 +298,9 @@ class CreateNetwork(common.NetworkAndComputeShowOne):
def take_action_network(self, client, parsed_args): def take_action_network(self, client, parsed_args):
attrs = _get_attrs(self.app.client_manager, parsed_args) attrs = _get_attrs(self.app.client_manager, parsed_args)
obj = client.create_network(**attrs) obj = client.create_network(**attrs)
columns = _get_columns(obj) display_columns, columns = _get_network_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)
def take_action_compute(self, client, parsed_args): def take_action_compute(self, client, parsed_args):
attrs = _get_attrs_compute(self.app.client_manager, parsed_args) attrs = _get_attrs_compute(self.app.client_manager, parsed_args)
@ -313,6 +336,8 @@ class DeleteNetwork(common.NetworkAndComputeDelete):
client.networks.delete(network.id) client.networks.delete(network.id)
# TODO(sindhu): Use the SDK resource mapped attribute names once the
# OSC minimum requirements include SDK 1.0.
class ListNetwork(common.NetworkAndComputeLister): class ListNetwork(common.NetworkAndComputeLister):
_description = _("List networks") _description = _("List networks")
@ -405,12 +430,12 @@ class ListNetwork(common.NetworkAndComputeLister):
'id', 'id',
'name', 'name',
'status', 'status',
'tenant_id', 'project_id',
'admin_state_up', 'is_admin_state_up',
'shared', 'is_shared',
'subnets', 'subnet_ids',
'provider_network_type', 'provider_network_type',
'router:external', 'is_router_external',
'availability_zones', 'availability_zones',
) )
column_headers = ( column_headers = (
@ -429,7 +454,7 @@ class ListNetwork(common.NetworkAndComputeLister):
columns = ( columns = (
'id', 'id',
'name', 'name',
'subnets' 'subnet_ids'
) )
column_headers = ( column_headers = (
'ID', 'ID',
@ -441,16 +466,20 @@ class ListNetwork(common.NetworkAndComputeLister):
if parsed_args.external: if parsed_args.external:
args['router:external'] = True args['router:external'] = True
args['is_router_external'] = True
elif parsed_args.internal: elif parsed_args.internal:
args['router:external'] = False args['router:external'] = False
args['is_router_external'] = False
if parsed_args.name is not None: if parsed_args.name is not None:
args['name'] = parsed_args.name args['name'] = parsed_args.name
if parsed_args.enable: if parsed_args.enable:
args['admin_state_up'] = True args['admin_state_up'] = True
args['is_admin_state_up'] = True
elif parsed_args.disable: elif parsed_args.disable:
args['admin_state_up'] = False args['admin_state_up'] = False
args['is_admin_state_up'] = False
if parsed_args.project: if parsed_args.project:
project = identity_common.find_project( project = identity_common.find_project(
@ -459,21 +488,27 @@ class ListNetwork(common.NetworkAndComputeLister):
parsed_args.project_domain, parsed_args.project_domain,
) )
args['tenant_id'] = project.id args['tenant_id'] = project.id
args['project_id'] = project.id
if parsed_args.share: if parsed_args.share:
args['shared'] = True args['shared'] = True
args['is_shared'] = True
elif parsed_args.no_share: elif parsed_args.no_share:
args['shared'] = False args['shared'] = False
args['is_shared'] = False
if parsed_args.status: if parsed_args.status:
args['status'] = parsed_args.status args['status'] = parsed_args.status
if parsed_args.provider_network_type: if parsed_args.provider_network_type:
args['provider:network_type'] = parsed_args.provider_network_type args['provider:network_type'] = parsed_args.provider_network_type
args['provider_network_type'] = parsed_args.provider_network_type
if parsed_args.physical_network: if parsed_args.physical_network:
args['provider:physical_network'] = parsed_args.physical_network args['provider:physical_network'] = parsed_args.physical_network
args['provider_physical_network'] = parsed_args.physical_network
if parsed_args.segmentation_id: if parsed_args.segmentation_id:
args['provider:segmentation_id'] = parsed_args.segmentation_id args['provider:segmentation_id'] = parsed_args.segmentation_id
args['provider_segmentation_id'] = parsed_args.segmentation_id
data = client.networks(**args) data = client.networks(**args)
@ -504,6 +539,8 @@ class ListNetwork(common.NetworkAndComputeLister):
) for s in data)) ) for s in data))
# TODO(sindhu): Use the SDK resource mapped attribute names once the
# OSC minimum requirements include SDK 1.0.
class SetNetwork(command.Command): class SetNetwork(command.Command):
_description = _("Set network properties") _description = _("Set network properties")
@ -619,9 +656,9 @@ class ShowNetwork(common.NetworkAndComputeShowOne):
def take_action_network(self, client, parsed_args): def take_action_network(self, client, parsed_args):
obj = client.find_network(parsed_args.network, ignore_missing=False) obj = client.find_network(parsed_args.network, ignore_missing=False)
columns = _get_columns(obj) display_columns, columns = _get_network_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)
def take_action_compute(self, client, parsed_args): def take_action_compute(self, client, parsed_args):
obj = utils.find_resource( obj = utils.find_resource(

View File

@ -297,15 +297,17 @@ class FakeNetwork(object):
'admin_state_up': True, 'admin_state_up': True,
'shared': False, 'shared': False,
'subnets': ['a', 'b'], 'subnets': ['a', 'b'],
'provider_network_type': 'vlan', 'provider:network_type': 'vlan',
'provider_physical_network': 'physnet1', 'provider:physical_network': 'physnet1',
'provider_segmentation_id': "400", 'provider:segmentation_id': "400",
'router:external': True, 'router:external': True,
'availability_zones': [], 'availability_zones': [],
'availability_zone_hints': [], 'availability_zone_hints': [],
'is_default': False, 'is_default': False,
'port_security_enabled': True, 'port_security_enabled': True,
'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex, 'qos_policy_id': 'qos-policy-id-' + uuid.uuid4().hex,
'ipv4_address_scope': 'ipv4' + uuid.uuid4().hex,
'ipv6_address_scope': 'ipv6' + uuid.uuid4().hex,
} }
# Overwrite default attributes. # Overwrite default attributes.
@ -317,8 +319,21 @@ class FakeNetwork(object):
# Set attributes with special mapping in OpenStack SDK. # Set attributes with special mapping in OpenStack SDK.
network.project_id = network_attrs['tenant_id'] network.project_id = network_attrs['tenant_id']
network.is_router_external = network_attrs['router:external'] network.is_router_external = network_attrs['router:external']
network.is_admin_state_up = network_attrs['admin_state_up']
network.is_port_security_enabled = \ network.is_port_security_enabled = \
network_attrs['port_security_enabled'] network_attrs['port_security_enabled']
network.subnet_ids = network_attrs['subnets']
network.is_shared = network_attrs['shared']
network.provider_network_type = \
network_attrs['provider:network_type']
network.provider_physical_network = \
network_attrs['provider:physical_network']
network.provider_segmentation_id = \
network_attrs['provider:segmentation_id']
network.ipv4_address_scope_id = \
network_attrs['ipv4_address_scope']
network.ipv6_address_scope_id = \
network_attrs['ipv6_address_scope']
return network return network

View File

@ -62,13 +62,15 @@ class TestCreateNetworkIdentityV3(TestNetwork):
'availability_zones', 'availability_zones',
'description', 'description',
'id', 'id',
'ipv4_address_scope',
'ipv6_address_scope',
'is_default', 'is_default',
'name', 'name',
'port_security_enabled', 'port_security_enabled',
'project_id', 'project_id',
'provider_network_type', 'provider:network_type',
'provider_physical_network', 'provider:physical_network',
'provider_segmentation_id', 'provider:segmentation_id',
'qos_policy_id', 'qos_policy_id',
'router:external', 'router:external',
'shared', 'shared',
@ -82,6 +84,8 @@ class TestCreateNetworkIdentityV3(TestNetwork):
utils.format_list(_network.availability_zones), utils.format_list(_network.availability_zones),
_network.description, _network.description,
_network.id, _network.id,
_network.ipv4_address_scope_id,
_network.ipv6_address_scope_id,
_network.is_default, _network.is_default,
_network.name, _network.name,
_network.is_port_security_enabled, _network.is_port_security_enabled,
@ -236,13 +240,15 @@ class TestCreateNetworkIdentityV2(TestNetwork):
'availability_zones', 'availability_zones',
'description', 'description',
'id', 'id',
'ipv4_address_scope',
'ipv6_address_scope',
'is_default', 'is_default',
'name', 'name',
'port_security_enabled', 'port_security_enabled',
'project_id', 'project_id',
'provider_network_type', 'provider:network_type',
'provider_physical_network', 'provider:physical_network',
'provider_segmentation_id', 'provider:segmentation_id',
'qos_policy_id', 'qos_policy_id',
'router:external', 'router:external',
'shared', 'shared',
@ -256,6 +262,8 @@ class TestCreateNetworkIdentityV2(TestNetwork):
utils.format_list(_network.availability_zones), utils.format_list(_network.availability_zones),
_network.description, _network.description,
_network.id, _network.id,
_network.ipv4_address_scope_id,
_network.ipv6_address_scope_id,
_network.is_default, _network.is_default,
_network.name, _network.name,
_network.is_port_security_enabled, _network.is_port_security_enabled,
@ -512,7 +520,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'router:external': True} **{'router:external': True, 'is_router_external': True}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -529,7 +537,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'router:external': False} **{'router:external': False, 'is_router_external': False}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -585,7 +593,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'admin_state_up': True} **{'admin_state_up': True, 'is_admin_state_up': True}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -603,7 +611,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'admin_state_up': False} **{'admin_state_up': False, 'is_admin_state_up': False}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -621,7 +629,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'tenant_id': project.id} **{'tenant_id': project.id, 'project_id': project.id}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
@ -640,7 +648,7 @@ class TestListNetwork(TestNetwork):
parsed_args = self.check_parser(self.cmd, arglist, verifylist) parsed_args = self.check_parser(self.cmd, arglist, verifylist)
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
filters = {'tenant_id': project.id} filters = {'tenant_id': project.id, 'project_id': project.id}
self.network.networks.assert_called_once_with(**filters) self.network.networks.assert_called_once_with(**filters)
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
@ -658,7 +666,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'shared': True} **{'shared': True, 'is_shared': True}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -675,7 +683,7 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'shared': False} **{'shared': False, 'is_shared': False}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -711,7 +719,8 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'provider:network_type': network_type} **{'provider:network_type': network_type,
'provider_network_type': network_type}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -728,7 +737,8 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'provider:physical_network': physical_network} **{'provider:physical_network': physical_network,
'provider_physical_network': physical_network}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -745,7 +755,8 @@ class TestListNetwork(TestNetwork):
columns, data = self.cmd.take_action(parsed_args) columns, data = self.cmd.take_action(parsed_args)
self.network.networks.assert_called_once_with( self.network.networks.assert_called_once_with(
**{'provider:segmentation_id': segmentation_id} **{'provider:segmentation_id': segmentation_id,
'provider_segmentation_id': segmentation_id}
) )
self.assertEqual(self.columns, columns) self.assertEqual(self.columns, columns)
self.assertEqual(self.data, list(data)) self.assertEqual(self.data, list(data))
@ -878,13 +889,15 @@ class TestShowNetwork(TestNetwork):
'availability_zones', 'availability_zones',
'description', 'description',
'id', 'id',
'ipv4_address_scope',
'ipv6_address_scope',
'is_default', 'is_default',
'name', 'name',
'port_security_enabled', 'port_security_enabled',
'project_id', 'project_id',
'provider_network_type', 'provider:network_type',
'provider_physical_network', 'provider:physical_network',
'provider_segmentation_id', 'provider:segmentation_id',
'qos_policy_id', 'qos_policy_id',
'router:external', 'router:external',
'shared', 'shared',
@ -898,6 +911,8 @@ class TestShowNetwork(TestNetwork):
utils.format_list(_network.availability_zones), utils.format_list(_network.availability_zones),
_network.description, _network.description,
_network.id, _network.id,
_network.ipv4_address_scope_id,
_network.ipv6_address_scope_id,
_network.is_default, _network.is_default,
_network.name, _network.name,
_network.is_port_security_enabled, _network.is_port_security_enabled,