]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/telegraf: define commands using CLICommand
authorKefu Chai <kchai@redhat.com>
Mon, 22 Mar 2021 09:54:34 +0000 (17:54 +0800)
committerKefu Chai <kchai@redhat.com>
Mon, 22 Mar 2021 11:42:03 +0000 (19:42 +0800)
for better readability

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/mgr/telegraf/module.py

index afbc7309c21abea818541018d0e42f689698d441..1421fce02fc44b3592383333a629a22d3b21c696 100644 (file)
@@ -7,31 +7,13 @@ from threading import Event
 
 from telegraf.basesocket import BaseSocket
 from telegraf.protocol import Line
-from mgr_module import MgrModule, Option, PG_STATES
+from mgr_module import CLICommand, CLIReadCommand, MgrModule, Option, PG_STATES
 
+from typing import Tuple
 from urllib.parse import urlparse
 
 
 class Module(MgrModule):
-    COMMANDS = [
-        {
-            "cmd": "telegraf config-set name=key,type=CephString "
-                   "name=value,type=CephString",
-            "desc": "Set a configuration value",
-            "perm": "rw"
-        },
-        {
-            "cmd": "telegraf config-show",
-            "desc": "Show current configuration",
-            "perm": "r"
-        },
-        {
-            "cmd": "telegraf send",
-            "desc": "Force sending data to Telegraf",
-            "perm": "rw"
-        },
-    ]
-
     MODULE_OPTIONS = [
         Option(name='address',
                default='unixgram:///tmp/telegraf.sock'),
@@ -250,25 +232,32 @@ class Module(MgrModule):
         self.run = False
         self.event.set()
 
-    def handle_command(self, inbuf, cmd):
-        if cmd['prefix'] == 'telegraf config-show':
-            return 0, json.dumps(self.config), ''
-        elif cmd['prefix'] == 'telegraf config-set':
-            key = cmd['key']
-            value = cmd['value']
-            if not value:
-                return -errno.EINVAL, '', 'Value should not be empty or None'
-
-            self.log.debug('Setting configuration option %s to %s', key, value)
-            self.set_config_option(key, value)
-            self.set_module_option(key, value)
-            return 0, 'Configuration option {0} updated'.format(key), ''
-        elif cmd['prefix'] == 'telegraf send':
-            self.send_to_telegraf()
-            return 0, 'Sending data to Telegraf', ''
-
-        return (-errno.EINVAL, '',
-                "Command not found '{0}'".format(cmd['prefix']))
+    @CLIReadCommand('telegraf config-show')
+    def config_show(self) -> Tuple[int, str, str]:
+        """
+        Show current configuration
+        """
+        return 0, json.dumps(self.config), ''
+
+    @CLICommand('telegraf config-set')
+    def config_set(self, key: str, value: str) -> Tuple[int, str, str]:
+        """
+        Set a configuration value
+        """
+        if not value:
+            return -errno.EINVAL, '', 'Value should not be empty or None'
+        self.log.debug('Setting configuration option %s to %s', key, value)
+        self.set_config_option(key, value)
+        self.set_module_option(key, value)
+        return 0, 'Configuration option {0} updated'.format(key), ''
+
+    @CLICommand('telegraf send')
+    def send(self) -> Tuple[int, str, str]:
+        """
+        Force sending data to Telegraf
+        """
+        self.send_to_telegraf()
+        return 0, 'Sending data to Telegraf', ''
 
     def self_test(self):
         measurements = list(self.gather_measurements())