From 58428750272e963497a4af03b3d672d392495112 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Sat, 8 Feb 2014 00:15:26 -0800 Subject: [PATCH] event-y jc.listMachines --- TODO.md | 2 +- lib/cli.js | 32 +++++++++++++++++++++----------- lib/joyentcloud.js | 36 ++++++++++++++++++++---------------- 3 files changed, 42 insertions(+), 28 deletions(-) diff --git a/TODO.md b/TODO.md index dd77911..ec3cbac 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/lib/cli.js b/lib/cli.js index dbb3717..6166878 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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 = [ diff --git a/lib/joyentcloud.js b/lib/joyentcloud.js index 1484f31..cd70d8d 100644 --- a/lib/joyentcloud.js +++ b/lib/joyentcloud.js @@ -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; };