add basic instance-audit support

This commit is contained in:
Dave Eddy 2015-09-08 12:41:31 -04:00
parent 219912beb1
commit 9d314def3d
2 changed files with 67 additions and 58 deletions

View File

@ -615,34 +615,16 @@ CloudApi.prototype.createMachine = function createMachine(options, callback) {
* *
* XXX IMO this endpoint should be called ListMachineAudit in cloudapi. * XXX IMO this endpoint should be called ListMachineAudit in cloudapi.
* *
* @param {Object} options * @param {String} id (required) The machine id.
* - {String} id (required) The machine id.
* @param {Function} callback of the form `function (err, audit, response)` * @param {Function} callback of the form `function (err, audit, response)`
*/ */
CloudApi.prototype.machineAudit = function machineAudit(options, callback) { CloudApi.prototype.machineAudit = function machineAudit(id, cb) {
var self = this; assert.uuid(id, 'id');
assert.object(options, 'options'); assert.func(cb, 'cb');
assert.string(options.id, 'options.id');
assert.func(callback, 'callback');
var path = format('/%s/machines/%s/audit', self.user, options.id); var endpoint = format('/%s/machines/%s/audit', this.user, id);
// XXX This `client.get` block is duplicated. Add a convenience func for it: this._request(endpoint, function (err, req, res, body) {
self._getAuthHeaders(function (hErr, headers) { cb(err, body, res);
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);
}
});
}); });
}; };

View File

@ -16,53 +16,80 @@ var tabula = require('tabula');
var common = require('./common'); var common = require('./common');
var errors = require('./errors'); 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) { if (opts.help) {
this.do_help('help', {}, [subcmd], callback); this.do_help('help', {}, [subcmd], cb);
return; return;
} else if (args.length > 1) { } else if (args.length !== 1) {
//XXX Support multiple machines. //XXX Support multiple machines.
return callback(new Error('too many args: ' + args)); return cb(new Error('incorrect args: ' + args));
} }
var id = args[0]; var columns = opts.o.split(',');
this.sdc.machineAudit({machine: id}, function (err, audit, dc) { 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) { if (err) {
return callback(err); cb(err);
return;
} }
for (var i = 0; i < audit.length; i++) { uuid = inst.id;
audit[i].dc = dc; 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) { if (opts.json) {
common.jsonStream(audit); common.jsonStream(audit);
} else { } else {
// XXX what to tabulate here? tabula(audit, {
return callback( skipHeader: opts.H,
new errors.InternalError('tabular output for audit NYI')); columns: columns,
// common.tabulate(audit, { sort: sort,
// columns: 'dc,id,name,state,created', dottedLookup: true
// sort: 'created',
// validFields: 'dc,id,name,type,state,image,package,memory,'
// + 'disk,created,updated,compute_node,primaryIp'
// });
}
callback();
}); });
} }
cb();
});
}
}
do_instance_audit.options = [ do_instance_audit.options = [
{ {
names: ['help', 'h'], names: ['help', 'h'],
type: 'bool', type: 'bool',
help: 'Show this help.' help: 'Show this help.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
} }
]; ].concat(common.getCliTableOptions({
columnsDefault: columnsDefault,
sortDefault: sortDefault
}));
do_instance_audit.help = ( do_instance_audit.help = (
'List instance actions.\n' 'List instance actions.\n'
+ '\n' + '\n'