]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/hello: a better command example
authorSage Weil <sage@redhat.com>
Mon, 5 Aug 2019 21:23:48 +0000 (16:23 -0500)
committerSage Weil <sage@redhat.com>
Wed, 7 Aug 2019 14:27:21 +0000 (09:27 -0500)
Signed-off-by: Sage Weil <sage@redhat.com>
src/pybind/mgr/hello/module.py

index 50eaa2e713326d568c0f16770ab6e0f42f807aff..cc3537cd326aef758cb3d6e75e6d08201d0ddab1 100644 (file)
@@ -7,7 +7,7 @@ See doc/mgr/hello.rst for more info.
 
 from mgr_module import MgrModule, HandleCommandResult
 from threading import Event
-
+import errno
 
 class Hello(MgrModule):
     # these are CLI commands we implement
@@ -15,7 +15,13 @@ class Hello(MgrModule):
         {
             "cmd": "hello "
                    "name=person_name,type=CephString,req=false",
-            "desc": "Prints hello world to mgr.x.log",
+            "desc": "Say hello",
+            "perm": "r"
+        },
+        {
+            "cmd": "count "
+                   "name=num,type=CephInt",
+            "desc": "Do some counting",
             "perm": "r"
         },
     ]
@@ -88,22 +94,31 @@ class Hello(MgrModule):
             self.log.debug(' native option %s = %s', opt, getattr(self, opt))
 
     def handle_command(self, inbuf, cmd):
-        self.log.info("hello_world_info")
-        self.log.debug("hello_world_debug")
-        self.log.error("hello_world_error")
-
-        status_code = 0
-        output_buffer = "Output buffer is for data results"
-        output_string = "Output string is for informative text"
-        if 'person_name' in cmd:
-            message = "Hello, " + cmd['person_name']
-        else:
-            message = "Hello " + self.get_module_option('place')
-        if self.get_module_option('emphatic'):
-            message += '!'
-
-        return HandleCommandResult(retval=status_code, stdout=output_buffer,
-                                   stderr=message + "\n" + output_string)
+        ret = 0
+        out = ''
+        err = ''
+        if cmd['prefix'] == 'hello':
+            if 'person_name' in cmd:
+                out = "Hello, " + cmd['person_name']
+            else:
+                out = "Hello " + self.get_module_option('place')
+            if self.get_module_option('emphatic'):
+                out += '!'
+        elif cmd['prefix'] == 'count':
+            num = cmd.get('num', 0)
+            if num < 1:
+                err = 'That\'s too small a number'
+                ret = -errno.EINVAL
+            elif num > 10:
+                err = 'That\'s too big a number'
+                ret = -errno.EINVAL
+            else:
+                out = 'Hello, I am the count!\n'
+                out += ', '.join([str(x) for x in range(1, num + 1)]) + '!'
+        return HandleCommandResult(
+            retval=ret,   # exit code
+            stdout=out,   # stdout
+            stderr=err)
 
     def serve(self):
         """