]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: introduce MgrModule.OPTIONS field
authorJohn Spray <john.spray@redhat.com>
Mon, 16 Apr 2018 21:29:08 +0000 (17:29 -0400)
committerJohn Spray <john.spray@redhat.com>
Mon, 23 Apr 2018 11:29:47 +0000 (07:29 -0400)
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 <john.spray@redhat.com>
src/pybind/mgr/mgr_module.py

index 00a9efdb22e8272e8ca497ed85a2fc3da97bb04e..a7c20f85da6e7dcceec7e1ada32cf61d15a7216c 100644 (file)
@@ -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):
         """