summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRodolfo Alonso Hernandez <ralonsoh@redhat.com>2021-10-26 13:03:22 +0000
committerRodolfo Alonso <ralonsoh@redhat.com>2021-12-02 11:12:47 +0000
commitc8c4f76498de3380c7cbf80c5dc800a588bed649 (patch)
tree68b14b8218f6da3e194230994b1f9f95c11b32d7
parent728401bbd76afc4d31b4f22e44bf98d1de40ef46 (diff)
downloadpython-openstackclient-c8c4f76498de3380c7cbf80c5dc800a588bed649.tar.gz
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
-rw-r--r--openstackclient/network/v2/port.py9
-rw-r--r--openstackclient/tests/unit/network/v2/test_port.py20
-rw-r--r--releasenotes/notes/port-list-security-group-4af5d2e789174ff9.yaml5
3 files changed, 34 insertions, 0 deletions
diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py
index 132c384a..887c5318 100644
--- a/openstackclient/network/v2/port.py
+++ b/openstackclient/network/v2/port.py
@@ -610,6 +610,13 @@ class ListPort(command.Lister):
metavar='<name>',
help=_("List ports according to their name")
)
+ parser.add_argument(
+ '--security-group',
+ action='append',
+ dest='security_groups',
+ metavar='<security-group>',
+ help=_("List only ports associated with this security group")
+ )
identity_common.add_project_domain_option_to_parser(parser)
parser.add_argument(
'--fixed-ip',
@@ -682,6 +689,8 @@ class ListPort(command.Lister):
if parsed_args.fixed_ip:
filters['fixed_ips'] = _prepare_filter_fixed_ips(
self.app.client_manager, parsed_args)
+ if parsed_args.security_groups:
+ filters['security_groups'] = parsed_args.security_groups
_tag.get_tag_filtering_args(parsed_args, filters)
diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py
index 5f2a1283..96edb74d 100644
--- a/openstackclient/tests/unit/network/v2/test_port.py
+++ b/openstackclient/tests/unit/network/v2/test_port.py
@@ -1296,6 +1296,26 @@ class TestListPort(TestPort):
self.assertEqual(self.columns, columns)
self.assertCountEqual(self.data, list(data))
+ def test_port_list_security_group(self):
+ arglist = [
+ '--security-group', 'sg-id1',
+ '--security-group', 'sg-id2',
+ ]
+ verifylist = [
+ ('security_groups', ['sg-id1', 'sg-id2']),
+ ]
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+
+ columns, data = self.cmd.take_action(parsed_args)
+ filters = {
+ 'security_groups': ['sg-id1', 'sg-id2'],
+ 'fields': LIST_FIELDS_TO_RETRIEVE,
+ }
+
+ self.network.ports.assert_called_once_with(**filters)
+ self.assertEqual(self.columns, columns)
+ self.assertCountEqual(self.data, list(data))
+
class TestSetPort(TestPort):
diff --git a/releasenotes/notes/port-list-security-group-4af5d2e789174ff9.yaml b/releasenotes/notes/port-list-security-group-4af5d2e789174ff9.yaml
new file mode 100644
index 00000000..c68eeafb
--- /dev/null
+++ b/releasenotes/notes/port-list-security-group-4af5d2e789174ff9.yaml
@@ -0,0 +1,5 @@
+---
+features:
+ - |
+ Added ``--security-group`` option to the ``os port list`` command. This
+ option is appendable and multiple security group IDs can be provided.