diff options
author | Doug Hellmann <doug@doughellmann.com> | 2015-02-18 10:59:02 -0500 |
---|---|---|
committer | Doug Hellmann <doug@doughellmann.com> | 2015-02-18 12:45:04 -0500 |
commit | 41d8bf1c07c84808ebd50313755cf77721c03e58 (patch) | |
tree | bba1d1324a5c3c5552d8e3956e6f883da6c669fd | |
parent | b475cd53daa44d2d0387fe2f891c06ccc76135c8 (diff) | |
download | oslo-policy-41d8bf1c07c84808ebd50313755cf77721c03e58.tar.gz |
Clean up configuration option management0.1.0
Move the option definitions into the opts module with the other
functions related to options.
Add set_defaults() to override the default settings for options and make
it part of the public API of the library by including it in the
documentation.
Add opts._register() so the options can be registered consistently from
multiple places.
Change-Id: Ib74beac58a07bf5ee0f92c2b7eda8281ef7d7c33
-rw-r--r-- | doc/source/api.rst | 9 | ||||
-rw-r--r-- | oslo_policy/opts.py | 61 | ||||
-rw-r--r-- | oslo_policy/policy.py | 26 | ||||
-rw-r--r-- | oslo_policy/tests/test_opts.py | 31 |
4 files changed, 100 insertions, 27 deletions
diff --git a/doc/source/api.rst b/doc/source/api.rst index 3e2a950..35d9338 100644 --- a/doc/source/api.rst +++ b/doc/source/api.rst @@ -2,5 +2,14 @@ API ===== +oslo_policy.policy +================== + .. automodule:: oslo_policy.policy :members: + +oslo_policy.opts +================ + +.. automodule:: oslo_policy.opts + :members: diff --git a/oslo_policy/opts.py b/oslo_policy/opts.py index 0880fdf..d836f57 100644 --- a/oslo_policy/opts.py +++ b/oslo_policy/opts.py @@ -11,12 +11,40 @@ # under the License. __all__ = [ - 'list_opts' + 'list_opts', + 'set_defaults', ] import copy -from oslo_policy import policy +from oslo_config import cfg + +from oslo_policy._i18n import _ + + +_option_group = 'oslo_policy' + +_options = [ + cfg.StrOpt('policy_file', + default='policy.json', + help=_('The JSON file that defines policies.'), + deprecated_group='DEFAULT'), + cfg.StrOpt('policy_default_rule', + default='default', + help=_('Default rule. Enforced when a requested rule is not ' + 'found.'), + deprecated_group='DEFAULT'), + cfg.MultiStrOpt('policy_dirs', + default=['policy.d'], + help=_('Directories where policy configuration files are ' + 'stored. They can be relative to any directory ' + 'in the search path defined by the config_dir ' + 'option, or absolute paths. The file defined by ' + 'policy_file must exist for these directories to ' + 'be searched. Missing or empty directories are' + 'ignored.'), + deprecated_group='DEFAULT'), +] def list_opts(): @@ -36,4 +64,31 @@ def list_opts(): :returns: a list of (group_name, opts) tuples """ - return [('oslo_policy', copy.deepcopy(policy._opts))] + return [(_option_group, copy.deepcopy(_options))] + + +def _register(conf): + """Register the policy options. + + We do this in a few places, so use a function to ensure it is done + consistently. + """ + conf.register_opts(_options, group=_option_group) + + +def set_defaults(conf, policy_file=None): + """Set defaults for configuration variables. + + Overrides default options values. + + :param conf: Configuration object, managed by the caller. + :type conf: oslo.config.cfg.ConfigOpts + + :param policy_file: The base filename for the JSON file that + defines policies. + :type policy_file: unicode + """ + _register(conf) + + if policy_file is not None: + conf.set_default('policy_file', policy_file, group=_option_group) diff --git a/oslo_policy/policy.py b/oslo_policy/policy.py index 4b202ab..0f617f0 100644 --- a/oslo_policy/policy.py +++ b/oslo_policy/policy.py @@ -213,29 +213,7 @@ from oslo_policy import _checks from oslo_policy._i18n import _ from oslo_policy import _parser from oslo_policy.openstack.common import fileutils - - -_opts = [ - cfg.StrOpt('policy_file', - default='policy.json', - help=_('The JSON file that defines policies.'), - deprecated_group='DEFAULT'), - cfg.StrOpt('policy_default_rule', - default='default', - help=_('Default rule. Enforced when a requested rule is not ' - 'found.'), - deprecated_group='DEFAULT'), - cfg.MultiStrOpt('policy_dirs', - default=['policy.d'], - help=_('Directories where policy configuration files are ' - 'stored. They can be relative to any directory ' - 'in the search path defined by the config_dir ' - 'option, or absolute paths. The file defined by ' - 'policy_file must exist for these directories to ' - 'be searched. Missing or empty directories are' - 'ignored.'), - deprecated_group='DEFAULT'), -] +from oslo_policy import opts LOG = logging.getLogger(__name__) @@ -327,7 +305,7 @@ class Enforcer(object): def __init__(self, conf, policy_file=None, rules=None, default_rule=None, use_conf=True, overwrite=True): self.conf = conf - self.conf.register_opts(_opts, group='oslo_policy') + opts._register(conf) self.default_rule = (default_rule or self.conf.oslo_policy.policy_default_rule) diff --git a/oslo_policy/tests/test_opts.py b/oslo_policy/tests/test_opts.py new file mode 100644 index 0000000..2ec5b34 --- /dev/null +++ b/oslo_policy/tests/test_opts.py @@ -0,0 +1,31 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from oslo_config import fixture as config +from oslotest import base as test_base + +from oslo_policy import opts + + +class OptsTestCase(test_base.BaseTestCase): + + def setUp(self): + super(OptsTestCase, self).setUp() + self.conf = self.useFixture(config.Config()).conf + + def test_set_defaults_policy_file(self): + opts._register(self.conf) + self.assertNotEqual('new-value.json', + self.conf.oslo_policy.policy_file) + opts.set_defaults(self.conf, policy_file='new-value.json') + self.assertEqual('new-value.json', + self.conf.oslo_policy.policy_file) |