Merge "NEC plugin: Allow to add prefix to OFC REST URL"

This commit is contained in:
Jenkins 2014-03-22 02:09:12 +00:00 committed by Gerrit Code Review
commit aa8acad98f
5 changed files with 25 additions and 8 deletions

View File

@ -21,6 +21,10 @@ firewall_driver = neutron.agent.linux.iptables_firewall.OVSHybridIptablesFirewal
# host = 127.0.0.1 # host = 127.0.0.1
# port = 8888 # 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/ . # Drivers are in neutron/plugins/nec/drivers/ .
# driver = trema # driver = trema

View File

@ -36,6 +36,9 @@ agent_opts = [
ofc_opts = [ ofc_opts = [
cfg.StrOpt('host', default='127.0.0.1', cfg.StrOpt('host', default='127.0.0.1',
help=_("Host to connect to")), 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', cfg.StrOpt('port', default='8888',
help=_("Port to connect to")), help=_("Port to connect to")),
cfg.StrOpt('driver', default='trema', cfg.StrOpt('driver', default='trema',

View File

@ -71,6 +71,7 @@ class OFCClient(object):
{'status': status, 'msg': detail}) {'status': status, 'msg': detail})
def do_single_request(self, method, action, body=None): def do_single_request(self, method, action, body=None):
action = config.OFC.path_prefix + action
LOG.debug(_("Client request: %(host)s:%(port)s " LOG.debug(_("Client request: %(host)s:%(port)s "
"%(method)s %(action)s [%(body)s]"), "%(method)s %(action)s [%(body)s]"),
{'host': self.host, 'port': self.port, {'host': self.host, 'port': self.port,

View File

@ -28,6 +28,8 @@ class ConfigurationTest(base.BaseTestCase):
self.assertEqual('127.0.0.1', config.CONF.OFC.host) self.assertEqual('127.0.0.1', config.CONF.OFC.host)
self.assertEqual('8888', config.CONF.OFC.port) 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.assertEqual('trema', config.CONF.OFC.driver)
self.assertTrue(config.CONF.OFC.enable_packet_filter) self.assertTrue(config.CONF.OFC.enable_packet_filter)
self.assertFalse(config.CONF.OFC.use_ssl) self.assertFalse(config.CONF.OFC.use_ssl)

View File

@ -22,6 +22,7 @@ import socket
import mock import mock
from oslo.config import cfg 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 exceptions as nexc
from neutron.plugins.nec.common import ofc_client from neutron.plugins.nec.common import ofc_client
from neutron.tests import base from neutron.tests import base
@ -29,8 +30,8 @@ from neutron.tests import base
class OFCClientTest(base.BaseTestCase): class OFCClientTest(base.BaseTestCase):
def _test_do_request(self, status, resbody, data, exctype=None, def _test_do_request(self, status, resbody, expected_data, exctype=None,
exc_checks=None): exc_checks=None, path_prefix=None):
res = mock.Mock() res = mock.Mock()
res.status = status res.status = status
res.read.return_value = resbody res.read.return_value = resbody
@ -41,21 +42,22 @@ class OFCClientTest(base.BaseTestCase):
with mock.patch.object(ofc_client.OFCClient, 'get_connection', with mock.patch.object(ofc_client.OFCClient, 'get_connection',
return_value=conn): return_value=conn):
client = ofc_client.OFCClient() client = ofc_client.OFCClient()
path = '/somewhere'
realpath = path_prefix + path if path_prefix else path
if exctype: if exctype:
e = self.assertRaises(exctype, client.do_request, e = self.assertRaises(exctype, client.do_request,
'GET', '/somewhere', body={}) 'GET', path, body={})
self.assertEqual(data, str(e)) self.assertEqual(expected_data, str(e))
if exc_checks: if exc_checks:
for k, v in exc_checks.items(): for k, v in exc_checks.items():
self.assertEqual(v, getattr(e, k)) self.assertEqual(v, getattr(e, k))
else: else:
response = client.do_request('GET', '/somewhere', body={}) response = client.do_request('GET', path, body={})
self.assertEqual(response, data) self.assertEqual(response, expected_data)
headers = {"Content-Type": "application/json"} headers = {"Content-Type": "application/json"}
expected = [ expected = [
mock.call.request('GET', '/somewhere', '{}', headers), mock.call.request('GET', realpath, '{}', headers),
mock.call.getresponse(), mock.call.getresponse(),
] ]
conn.assert_has_calls(expected) conn.assert_has_calls(expected)
@ -73,6 +75,11 @@ class OFCClientTest(base.BaseTestCase):
for status in [201, 202, 204]: for status in [201, 202, 204]:
self._test_do_request(status, None, None) 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): def test_do_request_returns_404(self):
resbody = '' resbody = ''
errmsg = _("The specified OFC resource (/somewhere) is not found.") errmsg = _("The specified OFC resource (/somewhere) is not found.")