joyent/node-triton#133 triton instance fwrule list and triton fwrules` are not recognized

Reviewed by: Trent Mick <trent.mick@joyent.com>
Approved by: Trent Mick <trent.mick@joyent.com>
This commit is contained in:
Yang Yong 2016-12-21 13:22:23 +09:00 committed by Trent Mick
parent 37d475ffd9
commit 16cff8d60a
8 changed files with 239 additions and 99 deletions

View File

@ -650,6 +650,7 @@ CLI.prototype.do_keys = require('./do_keys');
// Firewall rules
CLI.prototype.do_fwrule = require('./do_fwrule');
CLI.prototype.do_fwrules = require('./do_fwrules');
// Images
CLI.prototype.do_images = require('./do_images');

23
lib/do_fwrules.js Normal file
View File

@ -0,0 +1,23 @@
/*
* 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/.
*/
/*
* `triton fwrules ...` shortcut for `triton fwrule list ...`.
*/
function do_fwrules(subcmd, opts, args, callback) {
this.handlerFromSubcmd('fwrule').dispatch({
subcmd: 'list',
opts: opts,
args: args
}, callback);
}
do_fwrules.help = 'A shortcut for "triton fwrule list".';
do_fwrules.hidden = true;
do_fwrules.options = require('./do_fwrule/do_list').options;
module.exports = do_fwrules;

View File

@ -0,0 +1,112 @@
/*
* 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/.
*/
/*
* Copyright 2016 Joyent, Inc.
*
* `triton instance fwrules ...`
*/
var assert = require('assert-plus');
var tabula = require('tabula');
var common = require('../../common');
var errors = require('../../errors');
var COLUMNS_DEFAULT = 'shortid,enabled,global,rule';
var COLUMNS_LONG = 'id,enabled,global,rule,description';
var SORT_DEFAULT = 'rule';
function do_list(subcmd, opts, args, cb) {
assert.func(cb, 'cb');
if (opts.help) {
this.do_help('help', {}, [subcmd], cb);
return;
}
if (args.length === 0) {
cb(new errors.UsageError('missing INST argument'));
return;
} else if (args.length > 1) {
cb(new errors.UsageError('too many arguments: ' + args.join(' ')));
return;
}
var id = args[0];
var tritonapi = this.top.tritonapi;
common.cliSetupTritonApi({cli: this.top}, function onSetup(setupErr) {
if (setupErr) {
cb(setupErr);
}
tritonapi.listInstanceFirewallRules({
id: id
}, function onRules(err, rules) {
if (err) {
cb(err);
return;
}
if (opts.json) {
common.jsonStream(rules);
} else {
var columns = COLUMNS_DEFAULT;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = COLUMNS_LONG;
}
columns = columns.toLowerCase().split(',');
var sort = opts.s.toLowerCase().split(',');
if (columns.indexOf('shortid') !== -1) {
rules.forEach(function (rule) {
rule.shortid = common.normShortId(rule.id);
});
}
tabula(rules, {
skipHeader: opts.H,
columns: columns,
sort: sort
});
}
cb();
});
});
}
do_list.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: SORT_DEFAULT
}));
do_list.synopses = ['{{name}} {{cmd}} [OPTIONS] INST'];
do_list.help = [
'Show firewall rules applied to an instance.',
'',
'{{usage}}',
'',
'{{options}}',
'Where "INST" is an instance name, id, or short id.'
].join('\n');
do_list.completionArgtypes = ['tritoninstance', 'none'];
module.exports = do_list;

View File

@ -0,0 +1,45 @@
/*
* 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/.
*/
/*
* `triton instance fwrule ...`
*/
var Cmdln = require('cmdln').Cmdln;
var util = require('util');
// ---- CLI class
function InstanceFwruleCLI(parent) {
this.top = parent.top;
Cmdln.call(this, {
name: parent.name + ' fwrule',
desc: [
'List fwrules on Triton instances.'
].join('\n'),
helpOpts: {
minHelpCol: 24 /* line up with option help */
},
helpSubcmds: [
'help',
'list'
]
});
}
util.inherits(InstanceFwruleCLI, Cmdln);
// `triton instance fwrules` came first, so we'll hide this one.
InstanceFwruleCLI.hidden = true;
InstanceFwruleCLI.prototype.init = function init(opts, args, cb) {
this.log = this.top.log;
Cmdln.prototype.init.apply(this, arguments);
};
InstanceFwruleCLI.prototype.do_list = require('./do_list');
module.exports = InstanceFwruleCLI;

View File

@ -5,108 +5,22 @@
*/
/*
* Copyright 2016 Joyent, Inc.
*
* `triton instance fwrules ...`
* `triton instance fwrules ...` shortcut for
* `triton instance fwrule list ...`.
*/
var assert = require('assert-plus');
var tabula = require('tabula');
var common = require('../common');
var errors = require('../errors');
var COLUMNS_DEFAULT = 'shortid,enabled,global,rule';
var COLUMNS_LONG = 'id,enabled,global,rule,description';
var SORT_DEFAULT = 'rule';
function do_fwrules(subcmd, opts, args, cb) {
assert.func(cb, 'cb');
if (opts.help) {
this.do_help('help', {}, [subcmd], cb);
return;
}
if (args.length === 0) {
cb(new errors.UsageError('missing INST argument'));
return;
} else if (args.length > 1) {
cb(new errors.UsageError('too many arguments: ' + args.join(' ')));
return;
}
var id = args[0];
var cli = this.top;
common.cliSetupTritonApi({cli: this.top}, function onSetup(setupErr) {
if (setupErr) {
cb(setupErr);
}
cli.tritonapi.listInstanceFirewallRules({
id: id
}, function onRules(err, rules) {
if (err) {
cb(err);
return;
}
if (opts.json) {
common.jsonStream(rules);
} else {
var columns = COLUMNS_DEFAULT;
if (opts.o) {
columns = opts.o;
} else if (opts.long) {
columns = COLUMNS_LONG;
}
columns = columns.toLowerCase().split(',');
var sort = opts.s.toLowerCase().split(',');
if (columns.indexOf('shortid') !== -1) {
rules.forEach(function (rule) {
rule.shortid = common.normShortId(rule.id);
});
}
tabula(rules, {
skipHeader: opts.H,
columns: columns,
sort: sort
});
}
cb();
});
});
function do_fwrules(subcmd, opts, args, callback) {
this.handlerFromSubcmd('fwrule').dispatch({
subcmd: 'list',
opts: opts,
args: args
}, callback);
}
do_fwrules.options = [
{
names: ['help', 'h'],
type: 'bool',
help: 'Show this help.'
}
].concat(common.getCliTableOptions({
includeLong: true,
sortDefault: SORT_DEFAULT
}));
do_fwrules.synopses = ['{{name}} {{cmd}} [OPTIONS] INST'];
do_fwrules.help = [
'Show firewall rules applied to an instance.',
'',
'{{usage}}',
'',
'{{options}}',
'Where "INST" is an instance name, id, or short id.'
].join('\n');
do_fwrules.completionArgtypes = ['tritoninstance', 'none'];
var do_fwrule_list = require('./do_fwrule/do_list');
do_fwrules.help = do_fwrule_list.help;
do_fwrules.options = do_fwrule_list.options;
do_fwrules.synopses = do_fwrule_list.synopses;
do_fwrules.completionArgtypes = do_fwrule_list.completionArgtypes;
module.exports = do_fwrules;

View File

@ -70,6 +70,7 @@ InstanceCLI.prototype.do_start = require('./do_start');
InstanceCLI.prototype.do_stop = require('./do_stop');
InstanceCLI.prototype.do_reboot = require('./do_reboot');
InstanceCLI.prototype.do_fwrule = require('./do_fwrule');
InstanceCLI.prototype.do_fwrules = require('./do_fwrules');
InstanceCLI.prototype.do_enable_firewall = require('./do_enable_firewall');
InstanceCLI.prototype.do_disable_firewall = require('./do_disable_firewall');

View File

@ -187,6 +187,27 @@ test('triton fwrule', OPTS, function (tt) {
});
});
tt.test(' triton fwrules', function (t) {
h.triton('fwrules -l', function (err, stdout, stderr) {
if (h.ifErr(t, err, 'triton fwrule list'))
return t.end();
var rules = stdout.split('\n');
t.ok(rules[0].match(/ID\s+ENABLED\s+GLOBAL\s+RULE\s+DESCRIPTION/));
rules.shift();
t.ok(rules.length >= 1, 'triton fwrule list expected fwrule num');
var testRules = rules.filter(function (rule) {
return rule.match(ID);
});
t.equal(testRules.length, 1, 'triton fwrule list test rule found');
t.end();
});
});
tt.test(' triton fwrule instances', function (t) {
h.triton('fwrule instances -l ' + ID, function (err, stdout, stderr) {
if (h.ifErr(t, err, 'triton fwrule instances'))
@ -236,6 +257,28 @@ test('triton fwrule', OPTS, function (tt) {
});
});
tt.test(' triton instance fwrule list', function (t) {
h.triton('instance fwrule list -l ' + INST,
function (err, stdout, stderr) {
if (h.ifErr(t, err, 'triton fwrule list'))
return t.end();
var rules = stdout.split('\n');
t.ok(rules[0].match(/ID\s+ENABLED\s+GLOBAL\s+RULE\s+DESCRIPTION/));
rules.shift();
t.ok(rules.length >= 1, 'triton fwrule list expected fwrule num');
var testRules = rules.filter(function (rule) {
return rule.match(ID);
});
t.equal(testRules.length, 1, 'triton fwrule list test rule found');
t.end();
});
});
tt.test(' triton fwrule delete', function (t) {
var cmd = 'fwrule delete ' + ID + ' --force';
h.triton(cmd, function (err, stdout, stderr) {

View File

@ -72,6 +72,7 @@ var subs = [
['package', 'pkg'],
['package get'],
['package list', 'packages', 'pkgs'],
['fwrules'],
['fwrule'],
['fwrule create'],
['fwrule list', 'fwrule ls'],