From 09878818876cfa28753864e883603124f567cc45 Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Tue, 25 Aug 2015 15:47:29 -0700 Subject: [PATCH] 'triton image ID', update TODO.txt --- TODO.txt | 26 +++++++++---------- lib/cli.js | 2 ++ lib/cloudapi2.js | 64 +++++++++++++++++++++++------------------------ lib/do_image.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/do_images.js | 10 +------- 5 files changed, 112 insertions(+), 55 deletions(-) create mode 100644 lib/do_image.js diff --git a/TODO.txt b/TODO.txt index c3cae5c..29f5d12 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,26 +1,26 @@ # today -triton +triton instances|insts # list machines +triton instance|inst ID|NAME|UNIQUE-NAME-SUBSTRING # get machine + # -1 for unique match, a la 'vmadm lookup -1' -triton -v # bunyan trace logging - -triton vms|containers|machines # list machines -triton vm|container|machine ID|ALIAS # get machine - # -1 for unique match - -triton images # list -triton packages # list -triton image IMAGE triton package PACKAGE -triton provision # triton create-machine ?? - +triton create # triton create-instance triton create -p PKG [...] IMG triton create -i IMG -p PKG [-n NAME] [...] # example: triton create base64 -p t4-standard-1g +# DONE + +triton +triton -v # bunyan trace logging +triton images # list images +triton packages # list packages +triton image IMAGE # get image + @@ -86,4 +86,4 @@ For today: only the implicit 'env' profile. triton config get|set|list # see 'npm config' -triton --shell # or whatever, repl \ No newline at end of file +triton --shell # or whatever, repl diff --git a/lib/cli.js b/lib/cli.js index 2c7d2c7..00617b9 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -64,6 +64,7 @@ function CLI() { 'instance-audit', { group: 'Images' }, 'images', + 'image', { group: 'Other', unmatched: true } ] }); @@ -101,6 +102,7 @@ CLI.prototype.init = function (opts, args, callback) { // Images CLI.prototype.do_images = require('./do_images'); +CLI.prototype.do_image = require('./do_image'); // Instances (aka VMs/containers/machines) CLI.prototype.do_create = require('./do_create'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index 832b47a..69349b9 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -194,14 +194,14 @@ CloudAPI.prototype._path = function _path(subpath /*, qparams, ... */) { /** - * cloud API requset wrapper - modeled after http.request + * Cloud API request wrapper - modeled after http.request * * @param {Object|String} options - object or string for endpoint * - {String} path - URL endpoint to hit * - {String} method - HTTP(s) request method * @param {Function} callback passed via the restify client */ -CloudAPI.prototype.request = function _request(options, callback) { +CloudAPI.prototype._request = function _request(options, callback) { var self = this; if (typeof options === 'string') options = {path: options}; @@ -224,6 +224,8 @@ CloudAPI.prototype.request = function _request(options, callback) { }); }; + + // ---- accounts /** @@ -281,32 +283,32 @@ CloudAPI.prototype.listImages = function listImages(options, callback) { assert.object(options, 'options'); assert.func(callback, 'callback'); - var query = { - name: options.name, - os: options.os, - version: options.version, - public: options.public, - state: options.state, - owner: options.owner, - type: options.type - }; + var endpoint = self._path(format('/%s/images', self.user), options); + self._request(endpoint, function (err, req, res, body) { + callback(err, body, res); + }); +}; - self._getAuthHeaders(function (hErr, headers) { - if (hErr) { - callback(hErr); - return; - } - var opts = { - path: self._path(format('/%s/images', self.user), query), - headers: headers - }; - self.client.get(opts, function (err, req, res, body) { - if (err) { - callback(err, null, res); - } else { - callback(null, body, res); - } - }); + +/** + * + * + * @param {Object} options + * - id {UUID} + * @param {Function} callback of the form `function (err, image, res)` + */ +CloudAPI.prototype.getImage = function getImage(options, callback) { + if (callback === undefined) { + callback = options; + options = {}; + } + assert.object(options, 'options'); + assert.uuid(options.id, 'ID'); + assert.func(callback, 'callback'); + + var endpoint = this._path(format('/%s/images/%s', this.user, options.id)); + this._request(endpoint, function (err, req, res, body) { + callback(err, body, res); }); }; @@ -439,12 +441,8 @@ CloudAPI.prototype.listPackages = function listPackages(options, callback) { } var endpoint = self._path(format('/%s/packages', self.user), options); - self.request(endpoint, function (err, req, res, body) { - if (err) { - callback(err); - return; - } - callback(null, body); + self._request(endpoint, function (err, req, res, body) { + callback(err, body, res); }); }; diff --git a/lib/do_image.js b/lib/do_image.js new file mode 100644 index 0000000..0841646 --- /dev/null +++ b/lib/do_image.js @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2015 Joyent Inc. All rights reserved. + * + * `triton image ...` + */ + +var format = require('util').format; +var tabula = require('tabula'); + +var errors = require('./errors'); + + +function do_image(subcmd, opts, args, callback) { + if (opts.help) { + this.do_help('help', {}, [subcmd], callback); + return; + } else if (args.length !== 1) { + return callback(new errors.UsageError(format( + 'incorrect number of args (%d): %s', args.length, args.join(' ')))); + } + + var getOpts = { + id: args[0] + }; + this.triton.cloudapi.getImage(getOpts, function onRes(err, img, res) { + if (err) { + return callback(err); + } + + if (opts.json) { + console.log(JSON.stringify(img)); + } else { + console.log(JSON.stringify(img, null, 4)); + } + callback(); + }); +}; + +do_image.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, + { + names: ['json', 'j'], + type: 'bool', + help: 'JSON stream output.' + } +]; +do_image.help = ( + /* BEGIN JSSTYLED */ + 'Get an image.\n' + + '\n' + + 'Note: Currently this dumps prettified JSON by default. That might change\n' + + 'in the future. Use "-j" to explicitly get JSON output.\n' + + '\n' + + 'Usage:\n' + + ' {{name}} image [] ID\n' + + '\n' + + '{{options}}' + /* END JSSTYLED */ +); + +module.exports = do_image; diff --git a/lib/do_images.js b/lib/do_images.js index 2c37892..d8068a6 100644 --- a/lib/do_images.js +++ b/lib/do_images.js @@ -121,20 +121,12 @@ do_images.options = [ help: 'JSON stream output.' } ]; -do_images.help = ( - 'List images.\n' - + '\n' - + 'Usage:\n' - + ' {{name}} images\n' - + '\n' - + '{{options}}' -); do_images.help = ( /* BEGIN JSSTYLED */ 'List images.\n' + '\n' + 'Usage:\n' + - ' {{name}} list [] []\n' + + ' {{name}} images [] []\n' + '\n' + 'Filters:\n' + ' FIELD=VALUE Field equality filter. Supported fields: \n' +