From: Loic Dachary Date: Wed, 11 Sep 2013 16:19:47 +0000 (+0200) Subject: mon: convenience function to convert commands to json X-Git-Tag: v0.71~89^2~13 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=e0b5697387362d2221c4a8c9e81d6d2b8b934667;p=ceph.git mon: convenience function to convert commands to json 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 Signed-off-by: Loic Dachary --- diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 10f5bfb149c7..2c64a8f2ef28 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -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 @@ -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; diff --git a/src/mon/Monitor.h b/src/mon/Monitor.h index df4a751361ac..9b3044287322 100644 --- a/src/mon/Monitor.h +++ b/src/mon/Monitor.h @@ -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