2016-01-05 07:53:52 +02:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-01-18 01:39:43 +02:00
|
|
|
* Copyright 2017 Joyent, Inc.
|
2016-01-05 07:53:52 +02:00
|
|
|
*
|
|
|
|
* `triton network list ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var tabula = require('tabula');
|
2017-01-18 01:39:43 +02:00
|
|
|
var vasync = require('vasync');
|
2016-01-05 07:53:52 +02:00
|
|
|
|
|
|
|
var common = require('../common');
|
2017-01-18 01:39:43 +02:00
|
|
|
var errors = require('../errors');
|
|
|
|
|
2016-01-05 07:53:52 +02:00
|
|
|
|
|
|
|
var validFilters = [
|
2017-01-18 01:39:43 +02:00
|
|
|
'public'
|
2016-01-05 07:53:52 +02: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';
|
|
|
|
|
|
|
|
function do_list(subcmd, opts, args, callback) {
|
2017-01-18 01:39:43 +02:00
|
|
|
var self = this;
|
2016-01-05 07:53:52 +02:00
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], callback);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var columns = columnsDefault;
|
|
|
|
if (opts.o) {
|
|
|
|
columns = opts.o;
|
|
|
|
} else if (opts.long) {
|
|
|
|
columns = columnsDefaultLong;
|
|
|
|
}
|
|
|
|
columns = columns.split(',');
|
|
|
|
|
|
|
|
var sort = opts.s.split(',');
|
2017-01-18 01:39:43 +02:00
|
|
|
var filters;
|
|
|
|
try {
|
|
|
|
filters = common.kvToObj(args, validFilters);
|
|
|
|
} catch (e) {
|
|
|
|
callback(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (filters.hasOwnProperty('public')) {
|
|
|
|
filters.public =
|
|
|
|
common.boolFromString(filters.public, null, 'public');
|
|
|
|
}
|
2016-01-05 07:53:52 +02:00
|
|
|
|
2017-01-18 01:39:43 +02:00
|
|
|
vasync.pipeline({arg: {cli: this.top}, funcs: [
|
|
|
|
common.cliSetupTritonApi,
|
|
|
|
|
|
|
|
function searchNetworks(arg, next) {
|
|
|
|
self.top.tritonapi.cloudapi.listNetworks(function (err, networks) {
|
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
arg.networks = networks;
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function filterNetworks(arg, next) {
|
|
|
|
var filteredNetworks = [];
|
|
|
|
var filterKeys = Object.keys(filters);
|
|
|
|
for (var i = 0; i < arg.networks.length; i++) {
|
|
|
|
var network = arg.networks[i];
|
|
|
|
var keepIt = true;
|
|
|
|
for (var j = 0; j < filterKeys.length; j++) {
|
|
|
|
var k = filterKeys[j];
|
|
|
|
if (network[k] !== filters[k]) {
|
|
|
|
keepIt = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (keepIt) {
|
|
|
|
filteredNetworks.push(network);
|
|
|
|
}
|
2016-12-13 20:04:41 +02:00
|
|
|
}
|
2017-01-18 01:39:43 +02:00
|
|
|
arg.filteredNetworks = filteredNetworks;
|
|
|
|
next();
|
|
|
|
},
|
2016-01-05 07:53:52 +02:00
|
|
|
|
2017-01-18 01:39:43 +02:00
|
|
|
function doneNetworks(arg, next) {
|
|
|
|
var networks = arg.filteredNetworks;
|
2016-12-13 20:04:41 +02:00
|
|
|
if (opts.json) {
|
|
|
|
common.jsonStream(networks);
|
|
|
|
} else {
|
|
|
|
for (var i = 0; i < networks.length; i++) {
|
|
|
|
var net = networks[i];
|
|
|
|
net.shortid = net.id.split('-', 1)[0];
|
|
|
|
net.vlan = net.vlan_id;
|
|
|
|
}
|
|
|
|
tabula(networks, {
|
|
|
|
skipHeader: opts.H,
|
|
|
|
columns: columns,
|
|
|
|
sort: sort
|
|
|
|
});
|
2016-01-05 07:53:52 +02:00
|
|
|
}
|
2017-01-18 01:39:43 +02:00
|
|
|
next();
|
|
|
|
}
|
|
|
|
]}, callback);
|
2016-01-05 07:53:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do_list.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
}
|
|
|
|
].concat(common.getCliTableOptions({
|
|
|
|
includeLong: true,
|
|
|
|
sortDefault: sortDefault
|
|
|
|
}));
|
|
|
|
|
2017-01-18 01:39:43 +02:00
|
|
|
do_list.synopses = ['{{name}} {{cmd}} [FILTERS...]'];
|
2016-06-09 00:13:16 +03:00
|
|
|
|
2016-01-05 07:53:52 +02:00
|
|
|
do_list.help = [
|
|
|
|
'List available networks.',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{usage}}',
|
2016-01-05 07:53:52 +02:00
|
|
|
'',
|
2017-01-18 01:39:43 +02:00
|
|
|
'Filters:',
|
|
|
|
' FIELD=true|false Boolean filter. Supported fields: public',
|
|
|
|
'',
|
2016-06-09 00:13:16 +03:00
|
|
|
'{{options}}',
|
2016-01-05 07:53:52 +02:00
|
|
|
'Fields (most are self explanatory, the client adds some for convenience):',
|
|
|
|
' vlan A shorter alias for "vlan_id".',
|
2016-06-09 00:13:16 +03:00
|
|
|
' shortid A short ID prefix.'
|
2016-01-05 07:53:52 +02:00
|
|
|
].join('\n');
|
|
|
|
|
|
|
|
do_list.aliases = ['ls'];
|
|
|
|
|
|
|
|
module.exports = do_list;
|