From b5f9cbe054a039ab183886aefa2a58ee536152a6 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Tue, 25 Aug 2015 20:27:46 -0400 Subject: [PATCH] add instance (alias and uuid) --- lib/cli.js | 2 ++ lib/cloudapi2.js | 25 ++++--------------- lib/common.js | 11 ++++++++- lib/do_instance.js | 60 ++++++++++++++++++++++++++++++++++++++++++++++ lib/triton.js | 26 ++++++++++++++++++++ 5 files changed, 103 insertions(+), 21 deletions(-) create mode 100644 lib/do_instance.js diff --git a/lib/cli.js b/lib/cli.js index e801445..e20aa7f 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -61,6 +61,7 @@ function CLI() { { group: 'Instances (aka VMs/Machines/Containers)' }, 'create', 'instances', + 'instance', 'instance-audit', { group: 'Images' }, 'images', @@ -108,6 +109,7 @@ CLI.prototype.do_image = require('./do_image'); // Instances (aka VMs/containers/machines) CLI.prototype.do_create = require('./do_create'); +CLI.prototype.do_instance = require('./do_instance'); CLI.prototype.do_instances = require('./do_instances'); CLI.prototype.do_instance_audit = require('./do_instance_audit'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 8434906..ab50400 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -357,29 +357,14 @@ CloudAPI.prototype.getPackage = function getPackage(options, callback) { * - {String} id (required) The machine id. * @param {Function} callback of the form `function (err, machine, response)` */ -CloudAPI.prototype.getMachine = function getMachine(options, callback) { +CloudAPI.prototype.getMachine = function getMachine(uuid, callback) { var self = this; - assert.object(options, 'options'); - assert.string(options.id, 'options.id'); + assert.string(uuid, 'uuid'); assert.func(callback, 'callback'); - var path = sprintf('/%s/machines/%s', self.user, options.id); - 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/machines/%s', self.user, uuid); + this._request(endpoint, function (err, req, res, body) { + callback(err, body, res); }); }; diff --git a/lib/common.js b/lib/common.js index 83bff0b..7763507 100755 --- a/lib/common.js +++ b/lib/common.js @@ -134,6 +134,14 @@ function longAgo(when, now) { return '0s'; } +/** + * checks a string and returns a boolean based on if it + * is a UUID or not + */ +function isUUID(s) { + return /^([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}?)$/i.test(s); +} + //---- exports module.exports = { @@ -143,6 +151,7 @@ module.exports = { boolFromString: boolFromString, jsonStream: jsonStream, kvToObj: kvToObj, - longAgo: longAgo + longAgo: longAgo, + isUUID: isUUID, }; // vim: set softtabstop=4 shiftwidth=4: diff --git a/lib/do_instance.js b/lib/do_instance.js new file mode 100644 index 0000000..16168e2 --- /dev/null +++ b/lib/do_instance.js @@ -0,0 +1,60 @@ +/* + * Copyright 2015 Joyent Inc. + * + * `triton instance ...` + */ + +var common = require('./common'); + +function do_instance(subcmd, opts, args, callback) { + if (opts.help) { + this.do_help('help', {}, [subcmd], callback); + return; + } else if (args.length !== 1) { + callback(new Error('invalid args: ' + args)); + return; + } + + var id = args[0]; + + if (common.isUUID(id)) { + this.triton.cloudapi.getMachine(id, cb); + } else { + this.triton.getMachineByAlias(id, cb); + } + + function cb(err, machine) { + if (err) { + callback(err); + return; + } + + console.log(JSON.stringify(machine, null, 4)); + callback(); + } +} + +do_instance.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, + { + names: ['json', 'j'], + type: 'bool', + help: 'JSON output.' + } +]; +do_instance.help = ( + 'Show a single instance.\n' + + '\n' + + 'Usage:\n' + + ' {{name}} instance \n' + + '\n' + + '{{options}}' +); + +do_instance.aliases = ['inst']; + +module.exports = do_instance; diff --git a/lib/triton.js b/lib/triton.js index 02be813..85fead8 100644 --- a/lib/triton.js +++ b/lib/triton.js @@ -232,6 +232,32 @@ Triton.prototype.machineAudit = function machineAudit(options, callback) { }; +/** + * getMachine for an alias + * + * @param {String} alias - the machine alias + * @param {Function} callback `function (err, machine)` + */ +Triton.prototype.getMachineByAlias = function getMachineByAlias(alias, callback) { + this.cloudapi.listMachines({name: alias}, function (err, machines) { + if (err) { + callback(err); + return; + } + var found = false; + machines.forEach(function (machine) { + if (!found && machine.name === alias) { + callback(null, machine); + found = true; + } + }); + if (!found) { + callback(new Error('machine ' + alias + ' not found')); + return; + } + }); +}; + //---- exports