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

View File

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