Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 25 additions & 17 deletions lib/instance-detail-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ const {

const debug = require('debug')('mongodb-data-service:instance-detail-helper');

function getReadPreferenceOptions(db) {
// `db.command` does not use the read preference set on the
// connection, so here we explicitly to specify it in the options.
const readPreference = db.s ? db.s.readPreference : ReadPreference.PRIMARY;
return { readPreference };
}

/**
* aggregates stats across all found databases
* @param {Object} results async.auto results
Expand Down Expand Up @@ -78,7 +85,7 @@ function getBuildInfo(results, done) {
};

const adminDb = db.databaseName === 'admin' ? db : db.admin();
adminDb.command(spec, {}, function(err, res) {
adminDb.command(spec, getReadPreferenceOptions(db), function(err, res) {
if (err) {
// buildInfo doesn't require any privileges to run, so if it fails,
// something really went wrong and we should return the error.
Expand All @@ -98,7 +105,7 @@ function getCmdLineOpts(results, done) {
};

const adminDb = db.databaseName === 'admin' ? db : db.admin();
adminDb.command(spec, {}, function(err, res) {
adminDb.command(spec, getReadPreferenceOptions(db), function(err, res) {
if (err) {
debug('getCmdLineOpts failed!', err);
return done(null, { errmsg: err.message });
Expand Down Expand Up @@ -213,7 +220,7 @@ function getHostInfo(results, done) {
};

const adminDb = db.databaseName === 'admin' ? db : db.admin();
adminDb.command(spec, {}, function(err, res) {
adminDb.command(spec, getReadPreferenceOptions(db), function(err, res) {
if (err) {
if (isNotAuthorized(err)) {
// if the error is that the user is not authorized, silently ignore it
Expand Down Expand Up @@ -253,12 +260,14 @@ function listDatabases(results, done) {
listDatabases: 1
};

const options = getReadPreferenceOptions(db);

const adminDb = db.databaseName === 'admin' ? db : db.admin();
adminDb.command(spec, {}, function(err, res) {
adminDb.command(spec, options, function(err, res) {
if (err) {
if (isNotAuthorized(err)) {
// eslint-disable-next-line no-shadow
adminDb.command({connectionStatus: 1, showPrivileges: 1}, {}, function(err, res) {
adminDb.command({connectionStatus: 1, showPrivileges: 1}, options, function(err, res) {
if (err) {
done(err);
return;
Expand Down Expand Up @@ -343,7 +352,7 @@ function getDatabase(client, db, name, done) {
dbStats: 1
};

const options = {};
const options = getReadPreferenceOptions(db);
debug('running dbStats for `%s`...', name);
client.db(name).command(spec, options, function(err, res) {
if (err) {
Expand Down Expand Up @@ -376,8 +385,15 @@ function getDatabases(results, done) {
function getUserInfo(results, done) {
const db = results.db;

const options = getReadPreferenceOptions(db);

// get the user privileges
db.command({ connectionStatus: 1, showPrivileges: true }, {}, function(
db.command({
connectionStatus: 1,
showPrivileges: true
},
options,
function(
err,
res
) {
Expand All @@ -394,7 +410,7 @@ function getUserInfo(results, done) {
}
const user = res.authInfo.authenticatedUsers[0];

db.command({ usersInfo: user, showPrivileges: true }, {}, function(
db.command({ usersInfo: user, showPrivileges: true }, options, function(
_err,
_res
) {
Expand Down Expand Up @@ -491,15 +507,7 @@ function getDatabaseCollections(db, done) {

const spec = {};

/**
* @note: Durran: For some reason the listCollections call does not take into
* account the read preference that was set on the db instance - it only looks
* in the passed options: https://github.com/mongodb/node-mongodb-native/blob/2.2/lib/db.js#L671
*/
const rp = db.s ? db.s.readPreference : ReadPreference.PRIMARY;
const options = { readPreference: rp };

db.listCollections(spec, options).toArray(function(err, res) {
db.listCollections(spec, getReadPreferenceOptions(db)).toArray(function(err, res) {
if (err) {
if (isNotAuthorized(err) || isMongosLocalException(err)) {
// if the error is that the user is not authorized, silently ignore it
Expand Down
6 changes: 5 additions & 1 deletion lib/native-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ class NativeClient extends EventEmitter {
* @param {Function} callback - The callback.
*/
listDatabases(callback) {
this.database.admin().command({ listDatabases: 1 }, {}, (error, result) => {
this.database.admin().command({
listDatabases: 1
}, {
readPreference: this.model.readPreference
}, (error, result) => {
if (error) {
return callback(this._translateMessage(error));
}
Expand Down
Loading