Skip to content

Commit 4011649

Browse files
Jenkinsopenstack-gerrit
authored andcommitted
Merge "Add support to list all security group rules"
2 parents 774201b + 96cc5eb commit 4011649

3 files changed

Lines changed: 104 additions & 16 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ List security group rules
6262
.. code:: bash
6363
6464
os security group rule list
65-
<group>
65+
[<group>]
6666
6767
.. describe:: <group>
6868

openstackclient/compute/v2/security_group.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ def get_parser(self, prog_name):
276276
parser.add_argument(
277277
'group',
278278
metavar='<group>',
279+
nargs='?',
279280
help='List all rules in this security group (name or ID)',
280281
)
281282
return parser
@@ -284,26 +285,35 @@ def take_action(self, parsed_args):
284285
self.log.debug("take_action(%s)", parsed_args)
285286

286287
compute_client = self.app.client_manager.compute
287-
group = utils.find_resource(
288-
compute_client.security_groups,
289-
parsed_args.group,
288+
columns = column_headers = (
289+
"ID",
290+
"IP Protocol",
291+
"IP Range",
292+
"Port Range",
293+
"Remote Security Group",
290294
)
291295

296+
rules_to_list = []
297+
if parsed_args.group:
298+
group = utils.find_resource(
299+
compute_client.security_groups,
300+
parsed_args.group,
301+
)
302+
rules_to_list = group.rules
303+
else:
304+
columns = columns + ('parent_group_id',)
305+
column_headers = column_headers + ('Security Group',)
306+
for group in compute_client.security_groups.list():
307+
rules_to_list.extend(group.rules)
308+
292309
# Argh, the rules are not Resources...
293310
rules = []
294-
for rule in group.rules:
311+
for rule in rules_to_list:
295312
rules.append(security_group_rules.SecurityGroupRule(
296313
compute_client.security_group_rules,
297314
_xform_security_group_rule(rule),
298315
))
299316

300-
columns = column_headers = (
301-
"ID",
302-
"IP Protocol",
303-
"IP Range",
304-
"Port Range",
305-
"Remote Security Group",
306-
)
307317
return (column_headers,
308318
(utils.get_item_properties(
309319
s, columns,

openstackclient/tests/compute/v2/test_security_group_rule.py

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@
6868
SECURITY_GROUP_RULE_REMOTE_GROUP],
6969
}
7070

71+
security_group_2_id = '12'
72+
security_group_2_name = 'he-shoots'
73+
security_group_2_description = 'he scores'
74+
75+
SECURITY_GROUP_2_RULE = {
76+
'id': '2',
77+
'group': {},
78+
'ip_protocol': 'tcp',
79+
'ip_range': {},
80+
'parent_group_id': security_group_2_id,
81+
'from_port': 80,
82+
'to_port': 80,
83+
}
84+
85+
SECURITY_GROUP_2 = {
86+
'id': security_group_2_id,
87+
'name': security_group_2_name,
88+
'description': security_group_2_description,
89+
'tenant_id': identity_fakes.project_id,
90+
'rules': [SECURITY_GROUP_2_RULE],
91+
}
92+
7193

7294
class FakeSecurityGroupRuleResource(fakes.FakeResource):
7395

@@ -383,12 +405,22 @@ class TestSecurityGroupRuleList(TestSecurityGroupRule):
383405
def setUp(self):
384406
super(TestSecurityGroupRuleList, self).setUp()
385407

386-
self.secgroups_mock.get.return_value = FakeSecurityGroupRuleResource(
408+
security_group_mock = FakeSecurityGroupRuleResource(
387409
None,
388410
copy.deepcopy(SECURITY_GROUP),
389411
loaded=True,
390412
)
391413

414+
security_group_2_mock = FakeSecurityGroupRuleResource(
415+
None,
416+
copy.deepcopy(SECURITY_GROUP_2),
417+
loaded=True,
418+
)
419+
420+
self.secgroups_mock.get.return_value = security_group_mock
421+
self.secgroups_mock.list.return_value = [security_group_mock,
422+
security_group_2_mock]
423+
392424
# Get the command object to test
393425
self.cmd = security_group.ListSecurityGroupRule(self.app, None)
394426

@@ -420,18 +452,64 @@ def test_security_group_rule_list(self):
420452
security_group_rule_cidr,
421453
'0:0',
422454
'',
423-
), (
455+
), (
424456
security_group_rule_id,
425457
'icmp',
426458
security_group_rule_cidr,
427459
'',
428460
'',
429-
), (
461+
), (
430462
security_group_rule_id,
431463
'tcp',
432464
'',
433465
'80:80',
434466
'default',
435-
),
467+
),)
468+
self.assertEqual(datalist, tuple(data))
469+
470+
def test_security_group_rule_list_no_group(self):
471+
472+
parsed_args = self.check_parser(self.cmd, [], [])
473+
474+
# DisplayCommandBase.take_action() returns two tuples
475+
columns, data = self.cmd.take_action(parsed_args)
476+
477+
collist = (
478+
'ID',
479+
'IP Protocol',
480+
'IP Range',
481+
'Port Range',
482+
'Remote Security Group',
483+
'Security Group',
436484
)
485+
self.assertEqual(collist, columns)
486+
datalist = ((
487+
security_group_rule_id,
488+
'tcp',
489+
security_group_rule_cidr,
490+
'0:0',
491+
'',
492+
security_group_id,
493+
), (
494+
security_group_rule_id,
495+
'icmp',
496+
security_group_rule_cidr,
497+
'',
498+
'',
499+
security_group_id,
500+
), (
501+
security_group_rule_id,
502+
'tcp',
503+
'',
504+
'80:80',
505+
'default',
506+
security_group_id,
507+
), (
508+
'2',
509+
'tcp',
510+
'',
511+
'80:80',
512+
'',
513+
security_group_2_id,
514+
),)
437515
self.assertEqual(datalist, tuple(data))

0 commit comments

Comments
 (0)