Skip to content

Commit c89d441

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add direction and protocol options to os security group rule list cmd"
2 parents 55ddaf7 + 291b66e commit c89d441

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
@@ -133,6 +133,8 @@ List security group rules
133133
134134
os security group rule list
135135
[--all-projects]
136+
[--protocol <protocol>]
137+
[--ingress | --egress]
136138
[--long]
137139
[<group>]
138140
@@ -149,6 +151,28 @@ List security group rules
149151
150152
*Compute version 2 does not have additional fields to display.*
151153
154+
155+
.. option:: --protocol
156+
157+
List rules by the IP protocol (ah, dhcp, egp, esp, gre, icmp, igmp,
158+
ipv6-encap, ipv6-frag, ipv6-icmp, ipv6-nonxt,ipv6-opts, ipv6-route,
159+
ospf, pgm, rsvp, sctp, tcp, udp, udplite, vrrp and integer
160+
representations [0-255])
161+
162+
*Network version 2*
163+
164+
.. option:: --ingress
165+
166+
List rules applied to incoming network traffic
167+
168+
*Network version 2 only*
169+
170+
.. option:: --egress
171+
172+
List rules applied to outgoing network traffic
173+
174+
*Network version 2 only*
175+
152176
.. describe:: <group>
153177
154178
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
@@ -387,6 +387,28 @@ def update_parser_network(self, parser):
387387
default=False,
388388
help=argparse.SUPPRESS
389389
)
390+
parser.add_argument(
391+
'--protocol',
392+
metavar='<protocol>',
393+
type=_convert_to_lowercase,
394+
help=_("List rules by the IP protocol ("
395+
"ah, dhcp, egp, esp, gre, icmp, igmp, "
396+
"ipv6-encap, ipv6-frag, ipv6-icmp, ipv6-nonxt, "
397+
"ipv6-opts, ipv6-route, ospf, pgm, rsvp, sctp, tcp, "
398+
"udp, udplite, vrrp and integer representations [0-255])."
399+
)
400+
)
401+
direction_group = parser.add_mutually_exclusive_group()
402+
direction_group.add_argument(
403+
'--ingress',
404+
action='store_true',
405+
help=_("List rules applied to incoming network traffic")
406+
)
407+
direction_group.add_argument(
408+
'--egress',
409+
action='store_true',
410+
help=_("List rules applied to outgoing network traffic")
411+
)
390412
parser.add_argument(
391413
'--long',
392414
action='store_true',
@@ -451,6 +473,14 @@ def take_action_network(self, client, parsed_args):
451473
query = {'security_group_id': security_group_id}
452474
else:
453475
columns = columns + ('security_group_id',)
476+
477+
if parsed_args.ingress:
478+
query['direction'] = 'ingress'
479+
if parsed_args.egress:
480+
query['direction'] = 'egress'
481+
if parsed_args.protocol is not None:
482+
query['protocol'] = parsed_args.protocol
483+
454484
rules = list(client.security_group_rules(**query))
455485

456486
# 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
@@ -971,6 +971,60 @@ def test_list_with_ignored_options(self):
971971
self.assertEqual(self.expected_columns_no_group, columns)
972972
self.assertEqual(self.expected_data_no_group, list(data))
973973

974+
def test_list_with_protocol(self):
975+
self._security_group_rule_tcp.port_range_min = 80
976+
arglist = [
977+
'--protocol', 'tcp',
978+
]
979+
verifylist = [
980+
('protocol', 'tcp'),
981+
]
982+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
983+
984+
columns, data = self.cmd.take_action(parsed_args)
985+
986+
self.network.security_group_rules.assert_called_once_with(**{
987+
'protocol': 'tcp',
988+
})
989+
self.assertEqual(self.expected_columns_no_group, columns)
990+
self.assertEqual(self.expected_data_no_group, list(data))
991+
992+
def test_list_with_ingress(self):
993+
self._security_group_rule_tcp.port_range_min = 80
994+
arglist = [
995+
'--ingress',
996+
]
997+
verifylist = [
998+
('ingress', True),
999+
]
1000+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1001+
1002+
columns, data = self.cmd.take_action(parsed_args)
1003+
1004+
self.network.security_group_rules.assert_called_once_with(**{
1005+
'direction': 'ingress',
1006+
})
1007+
self.assertEqual(self.expected_columns_no_group, columns)
1008+
self.assertEqual(self.expected_data_no_group, list(data))
1009+
1010+
def test_list_with_wrong_egress(self):
1011+
self._security_group_rule_tcp.port_range_min = 80
1012+
arglist = [
1013+
'--egress',
1014+
]
1015+
verifylist = [
1016+
('egress', True),
1017+
]
1018+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1019+
1020+
columns, data = self.cmd.take_action(parsed_args)
1021+
1022+
self.network.security_group_rules.assert_called_once_with(**{
1023+
'direction': 'egress',
1024+
})
1025+
self.assertEqual(self.expected_columns_no_group, columns)
1026+
self.assertEqual(self.expected_data_no_group, list(data))
1027+
9741028

9751029
class TestListSecurityGroupRuleCompute(TestSecurityGroupRuleCompute):
9761030

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)