From 83b1cf188f7cff5fab1bb62c510ba14e8d893b26 Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Wed, 26 Aug 2015 17:09:50 -0400 Subject: [PATCH] do_network --- lib/cli.js | 4 ++- lib/cloudapi2.js | 25 +++++++++++--- lib/do_network.js | 84 ++++++++++++++++++++++++++++++++++++++++++++++ lib/do_networks.js | 5 +++ 4 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 lib/do_network.js diff --git a/lib/cli.js b/lib/cli.js index 071389e..9a92316 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -81,7 +81,8 @@ function CLI() { 'packages', 'package', { group: 'Networks' }, - 'networks' + 'networks', + 'network' ] }); } @@ -160,6 +161,7 @@ CLI.prototype.do_package = require('./do_package'); // Networks CLI.prototype.do_networks = require('./do_networks'); +CLI.prototype.do_network = require('./do_network'); // Hidden commands CLI.prototype.do_cloudapi = require('./do_cloudapi'); diff --git a/lib/cloudapi2.js b/lib/cloudapi2.js index f62ddc1..b8f4bf2 100644 --- a/lib/cloudapi2.js +++ b/lib/cloudapi2.js @@ -267,6 +267,22 @@ CloudAPI.prototype.listNetworks = function listNetworks(opts, cb) { this._passThrough(endpoint, opts, cb); }; +/** + * + * + * @param {String} - UUID + * @param {Function} callback of the form `function (err, network, res)` + */ +CloudAPI.prototype.getNetwork = function getNetwork(id, cb) { + assert.uuid(id, 'id'); + assert.func(cb, 'cb'); + + var endpoint = this._path(format('/%s/networks/%s', this.user, id)); + this._request(endpoint, function (err, req, res, body) { + cb(err, body, res); + }); +}; + // ---- accounts /** @@ -361,12 +377,11 @@ CloudAPI.prototype.getPackage = function getPackage(options, callback) { * @param {String} uuid (required) The machine id. * @param {Function} callback of the form `function (err, machine, response)` */ -CloudAPI.prototype.getMachine = function getMachine(uuid, callback) { - var self = this; - assert.string(uuid, 'uuid'); - assert.func(callback, 'callback'); +CloudAPI.prototype.getMachine = function getMachine(id, cb) { + assert.uuid(id, 'id'); + assert.func(cb, 'cb'); - var endpoint = sprintf('/%s/machines/%s', self.user, uuid); + var endpoint = sprintf('/%s/machines/%s', this.user, id); this._request(endpoint, function (err, req, res, body) { callback(err, body, res); }); diff --git a/lib/do_network.js b/lib/do_network.js new file mode 100644 index 0000000..03a1a48 --- /dev/null +++ b/lib/do_network.js @@ -0,0 +1,84 @@ +/* + * Copyright 2015 Joyent Inc. + * + * `triton network ...` + */ + +var common = require('./common'); + +function do_network(subcmd, opts, args, cb) { + if (opts.help) { + this.do_help('help', {}, [subcmd], cb); + return; + } else if (args.length !== 1) { + cb(new Error('invalid args: ' + args)); + return; + } + + var id = args[0]; + + if (common.isUUID(id)) { + this.triton.cloudapi.getNetwork(id, done); + } else { + // we have to list all networks and find the one pertaining + // to the alias given + this.triton.cloudapi.listNetworks(function (err, networks) { + if (err) { + done(err); + return; + } + + var net; + // try to find the network + networks.forEach(function (network) { + if (network.name === id) + net = network; + }); + + if (net) { + // found! + done(null, net); + } else { + // not found + done(new Error('network ' + id + ' not found')); + } + }); + } + + function done(err, network) { + if (err) { + cb(err); + return; + } + + if (opts.json) { + console.log(JSON.stringify(network)); + } else { + console.log(JSON.stringify(network, null, 4)); + } + cb(); + } +} + +do_network.options = [ + { + names: ['help', 'h'], + type: 'bool', + help: 'Show this help.' + }, + { + names: ['json', 'j'], + type: 'bool', + help: 'JSON output.' + } +]; +do_network.help = ( + 'Show a network.\n' + + '\n' + + 'Usage:\n' + + ' {{name}} network \n' + + '\n' + + '{{options}}' +); + +module.exports = do_network; diff --git a/lib/do_networks.js b/lib/do_networks.js index 6d5b37c..215866b 100644 --- a/lib/do_networks.js +++ b/lib/do_networks.js @@ -35,11 +35,15 @@ function do_networks(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; } var columns = opts.o.trim().split(','); var sort = opts.s.trim().split(','); + /* not supported var listOpts; try { listOpts = common.kvToObj(args, validFilters); @@ -47,6 +51,7 @@ function do_networks(subcmd, opts, args, callback) { callback(e); return; } + */ this.triton.cloudapi.listNetworks(function (err, networks) { if (err) {