From d99bbe80ca3f2000840e66032b983a01c99e0e75 Mon Sep 17 00:00:00 2001 From: reedip Date: Thu, 3 Mar 2016 12:13:03 +0900 Subject: [PATCH] Add/Remove port interface to a router Currently the interface actions(add/remove) on a router is limited to a subnet. Following patch introduces adding operation for port interface on a router as well. Change-Id: I9783bc4ccceae3d361dce52d51483ef2187920a9 --- openstack/network/v2/_proxy.py | 38 +++++++++++++-- openstack/network/v2/router.py | 10 ++-- .../tests/unit/network/v2/test_router.py | 47 +++++++++++++++---- 3 files changed, 77 insertions(+), 18 deletions(-) diff --git a/openstack/network/v2/_proxy.py b/openstack/network/v2/_proxy.py index d45bd1a1a..59ab084ea 100644 --- a/openstack/network/v2/_proxy.py +++ b/openstack/network/v2/_proxy.py @@ -1090,11 +1090,41 @@ class Proxy(proxy.BaseProxy): """ return self._update(_router.Router, router, **attrs) - def router_add_interface(self, router, subnet_id): - router.add_interface(self.session, subnet_id) + def router_add_interface(self, router, subnet_id=None, port_id=None): + """Add Interface to a router - def router_remove_interface(self, router, subnet_id): - router.remove_interface(self.session, subnet_id) + :param router: Either the router ID or an instance of + :class:`~openstack.network.v2.router.Router` + :param subnet_id: ID of the subnet + :param port_id: ID of the port + :returns: Router with updated interface + :rtype: :class: `~openstack.network.v2.router.Router` + """ + + body = {} + if port_id: + body = {'port_id': port_id} + else: + body = {'subnet_id': subnet_id} + return router.add_interface(self.session, **body) + + def router_remove_interface(self, router, subnet_id=None, port_id=None): + """Remove Interface from a router + + :param router: Either the router ID or an instance of + :class:`~openstack.network.v2.router.Router` + :param subnet: ID of the subnet + :param port: ID of the port + :returns: Router with updated interface + :rtype: :class: `~openstack.network.v2.router.Router` + """ + + body = {} + if port_id: + body = {'port_id': port_id} + else: + body = {'subnet_id': subnet_id} + return router.remove_interface(self.session, **body) def create_security_group(self, **attrs): """Create a new security group from attributes diff --git a/openstack/network/v2/router.py b/openstack/network/v2/router.py index a00759335..b92253497 100644 --- a/openstack/network/v2/router.py +++ b/openstack/network/v2/router.py @@ -55,30 +55,28 @@ class Router(resource.Resource): # The extra routes configuration for the router. routes = resource.prop('routes', type=list) - def add_interface(self, session, subnet_id): + def add_interface(self, session, **body): """Add an internal interface to a logical router. :param session: The session to communicate through. :type session: :class:`~openstack.session.Session` - :param str subnet_id: The ID of a subnet to add. + :param dict body : The body requested to be updated on the outer :returns: The body of the response as a dictionary. """ - body = {'subnet_id': subnet_id} url = utils.urljoin(self.base_path, self.id, 'add_router_interface') resp = session.put(url, endpoint_filter=self.service, json=body) return resp.json() - def remove_interface(self, session, subnet_id): + def remove_interface(self, session, **body): """Remove an internal interface from a logical router. :param session: The session to communicate through. :type session: :class:`~openstack.session.Session` - :param str subnet_id: The ID of a subnet to remove. + :param dict body : The body requested to be updated on the outer :returns: The body of the response as a dictionary. """ - body = {'subnet_id': subnet_id} url = utils.urljoin(self.base_path, self.id, 'remove_router_interface') resp = session.put(url, endpoint_filter=self.service, json=body) return resp.json() diff --git a/openstack/tests/unit/network/v2/test_router.py b/openstack/tests/unit/network/v2/test_router.py index 585168796..dbc0d7114 100644 --- a/openstack/tests/unit/network/v2/test_router.py +++ b/openstack/tests/unit/network/v2/test_router.py @@ -95,32 +95,63 @@ class TestRouter(testtools.TestCase): self.assertEqual(EXAMPLE_WITH_OPTIONAL['availability_zones'], sot.availability_zones) - def test_add_interface(self): + def test_add_interface_subnet(self): + # Add subnet to a router sot = router.Router(EXAMPLE) response = mock.Mock() response.body = {"subnet_id": "3", "port_id": "2"} response.json = mock.Mock(return_value=response.body) sess = mock.Mock() sess.put = mock.Mock(return_value=response) - - self.assertEqual(response.body, sot.add_interface(sess, '3')) + body = {"subnet_id": "3"} + self.assertEqual(response.body, sot.add_interface(sess, **body)) url = 'routers/IDENTIFIER/add_router_interface' - body = {"subnet_id": "3"} sess.put.assert_called_with(url, endpoint_filter=sot.service, json=body) - def test_remove_interface(self): + def test_add_interface_port(self): + # Add port to a router + sot = router.Router(EXAMPLE) + response = mock.Mock() + response.body = {"subnet_id": "3", "port_id": "3"} + response.json = mock.Mock(return_value=response.body) + sess = mock.Mock() + sess.put = mock.Mock(return_value=response) + + body = {"port_id": "3"} + self.assertEqual(response.body, sot.add_interface(sess, **body)) + + url = 'routers/IDENTIFIER/add_router_interface' + sess.put.assert_called_with(url, endpoint_filter=sot.service, + json=body) + + def test_remove_interface_subnet(self): + # Remove subnet from a router sot = router.Router(EXAMPLE) response = mock.Mock() response.body = {"subnet_id": "3", "port_id": "2"} response.json = mock.Mock(return_value=response.body) sess = mock.Mock() sess.put = mock.Mock(return_value=response) - - self.assertEqual(response.body, sot.remove_interface(sess, '3')) + body = {"subnet_id": "3"} + self.assertEqual(response.body, sot.remove_interface(sess, **body)) + + url = 'routers/IDENTIFIER/remove_router_interface' + sess.put.assert_called_with(url, endpoint_filter=sot.service, + json=body) + + def test_remove_interface_port(self): + # Remove port from a router + sot = router.Router(EXAMPLE) + response = mock.Mock() + response.body = {"subnet_id": "3", "port_id": "3"} + response.json = mock.Mock(return_value=response.body) + sess = mock.Mock() + sess.put = mock.Mock(return_value=response) + body = {"port_id": "3"} + self.assertEqual(response.body, sot.remove_interface(sess, **body)) url = 'routers/IDENTIFIER/remove_router_interface' - body = {"subnet_id": "3"} sess.put.assert_called_with(url, endpoint_filter=sot.service, json=body)