add basic instance-audit support
This commit is contained in:
		
							parent
							
								
									219912beb1
								
							
						
					
					
						commit
						9d314def3d
					
				@ -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);
 | 
			
		||||
    });
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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'
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user