Add Subnet add/remove support to router

The following patch adds the support for
"router add subnet" and "router remove subnet"
to the OSC as a part of migration of Neutron's
CLI commands.

Partial-Bug: #1546849
Implements: blueprint neutron-client-advanced-router
Change-Id: Ia3770c41026194bdb1543d4e67446f81936d44d1
This commit is contained in:
reedip 2016-03-20 16:05:49 +09:00 committed by Reedip
parent 8ecdc57ea6
commit 9e42daa577
5 changed files with 186 additions and 0 deletions

View File

@ -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>
<subnet>
.. _router_add_subnet:
.. describe:: <router>
Router to which subnet will be added (name or ID)
.. describe:: <subnet>
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>
<subnet>
.. _router_remove_subnet:
.. describe:: <router>
Router from which subnet will be removed (name or ID)
.. describe:: <subnet>
Subnet to be removed (name or ID)
router set
----------

View File

@ -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='<router>',
help="Router to which subnet will be added (name or ID)",
)
parser.add_argument(
'subnet',
metavar='<subnet>',
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='<router>',
help="Router from which the subnet will be removed (name or ID)",
)
parser.add_argument(
'subnet',
metavar='<subnet>',
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"""

View File

@ -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.

View File

@ -0,0 +1,8 @@
---
features:
- |
Add ``router add subnet`` command
[Bug `1546849 <https://bugs.launchpad.net/bugs/1546849>`_]
- |
Add ``router remove subnet`` command
[Bug `1546849 <https://bugs.launchpad.net/bugs/1546849>`_]

View File

@ -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