diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 84aef47757..c45b34ad33 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -792,6 +792,19 @@ class ListPort(command.Lister): metavar='', help=_("List only ports associated with this security group"), ) + # the API sadly reports these in upper case and while it would be + # wonderful to plaster over this ugliness client-side, there are + # already users in the wild doing this in upper case that we need to + # support + parser.add_argument( + '--status', + metavar='', + choices=('ACTIVE', 'BUILD', 'DOWN', 'ERROR'), + help=_( + "List ports according to their status " + "('ACTIVE', 'BUILD', 'DOWN', 'ERROR')" + ), + ) identity_common.add_project_domain_option_to_parser(parser) parser.add_argument( '--fixed-ip', @@ -859,6 +872,8 @@ class ListPort(command.Lister): filters['network_id'] = network.id if parsed_args.mac_address: filters['mac_address'] = parsed_args.mac_address + if parsed_args.status: + filters['status'] = parsed_args.status if parsed_args.project: project_id = identity_common.find_project( identity_client, diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index d7592e99be..81f3cd7e97 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -1712,6 +1712,29 @@ class TestListPort(compute_fakes.FakeClientMixin, TestPort): self.assertEqual(self.columns, columns) self.assertCountEqual(self.data, list(data)) + def test_port_list_status(self): + arglist = [ + '--status', + 'ACTIVE', + ] + verifylist = [ + ('status', 'ACTIVE'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + filters = { + 'status': 'ACTIVE', + 'fields': LIST_FIELDS_TO_RETRIEVE, + } + + self.network_client.ports.assert_called_once_with(**filters) + self.assertEqual(self.columns, columns) + self.assertEqual( + self.data, + list(data), + ) + class TestSetPort(TestPort): _port = network_fakes.create_one_port({'tags': ['green', 'red']}) diff --git a/releasenotes/notes/add-port-list-status-option-f51da0aed0528a5d.yaml b/releasenotes/notes/add-port-list-status-option-f51da0aed0528a5d.yaml new file mode 100644 index 0000000000..762ea6dcaa --- /dev/null +++ b/releasenotes/notes/add-port-list-status-option-f51da0aed0528a5d.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``--status`` option to ``port list`` command. + [Bug `1672680 `_]