diff options
author | Pavi Vetriselvan <pavithra.vetriselvan@mongodb.com> | 2020-08-31 08:53:04 -0400 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-09-03 01:31:54 +0000 |
commit | 2756fa678945f890c73318efa72d3793c6e918d8 (patch) | |
tree | 0525fe60a8b0126ee199a76300798f5d959a65c7 /src | |
parent | 63bba617ae96ae338f55c835d83f4b5dfd63f52e (diff) | |
download | mongo-2756fa678945f890c73318efa72d3793c6e918d8.tar.gz |
SERVER-50640 listCommands should list isMaster in addition to hello
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/commands/generic.cpp | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/src/mongo/db/commands/generic.cpp b/src/mongo/db/commands/generic.cpp index 4374cd38762..a27b1aa133d 100644 --- a/src/mongo/db/commands/generic.cpp +++ b/src/mongo/db/commands/generic.cpp @@ -31,6 +31,7 @@ #include "mongo/platform/basic.h" +#include "mongo/base/string_data.h" #include "mongo/bson/util/bson_extract.h" #include "mongo/bson/util/builder.h" #include "mongo/db/auth/authorization_session.h" @@ -52,6 +53,8 @@ using std::string; using std::stringstream; using std::vector; +constexpr auto kIsMasterString = "isMaster"_sd; + class PingCommand : public BasicCommand { public: PingCommand() : BasicCommand("ping") {} @@ -164,34 +167,38 @@ public: const string& ns, const BSONObj& cmdObj, BSONObjBuilder& result) { - // sort the commands before building the result BSON - std::vector<Command*> commands; - for (const auto command : globalCommandRegistry()->allCommands()) { - // don't show oldnames - if (command.first == command.second->getName()) - commands.push_back(command.second); + // Sort the command names before building the result BSON. + std::vector<std::string> commandNames; + const auto commandRegistry = globalCommandRegistry(); + for (const auto command : commandRegistry->allCommands()) { + // Don't show oldnames unless it's "isMaster". The output of the listCommands command + // must include "isMaster," even though it's an alias for the "hello" command, in order + // to preserve backwards compatibility with Ops Manager 4.4. + if (command.first == command.second->getName() || command.first == kIsMasterString) + commandNames.push_back(command.first); } - std::sort(commands.begin(), commands.end(), [](Command* lhs, Command* rhs) { - return (lhs->getName()) < (rhs->getName()); - }); + std::sort(commandNames.begin(), commandNames.end()); BSONObjBuilder b(result.subobjStart("commands")); - for (const auto& c : commands) { - BSONObjBuilder temp(b.subobjStart(c->getName())); - temp.append("help", c->help()); - temp.append("requiresAuth", c->requiresAuth()); + for (const auto& c : commandNames) { + const auto command = commandRegistry->findCommand(c); + auto name = (c == kIsMasterString) ? kIsMasterString : command->getName(); + BSONObjBuilder temp(b.subobjStart(name)); + temp.append("help", command->help()); + temp.append("requiresAuth", command->requiresAuth()); temp.append("slaveOk", - c->secondaryAllowed(opCtx->getServiceContext()) == + command->secondaryAllowed(opCtx->getServiceContext()) == Command::AllowedOnSecondary::kAlways); - temp.append("adminOnly", c->adminOnly()); - // optionally indicates that the command can be forced to run on a slave/secondary - if (c->secondaryAllowed(opCtx->getServiceContext()) == + temp.append("adminOnly", command->adminOnly()); + // Optionally indicates that the command can be forced to run on a secondary. + if (command->secondaryAllowed(opCtx->getServiceContext()) == Command::AllowedOnSecondary::kOptIn) temp.append("slaveOverrideOk", true); - temp.append("apiVersions", c->apiVersions()); - temp.append("deprecatedApiVersions", c->deprecatedApiVersions()); + temp.append("apiVersions", command->apiVersions()); + temp.append("deprecatedApiVersions", command->deprecatedApiVersions()); temp.done(); } + b.done(); return 1; |