From 839c5f7a84b48cecbc80606e894b7906fa01d66b Mon Sep 17 00:00:00 2001 From: songminglong Date: Sun, 13 Nov 2016 10:11:03 +0800 Subject: [PATCH] Add '--network' and other options to floating ip list The patch adds filtering '--network', '--port', '--fixed-ip-address' options to floating ip list command Partial-Bug: #1614379 Change-Id: I82319d0985d9e864431097c90264a20bf88167cc --- doc/source/command-objects/floating-ip.rst | 21 +++++++ openstackclient/network/v2/floating_ip.py | 36 +++++++++++ .../tests/unit/network/v2/test_floating_ip.py | 61 ++++++++++++++++++- .../notes/bug-1614379-da92ded6d19f5ad5.yaml | 6 ++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/bug-1614379-da92ded6d19f5ad5.yaml diff --git a/doc/source/command-objects/floating-ip.rst b/doc/source/command-objects/floating-ip.rst index 2ab21f36eb..b2cc8af0ef 100644 --- a/doc/source/command-objects/floating-ip.rst +++ b/doc/source/command-objects/floating-ip.rst @@ -72,6 +72,27 @@ List floating IP(s) .. code:: bash os floating ip list + [--network ] + [--port ] + [--fixed-ip-address ] + +.. option:: --network + + List floating IP(s) according to given network (name or ID) + + *Network version 2 only* + +.. option:: --port + + List floating IP(s) according to given port (name or ID) + + *Network version 2 only* + +.. option:: --fixed-ip-address + + List floating IP(s) according to given fixed IP address + + *Network version 2 only* floating ip show ---------------- diff --git a/openstackclient/network/v2/floating_ip.py b/openstackclient/network/v2/floating_ip.py index 7ae5d44816..2aac8e7bb7 100644 --- a/openstackclient/network/v2/floating_ip.py +++ b/openstackclient/network/v2/floating_ip.py @@ -205,7 +205,31 @@ class DeleteIPFloating(DeleteFloatingIP): class ListFloatingIP(common.NetworkAndComputeLister): """List floating IP(s)""" + def update_parser_network(self, parser): + parser.add_argument( + '--network', + metavar='', + help=_("List floating IP(s) according to " + "given network (name or ID)") + ) + parser.add_argument( + '--port', + metavar='', + help=_("List floating IP(s) according to " + "given port (name or ID)") + ) + parser.add_argument( + '--fixed-ip-address', + metavar='', + help=_("List floating IP(s) according to " + "given fixed IP address") + ) + + return parser + def take_action_network(self, client, parsed_args): + network_client = self.app.client_manager.network + columns = ( 'id', 'floating_ip_address', @@ -224,6 +248,18 @@ class ListFloatingIP(common.NetworkAndComputeLister): ) query = {} + + if parsed_args.network is not None: + network = network_client.find_network(parsed_args.network, + ignore_missing=False) + query['floating_network_id'] = network.id + if parsed_args.port is not None: + port = network_client.find_port(parsed_args.port, + ignore_missing=False) + query['port_id'] = port.id + if parsed_args.fixed_ip_address is not None: + query['fixed_ip_address'] = parsed_args.fixed_ip_address + data = client.ips(**query) return (headers, diff --git a/openstackclient/tests/unit/network/v2/test_floating_ip.py b/openstackclient/tests/unit/network/v2/test_floating_ip.py index 0454733b4d..10f3067d9a 100644 --- a/openstackclient/tests/unit/network/v2/test_floating_ip.py +++ b/openstackclient/tests/unit/network/v2/test_floating_ip.py @@ -231,6 +231,12 @@ class TestListFloatingIPNetwork(TestFloatingIPNetwork): # The floating ips to list up floating_ips = network_fakes.FakeFloatingIP.create_floating_ips(count=3) + fake_network = network_fakes.FakeNetwork.create_one_network({ + 'id': 'fake_network_id', + }) + fake_port = network_fakes.FakePort.create_one_port({ + 'id': 'fake_port_id', + }) columns = ( 'ID', @@ -256,6 +262,8 @@ class TestListFloatingIPNetwork(TestFloatingIPNetwork): super(TestListFloatingIPNetwork, self).setUp() self.network.ips = mock.Mock(return_value=self.floating_ips) + self.network.find_network = mock.Mock(return_value=self.fake_network) + self.network.find_port = mock.Mock(return_value=self.fake_port) # Get the command object to test self.cmd = floating_ip.ListFloatingIP(self.app, self.namespace) @@ -267,7 +275,58 @@ class TestListFloatingIPNetwork(TestFloatingIPNetwork): columns, data = self.cmd.take_action(parsed_args) - self.network.ips.assert_called_once_with(**{}) + self.network.ips.assert_called_once_with() + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_floating_ip_list_network(self): + arglist = [ + '--network', 'fake_network_id', + ] + verifylist = [ + ('network', 'fake_network_id'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ips.assert_called_once_with(**{ + 'floating_network_id': 'fake_network_id', + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_floating_ip_list_port(self): + arglist = [ + '--port', 'fake_port_id', + ] + verifylist = [ + ('port', 'fake_port_id'), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ips.assert_called_once_with(**{ + 'port_id': 'fake_port_id', + }) + self.assertEqual(self.columns, columns) + self.assertEqual(self.data, list(data)) + + def test_floating_ip_list_fixed_ip_address(self): + arglist = [ + '--fixed-ip-address', self.floating_ips[0].fixed_ip_address, + ] + verifylist = [ + ('fixed_ip_address', self.floating_ips[0].fixed_ip_address), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + columns, data = self.cmd.take_action(parsed_args) + + self.network.ips.assert_called_once_with(**{ + 'fixed_ip_address': self.floating_ips[0].fixed_ip_address, + }) self.assertEqual(self.columns, columns) self.assertEqual(self.data, list(data)) diff --git a/releasenotes/notes/bug-1614379-da92ded6d19f5ad5.yaml b/releasenotes/notes/bug-1614379-da92ded6d19f5ad5.yaml new file mode 100644 index 0000000000..a5ac74f131 --- /dev/null +++ b/releasenotes/notes/bug-1614379-da92ded6d19f5ad5.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Add ``--port``, ``--fixed-ip-address``, ``--network``, + options to ``floating ip list`` command + [Bug `1614379 `_] \ No newline at end of file