add triton account and touch up instance

This commit is contained in:
Dave Eddy 2015-08-25 23:44:08 -04:00
parent f9f9823525
commit 15ca8ecc32
4 changed files with 83 additions and 29 deletions

View File

@ -58,6 +58,8 @@ function CLI() {
}, },
helpSubcmds: [ helpSubcmds: [
'help', 'help',
{ group: 'Operator Commands' },
'account',
{ group: 'Instances (aka VMs/Machines/Containers)' }, { group: 'Instances (aka VMs/Machines/Containers)' },
'create', 'create',
'instances', 'instances',
@ -107,6 +109,9 @@ CLI.prototype.init = function (opts, args, callback) {
//CLI.prototype.do_profile = require('./do_profile'); //CLI.prototype.do_profile = require('./do_profile');
// Operator
CLI.prototype.do_account = require('./do_account');
// Images // Images
CLI.prototype.do_images = require('./do_images'); CLI.prototype.do_images = require('./do_images');
CLI.prototype.do_image = require('./do_image'); CLI.prototype.do_image = require('./do_image');

View File

@ -234,39 +234,19 @@ CloudAPI.prototype._request = function _request(options, callback) {
// ---- accounts // ---- accounts
/** /**
* Get the user's account data. * Get account information
* <http://apidocs.joyent.com/cloudapi/#GetAccount>
* *
* @param {Object} options (optional) * @param {Function} callback of the form `function (err, account, response)`
* @param {Function} callback of the form `function (err, user)`
*/ */
CloudAPI.prototype.getAccount = function getAccount(options, callback) { CloudAPI.prototype.getAccount = function getAccount(callback) {
var self = this; var self = this;
if (callback === undefined) {
callback = options;
options = {};
}
assert.object(options, 'options');
assert.func(callback, 'callback'); assert.func(callback, 'callback');
var path = '/' + self.user; var endpoint = sprintf('/%s', self.user);
self._getAuthHeaders(function (hErr, headers) { this._request(endpoint, function (err, req, res, body) {
if (hErr) { callback(err, body, res);
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);
}
});
}); });
}; };
@ -554,7 +534,6 @@ CloudAPI.prototype.machineAudit = function machineAudit(options, callback) {
}; };
// --- Exports // --- Exports
module.exports = { module.exports = {

66
lib/do_account.js Normal file
View File

@ -0,0 +1,66 @@
/*
* Copyright 2015 Joyent Inc.
*
* `triton account ...`
*/
var common = require('./common');
function do_account(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
}
this.triton.cloudapi.getAccount(function (err, account) {
if (err) {
callback(err);
return;
}
if (opts.json) {
console.log(JSON.stringify(account));
} else {
// pretty print
var dates = ['updated', 'created'];
Object.keys(account).forEach(function (key) {
var val = account[key];
if (dates.indexOf(key) >= 0) {
console.log('%s: %s (%s)', key, val,
common.longAgo(new Date(val)));
} else {
console.log('%s: %s', key, val);
}
});
}
callback();
});
}
do_account.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
do_account.help = (
'Show account information\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} account\n'
+ '\n'
+ '{{options}}'
);
do_account.aliases = ['whoami'];
module.exports = do_account;

View File

@ -29,7 +29,11 @@ function do_instance(subcmd, opts, args, callback) {
return; return;
} }
if (opts.json) {
console.log(JSON.stringify(machine));
} else {
console.log(JSON.stringify(machine, null, 4)); console.log(JSON.stringify(machine, null, 4));
}
callback(); callback();
} }
} }