Skip to content

Commit c8c4f76

Browse files
committed
Add --security-group to port list
The neutron API supports filtering ports by security group. Closes-Bug: #1405057 Depends-On: https://review.opendev.org/c/openstack/openstacksdk/+/804979 Change-Id: I0f626882716c21ac200c1b929ea04664d21874d8
1 parent 728401b commit c8c4f76

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

openstackclient/network/v2/port.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,13 @@ def get_parser(self, prog_name):
610610
metavar='<name>',
611611
help=_("List ports according to their name")
612612
)
613+
parser.add_argument(
614+
'--security-group',
615+
action='append',
616+
dest='security_groups',
617+
metavar='<security-group>',
618+
help=_("List only ports associated with this security group")
619+
)
613620
identity_common.add_project_domain_option_to_parser(parser)
614621
parser.add_argument(
615622
'--fixed-ip',
@@ -682,6 +689,8 @@ def take_action(self, parsed_args):
682689
if parsed_args.fixed_ip:
683690
filters['fixed_ips'] = _prepare_filter_fixed_ips(
684691
self.app.client_manager, parsed_args)
692+
if parsed_args.security_groups:
693+
filters['security_groups'] = parsed_args.security_groups
685694

686695
_tag.get_tag_filtering_args(parsed_args, filters)
687696

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,26 @@ def test_list_with_tag_options(self):
12961296
self.assertEqual(self.columns, columns)
12971297
self.assertCountEqual(self.data, list(data))
12981298

1299+
def test_port_list_security_group(self):
1300+
arglist = [
1301+
'--security-group', 'sg-id1',
1302+
'--security-group', 'sg-id2',
1303+
]
1304+
verifylist = [
1305+
('security_groups', ['sg-id1', 'sg-id2']),
1306+
]
1307+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
1308+
1309+
columns, data = self.cmd.take_action(parsed_args)
1310+
filters = {
1311+
'security_groups': ['sg-id1', 'sg-id2'],
1312+
'fields': LIST_FIELDS_TO_RETRIEVE,
1313+
}
1314+
1315+
self.network.ports.assert_called_once_with(**filters)
1316+
self.assertEqual(self.columns, columns)
1317+
self.assertCountEqual(self.data, list(data))
1318+
12991319

13001320
class TestSetPort(TestPort):
13011321

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
features:
3+
- |
4+
Added ``--security-group`` option to the ``os port list`` command. This
5+
option is appendable and multiple security group IDs can be provided.

0 commit comments

Comments
 (0)