Get rid of ConfigParser code in ProviderConfiguration
We may be unable to get rid of this code, but at least we can make it slimmer and more oslo_config friendly. This patch also fixes an issue where plain ConfigParser cannot handle config options with the same name under same section. Related-bug: #1492069 Change-Id: I596613eb7b2fc3e894a3c1cc7b7cb24d7137cbc5
This commit is contained in:
parent
e95904060a
commit
522b278963
|
@ -18,7 +18,6 @@ import os
|
||||||
|
|
||||||
from oslo_config import cfg
|
from oslo_config import cfg
|
||||||
from oslo_log import log as logging
|
from oslo_log import log as logging
|
||||||
from six.moves import configparser as ConfigParser
|
|
||||||
import stevedore
|
import stevedore
|
||||||
|
|
||||||
from neutron.common import exceptions as n_exc
|
from neutron.common import exceptions as n_exc
|
||||||
|
@ -62,23 +61,21 @@ class NeutronModule(object):
|
||||||
return self.repo['mod']
|
return self.repo['mod']
|
||||||
|
|
||||||
# Return an INI parser for the child module
|
# Return an INI parser for the child module
|
||||||
def ini(self):
|
def ini(self, neutron_dir=None):
|
||||||
if self.repo['ini'] is None:
|
if self.repo['ini'] is None:
|
||||||
neutron_dir = None
|
|
||||||
try:
|
try:
|
||||||
neutron_dir = cfg.CONF.config_dir
|
neutron_dir = neutron_dir or cfg.CONF.config_dir
|
||||||
except cfg.NoSuchOptError:
|
except cfg.NoSuchOptError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if neutron_dir is None:
|
if neutron_dir is None:
|
||||||
neutron_dir = '/etc/neutron'
|
neutron_dir = '/etc/neutron'
|
||||||
|
|
||||||
ini = ConfigParser.SafeConfigParser()
|
ini_file = cfg.ConfigOpts()
|
||||||
|
ini_file.register_opts(serviceprovider_opts, 'service_providers')
|
||||||
ini_path = os.path.join(neutron_dir, '%s.conf' % self.module_name)
|
ini_path = os.path.join(neutron_dir, '%s.conf' % self.module_name)
|
||||||
if os.path.exists(ini_path):
|
if os.path.exists(ini_path):
|
||||||
ini.read(ini_path)
|
ini_file(['--config-file', ini_path])
|
||||||
|
self.repo['ini'] = ini_file
|
||||||
self.repo['ini'] = ini
|
|
||||||
|
|
||||||
return self.repo['ini']
|
return self.repo['ini']
|
||||||
|
|
||||||
|
@ -102,13 +99,7 @@ class NeutronModule(object):
|
||||||
# this may be necessary, if modules are loaded on the fly
|
# this may be necessary, if modules are loaded on the fly
|
||||||
# (DevStack may be an example)
|
# (DevStack may be an example)
|
||||||
if not providers:
|
if not providers:
|
||||||
ini = self.ini()
|
providers = self.ini().service_providers.service_provider
|
||||||
try:
|
|
||||||
for name, value in ini.items('service_providers'):
|
|
||||||
if name == 'service_provider':
|
|
||||||
providers.append(value)
|
|
||||||
except ConfigParser.NoSectionError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return providers
|
return providers
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
[service_providers]
|
||||||
|
service_provider=foo
|
||||||
|
service_provider=bar
|
|
@ -203,3 +203,12 @@ class GetProviderDriverClassTestCase(base.BaseTestCase):
|
||||||
def test_get_provider_driver_class_miss(self):
|
def test_get_provider_driver_class_miss(self):
|
||||||
retval = provconf.get_provider_driver_class('foo')
|
retval = provconf.get_provider_driver_class('foo')
|
||||||
self.assertEqual('foo', retval)
|
self.assertEqual('foo', retval)
|
||||||
|
|
||||||
|
|
||||||
|
class NeutronModuleTestCase(base.BaseTestCase):
|
||||||
|
|
||||||
|
def test_can_parse_multi_opt_service_provider_from_conf_file(self):
|
||||||
|
mod = provconf.NeutronModule('neutron_test')
|
||||||
|
mod.ini(base.ETCDIR)
|
||||||
|
self.assertEqual(['foo', 'bar'], mod.service_providers(),
|
||||||
|
'Expected two providers, only one read')
|
||||||
|
|
Loading…
Reference in New Issue