From: Kefu Chai Date: Mon, 22 Mar 2021 09:54:34 +0000 (+0800) Subject: pybind/mgr/telegraf: define commands using CLICommand X-Git-Tag: v17.1.0~2475^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=0c99f7c912d9d9994fa076b9b29f3b2b8e4442f8;p=ceph.git pybind/mgr/telegraf: define commands using CLICommand for better readability Signed-off-by: Kefu Chai --- diff --git a/src/pybind/mgr/telegraf/module.py b/src/pybind/mgr/telegraf/module.py index afbc7309c21a..1421fce02fc4 100644 --- a/src/pybind/mgr/telegraf/module.py +++ b/src/pybind/mgr/telegraf/module.py @@ -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())