22 #include "mongodb_client_config.h"
24 #include <config/config.h>
25 #include <logging/logger.h>
49 logcomp_ =
"MongoDBClient|" + cfgname;
53 enabled_ = config->
get_bool((prefix +
"enabled").c_str());
57 std::string mode =
"connection";
59 mode = config->
get_string((prefix +
"mode").c_str());
62 "MongoDB config '%s' specifies no client "
63 "mode, assuming 'connection'.",
68 read_authinfo(config, logger, cfgname, prefix);
70 if (mode ==
"replica_set" || mode ==
"replicaset") {
72 replicaset_name_ = config->
get_string((prefix +
"name").c_str());
74 std::vector<std::string> hosts = config->
get_strings(prefix +
"hosts");
75 std::string uri{
"mongodb://"};
76 for (
auto it = hosts.begin(); it != hosts.end(); it++) {
78 if (std::next(it) != hosts.end()) {
82 uri +=
"/" + auth_dbname;
83 uri +=
"?replicaSet=" + replicaset_name_;
85 uri +=
"&readPreference=" + config->
get_string((prefix +
"read-preference").c_str());
90 uri +=
"&readPreferenceTags=" + config->
get_string((prefix +
"read-preference-tags").c_str());
93 conn_uri_ = mongocxx::uri{uri};
95 }
else if (mode ==
"sync_cluster" || mode ==
"synccluster") {
96 throw Exception(
"sync_cluster connections are no longer supported");
100 conn_uri_ = mongocxx::uri{
"mongodb://" + auth_string_ + config->
get_string(prefix +
"hostport")
101 +
"/" + auth_dbname};
120 auth_dbname = config->
get_string((prefix +
"auth_dbname").c_str());
121 std::string username = config->
get_string((prefix +
"auth_username").c_str());
122 std::string password = config->
get_string((prefix +
"auth_password").c_str());
123 if (!username.empty() && !password.empty()) {
124 auth_string_ = username +
":" + password +
"@";
128 "No default authentication info for "
129 "MongoDB client '%s'",
140 return new mongocxx::client(conn_uri_);
153 logger->
log_info(component,
"%smode: replica set", indent);
156 logger->
log_info(component,
"%smode: connection", indent);
159 std::string uri_string = conn_uri_.to_string();
160 if (!auth_string_.empty()) {
161 if (uri_string.find(auth_string_) != std::string::npos) {
162 uri_string.replace(uri_string.find(auth_string_), auth_string_.length(),
"****@****");
165 logger->
log_info(component,
"%suri: %s", indent, uri_string.c_str());
174 auto hosts = conn_uri_.hosts();
179 return hosts[0].name +
":" + std::to_string(hosts[0].port);