From 9d314def3da2c56f42d1b3dd59947e96924ae82a Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Tue, 8 Sep 2015 12:41:31 -0400 Subject: [PATCH] add basic instance-audit support --- lib/cloudapi2.js | 32 +++----------- lib/do_instance_audit.js | 93 ++++++++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 58 deletions(-) diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index c7bbc33..4c9f221 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -615,34 +615,16 @@ CloudApi.prototype.createMachine = function createMachine(options, callback) { * * XXX IMO this endpoint should be called ListMachineAudit in cloudapi. * - * @param {Object} options - * - {String} id (required) The machine id. + * @param {String} id (required) The machine id. * @param {Function} callback of the form `function (err, audit, response)` */ -CloudApi.prototype.machineAudit = function machineAudit(options, callback) { - var self = this; - assert.object(options, 'options'); - assert.string(options.id, 'options.id'); - assert.func(callback, 'callback'); +CloudApi.prototype.machineAudit = function machineAudit(id, cb) { + assert.uuid(id, 'id'); + assert.func(cb, 'cb'); - var path = format('/%s/machines/%s/audit', self.user, options.id); - // XXX This `client.get` block is duplicated. Add a convenience func for it: - self._getAuthHeaders(function (hErr, headers) { - if (hErr) { - callback(hErr); - return; - } - var opts = { - path: path, - headers: headers - }; - self.client.get(opts, function (err, req, res, body) { - if (err) { - callback(err, null, res); - } else { - callback(null, body, res); - } - }); + var endpoint = format('/%s/machines/%s/audit', this.user, id); + this._request(endpoint, function (err, req, res, body) { + cb(err, body, res); }); }; diff --git a/lib/do_instance_audit.js b/lib/do_instance_audit.js index d6496eb..bf9c90d 100644 --- a/lib/do_instance_audit.js +++ b/lib/do_instance_audit.js @@ -16,39 +16,67 @@ var tabula = require('tabula'); var common = require('./common'); var errors = require('./errors'); +// columns default without -o +var columnsDefault = 'id,time,action,success'; + +// sort default with -s +var sortDefault = 'id,time'; + +function do_instance_audit(subcmd, opts, args, cb) { + var self = this; -function do_instance_audit(subcmd, opts, args, callback) { if (opts.help) { - this.do_help('help', {}, [subcmd], callback); + this.do_help('help', {}, [subcmd], cb); return; - } else if (args.length > 1) { + } else if (args.length !== 1) { //XXX Support multiple machines. - return callback(new Error('too many args: ' + args)); + return cb(new Error('incorrect args: ' + args)); } - var id = args[0]; - this.sdc.machineAudit({machine: id}, function (err, audit, dc) { - if (err) { - return callback(err); - } - for (var i = 0; i < audit.length; i++) { - audit[i].dc = dc; - } - if (opts.json) { - common.jsonStream(audit); - } else { - // XXX what to tabulate here? - return callback( - new errors.InternalError('tabular output for audit NYI')); - // common.tabulate(audit, { - // columns: 'dc,id,name,state,created', - // sort: 'created', - // validFields: 'dc,id,name,type,state,image,package,memory,' - // + 'disk,created,updated,compute_node,primaryIp' - // }); - } - callback(); - }); + var columns = opts.o.split(','); + var sort = opts.s.split(','); + + var arg = args[0]; + var uuid; + + if (common.isUUID(arg)) { + uuid = arg; + go1(); + } else { + self.tritonapi.getInstance(arg, function (err, inst) { + if (err) { + cb(err); + return; + } + uuid = inst.id; + go1(); + }); + } + + function go1() { + self.tritonapi.cloudapi.machineAudit(uuid, function (err, audit) { + if (err) { + cb(err); + return; + } + + audit.forEach(function (a) { + a.id = uuid; + }); + + if (opts.json) { + common.jsonStream(audit); + } else { + tabula(audit, { + skipHeader: opts.H, + columns: columns, + sort: sort, + dottedLookup: true + }); + } + cb(); + }); + } } do_instance_audit.options = [ @@ -56,13 +84,12 @@ do_instance_audit.options = [ names: ['help', 'h'], type: 'bool', help: 'Show this help.' - }, - { - names: ['json', 'j'], - type: 'bool', - help: 'JSON output.' } -]; +].concat(common.getCliTableOptions({ + columnsDefault: columnsDefault, + sortDefault: sortDefault +})); + do_instance_audit.help = ( 'List instance actions.\n' + '\n'