From bca1bb43c60299ef3f4c5aeb650ceaa8bafef25a Mon Sep 17 00:00:00 2001 From: John Spray Date: Mon, 16 Apr 2018 17:29:08 -0400 Subject: [PATCH] mgr: introduce MgrModule.OPTIONS field Now is a good time to start requiring modules to explicitly list their configuration settings, so that we can do a proper job of migrating configuration from old config-key style, i.e. knowing what's a config setting and what's a KV store item. Throw an exception if a module tries to access a setting outside their schema, so that we have some confidence that the schema is complete. Signed-off-by: John Spray --- src/pybind/mgr/mgr_module.py | 43 ++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/src/pybind/mgr/mgr_module.py b/src/pybind/mgr/mgr_module.py index 00a9efdb22e82..a7c20f85da6e7 100644 --- a/src/pybind/mgr/mgr_module.py +++ b/src/pybind/mgr/mgr_module.py @@ -213,6 +213,7 @@ class MgrStandbyModule(ceph_module.BaseMgrStandbyModule): class MgrModule(ceph_module.BaseMgrModule): COMMANDS = [] + OPTIONS = [] # Priority definitions for perf counters PRIO_CRITICAL = 10 @@ -474,19 +475,33 @@ class MgrModule(ceph_module.BaseMgrModule): """ return self._ceph_get_mgr_id() - def get_config(self, key, default=None): + def _validate_option(self, key): """ - Retrieve the value of a persistent configuration setting - - :param str key: - :return: str + Helper: don't allow get/set config callers to + access config options that they didn't declare + in their schema. """ + if key not in [o['name'] for o in self.OPTIONS]: + raise RuntimeError("Config option '{0}' is not in {1}.OPTIONS".\ + format(key, self.__class__.__name__)) + + def _get_config(self, key, default): r = self._ceph_get_config(key) if r is None: return default else: return r + def get_config(self, key, default=None): + """ + Retrieve the value of a persistent configuration setting + + :param str key: + :return: str + """ + self._validate_option(key) + return self._get_config(key, default) + def get_store_prefix(self, key_prefix): """ Retrieve a dict of KV store keys to values, where the keys @@ -498,12 +513,10 @@ class MgrModule(ceph_module.BaseMgrModule): return self._ceph_get_store_prefix(key_prefix) def _get_localized(self, key, default, getter): - r = getter(self.get_mgr_id() + '/' + key) + r = getter(self.get_mgr_id() + '/' + key, None) if r is None: - r = getter(key) + r = getter(key, default) - if r is None: - r = default return r def _set_localized(self, key, val, setter): @@ -516,7 +529,11 @@ class MgrModule(ceph_module.BaseMgrModule): :param str default: :return: str """ - return self._get_localized(key, default, self.get_config) + self._validate_option(key) + return self._get_localized(key, default, self._get_config) + + def _set_config(self, key, val): + return self._ceph_set_config(key, val) def set_config(self, key, val): """ @@ -525,7 +542,8 @@ class MgrModule(ceph_module.BaseMgrModule): :param str key: :param str val: """ - self._ceph_set_config(key, val) + self._validate_option(key) + return self._set_config(key, val) def set_localized_config(self, key, val): """ @@ -534,7 +552,8 @@ class MgrModule(ceph_module.BaseMgrModule): :param str default: :return: str """ - return self._set_localized(key, val, self.set_config) + self._validate_option(key) + return self._set_localized(key, val, self._set_config) def set_store_json(self, key, val): """ -- 2.39.5