From: Akihiro Motoki Date: Wed, 19 Mar 2014 16:36:13 +0000 (+0900) Subject: NEC plugin: Allow to add prefix to OFC REST URL X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=3d2f3cbde7bb99ed1371bca835a8e63ddc6323d9;p=openstack-build%2Fneutron-build.git NEC plugin: Allow to add prefix to OFC REST URL Closes-Bug: #1295802 Change-Id: Ieaa3bb7c601fad98506168de1f8ac191849c6569 --- diff --git a/etc/neutron/plugins/nec/nec.ini b/etc/neutron/plugins/nec/nec.ini index 878f9e17a..bac5967d9 100644 --- a/etc/neutron/plugins/nec/nec.ini +++ b/etc/neutron/plugins/nec/nec.ini @@ -21,6 +21,10 @@ firewall_driver = neutron.agent.linux.iptables_firewall.OVSHybridIptablesFirewal # host = 127.0.0.1 # port = 8888 +# Base URL of OpenFlow Controller REST API. +# It is prepended to a path of each API request. +# path_prefix = + # Drivers are in neutron/plugins/nec/drivers/ . # driver = trema diff --git a/neutron/plugins/nec/common/config.py b/neutron/plugins/nec/common/config.py index 3717468a2..21c5e1824 100644 --- a/neutron/plugins/nec/common/config.py +++ b/neutron/plugins/nec/common/config.py @@ -36,6 +36,9 @@ agent_opts = [ ofc_opts = [ cfg.StrOpt('host', default='127.0.0.1', help=_("Host to connect to")), + cfg.StrOpt('path_prefix', default='', + help=_("Base URL of OFC REST API. " + "It is prepended to each API request.")), cfg.StrOpt('port', default='8888', help=_("Port to connect to")), cfg.StrOpt('driver', default='trema', diff --git a/neutron/plugins/nec/common/ofc_client.py b/neutron/plugins/nec/common/ofc_client.py index 6456cece8..b1e669300 100644 --- a/neutron/plugins/nec/common/ofc_client.py +++ b/neutron/plugins/nec/common/ofc_client.py @@ -70,6 +70,7 @@ class OFCClient(object): {'status': status, 'msg': detail}) def do_single_request(self, method, action, body=None): + action = config.OFC.path_prefix + action LOG.debug(_("Client request: %(host)s:%(port)s " "%(method)s %(action)s [%(body)s]"), {'host': self.host, 'port': self.port, diff --git a/neutron/tests/unit/nec/test_config.py b/neutron/tests/unit/nec/test_config.py index 907c33dfe..9d9fad521 100644 --- a/neutron/tests/unit/nec/test_config.py +++ b/neutron/tests/unit/nec/test_config.py @@ -28,6 +28,8 @@ class ConfigurationTest(base.BaseTestCase): self.assertEqual('127.0.0.1', config.CONF.OFC.host) self.assertEqual('8888', config.CONF.OFC.port) + # Check path_prefix is an empty string explicitly. + self.assertEqual('', config.CONF.OFC.path_prefix) self.assertEqual('trema', config.CONF.OFC.driver) self.assertTrue(config.CONF.OFC.enable_packet_filter) self.assertFalse(config.CONF.OFC.use_ssl) diff --git a/neutron/tests/unit/nec/test_ofc_client.py b/neutron/tests/unit/nec/test_ofc_client.py index 101cca841..68668d4d4 100644 --- a/neutron/tests/unit/nec/test_ofc_client.py +++ b/neutron/tests/unit/nec/test_ofc_client.py @@ -22,6 +22,7 @@ import socket import mock from oslo.config import cfg +from neutron.plugins.nec.common import config from neutron.plugins.nec.common import exceptions as nexc from neutron.plugins.nec.common import ofc_client from neutron.tests import base @@ -29,8 +30,8 @@ from neutron.tests import base class OFCClientTest(base.BaseTestCase): - def _test_do_request(self, status, resbody, data, exctype=None, - exc_checks=None): + def _test_do_request(self, status, resbody, expected_data, exctype=None, + exc_checks=None, path_prefix=None): res = mock.Mock() res.status = status res.read.return_value = resbody @@ -41,21 +42,22 @@ class OFCClientTest(base.BaseTestCase): with mock.patch.object(ofc_client.OFCClient, 'get_connection', return_value=conn): client = ofc_client.OFCClient() - + path = '/somewhere' + realpath = path_prefix + path if path_prefix else path if exctype: e = self.assertRaises(exctype, client.do_request, - 'GET', '/somewhere', body={}) - self.assertEqual(data, str(e)) + 'GET', path, body={}) + self.assertEqual(expected_data, str(e)) if exc_checks: for k, v in exc_checks.items(): self.assertEqual(v, getattr(e, k)) else: - response = client.do_request('GET', '/somewhere', body={}) - self.assertEqual(response, data) + response = client.do_request('GET', path, body={}) + self.assertEqual(response, expected_data) headers = {"Content-Type": "application/json"} expected = [ - mock.call.request('GET', '/somewhere', '{}', headers), + mock.call.request('GET', realpath, '{}', headers), mock.call.getresponse(), ] conn.assert_has_calls(expected) @@ -73,6 +75,11 @@ class OFCClientTest(base.BaseTestCase): for status in [201, 202, 204]: self._test_do_request(status, None, None) + def test_do_request_with_path_prefix(self): + config.CONF.set_override('path_prefix', '/dummy', group='OFC') + self._test_do_request(200, json.dumps([1, 2, 3]), [1, 2, 3], + path_prefix='/dummy') + def test_do_request_returns_404(self): resbody = '' errmsg = _("The specified OFC resource (/somewhere) is not found.")