Skip to content

Commit 4d05851

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add options to security group rule list"
2 parents c9cfd56 + 94c9cd5 commit 4d05851

5 files changed

Lines changed: 149 additions & 23 deletions

File tree

doc/source/command-objects/security-group-rule.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,23 @@ List security group rules
9898
.. code:: bash
9999
100100
os security group rule list
101+
[--all-projects]
102+
[--long]
101103
[<group>]
102104
105+
.. option:: --all-projects
106+
107+
Display information from all projects (admin only)
108+
109+
*Network version 2 ignores this option and will always display information*
110+
*for all projects (admin only).*
111+
112+
.. option:: --long
113+
114+
List additional fields in output
115+
116+
*Compute version 2 does not have additional fields to display.*
117+
103118
.. describe:: <group>
104119
105120
List all rules in this security group (name or ID)

doc/source/command-objects/security-group.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ List security groups
6969
Display information from all projects (admin only)
7070
7171
*Network version 2 ignores this option and will always display information*
72-
*for all projects.*
72+
*for all projects (admin only).*
7373
7474
security group set
7575
------------------

openstackclient/network/v2/security_group_rule.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
"""Security Group Rule action implementations"""
1515

16+
import argparse
1617
import six
1718

1819
try:
@@ -242,14 +243,50 @@ def update_parser_common(self, parser):
242243
)
243244
return parser
244245

246+
def update_parser_network(self, parser):
247+
# Accept but hide the argument for consistency with compute.
248+
# Network will always return all projects for an admin.
249+
parser.add_argument(
250+
'--all-projects',
251+
action='store_true',
252+
default=False,
253+
help=argparse.SUPPRESS
254+
)
255+
parser.add_argument(
256+
'--long',
257+
action='store_true',
258+
default=False,
259+
help=_("List additional fields in output")
260+
)
261+
return parser
262+
263+
def update_parser_compute(self, parser):
264+
parser.add_argument(
265+
'--all-projects',
266+
action='store_true',
267+
default=False,
268+
help=_("Display information from all projects (admin only)")
269+
)
270+
# Accept but hide the argument for consistency with network.
271+
# There are no additional fields to display at this time.
272+
parser.add_argument(
273+
'--long',
274+
action='store_false',
275+
default=False,
276+
help=argparse.SUPPRESS
277+
)
278+
return parser
279+
245280
def _get_column_headers(self, parsed_args):
246281
column_headers = (
247282
'ID',
248283
'IP Protocol',
249284
'IP Range',
250285
'Port Range',
251-
'Remote Security Group',
252286
)
287+
if parsed_args.long:
288+
column_headers = column_headers + ('Direction', 'Ethertype',)
289+
column_headers = column_headers + ('Remote Security Group',)
253290
if parsed_args.group is None:
254291
column_headers = column_headers + ('Security Group',)
255292
return column_headers
@@ -261,8 +298,10 @@ def take_action_network(self, client, parsed_args):
261298
'protocol',
262299
'remote_ip_prefix',
263300
'port_range_min',
264-
'remote_group_id',
265301
)
302+
if parsed_args.long:
303+
columns = columns + ('direction', 'ethertype',)
304+
columns = columns + ('remote_group_id',)
266305

267306
# Get the security group rules using the requested query.
268307
query = {}
@@ -309,7 +348,8 @@ def take_action_compute(self, client, parsed_args):
309348
rules_to_list = group.rules
310349
else:
311350
columns = columns + ('parent_group_id',)
312-
for group in client.security_groups.list():
351+
search = {'all_tenants': parsed_args.all_projects}
352+
for group in client.security_groups.list(search_opts=search):
313353
rules_to_list.extend(group.rules)
314354

315355
# NOTE(rtheis): Turn the raw rules into resources.

openstackclient/tests/network/v2/test_security_group_rule.py

Lines changed: 83 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -532,31 +532,46 @@ class TestListSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork):
532532
_security_group_rules = [_security_group_rule_tcp,
533533
_security_group_rule_icmp]
534534

535-
expected_columns_with_group = (
535+
expected_columns_with_group_and_long = (
536536
'ID',
537537
'IP Protocol',
538538
'IP Range',
539539
'Port Range',
540+
'Direction',
541+
'Ethertype',
540542
'Remote Security Group',
541543
)
542-
expected_columns_no_group = \
543-
expected_columns_with_group + ('Security Group',)
544+
expected_columns_no_group = (
545+
'ID',
546+
'IP Protocol',
547+
'IP Range',
548+
'Port Range',
549+
'Remote Security Group',
550+
'Security Group',
551+
)
544552

545-
expected_data_with_group = []
553+
expected_data_with_group_and_long = []
546554
expected_data_no_group = []
547555
for _security_group_rule in _security_group_rules:
548-
expected_rule_with_group = (
556+
expected_data_with_group_and_long.append((
549557
_security_group_rule.id,
550558
_security_group_rule.protocol,
551559
_security_group_rule.remote_ip_prefix,
552560
security_group_rule._format_network_port_range(
553561
_security_group_rule),
562+
_security_group_rule.direction,
563+
_security_group_rule.ethertype,
554564
_security_group_rule.remote_group_id,
555-
)
556-
expected_rule_no_group = expected_rule_with_group + \
557-
(_security_group_rule.security_group_id,)
558-
expected_data_with_group.append(expected_rule_with_group)
559-
expected_data_no_group.append(expected_rule_no_group)
565+
))
566+
expected_data_no_group.append((
567+
_security_group_rule.id,
568+
_security_group_rule.protocol,
569+
_security_group_rule.remote_ip_prefix,
570+
security_group_rule._format_network_port_range(
571+
_security_group_rule),
572+
_security_group_rule.remote_group_id,
573+
_security_group_rule.security_group_id,
574+
))
560575

561576
def setUp(self):
562577
super(TestListSecurityGroupRuleNetwork, self).setUp()
@@ -570,7 +585,7 @@ def setUp(self):
570585
self.cmd = security_group_rule.ListSecurityGroupRule(
571586
self.app, self.namespace)
572587

573-
def test_list_no_group(self):
588+
def test_list_default(self):
574589
self._security_group_rule_tcp.port_range_min = 80
575590
parsed_args = self.check_parser(self.cmd, [], [])
576591

@@ -580,12 +595,14 @@ def test_list_no_group(self):
580595
self.assertEqual(self.expected_columns_no_group, columns)
581596
self.assertEqual(self.expected_data_no_group, list(data))
582597

583-
def test_list_with_group(self):
598+
def test_list_with_group_and_long(self):
584599
self._security_group_rule_tcp.port_range_min = 80
585600
arglist = [
601+
'--long',
586602
self._security_group.id,
587603
]
588604
verifylist = [
605+
('long', True),
589606
('group', self._security_group.id),
590607
]
591608
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
@@ -595,8 +612,24 @@ def test_list_with_group(self):
595612
self.network.security_group_rules.assert_called_once_with(**{
596613
'security_group_id': self._security_group.id,
597614
})
598-
self.assertEqual(self.expected_columns_with_group, columns)
599-
self.assertEqual(self.expected_data_with_group, list(data))
615+
self.assertEqual(self.expected_columns_with_group_and_long, columns)
616+
self.assertEqual(self.expected_data_with_group_and_long, list(data))
617+
618+
def test_list_with_ignored_options(self):
619+
self._security_group_rule_tcp.port_range_min = 80
620+
arglist = [
621+
'--all-projects',
622+
]
623+
verifylist = [
624+
('all_projects', True),
625+
]
626+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
627+
628+
columns, data = self.cmd.take_action(parsed_args)
629+
630+
self.network.security_group_rules.assert_called_once_with(**{})
631+
self.assertEqual(self.expected_columns_no_group, columns)
632+
self.assertEqual(self.expected_data_no_group, list(data))
600633

601634

602635
class TestListSecurityGroupRuleCompute(TestSecurityGroupRuleCompute):
@@ -665,11 +698,13 @@ def setUp(self):
665698
# Get the command object to test
666699
self.cmd = security_group_rule.ListSecurityGroupRule(self.app, None)
667700

668-
def test_list_no_group(self):
701+
def test_list_default(self):
669702
parsed_args = self.check_parser(self.cmd, [], [])
670703

671704
columns, data = self.cmd.take_action(parsed_args)
672-
self.compute.security_groups.list.assert_called_once_with()
705+
self.compute.security_groups.list.assert_called_once_with(
706+
search_opts={'all_tenants': False}
707+
)
673708
self.assertEqual(self.expected_columns_no_group, columns)
674709
self.assertEqual(self.expected_data_no_group, list(data))
675710

@@ -689,6 +724,38 @@ def test_list_with_group(self):
689724
self.assertEqual(self.expected_columns_with_group, columns)
690725
self.assertEqual(self.expected_data_with_group, list(data))
691726

727+
def test_list_all_projects(self):
728+
arglist = [
729+
'--all-projects',
730+
]
731+
verifylist = [
732+
('all_projects', True),
733+
]
734+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
735+
736+
columns, data = self.cmd.take_action(parsed_args)
737+
self.compute.security_groups.list.assert_called_once_with(
738+
search_opts={'all_tenants': True}
739+
)
740+
self.assertEqual(self.expected_columns_no_group, columns)
741+
self.assertEqual(self.expected_data_no_group, list(data))
742+
743+
def test_list_with_ignored_options(self):
744+
arglist = [
745+
'--long',
746+
]
747+
verifylist = [
748+
('long', False),
749+
]
750+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
751+
752+
columns, data = self.cmd.take_action(parsed_args)
753+
self.compute.security_groups.list.assert_called_once_with(
754+
search_opts={'all_tenants': False}
755+
)
756+
self.assertEqual(self.expected_columns_no_group, columns)
757+
self.assertEqual(self.expected_data_no_group, list(data))
758+
692759

693760
class TestShowSecurityGroupRuleNetwork(TestSecurityGroupRuleNetwork):
694761

releasenotes/notes/bug-1519512-65df002102b7fb99.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
features:
33
- The ``security group rule list`` command now uses Network v2
44
when enabled which results in ``egress`` security group rules
5-
being displayed. In addition, security group rules for all
6-
projects will be displayed when the ``group`` argument is not
7-
specified (admin only).
5+
being displayed. The ``--long`` option was also added for
6+
Network v2 to display direction and ethertype information.
7+
In addition, security group rules for all projects will be
8+
displayed when the ``group`` argument is not specified
9+
(admin only). This is done by default when using Network v2,
10+
but requires the new ``--all-projects`` option when using
11+
Compute v2.
812
[Bug `1519512 <https://bugs.launchpad.net/bugs/1519512>`_]
913
fixes:
1014
- The ``security group rule list`` command no longer ignores

0 commit comments

Comments
 (0)