From f100c4dbb59bfbff5b6e381f8648133a73499e0d Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 19 Jun 2018 14:42:10 -0700 Subject: [PATCH] TRITON-524 'triton inst get' should support '--credentials' Reviewed by: Marsell Kukuljevic Approved by: Marsell Kukuljevic --- CHANGES.md | 3 +++ lib/cloudapi2.js | 16 ++++++++++------ lib/do_instance/do_get.js | 13 +++++++++++-- lib/tritonapi.js | 13 +++++++++++-- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 51acbd5..0d2c70d 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,9 @@ Known issues: ## not yet released +- [TRITON-524] Add `triton inst get --credentials ...` option to match + `triton inst list --credentials ...` for including generated credentials + in instance metadata. - [joyent/node-triton#245] `triton profile` now generates fresh new keys during Docker setup and signs them with an account key, rather than copying (and decrypting) the account key itself. This makes using Docker simpler with keys diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 94c57eb..26f80fd 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -872,13 +872,12 @@ CloudApi.prototype.getPackage = function getPackage(opts, cb) { /** * Get a machine by id. * - * XXX add getCredentials equivalent - * XXX cloudapi docs don't doc the credentials=true option - * * For backwards compat, calling with `getMachine(id, cb)` is allowed. * * @param {Object} opts - * - id {UUID} Required. The machine id. + * - {UUID} id - Required. The machine id. + * - {Boolean} credentials - Optional. Set to true to include generated + * credentials for this machine in `machine.metadata.credentials`. * @param {Function} cb of the form `function (err, machine, res)` */ CloudApi.prototype.getMachine = function getMachine(opts, cb) { @@ -887,9 +886,14 @@ CloudApi.prototype.getMachine = function getMachine(opts, cb) { } assert.object(opts, 'opts'); assert.uuid(opts.id, 'opts.id'); + assert.optionalBool(opts.credentials, 'opts.credentials'); - var endpoint = format('/%s/machines/%s', this.account, opts.id); - this._request(endpoint, function (err, req, res, body) { + var query = {}; + if (opts.credentials) { + query.credentials = 'true'; + } + var p = this._path(format('/%s/machines/%s', this.account, opts.id), query); + this._request(p, function (err, req, res, body) { cb(err, body, res); }); }; diff --git a/lib/do_instance/do_get.js b/lib/do_instance/do_get.js index c7f3c4e..ce22914 100644 --- a/lib/do_instance/do_get.js +++ b/lib/do_instance/do_get.js @@ -25,7 +25,10 @@ function do_get(subcmd, opts, args, cb) { cb(setupErr); return; } - tritonapi.getInstance(args[0], function (err, inst) { + tritonapi.getInstance({ + id: args[0], + credentials: opts.credentials + }, function onInst(err, inst) { if (inst) { if (opts.json) { console.log(JSON.stringify(inst)); @@ -44,6 +47,13 @@ do_get.options = [ type: 'bool', help: 'Show this help.' }, + { + names: ['credentials'], + type: 'bool', + help: 'Include generated credentials, in the "metadata.credentials" ' + + 'field, if any. Typically used with "-j", though one can show ' + + 'values with "-o metadata.credentials".' + }, { names: ['json', 'j'], type: 'bool', @@ -58,7 +68,6 @@ do_get.help = [ '{{usage}}', '', '{{options}}', - '', 'Where "INST" is an instance name, id, or short id.', '', 'A *deleted* instance may still respond with the instance object. In that', diff --git a/lib/tritonapi.js b/lib/tritonapi.js index 7bc46df..92882cc 100644 --- a/lib/tritonapi.js +++ b/lib/tritonapi.js @@ -1123,6 +1123,8 @@ TritonApi.prototype.updateNetworkIp = function updateNetworkIp(opts, cb) { * - {Array} fields: Optional. An array of instance field names that are * wanted by the caller. This *can* allow the implementation to avoid * extra API calls. E.g. `['id', 'name']`. + * - {Boolean} credentials: Optional. Set to true to include generated + * credentials for this instance in `inst.metadata.credentials`. * @param {Function} cb `function (err, inst, res)` * Note that deleted instances will result in `err` being a * `InstanceDeletedError` and `inst` being defined. On success, `res` is @@ -1137,6 +1139,7 @@ TritonApi.prototype.getInstance = function getInstance(opts, cb) { assert.object(opts, 'opts'); assert.string(opts.id, 'opts.id'); assert.optionalArrayOfString(opts.fields, 'opts.fields'); + assert.optionalBool(opts.credentials, 'opts.credentials'); assert.func(cb, 'cb'); /* @@ -1176,7 +1179,10 @@ TritonApi.prototype.getInstance = function getInstance(opts, cb) { return next(); } } - self.cloudapi.getMachine(uuid, function (err, inst_, res_) { + self.cloudapi.getMachine({ + id: uuid, + credentials: opts.credentials + }, function onMachine(err, inst_, res_) { res = res_; inst = inst_; err = errFromGetMachineErr(err); @@ -1264,7 +1270,10 @@ TritonApi.prototype.getInstance = function getInstance(opts, cb) { } var uuid = instFromList.id; - self.cloudapi.getMachine(uuid, function (err, inst_, res_) { + self.cloudapi.getMachine({ + id: uuid, + credentials: opts.credentials + }, function onMachine(err, inst_, res_) { res = res_; inst = inst_; err = errFromGetMachineErr(err);