Merge "Implement mandatory configuration parameters"
This commit is contained in:
commit
96107db929
@ -67,6 +67,8 @@ class OSBaseCharm(CharmBase):
|
||||
|
||||
REQUIRED_RELATIONS = []
|
||||
|
||||
MANDATORY_CONFIG = []
|
||||
|
||||
def __init__(self, framework):
|
||||
super().__init__(framework)
|
||||
self.custom_status_checks = []
|
||||
@ -75,6 +77,7 @@ class OSBaseCharm(CharmBase):
|
||||
self._stored.set_default(series_upgrade=False)
|
||||
self.framework.observe(self.on.install, self.on_install)
|
||||
self.framework.observe(self.on.update_status, self.on_update_status)
|
||||
self.framework.observe(self.on.config_changed, self._on_config)
|
||||
# A charm may not have pause/resume actions if it does not manage a
|
||||
# daemon.
|
||||
try:
|
||||
@ -237,6 +240,22 @@ class OSBaseCharm(CharmBase):
|
||||
self._stored.is_paused = False
|
||||
self.update_status()
|
||||
|
||||
def on_config(self, event):
|
||||
"""Main entry point for configuration changes."""
|
||||
pass
|
||||
|
||||
def _on_config(self, event):
|
||||
missing = []
|
||||
config = self.framework.model.config
|
||||
for param in self.MANDATORY_CONFIG:
|
||||
if param not in config:
|
||||
missing.append(param)
|
||||
if missing:
|
||||
self.unit.status = BlockedStatus(
|
||||
'Missing option(s): ' + ','.join(missing))
|
||||
return
|
||||
self.on_config(event)
|
||||
|
||||
|
||||
def charm_class(cls):
|
||||
_releases[cls.release] = {'deb': cls}
|
||||
|
@ -77,6 +77,9 @@ class OpenStackTestAPICharm(OpenStackTestPlugin1,
|
||||
else:
|
||||
return ActiveStatus()
|
||||
|
||||
def on_config(self, _):
|
||||
self.unit.status = ActiveStatus()
|
||||
|
||||
|
||||
class CharmTestCase(unittest.TestCase):
|
||||
|
||||
@ -150,6 +153,9 @@ class TestOSBaseCharm(CharmTestCase):
|
||||
description: yet another failure to report
|
||||
''')
|
||||
|
||||
def tearDown(self):
|
||||
OpenStackTestAPICharm.MANDATORY_CONFIG = []
|
||||
|
||||
def test_init(self):
|
||||
self.harness.begin()
|
||||
self.assertFalse(self.harness.charm._stored.is_started)
|
||||
@ -352,3 +358,13 @@ class TestOSBaseCharm(CharmTestCase):
|
||||
'resume',
|
||||
services=['apache2', 'ks-api'],
|
||||
charm_func=None)
|
||||
|
||||
def test_mandatory_config(self):
|
||||
OpenStackTestAPICharm.MANDATORY_CONFIG = ['source']
|
||||
self.harness.begin()
|
||||
self.harness.update_config({})
|
||||
self.assertTrue(
|
||||
isinstance(self.harness.charm.unit.status, BlockedStatus))
|
||||
self.harness.update_config({'source': 'value'})
|
||||
self.assertTrue(
|
||||
isinstance(self.harness.charm.unit.status, ActiveStatus))
|
||||
|
Loading…
Reference in New Issue
Block a user