]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/dashboard: feature-toggles: Add REST hook
authorErnesto Puerta <epuertat@redhat.com>
Tue, 29 Jan 2019 12:16:54 +0000 (13:16 +0100)
committerErnesto Puerta <epuertat@redhat.com>
Wed, 6 Feb 2019 17:08:01 +0000 (18:08 +0100)
Provide a hook for creating REST controllers and endpoints. Provide also
a REST controller for Feature Toggles plugin.

Fixes: http://tracker.ceph.com/issues/37530
Signed-off-by: Ernesto Puerta <epuertat@redhat.com>
src/pybind/mgr/dashboard/controllers/__init__.py
src/pybind/mgr/dashboard/plugins/feature_toggles.py
src/pybind/mgr/dashboard/plugins/interfaces.py

index 793331d543f40982c95b529841093b93ea907347..e8839a43b3cba3d1046971180f405b593d043470 100644 (file)
@@ -24,6 +24,7 @@ from ..security import Scope, Permission
 from ..tools import wraps, getargspec, TaskManager, get_request_body_params
 from ..exceptions import ScopeNotValid, PermissionNotValid
 from ..services.auth import AuthManager, JwtManager
+from ..plugins import PLUGIN_MANAGER
 
 
 class Controller(object):
@@ -177,6 +178,9 @@ def load_controllers():
                     continue
                 controllers.append(cls)
 
+    for clist in PLUGIN_MANAGER.hook.get_controllers() or []:
+        controllers.extend(clist)
+
     return controllers
 
 
index a8863f6fa365f9e8686ccd77a1abdb62c5a08509..5174f3f6d8eb7ae002ae6b6f774f649fba5ef616 100644 (file)
@@ -89,7 +89,7 @@ Feature2Endpoint = {
 @PM.add_plugin
 class FeatureToggles(I.CanMgr, I.CanLog, I.Setupable, I.HasOptions,
                      I.HasCommands, I.FilterRequest.BeforeHandler,
-                     I.HasEndpoints):
+                     I.HasControllers):
     OPTION_FMT = 'FEATURE_TOGGLE_{}'
     CACHE_MAX_SIZE = 128  # Optimum performance with 2^N sizes
 
@@ -148,6 +148,10 @@ class FeatureToggles(I.CanMgr, I.CanLog, I.Setupable, I.HasOptions,
                 return self.Endpoint2Feature[endpoint]
         return None
 
+    def _get_feature_status(self, feature):
+        return self.mgr.get_module_option(
+            self.OPTION_FMT.format(feature.value))
+
     @PM.add_hook
     def filter_request_before_handler(self, request):
         feature = self.__get_feature_from_path(request.path_info)
@@ -155,7 +159,7 @@ class FeatureToggles(I.CanMgr, I.CanLog, I.Setupable, I.HasOptions,
         if feature is None:
             return
 
-        if self.mgr.get_module_option(self.OPTION_FMT.format(feature.value)) is False:
+        if self._get_feature_status(feature) is False:
             raise cherrypy.HTTPError(
                 501, "Feature='{}' (path='{}') disabled by option '{}'".format(
                     feature.value,
@@ -164,5 +168,20 @@ class FeatureToggles(I.CanMgr, I.CanLog, I.Setupable, I.HasOptions,
                     ))
 
     @PM.add_hook
-    def register_endpoints(self):
-        pass
+    def get_controllers(self):
+        from ..controllers import ApiController,\
+            RESTController, Endpoint, ReadPermission
+        from ..security import Scope
+
+        @ApiController('/feature_toggles')
+        class FeatureTogglesEndpoint(RESTController):
+
+            def list(_):
+                return {
+                    feature.value: self._get_feature_status(feature)
+                    for feature in Features
+                }
+
+        return [
+            FeatureTogglesEndpoint,
+        ]
index 56cb1d9c04534e0a3c148b2bf44e606808b0d090..e990b5abce92f052e45faee35ca3b0475fa9cab1 100644 (file)
@@ -38,9 +38,9 @@ class HasCommands(Interface):
 
 
 @PM.add_interface
-class HasEndpoints(Interface):
+class HasControllers(Interface):
     @PM.add_abcspec
-    def register_endpoints(self): pass
+    def get_controllers(self): pass
 
 
 class FilterRequest: