Make _option_dict() a method for domain_config_api
As I was getting familiar with the domain config API, I noticed a cool little nugget of a method nestled within get_config_default(). The _option_dict() method will go through and parse the configuration information for a given group and option. This will be really helpful when we go to implement the password requirements API, since we'll be reading configuration information in a very similar way. This commit bumps _option_dict() outside of the get_config_default() method, officially making it a class method of the domain config Manager(). This commit also add some unit tests, verifying its behavior. Change-Id: Ia2695628d19190f422ba079cc77e2d5e0e86c63b partially-implements: bp pci-dss-password-requirements-api
This commit is contained in:
parent
31713100f9
commit
48573a725d
|
@ -1018,6 +1018,14 @@ class DomainConfigManager(manager.Manager):
|
|||
|
||||
return option_list
|
||||
|
||||
def _option_dict(self, group, option):
|
||||
group_attr = getattr(CONF, group)
|
||||
if group_attr is None:
|
||||
msg = _('Group %s not found in config') % group
|
||||
raise exception.UnexpectedError(msg)
|
||||
return {'group': group, 'option': option,
|
||||
'value': getattr(group_attr, option)}
|
||||
|
||||
def _list_to_config(self, whitelisted, sensitive=None, req_option=None):
|
||||
"""Build config dict from a list of option dicts.
|
||||
|
||||
|
@ -1359,14 +1367,6 @@ class DomainConfigManager(manager.Manager):
|
|||
}
|
||||
|
||||
"""
|
||||
def _option_dict(group, option):
|
||||
group_attr = getattr(CONF, group)
|
||||
if group_attr is None:
|
||||
msg = _('Group %s not found in config') % group
|
||||
raise exception.UnexpectedError(msg)
|
||||
return {'group': group, 'option': option,
|
||||
'value': getattr(group_attr, option)}
|
||||
|
||||
self._assert_valid_group_and_option(group, option)
|
||||
config_list = []
|
||||
if group:
|
||||
|
@ -1376,13 +1376,15 @@ class DomainConfigManager(manager.Manager):
|
|||
'group %(group)s is not supported') % {
|
||||
'option': option, 'group': group}
|
||||
raise exception.InvalidDomainConfig(reason=msg)
|
||||
config_list.append(_option_dict(group, option))
|
||||
config_list.append(self._option_dict(group, option))
|
||||
else:
|
||||
for each_option in self.whitelisted_options[group]:
|
||||
config_list.append(_option_dict(group, each_option))
|
||||
config_list.append(self._option_dict(group, each_option))
|
||||
else:
|
||||
for each_group in self.whitelisted_options:
|
||||
for each_option in self.whitelisted_options[each_group]:
|
||||
config_list.append(_option_dict(each_group, each_option))
|
||||
config_list.append(
|
||||
self._option_dict(each_group, each_option)
|
||||
)
|
||||
|
||||
return self._list_to_config(config_list, req_option=option)
|
||||
|
|
|
@ -15,6 +15,7 @@ import uuid
|
|||
|
||||
import fixtures
|
||||
import mock
|
||||
from oslo_config import cfg
|
||||
from testtools import matchers
|
||||
|
||||
import keystone.conf
|
||||
|
@ -681,3 +682,28 @@ class DomainConfigTests(object):
|
|||
self.assertRaises(exception.ConfigRegistrationNotFound,
|
||||
self.domain_config_api.read_registration,
|
||||
type2)
|
||||
|
||||
def test_option_dict_fails_when_group_is_none(self):
|
||||
group = 'foo'
|
||||
option = 'bar'
|
||||
self.assertRaises(
|
||||
cfg.NoSuchOptError,
|
||||
self.domain_config_api._option_dict,
|
||||
group,
|
||||
option
|
||||
)
|
||||
|
||||
def test_option_dict_returns_valid_config_values(self):
|
||||
regex = uuid.uuid4().hex
|
||||
self.config_fixture.config(
|
||||
group='security_compliance', password_regex=regex
|
||||
)
|
||||
expected_dict = {
|
||||
'group': 'security_compliance',
|
||||
'option': 'password_regex',
|
||||
'value': regex
|
||||
}
|
||||
option_dict = self.domain_config_api._option_dict(
|
||||
'security_compliance', 'password_regex'
|
||||
)
|
||||
self.assertEqual(option_dict, expected_dict)
|
||||
|
|
Loading…
Reference in New Issue