diff --git a/lib/commands/mongo.js b/lib/commands/mongo.js index 6ca1552..8c0979c 100644 --- a/lib/commands/mongo.js +++ b/lib/commands/mongo.js @@ -19,6 +19,114 @@ var databaseInArray = function(databases, name) { return -1; }; +mongo.list = function (cb) { + mongoController.find({ + userId : userConfig.data.userId + }, + function (err, mongos) { + var table = []; + if (err) { + err = error.handleApiError(err, 'MONGO_LIST', cb); + if (err.length > 0) { + return cb(err); + } + } + + if (mongos.length === 0) { + xervo.io.print('You currently have no mongos. You can create one with "mongo create".'); + return cb(); + } + + function getURI(mongo) { + var ret = ''; + + if (database.uri.length > 0) { + ret = database.uri[0].uri; + } else { + ret = database.uri; + } + + ret = ret.replace('*.', ''); + + return ret.indexOf('http') >= 0 ? ret : 'http://' + ret; + } + + mongos = _.sortBy(mongos, 'creator'); + + var userMongos = []; + var orgMongos = []; + + mongos.forEach(function (mongo) { + if (userConfig.data.userId === mongo.creator) { + userMongos.push(mongo); + } else { + orgMongos.push(mongo); + } + }); + + // make sure the current user's mongos display first + var mong = userMongos.concat(orgMongos); + + var owners = {}; + xervo.io.print('Current mongos:'.input); + async.mapSeries(mong, function (mongo, callback) { + if (mongo.creator === userConfig.data.userId) { + owners[mongo.creator] = userConfig.data.username; + return callback(null, userConfig.data.username); + } + + if (owners.hasOwnProperty(mongo.creator)) { + return callback(null, owners[mongo.creator]); + } + + userController.get(mongo.creator, function (err, user) { + if (err) { + if (err.errors && err.errors.length >= 1 && + err.errors[0].id === 'INVALID_AUTH') { + // An invalid authorization error means that the user does not + // have administrator access to the organization. In this case, + // use a generic user header for the mongos. + owners[mongo.creator] = 'User ' + mongo.creator; + return callback(null, 'User ' + mongo.creator); + } + + return callback(err); + } + + owners[mongo.creator] = user.username; + return callback(null, user.username); + }); + }, function (err, results) { + if (err) { + err = error.handleApiError(err, 'MONGO_LIST', cb); + if (err.length > 0) { + cb(err); + } + + return; + } + + mong.forEach(function (mongo, index) { + // display only one user/organization header + if (owners[mongo.creator] === results[index]) { + table.push([], [owners[mongo.creator].main], ['----------------------']); + delete owners[mongo.creator]; + } + + table.push([ + mongo.name.data, + getDomain(mongo) + ]); + }); + + xervo.io.print(createTable(table)); + + return cb(err); + }); + } + ); +}; + //------------------------------------------------------------------------------ mongo.create = function(dbName, cb) { var createDatabase = function(dname, regionData) { @@ -42,7 +150,7 @@ mongo.create = function(dbName, cb) { }); }; - // If project name is passed do NOT prompt for name. + // If mongo name is passed do NOT prompt for name. if (dbName && dbName !== '') { xervo.io.print('Creating MongoDB database ' + dbName.data); diff --git a/lib/routes/mongo.js b/lib/routes/mongo.js index 60c0da6..c717ce9 100644 --- a/lib/routes/mongo.js +++ b/lib/routes/mongo.js @@ -1,6 +1,20 @@ module.exports = function(xervo) { var help = new xervo.help('MongoDB', xervo); + // list commands + help.add('list', function () { + this.line('mongo list (list)'.verbose); + this.line('View all mongos you have.'.input); + }); + + xervo.program + .command('mongo list') + .description('Lists all mongos you currently have access to.') + .on('--help', help.commands.list) + .action(function () { + xervo.runCommand(xervo.commands.mongo.list, true); + }); + // create command help.add('create', function() { this.line('mongo create '.verbose);