From b2105ca41fdfe0162a902126c64874de1e7b676b Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 00:40:32 -0400 Subject: [PATCH] add triton keys --- lib/cli.js | 2 ++ lib/cloudapi2.js | 14 ++++++++++++ lib/do_keys.js | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 lib/do_keys.js diff --git a/lib/cli.js b/lib/cli.js index 3f5b2a1..c41f96a 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -61,6 +61,7 @@ function CLI() { { group: 'Operator Commands' }, 'account', 'info', + 'keys', { group: 'Instances (aka VMs/Machines/Containers)' }, 'create-instance', 'instances', @@ -113,6 +114,7 @@ CLI.prototype.init = function (opts, args, callback) { // Operator CLI.prototype.do_account = require('./do_account'); CLI.prototype.do_info = require('./do_info'); +CLI.prototype.do_keys = require('./do_keys'); // Images CLI.prototype.do_images = require('./do_images'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 72e4673..7a4e94e 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -250,6 +250,20 @@ CloudAPI.prototype.getAccount = function getAccount(callback) { }); }; +/** + * Get public key information + * + * @param {Function} callback of the form `function (err, keys, response)` + */ +CloudAPI.prototype.listKeys = function listKeys(callback) { + var self = this; + assert.func(callback, 'callback'); + + var endpoint = sprintf('/%s/keys', self.user); + this._request(endpoint, function (err, req, res, body) { + callback(err, body, res); + }); +}; // ---- images diff --git a/lib/do_keys.js b/lib/do_keys.js new file mode 100644 index 0000000..776431a --- /dev/null +++ b/lib/do_keys.js @@ -0,0 +1,57 @@ +/* + * Copyright 2015 Joyent Inc. + * + * `triton account ...` + */ + +var common = require('./common'); + +function do_keys(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.listKeys(function (err, keys) { + if (err) { + callback(err); + return; + } + + if (opts.json) { + console.log(JSON.stringify(keys)); + } else { + // pretty print + keys.forEach(function (key) { + console.log(key.key); + }); + } + callback(); + }); +} + +do_keys.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, + { + names: ['json', 'j'], + type: 'bool', + help: 'JSON output.' + } +]; +do_keys.help = ( + 'Show public keys.\n' + + '\n' + + 'Usage:\n' + + ' {{name}} keys\n' + + '\n' + + '{{options}}' +); + +module.exports = do_keys;