From 15ca8ecc32ba1b137e5b9b18264f22af272a5f0d Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Tue, 25 Aug 2015 23:44:08 -0400 Subject: [PATCH] add triton account and touch up instance --- lib/cli.js | 5 ++++ lib/cloudapi2.js | 35 +++++------------------- lib/do_account.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++ lib/do_instance.js | 6 ++++- 4 files changed, 83 insertions(+), 29 deletions(-) create mode 100644 lib/do_account.js diff --git a/lib/cli.js b/lib/cli.js index 977c15a..c0efae7 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 1018697..b63707c 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -234,39 +234,19 @@ CloudAPI.prototype._request = function _request(options, callback) { // ---- accounts + /** - * Get the user's account data. - * + * 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 = { diff --git a/lib/do_account.js b/lib/do_account.js new file mode 100644 index 0000000..5e01b8a --- /dev/null +++ b/lib/do_account.js @@ -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; diff --git a/lib/do_instance.js b/lib/do_instance.js index 16168e2..127296b 100644 --- a/lib/do_instance.js +++ b/lib/do_instance.js @@ -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(); } }