Skip to content

Commit ab88573

Browse files
committed
SDK refactor: Prepare security group commands
Prepare the OSC "security group" commands for the SDK refactor. Change-Id: If9918fad2474f9b4d68424f2806f0de61fd58b2e Partially-Implements: blueprint network-command-sdk-support
1 parent d5745ea commit ab88573

2 files changed

Lines changed: 14 additions & 23 deletions

File tree

openstackclient/network/v2/security_group.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from openstackclient.i18n import _
2222
from openstackclient.identity import common as identity_common
2323
from openstackclient.network import common
24+
from openstackclient.network import sdk_utils
2425
from openstackclient.network import utils as network_utils
2526

2627

@@ -34,6 +35,7 @@ def _format_network_security_group_rules(sg_rules):
3435
sg_rule.pop(key)
3536
sg_rule.pop('security_group_id', None)
3637
sg_rule.pop('tenant_id', None)
38+
sg_rule.pop('project_id', None)
3739
return utils.format_list_of_dicts(sg_rules)
3840

3941

@@ -72,29 +74,15 @@ def _format_compute_security_group_rules(sg_rules):
7274

7375

7476
def _get_columns(item):
75-
# Build the display columns and a list of the property columns
76-
# that need to be mapped (display column name, property name).
77-
columns = list(item.to_dict().keys())
78-
property_column_mappings = []
79-
if 'security_group_rules' in columns:
80-
columns.append('rules')
81-
columns.remove('security_group_rules')
82-
property_column_mappings.append(('rules', 'security_group_rules'))
83-
if 'tenant_id' in columns:
84-
columns.remove('tenant_id')
85-
if 'project_id' not in columns:
86-
columns.append('project_id')
87-
property_column_mappings.append(('project_id', 'tenant_id'))
88-
display_columns = sorted(columns)
89-
90-
# Build the property columns and apply any column mappings.
91-
property_columns = sorted(columns)
92-
for property_column_mapping in property_column_mappings:
93-
property_index = property_columns.index(property_column_mapping[0])
94-
property_columns[property_index] = property_column_mapping[1]
95-
return tuple(display_columns), property_columns
77+
column_map = {
78+
'security_group_rules': 'rules',
79+
'tenant_id': 'project_id',
80+
}
81+
return sdk_utils.get_osc_show_columns_for_sdk_resource(item, column_map)
9682

9783

84+
# TODO(abhiraut): Use the SDK resource mapped attribute names once the
85+
# OSC minimum requirements include SDK 1.0.
9886
class CreateSecurityGroup(common.NetworkAndComputeShowOne):
9987
_description = _("Create a new security group")
10088

@@ -190,6 +178,8 @@ def take_action_compute(self, client, parsed_args):
190178
client.security_groups.delete(data.id)
191179

192180

181+
# TODO(rauta): Use the SDK resource mapped attribute names once
182+
# the OSC minimum requirements include SDK 1.0.
193183
class ListSecurityGroup(common.NetworkAndComputeLister):
194184
_description = _("List security groups")
195185

@@ -245,6 +235,7 @@ def take_action_network(self, client, parsed_args):
245235
parsed_args.project_domain,
246236
).id
247237
filters['tenant_id'] = project_id
238+
filters['project_id'] = project_id
248239
return self._get_return_data(client.security_groups(**filters))
249240

250241
def take_action_compute(self, client, parsed_args):

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def test_security_group_list_project(self):
456456
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
457457

458458
columns, data = self.cmd.take_action(parsed_args)
459-
filters = {'tenant_id': project.id}
459+
filters = {'tenant_id': project.id, 'project_id': project.id}
460460

461461
self.network.security_groups.assert_called_once_with(**filters)
462462
self.assertEqual(self.columns, columns)
@@ -476,7 +476,7 @@ def test_security_group_list_project_domain(self):
476476
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
477477

478478
columns, data = self.cmd.take_action(parsed_args)
479-
filters = {'tenant_id': project.id}
479+
filters = {'tenant_id': project.id, 'project_id': project.id}
480480

481481
self.network.security_groups.assert_called_once_with(**filters)
482482
self.assertEqual(self.columns, columns)

0 commit comments

Comments
 (0)