event-y jc.listMachines

This commit is contained in:
Trent Mick 2014-02-08 00:15:26 -08:00
parent 67b170e68f
commit 5842875027
3 changed files with 42 additions and 28 deletions

View File

@ -4,7 +4,7 @@
- short default output - short default output
- long '-l' output, -H, -o, -s - long '-l' output, -H, -o, -s
- get image defaults and fill those in - get image defaults and fill those in
- couple commands: machines, machine, provision (create-machine?) - couple commands: machine, provision (create-machine?)
- re-write of cloudapi.js (eventually a separate module) - re-write of cloudapi.js (eventually a separate module)
- uuid caching - uuid caching
- UUID prefix support - UUID prefix support

View File

@ -13,7 +13,6 @@ var child_process = require('child_process'),
exec = child_process.exec; exec = child_process.exec;
var fs = require('fs'); var fs = require('fs');
var assert = require('assert-plus'); var assert = require('assert-plus');
var async = require('async'); var async = require('async');
var bunyan = require('bunyan'); var bunyan = require('bunyan');
@ -21,6 +20,7 @@ var cmdln = require('cmdln'),
Cmdln = cmdln.Cmdln; Cmdln = cmdln.Cmdln;
var common = require('./common'); var common = require('./common');
var errors = require('./errors');
var JoyentCloud = require('./joyentcloud'); var JoyentCloud = require('./joyentcloud');
@ -188,16 +188,20 @@ CLI.prototype.do_machines = function (subcmd, opts, args, callback) {
return callback(new Error('too many args: ' + args)); return callback(new Error('too many args: ' + args));
} }
//XXX jc.listMachines should change to return a 'res' event emitter var machines = [];
// that emits 'dcError' and 'data'. var errs = [];
var listOpts = { var res = this.jc.listMachines();
onDcError: function (dc, dcErr) { res.on('data', function (dc, dcMachines) {
console.warn('%s machines: dc %s error: %s', self.name, dc, dcErr); for (var i = 0; i < dcMachines.length; i++) {
dcMachines[i].dc = dc;
machines.push(dcMachines[i]);
} }
}; });
this.jc.listMachines(listOpts, function (err, machines) { res.on('dcError', function (dc, dcErr) {
if (err) dcErr.dc = dc;
return callback(err); errs.push(dcErr);
});
res.on('end', function () {
if (opts.json) { if (opts.json) {
p(JSON.stringify(machines, null, 4)); p(JSON.stringify(machines, null, 4));
} else { } else {
@ -213,7 +217,13 @@ CLI.prototype.do_machines = function (subcmd, opts, args, callback) {
+ 'disk,created,updated,compute_node,primaryIp' + 'disk,created,updated,compute_node,primaryIp'
}); });
} }
callback(); var err;
if (errs.length === 1) {
err = errs[0];
} else if (errs.length > 1) {
err = new errors.MultiError(errs);
}
callback(err);
}); });
}; };
CLI.prototype.do_machines.options = [ CLI.prototype.do_machines.options = [

View File

@ -7,6 +7,7 @@
var p = console.log; var p = console.log;
var assert = require('assert-plus'); var assert = require('assert-plus');
var async = require('async'); var async = require('async');
var EventEmitter = require('events').EventEmitter;
var format = require('util').format; var format = require('util').format;
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
@ -154,43 +155,46 @@ JoyentCloud.prototype._clientFromDc = function _clientFromDc(dc) {
/** /**
* List machines for the current profile. * List machines for the current profile.
* *
* var res = this.jc.listMachines();
* res.on('data', function (dc, dcMachines) {
* //...
* });
* res.on('dcError', function (dc, dcErr) {
* //...
* });
* res.on('end', function () {
* //...
* });
*
* @param {Object} options Optional * @param {Object} options Optional
* - {Function} onDcError `function (dc, err)` called for each DC client
* error.
*/ */
JoyentCloud.prototype.listMachines = function listMachines(options, callback) { JoyentCloud.prototype.listMachines = function listMachines(options) {
var self = this; var self = this;
if (callback === undefined) { if (options === undefined) {
callback = options;
options = {}; options = {};
} }
assert.object(options, 'options'); assert.object(options, 'options');
assert.optionalFunc(options.onDcError, 'options.onDcError');
assert.func(callback, 'callback');
var allMachines = []; var emitter = new EventEmitter();
async.each( async.each(
self.profile.dcs || Object.keys(self.config.dcs), self.profile.dcs || Object.keys(self.config.dcs),
function oneDc(dc, next) { function oneDc(dc, next) {
var client = self._clientFromDc(dc); var client = self._clientFromDc(dc);
client.listMachines(function (err, machines) { client.listMachines(function (err, machines) {
if (err) { if (err) {
if (options.onDcError) { emitter.emit('dcError', dc, err);
options.onDcError(dc, err);
}
} else { } else {
for (var i = 0; i < machines.length; i++) { emitter.emit('data', dc, machines);
machines[i].dc = dc;
allMachines.push(machines[i]);
}
} }
next(); next();
}); });
}, },
function done(err) { function done(err) {
callback(err, allMachines); emitter.emit('end');
} }
); );
return emitter;
}; };