joyent/node-triton#80 would be nice if triton network list public=false worked

Reviewed by: Trent Mick <trentm@gmail.com>
Approved by: Trent Mick <trentm@gmail.com>
This commit is contained in:
Yang Yong 2017-01-17 15:39:43 -08:00 committed by Trent Mick
parent 41b5c506ce
commit c7c91aea83
3 changed files with 109 additions and 23 deletions

View File

@ -7,6 +7,9 @@ Known issues:
## not yet released ## not yet released
- [joyent/node-triton#80] Add `triton network list public=true|false`
filtering. Note that this filtering is client-side.
- [joyent/node-triton#146] Add `--wait` flag to `triton instance rename`. - [joyent/node-triton#146] Add `--wait` flag to `triton instance rename`.
- [joyent/node-triton#133] Add `triton inst fwrule list` and `triton fwrules` shortcuts - [joyent/node-triton#133] Add `triton inst fwrule list` and `triton fwrules` shortcuts

View File

@ -5,21 +5,20 @@
*/ */
/* /*
* Copyright 2016 Joyent, Inc. * Copyright 2017 Joyent, Inc.
* *
* `triton network list ...` * `triton network list ...`
*/ */
var tabula = require('tabula'); var tabula = require('tabula');
var vasync = require('vasync');
var common = require('../common'); var common = require('../common');
var errors = require('../errors');
// to be passed as query string args to /my/networks
var validFilters = [ var validFilters = [
'id', 'public'
'name',
'public',
'description'
]; ];
// columns default without -o // columns default without -o
@ -32,12 +31,10 @@ var columnsDefaultLong = 'id,name,subnet,gateway,fabric,vlan,public';
var sortDefault = 'name'; var sortDefault = 'name';
function do_list(subcmd, opts, args, callback) { function do_list(subcmd, opts, args, callback) {
var self = this;
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 = columnsDefault; var columns = columnsDefault;
@ -49,18 +46,55 @@ function do_list(subcmd, opts, args, callback) {
columns = columns.split(','); columns = columns.split(',');
var sort = opts.s.split(','); var sort = opts.s.split(',');
var tritonapi = this.top.tritonapi; 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');
}
common.cliSetupTritonApi({cli: this.top}, function onSetup(setupErr) { vasync.pipeline({arg: {cli: this.top}, funcs: [
if (setupErr) { common.cliSetupTritonApi,
callback(setupErr);
} function searchNetworks(arg, next) {
tritonapi.cloudapi.listNetworks(function (err, networks) { self.top.tritonapi.cloudapi.listNetworks(function (err, networks) {
if (err) { if (err) {
callback(err); next(err);
return; 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);
}
} }
arg.filteredNetworks = filteredNetworks;
next();
},
function doneNetworks(arg, next) {
var networks = arg.filteredNetworks;
if (opts.json) { if (opts.json) {
common.jsonStream(networks); common.jsonStream(networks);
} else { } else {
@ -75,9 +109,9 @@ function do_list(subcmd, opts, args, callback) {
sort: sort sort: sort
}); });
} }
callback(); next();
}); }
}); ]}, callback);
} }
do_list.options = [ do_list.options = [
@ -91,13 +125,16 @@ do_list.options = [
sortDefault: sortDefault sortDefault: sortDefault
})); }));
do_list.synopses = ['{{name}} {{cmd}}']; do_list.synopses = ['{{name}} {{cmd}} [FILTERS...]'];
do_list.help = [ do_list.help = [
'List available networks.', 'List available networks.',
'', '',
'{{usage}}', '{{usage}}',
'', '',
'Filters:',
' FIELD=true|false Boolean filter. Supported fields: public',
'',
'{{options}}', '{{options}}',
'Fields (most are self explanatory, the client adds some for convenience):', 'Fields (most are self explanatory, the client adds some for convenience):',
' vlan A shorter alias for "vlan_id".', ' vlan A shorter alias for "vlan_id".',

View File

@ -5,7 +5,7 @@
*/ */
/* /*
* Copyright (c) 2016, Joyent, Inc. * Copyright 2017 Joyent, Inc.
*/ */
/* /*
@ -66,6 +66,52 @@ test('triton networks', function (tt) {
}); });
}); });
tt.test(' triton networks public=false', function (t) {
h.triton('networks public=false -H -o public',
function (err, stdout, stderr) {
if (h.ifErr(t, err))
return t.end();
var results = stdout.trim().split('\n');
results.forEach(function (result) {
t.equal(false, common.boolFromString(result, null, 'public'));
});
t.end();
});
});
tt.test(' triton network list public=false', function (t) {
h.triton('network list public=false -H -o public',
function (err, stdout, stderr) {
if (h.ifErr(t, err))
return t.end();
var results = stdout.trim().split('\n');
results.forEach(function (result) {
t.equal(false, common.boolFromString(result, null, 'public'));
});
t.end();
});
});
tt.test(' triton network list public=true', function (t) {
h.triton('network list public=true -H -o public',
function (err, stdout, stderr) {
if (h.ifErr(t, err))
return t.end();
var results = stdout.trim().split('\n');
results.forEach(function (result) {
t.equal(true, common.boolFromString(result, null, 'public'));
});
t.end();
});
});
tt.test(' triton network list public=bogus', function (t) {
h.triton('network list public=bogus', function (err, stdout, stderr) {
t.ok(err, err);
t.end();
});
});
tt.test(' triton networks -l', function (t) { tt.test(' triton networks -l', function (t) {
h.triton('networks -l', function (err, stdout, stderr) { h.triton('networks -l', function (err, stdout, stderr) {
if (h.ifErr(t, err)) if (h.ifErr(t, err))