]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
mon: convenience function to convert commands to json
authorLoic Dachary <loic@dachary.org>
Wed, 11 Sep 2013 16:19:47 +0000 (18:19 +0200)
committerLoic Dachary <loic@dachary.org>
Mon, 23 Sep 2013 21:46:43 +0000 (23:46 +0200)
The get_command_descriptions is added to Monitor.h and contains
the code previously inlined in Monitor::handle_command to implement
the get_command_descriptions command. It is intended for tests to
convert command descriptions into json, including error cases.

http://tracker.ceph.com/issues/6274 refs #6274

Reviewed-by: Joao Eduardo Luis <joao.luis@inktank.com>
Signed-off-by: Loic Dachary <loic@dachary.org>
src/mon/Monitor.cc
src/mon/Monitor.h

index 10f5bfb149c72547a805c5ad0c8791b53d417a26..2c64a8f2ef28eae4d055123c3104893a275a022f 100644 (file)
@@ -1854,13 +1854,7 @@ void Monitor::get_status(stringstream &ss, Formatter *f)
 }
 
 #undef COMMAND
-struct MonCommand {
-  string cmdstring;
-  string helpstring;
-  string module;
-  string req_perms;
-  string availability;
-} mon_commands[] = {
+MonCommand mon_commands[] = {
 #define COMMAND(parsesig, helptext, modulename, req_perms, avail) \
   {parsesig, helptext, modulename, req_perms, avail},
 #include <mon/MonCommands.h>
@@ -1909,6 +1903,26 @@ bool Monitor::_allowed_command(MonSession *s, string &module, string &prefix,
   return capable;
 }
 
+void get_command_descriptions(const MonCommand *commands,
+                             unsigned commands_size,
+                             Formatter *f,
+                             bufferlist *rdata) {
+  int cmdnum = 0;
+  f->open_object_section("command_descriptions");
+  for (const MonCommand *cp = commands;
+       cp < &commands[commands_size]; cp++) {
+
+    ostringstream secname;
+    secname << "cmd" << setfill('0') << std::setw(3) << cmdnum;
+    dump_cmddesc_to_json(f, secname.str(),
+                        cp->cmdstring, cp->helpstring, cp->module,
+                        cp->req_perms, cp->availability);
+    cmdnum++;
+  }
+  f->close_section();  // command_descriptions
+
+  f->flush(*rdata);
+}
 
 void Monitor::handle_command(MMonCommand *m)
 {
@@ -1953,23 +1967,9 @@ void Monitor::handle_command(MMonCommand *m)
 
   cmd_getval(g_ceph_context, cmdmap, "prefix", prefix);
   if (prefix == "get_command_descriptions") {
-    int cmdnum = 0;
-    Formatter *f = new_formatter("json");
-    f->open_object_section("command_descriptions");
-    for (MonCommand *cp = mon_commands;
-        cp < &mon_commands[ARRAY_SIZE(mon_commands)]; cp++) {
-
-      ostringstream secname;
-      secname << "cmd" << setfill('0') << std::setw(3) << cmdnum;
-      dump_cmddesc_to_json(f, secname.str(),
-                          cp->cmdstring, cp->helpstring, cp->module,
-                          cp->req_perms, cp->availability);
-      cmdnum++;
-    }
-    f->close_section();        // command_descriptions
-
     bufferlist rdata;
-    f->flush(rdata);
+    Formatter *f = new_formatter("json");
+    get_command_descriptions(mon_commands, ARRAY_SIZE(mon_commands), f, &rdata);
     delete f;
     reply_command(m, 0, "", rdata, 0);
     return;
index df4a751361ac54d6594fd197097aff64dd71f51a..9b3044287322792589cbbb3b97ccf5f25cc6cd1f 100644 (file)
@@ -844,5 +844,17 @@ public:
 
 long parse_pos_long(const char *s, ostream *pss = NULL);
 
+struct MonCommand {
+  string cmdstring;
+  string helpstring;
+  string module;
+  string req_perms;
+  string availability;
+};
+
+void get_command_descriptions(const MonCommand *commands,
+                             unsigned commands_size,
+                             Formatter *f,
+                             bufferlist *rdata);
 
 #endif