]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
mgr: add new Command helper
authorErnesto Puerta <epuertat@redhat.com>
Thu, 10 Oct 2019 09:07:10 +0000 (11:07 +0200)
committerErnesto Puerta <epuertat@redhat.com>
Thu, 10 Oct 2019 09:07:10 +0000 (11:07 +0200)
Fixes: https://tracker.ceph.com/issues/41990
Signed-off-by: Ernesto Puerta <epuertat@redhat.com>
src/pybind/mgr/mgr_module.py

index e6b74ef72f7331d443f6bc66ad81417fbddf6722..245620012286f0288564a1a6e09f8684084e0361 100644 (file)
@@ -413,6 +413,57 @@ class Option(dict):
             (k, v) for k, v in vars().items()
             if k != 'self' and v is not None)
 
+class Command(dict):
+    """
+    Helper class to declare options for COMMANDS list.
+
+    It also allows to specify prefix and args separately, as well as storing a
+    handler callable.
+
+    Usage:
+    >>> Command(prefix="example",
+    ...         args="name=arg,type=CephInt",
+    ...         perm='w',
+    ...         desc="Blah")
+    {'poll': False, 'cmd': 'example name=arg,type=CephInt', 'perm': 'w', 'desc': 'Blah'}
+    """
+
+    def __init__(
+            self,
+            prefix,
+            args=None,
+            perm="rw",
+            desc=None,
+            poll=False,
+            handler=None
+    ):
+        super(Command, self).__init__(
+            cmd=prefix + (' ' + args if args else ''),
+            perm=perm,
+            desc=desc,
+            poll=poll)
+        self.prefix = prefix
+        self.args = args
+        self.handler = handler
+
+    def register(self, instance=False):
+        """
+        Register a CLICommand handler. It allows an instance to register bound
+        methods. In that case, the mgr instance is not passed, and it's expected
+        to be available in the class instance.
+        It also uses HandleCommandResult helper to return a wrapped a tuple of 3
+        items.
+        """
+        return CLICommand(
+            prefix=self.prefix,
+            args=self.args,
+            desc=self['desc'],
+            perm=self['perm']
+        )(
+            func=lambda mgr, *args, **kwargs: HandleCommandResult(*self.handler(
+                *((instance or mgr,) + args), **kwargs))
+        )
+
 
 class MgrStandbyModule(ceph_module.BaseMgrStandbyModule):
     """