diff --git a/ops_openstack/core.py b/ops_openstack/core.py index 776d332..4530874 100644 --- a/ops_openstack/core.py +++ b/ops_openstack/core.py @@ -64,6 +64,8 @@ class OSBaseCharm(CharmBase): REQUIRED_RELATIONS = [] + MANDATORY_CONFIG = [] + def __init__(self, framework): super().__init__(framework) self.custom_status_checks = [] @@ -72,6 +74,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: @@ -234,6 +237,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} diff --git a/unit_tests/test_ops_openstack.py b/unit_tests/test_ops_openstack.py index 12d13cc..9ec0e0c 100644 --- a/unit_tests/test_ops_openstack.py +++ b/unit_tests/test_ops_openstack.py @@ -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))