do_networks

This commit is contained in:
Dave Eddy 2015-08-26 16:40:50 -04:00
parent 835e1895bf
commit d2ce855a69
3 changed files with 156 additions and 18 deletions

View File

@ -79,7 +79,9 @@ function CLI() {
'image',
{ group: 'Packages' },
'packages',
'package'
'package',
{ group: 'Networks' },
'networks'
]
});
}
@ -156,6 +158,9 @@ CLI.prototype.do_ssh = require('./do_ssh');
CLI.prototype.do_packages = require('./do_packages');
CLI.prototype.do_package = require('./do_package');
// Networks
CLI.prototype.do_networks = require('./do_networks');
// Hidden commands
CLI.prototype.do_cloudapi = require('./do_cloudapi');
CLI.prototype.do_badger = require('./do_badger');

View File

@ -233,24 +233,50 @@ CloudAPI.prototype._request = function _request(options, callback) {
});
};
/**
* A simple wrapper around making a GET request to an endpoint and
* passing back the body returned
*/
CloudAPI.prototype._passThrough =
function _passThrough(endpoint, opts, cb) {
if (typeof (opts) === 'function') {
cb = opts;
opts = null;
}
opts = opts || {};
assert.string(endpoint, 'endpoint');
assert.object(opts, 'opts');
assert.func(cb, 'cb');
var p = this._path(endpoint, opts);
this._request(p, function (err, req, res, body) {
cb(err, body, res);
});
};
// ---- networks
/**
* Get network information
*
* @param {Function} callback of the form `function (err, networks, response)`
*/
CloudAPI.prototype.listNetworks = function listNetworks(opts, cb) {
var endpoint = format('/%s/networks', this.user);
this._passThrough(endpoint, opts, cb);
};
// ---- accounts
/**
* Get account information
*
* @param {Function} callback of the form `function (err, account, response)`
*/
CloudAPI.prototype.getAccount = function getAccount(callback) {
var self = this;
assert.func(callback, 'callback');
var endpoint = sprintf('/%s', self.user);
this._request(endpoint, function (err, req, res, body) {
callback(err, body, res);
});
CloudAPI.prototype.getAccount = function getAccount(opts, cb) {
var endpoint = sprintf('/%s', this.user);
this._passThrough(endpoint, opts, cb);
};
/**
@ -258,14 +284,9 @@ CloudAPI.prototype.getAccount = function getAccount(callback) {
*
* @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);
});
CloudAPI.prototype.listKeys = function listKeys(opts, cb) {
var endpoint = sprintf('/%s/keys', this.user);
this._passThrough(endpoint, opts, cb);
};
// ---- images

112
lib/do_networks.js Normal file
View File

@ -0,0 +1,112 @@
/*
* Copyright 2015 Joyent Inc.
*
* `triton networks ...`
*/
var tabula = require('tabula');
var common = require('./common');
// to be passed as query string args to /my/networks
var validFilters = [
'id',
'name',
'public',
'description'
];
// valid output fields to be printed
var validFields = [
'id',
'name',
'public',
'fabric',
'gateway',
'internet_nat',
'provision_end_ip',
'provision_start_ip',
'resolvers',
'subnet',
'vlan_id'
];
function do_networks(subcmd, opts, args, callback) {
if (opts.help) {
this.do_help('help', {}, [subcmd], callback);
return;
}
var columns = opts.o.trim().split(',');
var sort = opts.s.trim().split(',');
var listOpts;
try {
listOpts = common.kvToObj(args, validFilters);
} catch (e) {
callback(e);
return;
}
this.triton.cloudapi.listNetworks(function (err, networks) {
if (err) {
callback(err);
return;
}
if (opts.json) {
console.log(common.jsonStream(networks));
} else {
// pretty print
tabula(networks, {
skipHeader: opts.H,
columns: columns,
sort: sort,
validFields: validFields
});
}
callback();
});
}
do_networks.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
},
{
names: ['H'],
type: 'bool',
help: 'Omit table header row.'
},
{
names: ['o'],
type: 'string',
default: 'id,name,subnet,public,vlan_id,gateway',
help: 'Specify fields (columns) to output.',
helpArg: 'field1,...'
},
{
names: ['s'],
type: 'string',
default: 'name',
help: 'Sort on the given fields. Default is "name".',
helpArg: 'field1,...'
},
{
names: ['json', 'j'],
type: 'bool',
help: 'JSON output.'
}
];
do_networks.help = (
'Show networks.\n'
+ '\n'
+ 'Usage:\n'
+ ' {{name}} account\n'
+ '\n'
+ '{{options}}'
);
module.exports = do_networks;