Add observer for add multiple arbitrary options with config-flags
Add config_property_with_config_flags observer to handle extra option. The config-flags are comma separated list of key-value pairs. Change-Id: I74a693df95bebb14a6edcb796f1c8e6b17a95317
This commit is contained in:
parent
1079a19e2e
commit
18ac668cd7
|
@ -84,6 +84,31 @@ def config_property(f):
|
|||
_custom_config_properties[property_name] = f
|
||||
return f
|
||||
|
||||
|
||||
@config_property
|
||||
def config_flags(cls):
|
||||
"""Decorator to add a custom configuration property with
|
||||
config-flags to add extra options.
|
||||
|
||||
:param cls: Configuration Adapter class
|
||||
:type cls: charms_openstack.adapters.DefaultConfigurationAdapter
|
||||
"""
|
||||
cf_config = {}
|
||||
config = hookenv.config
|
||||
if config and 'config-flags' in config:
|
||||
# match 0 or multiple time any pair of character a=b divide by a coma
|
||||
# zero or one spaces are allows between tokens
|
||||
parsing_regex = r"^(?:\s{0,1}[^=]\s{0,1}=\s{0,1}[^,]\s{0,1}(?:,|$))*$"
|
||||
if re.match(parsing_regex, config['config-flags']):
|
||||
cf_config = dict(map(lambda x: [s.strip() for s in x.split('=')],
|
||||
config['config-flags']
|
||||
.split(',')))
|
||||
else:
|
||||
raise RuntimeError("config-flags string error: {}".format(config))
|
||||
|
||||
return cf_config
|
||||
|
||||
|
||||
##
|
||||
|
||||
|
||||
|
|
|
@ -51,6 +51,66 @@ class TestCustomProperties(unittest.TestCase):
|
|||
self.assertTrue(adapters._custom_config_properties['test_func'],
|
||||
test_func)
|
||||
|
||||
def test_user_config_flags(self):
|
||||
cfg = {
|
||||
'config-flags': "a = b,c=d, e= f",
|
||||
}
|
||||
cls_mock = mock.MagicMock()
|
||||
with mock.patch.object(adapters.hookenv,
|
||||
'config',
|
||||
new=cfg):
|
||||
|
||||
conf = adapters.config_flags(cls_mock)
|
||||
self.assertEqual(conf['a'], 'b')
|
||||
self.assertEqual(conf['c'], 'd')
|
||||
self.assertEqual(conf['e'], 'f')
|
||||
|
||||
def test_user_config_flags_parsing_error_1(self):
|
||||
cfg = {
|
||||
'config-flags': "a = b, c = d e= f",
|
||||
}
|
||||
cls_mock = mock.MagicMock()
|
||||
with mock.patch.object(adapters.hookenv,
|
||||
'config',
|
||||
new=cfg):
|
||||
|
||||
with self.assertRaises(RuntimeError):
|
||||
adapters.config_flags(cls_mock)
|
||||
|
||||
def test_user_config_flags_parsing_error_2(self):
|
||||
cfg = {
|
||||
'config-flags': "a = b, c d, e= f",
|
||||
}
|
||||
cls_mock = mock.MagicMock()
|
||||
with mock.patch.object(adapters.hookenv,
|
||||
'config',
|
||||
new=cfg):
|
||||
|
||||
with self.assertRaises(RuntimeError):
|
||||
adapters.config_flags(cls_mock)
|
||||
|
||||
def test_user_config_flags_missing(self):
|
||||
cfg = {
|
||||
'other-flags': 1
|
||||
}
|
||||
cls_mock = mock.MagicMock()
|
||||
with mock.patch.object(adapters.hookenv,
|
||||
'config',
|
||||
new=cfg):
|
||||
|
||||
conf = adapters.config_flags(cls_mock)
|
||||
self.assertEqual(conf, {})
|
||||
|
||||
def test_user_config_none(self):
|
||||
cfg = None
|
||||
cls_mock = mock.MagicMock()
|
||||
with mock.patch.object(adapters.hookenv,
|
||||
'config',
|
||||
new=cfg):
|
||||
|
||||
conf = adapters.config_flags(cls_mock)
|
||||
self.assertEqual(conf, {})
|
||||
|
||||
|
||||
class MyRelation(object):
|
||||
|
||||
|
|
Loading…
Reference in New Issue