do_network

This commit is contained in:
Dave Eddy 2015-08-26 17:09:50 -04:00
parent f11bf0c247
commit 83b1cf188f
4 changed files with 112 additions and 6 deletions

View File

@ -81,7 +81,8 @@ function CLI() {
'packages', 'packages',
'package', 'package',
{ group: 'Networks' }, { group: 'Networks' },
'networks' 'networks',
'network'
] ]
}); });
} }
@ -160,6 +161,7 @@ CLI.prototype.do_package = require('./do_package');
// Networks // Networks
CLI.prototype.do_networks = require('./do_networks'); CLI.prototype.do_networks = require('./do_networks');
CLI.prototype.do_network = require('./do_network');
// Hidden commands // Hidden commands
CLI.prototype.do_cloudapi = require('./do_cloudapi'); CLI.prototype.do_cloudapi = require('./do_cloudapi');

View File

@ -267,6 +267,22 @@ CloudAPI.prototype.listNetworks = function listNetworks(opts, cb) {
this._passThrough(endpoint, opts, cb); this._passThrough(endpoint, opts, cb);
}; };
/**
* <http://apidocs.joyent.com/cloudapi/#GetNetwork>
*
* @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 // ---- accounts
/** /**
@ -361,12 +377,11 @@ CloudAPI.prototype.getPackage = function getPackage(options, callback) {
* @param {String} uuid (required) The machine id. * @param {String} uuid (required) The machine id.
* @param {Function} callback of the form `function (err, machine, response)` * @param {Function} callback of the form `function (err, machine, response)`
*/ */
CloudAPI.prototype.getMachine = function getMachine(uuid, callback) { CloudAPI.prototype.getMachine = function getMachine(id, cb) {
var self = this; assert.uuid(id, 'id');
assert.string(uuid, 'uuid'); assert.func(cb, 'cb');
assert.func(callback, 'callback');
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) { this._request(endpoint, function (err, req, res, body) {
callback(err, body, res); callback(err, body, res);
}); });

84
lib/do_network.js Normal file
View File

@ -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 <id>\n'
+ '\n'
+ '{{options}}'
);
module.exports = do_network;

View File

@ -35,11 +35,15 @@ function do_networks(subcmd, opts, args, callback) {
if (opts.help) { if (opts.help) {
this.do_help('help', {}, [subcmd], callback); this.do_help('help', {}, [subcmd], callback);
return; return;
} else if (args.length !== 0) {
callback(new Error('invalid args: ' + args));
return;
} }
var columns = opts.o.trim().split(','); var columns = opts.o.trim().split(',');
var sort = opts.s.trim().split(','); var sort = opts.s.trim().split(',');
/* not supported
var listOpts; var listOpts;
try { try {
listOpts = common.kvToObj(args, validFilters); listOpts = common.kvToObj(args, validFilters);
@ -47,6 +51,7 @@ function do_networks(subcmd, opts, args, callback) {
callback(e); callback(e);
return; return;
} }
*/
this.triton.cloudapi.listNetworks(function (err, networks) { this.triton.cloudapi.listNetworks(function (err, networks) {
if (err) { if (err) {