Skip to content

Commit 291b66e

Browse files
namnh68Ha Van Tu
authored andcommitted
Add direction and protocol options to os security group rule list cmd
This patch added direction options (--ingress, --egress) and protocol option (--protocol) to filter rules by os security group rule list command. Change-Id: I56ace3f97eb927fd2a868f728c7347a29d028b67 Closes-Bug: #1613533 Partially-Implements: blueprint network-commands-options
1 parent 43d1646 commit 291b66e

4 files changed

Lines changed: 114 additions & 0 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ List security group rules
126126
127127
os security group rule list
128128
[--all-projects]
129+
[--protocol <protocol>]
130+
[--ingress | --egress]
129131
[--long]
130132
[<group>]
131133
@@ -142,6 +144,28 @@ List security group rules
142144
143145
*Compute version 2 does not have additional fields to display.*
144146
147+
148+
.. option:: --protocol
149+
150+
List rules by the IP protocol (ah, dhcp, egp, esp, gre, icmp, igmp,
151+
ipv6-encap, ipv6-frag, ipv6-icmp, ipv6-nonxt,ipv6-opts, ipv6-route,
152+
ospf, pgm, rsvp, sctp, tcp, udp, udplite, vrrp and integer
153+
representations [0-255])
154+
155+
*Network version 2*
156+
157+
.. option:: --ingress
158+
159+
List rules applied to incoming network traffic
160+
161+
*Network version 2 only*
162+
163+
.. option:: --egress
164+
165+
List rules applied to outgoing network traffic
166+
167+
*Network version 2 only*
168+
145169
.. describe:: <group>
146170
147171
List all rules in this security group (name or ID)

openstackclient/network/v2/security_group_rule.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,28 @@ def update_parser_network(self, parser):
379379
default=False,
380380
help=argparse.SUPPRESS
381381
)
382+
parser.add_argument(
383+
'--protocol',
384+
metavar='<protocol>',
385+
type=_convert_to_lowercase,
386+
help=_("List rules by the IP protocol ("
387+
"ah, dhcp, egp, esp, gre, icmp, igmp, "
388+
"ipv6-encap, ipv6-frag, ipv6-icmp, ipv6-nonxt, "
389+
"ipv6-opts, ipv6-route, ospf, pgm, rsvp, sctp, tcp, "
390+
"udp, udplite, vrrp and integer representations [0-255])."
391+
)
392+
)
393+
direction_group = parser.add_mutually_exclusive_group()
394+
direction_group.add_argument(
395+
'--ingress',
396+
action='store_true',
397+
help=_("List rules applied to incoming network traffic")
398+
)
399+
direction_group.add_argument(
400+
'--egress',
401+
action='store_true',
402+
help=_("List rules applied to outgoing network traffic")
403+
)
382404
parser.add_argument(
383405
'--long',
384406
action='store_true',
@@ -443,6 +465,14 @@ def take_action_network(self, client, parsed_args):
443465
query = {'security_group_id': security_group_id}
444466
else:
445467
columns = columns + ('security_group_id',)
468+
469+
if parsed_args.ingress:
470+
query['direction'] = 'ingress'
471+
if parsed_args.egress:
472+
query['direction'] = 'egress'
473+
if parsed_args.protocol is not None:
474+
query['protocol'] = parsed_args.protocol
475+
446476
rules = list(client.security_group_rules(**query))
447477

448478
# Reformat the rules to display a port range instead

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,60 @@ def test_list_with_ignored_options(self):
942942
self.assertEqual(self.expected_columns_no_group, columns)
943943
self.assertEqual(self.expected_data_no_group, list(data))
944944

945+
def test_list_with_protocol(self):
946+
self._security_group_rule_tcp.port_range_min = 80
947+
arglist = [
948+
'--protocol', 'tcp',
949+
]
950+
verifylist = [
951+
('protocol', 'tcp'),
952+
]
953+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
954+
955+
columns, data = self.cmd.take_action(parsed_args)
956+
957+
self.network.security_group_rules.assert_called_once_with(**{
958+
'protocol': 'tcp',
959+
})
960+
self.assertEqual(self.expected_columns_no_group, columns)
961+
self.assertEqual(self.expected_data_no_group, list(data))
962+
963+
def test_list_with_ingress(self):
964+
self._security_group_rule_tcp.port_range_min = 80
965+
arglist = [
966+
'--ingress',
967+
]
968+
verifylist = [
969+
('ingress', True),
970+
]
971+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
972+
973+
columns, data = self.cmd.take_action(parsed_args)
974+
975+
self.network.security_group_rules.assert_called_once_with(**{
976+
'direction': 'ingress',
977+
})
978+
self.assertEqual(self.expected_columns_no_group, columns)
979+
self.assertEqual(self.expected_data_no_group, list(data))
980+
981+
def test_list_with_wrong_egress(self):
982+
self._security_group_rule_tcp.port_range_min = 80
983+
arglist = [
984+
'--egress',
985+
]
986+
verifylist = [
987+
('egress', True),
988+
]
989+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
990+
991+
columns, data = self.cmd.take_action(parsed_args)
992+
993+
self.network.security_group_rules.assert_called_once_with(**{
994+
'direction': 'egress',
995+
})
996+
self.assertEqual(self.expected_columns_no_group, columns)
997+
self.assertEqual(self.expected_data_no_group, list(data))
998+
945999

9461000
class TestListSecurityGroupRuleCompute(TestSecurityGroupRuleCompute):
9471001

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
features:
3+
- |
4+
Add ``--ingress``, ``--egress`` and ``--protocol`` options to
5+
``security group rule list`` command.
6+
[Bug `1613533 <https://bugs.launchpad.net/bugs/1613533>`_]

0 commit comments

Comments
 (0)