From 2e94f2803fca3862589fe2b10c76c2ebc9e17229 Mon Sep 17 00:00:00 2001 From: reedip Date: Thu, 18 Feb 2016 17:12:05 +0900 Subject: [PATCH] Add "router add port" to osc This patch proposes the migration of port addition of Neutron's "router-interface-add" to OSC's "router add port". Change-Id: I3a8a76d384caa32975f8e77f50f3cf3c3be13786 Closes-Bug: #1546849 Depends-On: I9783bc4ccceae3d361dce52d51483ef2187920a9 Implements: blueprint neutron-client-advanced-router --- doc/source/command-objects/router.rst | 25 ++++++++++- openstackclient/network/v2/router.py | 26 +++++++++++- .../tests/network/v2/test_router.py | 41 +++++++++++++++++++ .../router-port-add-0afe7392c080bcb8.yaml | 5 +++ setup.cfg | 1 + 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 releasenotes/notes/router-port-add-0afe7392c080bcb8.yaml diff --git a/doc/source/command-objects/router.rst b/doc/source/command-objects/router.rst index 6b8b357b1c..9d094d3769 100644 --- a/doc/source/command-objects/router.rst +++ b/doc/source/command-objects/router.rst @@ -4,6 +4,27 @@ router Network v2 +router add port +--------------- +Add a port to a router + +.. program:: router add port +.. code:: bash + + os router add port + + + +.. _router_add_port: + +.. describe:: + + Router to which port will be added (name or ID) + +.. describe:: + + Port to be added (name or ID) + router create ------------- @@ -16,7 +37,7 @@ Create new router [--project [--project-domain ]] [--enable | --disable] [--distributed] - [--availability-zone-hint ] + [--availability-zone-hint ] .. option:: --project @@ -146,4 +167,4 @@ Display router details .. _router_show-router: .. describe:: - Router to display (name or ID) + Router to display (name or ID) \ No newline at end of file diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index 96aa55b2f1..9fcb8bce13 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -82,6 +82,30 @@ def _get_attrs(client_manager, parsed_args): return attrs +class AddPortToRouter(command.Command): + """Add a port to a router""" + + def get_parser(self, prog_name): + parser = super(AddPortToRouter, self).get_parser(prog_name) + parser.add_argument( + 'router', + metavar='', + help="Router to which port will be added (name or ID)", + ) + parser.add_argument( + 'port', + metavar='', + help="Port to be added (name or ID)", + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + port = client.find_port(parsed_args.port, ignore_missing=False) + client.router_add_interface(client.find_router( + parsed_args.router, ignore_missing=False), port_id=port.id) + + class CreateRouter(command.ShowOne): """Create a new router""" @@ -318,4 +342,4 @@ class ShowRouter(command.ShowOne): obj = client.find_router(parsed_args.router, ignore_missing=False) columns = _get_columns(obj) data = utils.get_item_properties(obj, columns, formatters=_formatters) - return columns, data + return columns, data \ No newline at end of file diff --git a/openstackclient/tests/network/v2/test_router.py b/openstackclient/tests/network/v2/test_router.py index 68c225e77d..03a00003e7 100644 --- a/openstackclient/tests/network/v2/test_router.py +++ b/openstackclient/tests/network/v2/test_router.py @@ -29,6 +29,47 @@ class TestRouter(network_fakes.TestNetworkV2): self.network = self.app.client_manager.network +class TestAddPortToRouter(TestRouter): + '''Add port to Router ''' + + _port = network_fakes.FakePort.create_one_port() + _router = network_fakes.FakeRouter.create_one_router( + attrs={'port': _port.id}) + + def setUp(self): + super(TestAddPortToRouter, self).setUp() + self.network.router_add_interface = mock.Mock() + self.cmd = router.AddPortToRouter(self.app, self.namespace) + self.network.find_router = mock.Mock(return_value=self._router) + self.network.find_port = mock.Mock(return_value=self._port) + + def test_add_port_no_option(self): + arglist = [] + verifylist = [] + + # Missing required args should bail here + self.assertRaises(tests_utils.ParserException, self.check_parser, + self.cmd, arglist, verifylist) + + def test_add_port_required_options(self): + arglist = [ + self._router.id, + self._router.port, + ] + verifylist = [ + ('router', self._router.id), + ('port', self._router.port), + ] + parsed_args = self.check_parser(self.cmd, arglist, verifylist) + + result = self.cmd.take_action(parsed_args) + + self.network.router_add_interface.assert_called_with(self._router, **{ + 'port_id': self._router.port, + }) + self.assertIsNone(result) + + class TestCreateRouter(TestRouter): # The new router created. diff --git a/releasenotes/notes/router-port-add-0afe7392c080bcb8.yaml b/releasenotes/notes/router-port-add-0afe7392c080bcb8.yaml new file mode 100644 index 0000000000..1e2ee7ed32 --- /dev/null +++ b/releasenotes/notes/router-port-add-0afe7392c080bcb8.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``router add port`` command + [Bug `1546849 `_] diff --git a/setup.cfg b/setup.cfg index 728b5d646b..2fc3f5ce92 100644 --- a/setup.cfg +++ b/setup.cfg @@ -338,6 +338,7 @@ openstack.network.v2 = port_list = openstackclient.network.v2.port:ListPort port_show = openstackclient.network.v2.port:ShowPort + router_add_port = openstackclient.network.v2.router:AddPortToRouter router_create = openstackclient.network.v2.router:CreateRouter router_delete = openstackclient.network.v2.router:DeleteRouter router_list = openstackclient.network.v2.router:ListRouter