Clean up configuration option management

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
This commit is contained in:
Doug Hellmann 2015-02-18 10:59:02 -05:00
parent b475cd53da
commit 41d8bf1c07
4 changed files with 100 additions and 27 deletions

View File

@ -2,5 +2,14 @@
API
=====
oslo_policy.policy
==================
.. automodule:: oslo_policy.policy
:members:
oslo_policy.opts
================
.. automodule:: oslo_policy.opts
:members:

View File

@ -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)

View File

@ -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)

View File

@ -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)