Skip to content

Commit 11560a0

Browse files
committed
SDK Refactor: Prepare subnet commands
Prepare the OSC "subnet" commands for the SDK refactor. See [1] for details. [1] https://etherpad.openstack.org/p/osc-network-command-sdk-support Partially-Implements: blueprint network-command-sdk-support Change-Id: I5d58b189e822b2ee61c9c95ccf909113ff59de46
1 parent d6e058f commit 11560a0

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

openstackclient/network/v2/subnet.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from openstackclient.i18n import _
2525
from openstackclient.identity import common as identity_common
26+
from openstackclient.network import sdk_utils
2627

2728

2829
LOG = logging.getLogger(__name__)
@@ -117,11 +118,12 @@ def _get_common_parse_arguments(parser, is_create=True):
117118

118119

119120
def _get_columns(item):
120-
columns = list(item.keys())
121-
if 'tenant_id' in columns:
122-
columns.remove('tenant_id')
123-
columns.append('project_id')
124-
return tuple(sorted(columns))
121+
column_map = {
122+
'is_dhcp_enabled': 'enable_dhcp',
123+
'subnet_pool_id': 'subnetpool_id',
124+
'tenant_id': 'project_id',
125+
}
126+
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
125127

126128

127129
def convert_entries_to_nexthop(entries):
@@ -217,6 +219,8 @@ def _get_attrs(client_manager, parsed_args, is_create=True):
217219
return attrs
218220

219221

222+
# TODO(abhiraut): Use the SDK resource mapped attribute names once the
223+
# OSC minimum requirements include SDK 1.0.
220224
class CreateSubnet(command.ShowOne):
221225
_description = _("Create a subnet")
222226

@@ -323,9 +327,9 @@ def take_action(self, parsed_args):
323327
client = self.app.client_manager.network
324328
attrs = _get_attrs(self.app.client_manager, parsed_args)
325329
obj = client.create_subnet(**attrs)
326-
columns = _get_columns(obj)
330+
display_columns, columns = _get_columns(obj)
327331
data = utils.get_item_properties(obj, columns, formatters=_formatters)
328-
return (columns, data)
332+
return (display_columns, data)
329333

330334

331335
class DeleteSubnet(command.Command):
@@ -362,6 +366,8 @@ def take_action(self, parsed_args):
362366
raise exceptions.CommandError(msg)
363367

364368

369+
# TODO(abhiraut): Use only the SDK resource mapped attribute names once the
370+
# OSC minimum requirements include SDK 1.0.
365371
class ListSubnet(command.Lister):
366372
_description = _("List subnets")
367373

@@ -443,8 +449,10 @@ def take_action(self, parsed_args):
443449
filters['ip_version'] = parsed_args.ip_version
444450
if parsed_args.dhcp:
445451
filters['enable_dhcp'] = True
452+
filters['is_dhcp_enabled'] = True
446453
elif parsed_args.no_dhcp:
447454
filters['enable_dhcp'] = False
455+
filters['is_dhcp_enabled'] = False
448456
if parsed_args.service_types:
449457
filters['service_types'] = parsed_args.service_types
450458
if parsed_args.project:
@@ -454,6 +462,7 @@ def take_action(self, parsed_args):
454462
parsed_args.project_domain,
455463
).id
456464
filters['tenant_id'] = project_id
465+
filters['project_id'] = project_id
457466
if parsed_args.network:
458467
network_id = network_client.find_network(parsed_args.network,
459468
ignore_missing=False).id
@@ -472,7 +481,7 @@ def take_action(self, parsed_args):
472481
headers += ('Project', 'DHCP', 'Name Servers',
473482
'Allocation Pools', 'Host Routes', 'IP Version',
474483
'Gateway', 'Service Types')
475-
columns += ('tenant_id', 'enable_dhcp', 'dns_nameservers',
484+
columns += ('project_id', 'is_dhcp_enabled', 'dns_nameservers',
476485
'allocation_pools', 'host_routes', 'ip_version',
477486
'gateway_ip', 'service_types')
478487

@@ -483,6 +492,8 @@ def take_action(self, parsed_args):
483492
) for s in data))
484493

485494

495+
# TODO(abhiraut): Use the SDK resource mapped attribute names once the
496+
# OSC minimum requirements include SDK 1.0.
486497
class SetSubnet(command.Command):
487498
_description = _("Set subnet properties")
488499

@@ -564,9 +575,9 @@ def get_parser(self, prog_name):
564575
def take_action(self, parsed_args):
565576
obj = self.app.client_manager.network.find_subnet(parsed_args.subnet,
566577
ignore_missing=False)
567-
columns = _get_columns(obj)
578+
display_columns, columns = _get_columns(obj)
568579
data = utils.get_item_properties(obj, columns, formatters=_formatters)
569-
return (columns, data)
580+
return (display_columns, data)
570581

571582

572583
class UnsetSubnet(command.Command):

openstackclient/tests/unit/network/v2/fakes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,8 @@ def create_one_subnet(attrs=None):
10641064
loaded=True)
10651065

10661066
# Set attributes with special mappings in OpenStack SDK.
1067+
subnet.is_dhcp_enabled = subnet_attrs['enable_dhcp']
1068+
subnet.subnet_pool_id = subnet_attrs['subnetpool_id']
10671069
subnet.project_id = subnet_attrs['tenant_id']
10681070

10691071
return subnet

openstackclient/tests/unit/network/v2/test_subnet.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ def test_subnet_list_dhcp(self):
636636
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
637637

638638
columns, data = self.cmd.take_action(parsed_args)
639-
filters = {'enable_dhcp': True}
639+
filters = {'enable_dhcp': True, 'is_dhcp_enabled': True}
640640

641641
self.network.subnets.assert_called_once_with(**filters)
642642
self.assertEqual(self.columns, columns)
@@ -652,7 +652,7 @@ def test_subnet_list_no_dhcp(self):
652652
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
653653

654654
columns, data = self.cmd.take_action(parsed_args)
655-
filters = {'enable_dhcp': False}
655+
filters = {'enable_dhcp': False, 'is_dhcp_enabled': False}
656656

657657
self.network.subnets.assert_called_once_with(**filters)
658658
self.assertEqual(self.columns, columns)
@@ -685,7 +685,7 @@ def test_subnet_list_project(self):
685685
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
686686

687687
columns, data = self.cmd.take_action(parsed_args)
688-
filters = {'tenant_id': project.id}
688+
filters = {'tenant_id': project.id, 'project_id': project.id}
689689

690690
self.network.subnets.assert_called_once_with(**filters)
691691
self.assertEqual(self.columns, columns)
@@ -723,7 +723,7 @@ def test_subnet_list_project_domain(self):
723723
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
724724

725725
columns, data = self.cmd.take_action(parsed_args)
726-
filters = {'tenant_id': project.id}
726+
filters = {'tenant_id': project.id, 'project_id': project.id}
727727

728728
self.network.subnets.assert_called_once_with(**filters)
729729
self.assertEqual(self.columns, columns)

0 commit comments

Comments
 (0)