diff --git a/doc/source/command-objects/router.rst b/doc/source/command-objects/router.rst index d4e3f4a5e2..9479af5f5d 100644 --- a/doc/source/command-objects/router.rst +++ b/doc/source/command-objects/router.rst @@ -26,6 +26,28 @@ Add a port to a router Port to be added (name or ID) +router add subnet +----------------- + +Add a subnet to a router + +.. program:: router add subnet +.. code:: bash + + os router add subnet + + + +.. _router_add_subnet: + +.. describe:: + + Router to which subnet will be added (name or ID) + +.. describe:: + + Subnet to be added (name or ID) + router create ------------- @@ -125,6 +147,27 @@ Remove a port from a router Port to be removed (name or ID) +router remove subnet +-------------------- + +Remove a subnet from a router + +.. program:: router remove subnet +.. code:: bash + + os router remove subnet + + + +.. _router_remove_subnet: + +.. describe:: + + Router from which subnet will be removed (name or ID) + +.. describe:: + + Subnet to be removed (name or ID) router set ---------- diff --git a/openstackclient/network/v2/router.py b/openstackclient/network/v2/router.py index caf6d5ce23..f4f12087d6 100644 --- a/openstackclient/network/v2/router.py +++ b/openstackclient/network/v2/router.py @@ -106,6 +106,33 @@ class AddPortToRouter(command.Command): parsed_args.router, ignore_missing=False), port_id=port.id) +class AddSubnetToRouter(command.Command): + """Add a subnet to a router""" + + def get_parser(self, prog_name): + parser = super(AddSubnetToRouter, self).get_parser(prog_name) + parser.add_argument( + 'router', + metavar='', + help="Router to which subnet will be added (name or ID)", + ) + parser.add_argument( + 'subnet', + metavar='', + help="Subnet to be added (name or ID)", + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + subnet = client.find_subnet(parsed_args.subnet, + ignore_missing=False) + client.router_add_interface( + client.find_router(parsed_args.router, + ignore_missing=False), + subnet_id=subnet.id) + + class CreateRouter(command.ShowOne): """Create a new router""" @@ -265,6 +292,33 @@ class RemovePortFromRouter(command.Command): parsed_args.router, ignore_missing=False), port_id=port.id) +class RemoveSubnetFromRouter(command.Command): + """Remove a subnet from a router""" + + def get_parser(self, prog_name): + parser = super(RemoveSubnetFromRouter, self).get_parser(prog_name) + parser.add_argument( + 'router', + metavar='', + help="Router from which the subnet will be removed (name or ID)", + ) + parser.add_argument( + 'subnet', + metavar='', + help="Subnet to be removed (name or ID)", + ) + return parser + + def take_action(self, parsed_args): + client = self.app.client_manager.network + subnet = client.find_subnet(parsed_args.subnet, + ignore_missing=False) + client.router_remove_interface( + client.find_router(parsed_args.router, + ignore_missing=False), + subnet_id=subnet.id) + + class SetRouter(command.Command): """Set router properties""" diff --git a/openstackclient/tests/network/v2/test_router.py b/openstackclient/tests/network/v2/test_router.py index 40941fbcc5..8de4b3b2d2 100644 --- a/openstackclient/tests/network/v2/test_router.py +++ b/openstackclient/tests/network/v2/test_router.py @@ -70,6 +70,46 @@ class TestAddPortToRouter(TestRouter): self.assertIsNone(result) +class TestAddSubnetToRouter(TestRouter): + '''Add subnet to Router ''' + + _subnet = network_fakes.FakeSubnet.create_one_subnet() + _router = network_fakes.FakeRouter.create_one_router( + attrs={'subnet': _subnet.id}) + + def setUp(self): + super(TestAddSubnetToRouter, self).setUp() + self.network.router_add_interface = mock.Mock() + self.cmd = router.AddSubnetToRouter(self.app, self.namespace) + self.network.find_router = mock.Mock(return_value=self._router) + self.network.find_subnet = mock.Mock(return_value=self._subnet) + + def test_add_subnet_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_subnet_required_options(self): + arglist = [ + self._router.id, + self._router.subnet, + ] + verifylist = [ + ('router', self._router.id), + ('subnet', self._router.subnet), + ] + 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, **{'subnet_id': self._router.subnet}) + + self.assertIsNone(result) + + class TestCreateRouter(TestRouter): # The new router created. @@ -309,6 +349,45 @@ class TestRemovePortFromRouter(TestRouter): self.assertIsNone(result) +class TestRemoveSubnetFromRouter(TestRouter): + '''Remove subnet from Router ''' + + _subnet = network_fakes.FakeSubnet.create_one_subnet() + _router = network_fakes.FakeRouter.create_one_router( + attrs={'subnet': _subnet.id}) + + def setUp(self): + super(TestRemoveSubnetFromRouter, self).setUp() + self.network.router_remove_interface = mock.Mock() + self.cmd = router.RemoveSubnetFromRouter(self.app, self.namespace) + self.network.find_router = mock.Mock(return_value=self._router) + self.network.find_subnet = mock.Mock(return_value=self._subnet) + + def test_remove_subnet_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_subnet_required_options(self): + arglist = [ + self._router.id, + self._router.subnet, + ] + verifylist = [ + ('subnet', self._router.subnet), + ('router', self._router.id), + ] + 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, **{'subnet_id': self._router.subnet}) + self.assertIsNone(result) + + class TestSetRouter(TestRouter): # The router to set. diff --git a/releasenotes/notes/router-subnet-469d095ae0bac884.yaml b/releasenotes/notes/router-subnet-469d095ae0bac884.yaml new file mode 100644 index 0000000000..db94b641e6 --- /dev/null +++ b/releasenotes/notes/router-subnet-469d095ae0bac884.yaml @@ -0,0 +1,8 @@ +--- +features: + - | + Add ``router add subnet`` command + [Bug `1546849 `_] + - | + Add ``router remove subnet`` command + [Bug `1546849 `_] diff --git a/setup.cfg b/setup.cfg index 942496833a..3af3b3503f 100644 --- a/setup.cfg +++ b/setup.cfg @@ -339,10 +339,12 @@ openstack.network.v2 = port_show = openstackclient.network.v2.port:ShowPort router_add_port = openstackclient.network.v2.router:AddPortToRouter + router_add_subnet = openstackclient.network.v2.router:AddSubnetToRouter 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_remove_subnet = openstackclient.network.v2.router:RemoveSubnetFromRouter router_set = openstackclient.network.v2.router:SetRouter router_show = openstackclient.network.v2.router:ShowRouter