2015-08-26 23:40:50 +03:00
|
|
|
/*
|
|
|
|
* 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'
|
|
|
|
];
|
|
|
|
|
2015-09-04 01:09:21 +03:00
|
|
|
// columns default without -o
|
|
|
|
var columnsDefault = 'shortid,name,subnet,gateway,fabric,vlan,public';
|
|
|
|
|
|
|
|
// columns default with -l
|
|
|
|
var columnsDefaultLong = 'id,name,subnet,gateway,fabric,vlan,public';
|
|
|
|
|
|
|
|
// sort default with -s
|
|
|
|
var sortDefault = 'name';
|
|
|
|
|
2015-08-26 23:40:50 +03:00
|
|
|
function do_networks(subcmd, opts, args, callback) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
2015-08-27 00:09:50 +03:00
|
|
|
} else if (args.length !== 0) {
|
|
|
|
callback(new Error('invalid args: ' + args));
|
|
|
|
return;
|
2015-08-26 23:40:50 +03:00
|
|
|
}
|
|
|
|
|
2015-09-04 01:09:21 +03:00
|
|
|
var columns = columnsDefault;
|
2015-09-02 10:03:17 +03:00
|
|
|
if (opts.o) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = opts.o;
|
2015-09-02 10:03:17 +03:00
|
|
|
} else if (opts.long) {
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columnsDefaultLong;
|
2015-08-26 23:40:50 +03:00
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
columns = columns.split(',');
|
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
2015-08-26 23:40:50 +03:00
|
|
|
|
|
|
|
this.triton.cloudapi.listNetworks(function (err, networks) {
|
|
|
|
if (err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.json) {
|
2015-08-26 23:53:23 +03:00
|
|
|
common.jsonStream(networks);
|
2015-08-26 23:40:50 +03:00
|
|
|
} else {
|
2015-09-02 10:03:17 +03:00
|
|
|
for (var i = 0; i < networks.length; i++) {
|
|
|
|
var net = networks[i];
|
|
|
|
net.shortid = net.id.split('-', 1)[0];
|
|
|
|
net.vlan = net.vlan_id;
|
|
|
|
}
|
2015-08-26 23:40:50 +03:00
|
|
|
tabula(networks, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
2015-09-02 10:03:17 +03:00
|
|
|
sort: sort
|
2015-08-26 23:40:50 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
do_networks.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
}
|
2015-09-04 01:09:21 +03:00
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
|
|
|
|
2015-09-02 10:03:17 +03:00
|
|
|
do_networks.help = [
|
|
|
|
'List available networks.',
|
|
|
|
'',
|
|
|
|
'Usage:',
|
|
|
|
' {{name}} networks',
|
|
|
|
'',
|
|
|
|
'Fields (most are self explanatory, the client adds some for convenience):',
|
|
|
|
' vlan A shorter alias for "vlan_id".',
|
|
|
|
' shortid A short ID prefix.',
|
|
|
|
'',
|
|
|
|
'{{options}}'
|
|
|
|
].join('\n');
|
2015-08-26 23:40:50 +03:00
|
|
|
|
|
|
|
module.exports = do_networks;
|