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
This commit is contained in:
reedip
2016-03-03 12:13:03 +09:00
committed by Reedip
parent 1417c9a3a3
commit d99bbe80ca
3 changed files with 77 additions and 18 deletions

View File

@@ -1090,11 +1090,41 @@ class Proxy(proxy.BaseProxy):
""" """
return self._update(_router.Router, router, **attrs) return self._update(_router.Router, router, **attrs)
def router_add_interface(self, router, subnet_id): def router_add_interface(self, router, subnet_id=None, port_id=None):
router.add_interface(self.session, subnet_id) """Add Interface to a router
def router_remove_interface(self, router, subnet_id): :param router: Either the router ID or an instance of
router.remove_interface(self.session, subnet_id) :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): def create_security_group(self, **attrs):
"""Create a new security group from attributes """Create a new security group from attributes

View File

@@ -55,30 +55,28 @@ class Router(resource.Resource):
# The extra routes configuration for the router. # The extra routes configuration for the router.
routes = resource.prop('routes', type=list) 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. """Add an internal interface to a logical router.
:param session: The session to communicate through. :param session: The session to communicate through.
:type session: :class:`~openstack.session.Session` :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. :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') url = utils.urljoin(self.base_path, self.id, 'add_router_interface')
resp = session.put(url, endpoint_filter=self.service, json=body) resp = session.put(url, endpoint_filter=self.service, json=body)
return resp.json() return resp.json()
def remove_interface(self, session, subnet_id): def remove_interface(self, session, **body):
"""Remove an internal interface from a logical router. """Remove an internal interface from a logical router.
:param session: The session to communicate through. :param session: The session to communicate through.
:type session: :class:`~openstack.session.Session` :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. :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') url = utils.urljoin(self.base_path, self.id, 'remove_router_interface')
resp = session.put(url, endpoint_filter=self.service, json=body) resp = session.put(url, endpoint_filter=self.service, json=body)
return resp.json() return resp.json()

View File

@@ -95,32 +95,63 @@ class TestRouter(testtools.TestCase):
self.assertEqual(EXAMPLE_WITH_OPTIONAL['availability_zones'], self.assertEqual(EXAMPLE_WITH_OPTIONAL['availability_zones'],
sot.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) sot = router.Router(EXAMPLE)
response = mock.Mock() response = mock.Mock()
response.body = {"subnet_id": "3", "port_id": "2"} response.body = {"subnet_id": "3", "port_id": "2"}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {"subnet_id": "3"}
self.assertEqual(response.body, sot.add_interface(sess, '3')) self.assertEqual(response.body, sot.add_interface(sess, **body))
url = 'routers/IDENTIFIER/add_router_interface' url = 'routers/IDENTIFIER/add_router_interface'
body = {"subnet_id": "3"}
sess.put.assert_called_with(url, endpoint_filter=sot.service, sess.put.assert_called_with(url, endpoint_filter=sot.service,
json=body) 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) sot = router.Router(EXAMPLE)
response = mock.Mock() response = mock.Mock()
response.body = {"subnet_id": "3", "port_id": "2"} response.body = {"subnet_id": "3", "port_id": "2"}
response.json = mock.Mock(return_value=response.body) response.json = mock.Mock(return_value=response.body)
sess = mock.Mock() sess = mock.Mock()
sess.put = mock.Mock(return_value=response) sess.put = mock.Mock(return_value=response)
body = {"subnet_id": "3"}
self.assertEqual(response.body, sot.remove_interface(sess, '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' url = 'routers/IDENTIFIER/remove_router_interface'
body = {"subnet_id": "3"}
sess.put.assert_called_with(url, endpoint_filter=sot.service, sess.put.assert_called_with(url, endpoint_filter=sot.service,
json=body) json=body)