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'
|
|
|
|
];
|
|
|
|
|
|
|
|
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-02 10:03:17 +03:00
|
|
|
var columns = 'shortid,name,subnet,gateway,fabric,vlan,public'.split(',');
|
|
|
|
if (opts.o) {
|
|
|
|
/* JSSTYLED */
|
|
|
|
columns = opts.o.trim().split(/\s*,\s*/g);
|
|
|
|
} else if (opts.long) {
|
|
|
|
columns[0] = 'id';
|
2015-08-26 23:40:50 +03:00
|
|
|
}
|
2015-09-02 10:03:17 +03:00
|
|
|
var sort = opts.s.trim().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.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['H'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Omit table header row.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['o'],
|
|
|
|
type: 'string',
|
|
|
|
help: 'Specify fields (columns) to output.',
|
|
|
|
helpArg: 'field1,...'
|
|
|
|
},
|
2015-09-02 10:03:17 +03:00
|
|
|
{
|
|
|
|
names: ['long', 'l'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Long/wider output. Ignored if "-o ..." is used.'
|
|
|
|
},
|
2015-08-26 23:40:50 +03:00
|
|
|
{
|
|
|
|
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.'
|
|
|
|
}
|
|
|
|
];
|
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;
|