Merge "Add example group for the URI driver"

This commit is contained in:
Zuul 2018-07-13 16:08:33 +00:00 committed by Gerrit Code Review
commit 6ddee6d29a
5 changed files with 86 additions and 15 deletions

View File

@ -12,6 +12,8 @@
from oslo_config import cfg
import stevedore
def list_opts():
default_config_files = [
@ -26,8 +28,17 @@ def list_opts():
'/etc/project/project.conf.d/',
'/etc/project.conf.d/',
]
options = cfg.ConfigOpts._list_options_for_discovery(
options = [(None, cfg.ConfigOpts._list_options_for_discovery(
default_config_files,
default_config_dirs,
)
return [(None, options)]
))]
ext_mgr = stevedore.ExtensionManager(
"oslo.config.driver",
invoke_on_load=True)
for driver in ext_mgr.names():
options.append(('sample_%s_source' % driver,
ext_mgr[driver].obj.list_options_for_discovery()))
return options

View File

@ -510,7 +510,7 @@ from oslo_config import iniparser
from oslo_config import sources
from oslo_config import types
from stevedore.extension import ExtensionManager
import stevedore
LOG = logging.getLogger(__name__)
@ -2570,7 +2570,7 @@ class ConfigOpts(collections.Mapping):
def _open_source_from_opt_group(self, group_name):
if not self._ext_mgr:
self._ext_mgr = ExtensionManager(
self._ext_mgr = stevedore.ExtensionManager(
"oslo.config.driver",
invoke_on_load=True)

View File

@ -46,6 +46,19 @@ class ConfigurationSourceDriver(object):
:returns: an instance of a subclass of ConfigurationSource
"""
@abc.abstractmethod
def list_options_for_discovery(self):
"""Return a list of options used by the driver to create the source.
Drivers should advertise all supported options in this method for the
purpose of sample generation by oslo-config-generator.
For an example on how to implement this method you can check the URI
driver at oslo_config.sources._uri.URIConfigurationSourceDriver.
:returns: a list of supported options of a ConfigurationSource.
"""
@six.add_metaclass(abc.ABCMeta)
class ConfigurationSource(object):

View File

@ -35,17 +35,49 @@ class URIConfigurationSourceDriver(sources.ConfigurationSourceDriver):
specified but does not includes the private key.
"""
_uri_driver_opts = [
cfg.URIOpt(
'uri',
schemes=['http', 'https'],
required=True,
help=('Required option with the URI of the '
'extra configuration file\'s location.'),
),
cfg.StrOpt(
'ca_path',
help=('The path to a CA_BUNDLE file or directory '
'with certificates of trusted CAs.'),
),
cfg.StrOpt(
'client_cert',
help=('Client side certificate, as a single file path '
'containing either the certificate only or the '
'private key and the certificate.'),
),
cfg.StrOpt(
'client_key',
help=('Client side private key, in case client_cert is '
'specified but does not includes the private key.'),
),
]
def list_options_for_discovery(self):
# NOTE(moguimar): This option is only used to provide a better
# description of the driver option registered
# by ConfigOpts._open_source_from_opt_group().
driver_opt = cfg.StrOpt(
'driver',
default='remote_file',
help=('Required option and value for this group to be '
'parsed as an extra source by the URI driver. '
'This group\'s name must be set as one of the '
'config_source\'s values in the [DEFAULT] group.'),
)
return [driver_opt] + self._uri_driver_opts
def open_source_from_opt_group(self, conf, group_name):
group = cfg.OptGroup(group_name)
conf.register_opt(cfg.URIOpt("uri",
schemes=["http", "https"],
required=True),
group)
conf.register_opt(cfg.StrOpt("ca_path"), group)
conf.register_opt(cfg.StrOpt("client_cert"), group)
conf.register_opt(cfg.StrOpt("client_key"), group)
conf.register_opts(self._uri_driver_opts, group_name)
return URIConfigurationSource(
conf[group_name].uri,

View File

@ -12,6 +12,7 @@
from requests import HTTPError
from oslo_config import _list_opts
from oslo_config import cfg
from oslo_config import fixture
from oslo_config import sources
@ -279,3 +280,17 @@ class URISourceTestCase(base.BaseTestCase):
# the option name and option value will match correctly
for option in _extra_configs[uri]["data"]["DEFAULT"]:
self.assertEqual(option, self.conf[option])
def test_list_opts(self):
expected_group = None
for group in _list_opts.list_opts():
if group[0] == "sample_remote_file_source":
expected_group = group
break
self.assertIsNotNone(expected_group)
self.assertEqual(
expected_group[1],
_uri.URIConfigurationSourceDriver().list_options_for_discovery()
)