Skip to content

Commit 3730fe9

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "SDK Refactor: Prepare subnet commands"
2 parents 7078502 + 11560a0 commit 3730fe9

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__)
@@ -126,11 +127,12 @@ def _get_common_parse_arguments(parser, is_create=True):
126127

127128

128129
def _get_columns(item):
129-
columns = list(item.keys())
130-
if 'tenant_id' in columns:
131-
columns.remove('tenant_id')
132-
columns.append('project_id')
133-
return tuple(sorted(columns))
130+
column_map = {
131+
'is_dhcp_enabled': 'enable_dhcp',
132+
'subnet_pool_id': 'subnetpool_id',
133+
'tenant_id': 'project_id',
134+
}
135+
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
134136

135137

136138
def convert_entries_to_nexthop(entries):
@@ -226,6 +228,8 @@ def _get_attrs(client_manager, parsed_args, is_create=True):
226228
return attrs
227229

228230

231+
# TODO(abhiraut): Use the SDK resource mapped attribute names once the
232+
# OSC minimum requirements include SDK 1.0.
229233
class CreateSubnet(command.ShowOne):
230234
_description = _("Create a subnet")
231235

@@ -332,9 +336,9 @@ def take_action(self, parsed_args):
332336
client = self.app.client_manager.network
333337
attrs = _get_attrs(self.app.client_manager, parsed_args)
334338
obj = client.create_subnet(**attrs)
335-
columns = _get_columns(obj)
339+
display_columns, columns = _get_columns(obj)
336340
data = utils.get_item_properties(obj, columns, formatters=_formatters)
337-
return (columns, data)
341+
return (display_columns, data)
338342

339343

340344
class DeleteSubnet(command.Command):
@@ -371,6 +375,8 @@ def take_action(self, parsed_args):
371375
raise exceptions.CommandError(msg)
372376

373377

378+
# TODO(abhiraut): Use only the SDK resource mapped attribute names once the
379+
# OSC minimum requirements include SDK 1.0.
374380
class ListSubnet(command.Lister):
375381
_description = _("List subnets")
376382

@@ -452,8 +458,10 @@ def take_action(self, parsed_args):
452458
filters['ip_version'] = parsed_args.ip_version
453459
if parsed_args.dhcp:
454460
filters['enable_dhcp'] = True
461+
filters['is_dhcp_enabled'] = True
455462
elif parsed_args.no_dhcp:
456463
filters['enable_dhcp'] = False
464+
filters['is_dhcp_enabled'] = False
457465
if parsed_args.service_types:
458466
filters['service_types'] = parsed_args.service_types
459467
if parsed_args.project:
@@ -463,6 +471,7 @@ def take_action(self, parsed_args):
463471
parsed_args.project_domain,
464472
).id
465473
filters['tenant_id'] = project_id
474+
filters['project_id'] = project_id
466475
if parsed_args.network:
467476
network_id = network_client.find_network(parsed_args.network,
468477
ignore_missing=False).id
@@ -481,7 +490,7 @@ def take_action(self, parsed_args):
481490
headers += ('Project', 'DHCP', 'Name Servers',
482491
'Allocation Pools', 'Host Routes', 'IP Version',
483492
'Gateway', 'Service Types')
484-
columns += ('tenant_id', 'enable_dhcp', 'dns_nameservers',
493+
columns += ('project_id', 'is_dhcp_enabled', 'dns_nameservers',
485494
'allocation_pools', 'host_routes', 'ip_version',
486495
'gateway_ip', 'service_types')
487496

@@ -492,6 +501,8 @@ def take_action(self, parsed_args):
492501
) for s in data))
493502

494503

504+
# TODO(abhiraut): Use the SDK resource mapped attribute names once the
505+
# OSC minimum requirements include SDK 1.0.
495506
class SetSubnet(command.Command):
496507
_description = _("Set subnet properties")
497508

@@ -576,9 +587,9 @@ def get_parser(self, prog_name):
576587
def take_action(self, parsed_args):
577588
obj = self.app.client_manager.network.find_subnet(parsed_args.subnet,
578589
ignore_missing=False)
579-
columns = _get_columns(obj)
590+
display_columns, columns = _get_columns(obj)
580591
data = utils.get_item_properties(obj, columns, formatters=_formatters)
581-
return (columns, data)
592+
return (display_columns, data)
582593

583594

584595
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)