Enforce required config params for ODL driver

Raise a config error during initialization if
there is no URL, username, or password specified
in the config for the OpenDayLight ML2 driver.

Closes-Bug: #1301432
Change-Id: I65fc94d3eaaade3d1402d1c82d2c1edfa7133d5a
This commit is contained in:
Kevin Benton 2014-04-06 04:29:07 -07:00 committed by Gerrit Code Review
parent d3cb1aa547
commit 429ac5a718
2 changed files with 46 additions and 11 deletions

View File

@ -129,6 +129,10 @@ class OpenDaylightMechanismDriver(api.MechanismDriver):
self.timeout = cfg.CONF.ml2_odl.timeout
self.username = cfg.CONF.ml2_odl.username
self.password = cfg.CONF.ml2_odl.password
required_opts = ('url', 'username', 'password')
for opt in required_opts:
if not getattr(self, opt):
raise cfg.RequiredOptError(opt, 'ml2_odl')
self.auth = JsessionId(self.url, self.username, self.password)
self.vif_type = portbindings.VIF_TYPE_OVS
self.vif_details = {portbindings.CAP_PORT_FILTER: True}
@ -311,18 +315,17 @@ class OpenDaylightMechanismDriver(api.MechanismDriver):
headers = {'Content-Type': 'application/json'}
data = jsonutils.dumps(obj, indent=2) if obj else None
if self.url:
url = '/'.join([self.url, urlpath])
LOG.debug(_('ODL-----> sending URL (%s) <-----ODL') % url)
LOG.debug(_('ODL-----> sending JSON (%s) <-----ODL') % obj)
r = requests.request(method, url=url,
headers=headers, data=data,
auth=self.auth, timeout=self.timeout)
url = '/'.join([self.url, urlpath])
LOG.debug(_('ODL-----> sending URL (%s) <-----ODL') % url)
LOG.debug(_('ODL-----> sending JSON (%s) <-----ODL') % obj)
r = requests.request(method, url=url,
headers=headers, data=data,
auth=self.auth, timeout=self.timeout)
# ignorecodes contains a list of HTTP error codes to ignore.
if r.status_code in ignorecodes:
return
r.raise_for_status()
# ignorecodes contains a list of HTTP error codes to ignore.
if r.status_code in ignorecodes:
return
r.raise_for_status()
def bind_port(self, context):
LOG.debug(_("Attempting to bind port %(port)s on "

View File

@ -32,6 +32,12 @@ class OpenDaylightTestCase(test_plugin.NeutronDbPluginV2TestCase):
config.cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight'],
'ml2')
# Set URL/user/pass so init doesn't throw a cfg required error.
# They are not used in these tests since sendjson is overwritten.
config.cfg.CONF.set_override('url', 'http://127.0.0.1:9999', 'ml2_odl')
config.cfg.CONF.set_override('username', 'someuser', 'ml2_odl')
config.cfg.CONF.set_override('password', 'somepass', 'ml2_odl')
super(OpenDaylightTestCase, self).setUp(PLUGIN_NAME)
self.port_create_status = 'DOWN'
self.segment = {'api.NETWORK_TYPE': ""}
@ -59,6 +65,32 @@ class OpenDaylightTestCase(test_plugin.NeutronDbPluginV2TestCase):
self.assertFalse(self.mech.check_segment(self.segment))
class OpenDayLightMechanismConfigTests(test_plugin.NeutronDbPluginV2TestCase):
def _setUp(self):
config.cfg.CONF.set_override('mechanism_drivers',
['logger', 'opendaylight'],
'ml2')
config.cfg.CONF.set_override('url', 'http://127.0.0.1:9999', 'ml2_odl')
config.cfg.CONF.set_override('username', 'someuser', 'ml2_odl')
config.cfg.CONF.set_override('password', 'somepass', 'ml2_odl')
def test_url_required(self):
self._setUp()
config.cfg.CONF.set_override('url', None, 'ml2_odl')
self.assertRaises(config.cfg.RequiredOptError, self.setUp, PLUGIN_NAME)
def test_username_required(self):
self._setUp()
config.cfg.CONF.set_override('username', None, 'ml2_odl')
self.assertRaises(config.cfg.RequiredOptError, self.setUp, PLUGIN_NAME)
def test_password_required(self):
self._setUp()
config.cfg.CONF.set_override('password', None, 'ml2_odl')
self.assertRaises(config.cfg.RequiredOptError, self.setUp, PLUGIN_NAME)
class OpenDaylightMechanismTestBasicGet(test_plugin.TestBasicGet,
OpenDaylightTestCase):
pass