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
- long '-l' output, -H, -o, -s
- 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)
- uuid caching
- UUID prefix support

View File

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

View File

@ -7,6 +7,7 @@
var p = console.log;
var assert = require('assert-plus');
var async = require('async');
var EventEmitter = require('events').EventEmitter;
var format = require('util').format;
var fs = require('fs');
var path = require('path');
@ -154,43 +155,46 @@ JoyentCloud.prototype._clientFromDc = function _clientFromDc(dc) {
/**
* 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
* - {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;
if (callback === undefined) {
callback = options;
if (options === undefined) {
options = {};
}
assert.object(options, 'options');
assert.optionalFunc(options.onDcError, 'options.onDcError');
assert.func(callback, 'callback');
var allMachines = [];
var emitter = new EventEmitter();
async.each(
self.profile.dcs || Object.keys(self.config.dcs),
function oneDc(dc, next) {
var client = self._clientFromDc(dc);
client.listMachines(function (err, machines) {
if (err) {
if (options.onDcError) {
options.onDcError(dc, err);
}
emitter.emit('dcError', dc, err);
} else {
for (var i = 0; i < machines.length; i++) {
machines[i].dc = dc;
allMachines.push(machines[i]);
}
emitter.emit('data', dc, machines);
}
next();
});
},
function done(err) {
callback(err, allMachines);
emitter.emit('end');
}
);
return emitter;
};