From a1e305641430af48b72c941f87c7ffcc182b3f9a Mon Sep 17 00:00:00 2001 From: Yan Xing'an Date: Mon, 17 Oct 2016 22:33:35 -0700 Subject: [PATCH] Add option to allow filtering by mac-address on port list Added support to allow filtering ports via --mac-address option to the port list command. Change-Id: I903e443f2f9057571d46520c4cafb88d1972c0cb Partial-bug: #1634333 Partially-Implements: blueprint network-commands-options --- doc/source/command-objects/port.rst | 5 ++++ openstackclient/network/v2/port.py | 7 +++++ .../tests/unit/network/v2/test_port.py | 26 +++++++++++++++++-- .../notes/bug-1634333-a2b04d33ca39440e.yaml | 6 +++++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 releasenotes/notes/bug-1634333-a2b04d33ca39440e.yaml diff --git a/doc/source/command-objects/port.rst b/doc/source/command-objects/port.rst index fe256d0940..f4dbe26393 100644 --- a/doc/source/command-objects/port.rst +++ b/doc/source/command-objects/port.rst @@ -127,6 +127,7 @@ List ports [--device-owner ] [--router | --server ] [--network ] + [--mac-address ] [--long] .. option:: --device-owner @@ -146,6 +147,10 @@ List ports List only ports attached to this network (name or ID) +.. option:: --mac-address + + List only ports with this MAC address + .. option:: --long List additional fields in output diff --git a/openstackclient/network/v2/port.py b/openstackclient/network/v2/port.py index 86174d535a..8916fd7304 100644 --- a/openstackclient/network/v2/port.py +++ b/openstackclient/network/v2/port.py @@ -384,6 +384,11 @@ class ListPort(command.Lister): metavar='', help=_("List only ports attached to this server (name or ID)"), ) + parser.add_argument( + '--mac-address', + metavar='', + help=_("List only ports with this MAC address") + ) parser.add_argument( '--long', action='store_true', @@ -429,6 +434,8 @@ class ListPort(command.Lister): network = network_client.find_network(parsed_args.network, ignore_missing=False) filters['network_id'] = network.id + if parsed_args.mac_address: + filters['mac_address'] = parsed_args.mac_address data = network_client.ports(**filters) diff --git a/openstackclient/tests/unit/network/v2/test_port.py b/openstackclient/tests/unit/network/v2/test_port.py index 4ff278a994..4f6eac53e4 100644 --- a/openstackclient/tests/unit/network/v2/test_port.py +++ b/openstackclient/tests/unit/network/v2/test_port.py @@ -530,12 +530,14 @@ class TestListPort(TestPort): '--device-owner', self._ports[0].device_owner, '--router', 'fake-router-name', '--network', 'fake-network-name', + '--mac-address', self._ports[0].mac_address, ] verifylist = [ ('device_owner', self._ports[0].device_owner), ('router', 'fake-router-name'), - ('network', 'fake-network-name') + ('network', 'fake-network-name'), + ('mac_address', self._ports[0].mac_address) ] parsed_args = self.check_parser(self.cmd, arglist, verifylist) @@ -545,7 +547,27 @@ class TestListPort(TestPort): self.network.ports.assert_called_once_with(**{ 'device_owner': self._ports[0].device_owner, 'device_id': 'fake-router-id', - 'network_id': 'fake-network-id' + 'network_id': 'fake-network-id', + 'mac_address': self._ports[0].mac_address + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_port_list_mac_address_opt(self): + arglist = [ + '--mac-address', self._ports[0].mac_address, + ] + + verifylist = [ + ('mac_address', self._ports[0].mac_address) + ] + + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ports.assert_called_once_with(**{ + 'mac_address': self._ports[0].mac_address }) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) diff --git a/releasenotes/notes/bug-1634333-a2b04d33ca39440e.yaml b/releasenotes/notes/bug-1634333-a2b04d33ca39440e.yaml new file mode 100644 index 0000000000..4ecee30aa2 --- /dev/null +++ b/releasenotes/notes/bug-1634333-a2b04d33ca39440e.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add support to allow filtering ports via ``--mac-address`` + option to the ``port list`` command. + [Bug `1634333 `_]