diff --git a/doc/source/command-objects/router.rst b/doc/source/command-objects/router.rst index 9d094d3769..d4e3f4a5e2 100644 --- a/doc/source/command-objects/router.rst +++ b/doc/source/command-objects/router.rst @@ -6,6 +6,7 @@ Network v2 router add port --------------- + Add a port to a router .. program:: router add port @@ -102,6 +103,29 @@ List routers List additional fields in output +router remove port +------------------ + +Remove a port from a router + +.. program:: router remove port +.. code:: bash + + os router remove port + + + +.. _router_remove_port: + +.. describe:: + + Router from which port will be removed (name or ID) + +.. describe:: + + Port to be removed (name or ID) + + router set ---------- @@ -167,4 +191,4 @@ Display router details .. _router_show-router: .. describe:: - Router to display (name or ID) \ No newline at end of file + Router to display (name or ID) diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index 9fcb8bce13..caf6d5ce23 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -241,6 +241,30 @@ class ListRouter(command.Lister): ) for s in data)) +class RemovePortFromRouter(command.Command): + """Remove a port from a router""" + + def get_parser(self, prog_name): + parser = super(RemovePortFromRouter, self).get_parser(prog_name) + parser.add_argument( + 'router', + metavar='', + help="Router from which port will be removed (name or ID)", + ) + parser.add_argument( + 'port', + metavar='', + help="Port to be removed (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_remove_interface(client.find_router( + parsed_args.router, ignore_missing=False), port_id=port.id) + + class SetRouter(command.Command): """Set router properties""" @@ -342,4 +366,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 \ No newline at end of file + return columns, data diff --git a/openstackclient/tests/network/v2/test_router.py b/openstackclient/tests/network/v2/test_router.py index 03a00003e7..40941fbcc5 100644 --- a/openstackclient/tests/network/v2/test_router.py +++ b/openstackclient/tests/network/v2/test_router.py @@ -269,6 +269,46 @@ class TestListRouter(TestRouter): self.assertEqual(self.data_long, list(data)) +class TestRemovePortFromRouter(TestRouter): + '''Remove port from a Router ''' + + _port = network_fakes.FakePort.create_one_port() + _router = network_fakes.FakeRouter.create_one_router( + attrs={'port': _port.id}) + + def setUp(self): + super(TestRemovePortFromRouter, self).setUp() + self.network.router_remove_interface = mock.Mock() + self.cmd = router.RemovePortFromRouter(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_remove_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_remove_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_remove_interface.assert_called_with( + self._router, **{'port_id': self._router.port}) + self.assertIsNone(result) + + class TestSetRouter(TestRouter): # The router to set. diff --git a/releasenotes/notes/router-remove-port-058078c93819b0f4.yaml b/releasenotes/notes/router-remove-port-058078c93819b0f4.yaml new file mode 100644 index 0000000000..187026a7d3 --- /dev/null +++ b/releasenotes/notes/router-remove-port-058078c93819b0f4.yaml @@ -0,0 +1,5 @@ +--- +features: + - | + Add ``router remove port`` command + [Bug `1546849 `_] diff --git a/setup.cfg b/setup.cfg index 2fc3f5ce92..942496833a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -342,6 +342,7 @@ openstack.network.v2 = router_create = openstackclient.network.v2.router:CreateRouter router_delete = openstackclient.network.v2.router:DeleteRouter router_list = openstackclient.network.v2.router:ListRouter + router_remove_port = openstackclient.network.v2.router:RemovePortFromRouter router_set = openstackclient.network.v2.router:SetRouter router_show = openstackclient.network.v2.router:ShowRouter