From 16cff8d60a65e3bad6c90f52130230823d9f375b Mon Sep 17 00:00:00 2001 From: Yang Yong Date: Wed, 21 Dec 2016 13:22:23 +0900 Subject: [PATCH] joyent/node-triton#133 triton instance fwrule list` and `triton fwrules` are not recognized Reviewed by: Trent Mick Approved by: Trent Mick --- lib/cli.js | 1 + lib/do_fwrules.js | 23 +++++ lib/do_instance/do_fwrule/do_list.js | 112 +++++++++++++++++++++++ lib/do_instance/do_fwrule/index.js | 45 +++++++++ lib/do_instance/do_fwrules.js | 112 +++-------------------- lib/do_instance/index.js | 1 + test/integration/cli-fwrules.test.js | 43 +++++++++ test/integration/cli-subcommands.test.js | 1 + 8 files changed, 239 insertions(+), 99 deletions(-) create mode 100644 lib/do_fwrules.js create mode 100644 lib/do_instance/do_fwrule/do_list.js create mode 100644 lib/do_instance/do_fwrule/index.js diff --git a/lib/cli.js b/lib/cli.js index 7373186..df36711 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -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'); diff --git a/lib/do_fwrules.js b/lib/do_fwrules.js new file mode 100644 index 0000000..fba1a8a --- /dev/null +++ b/lib/do_fwrules.js @@ -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; diff --git a/lib/do_instance/do_fwrule/do_list.js b/lib/do_instance/do_fwrule/do_list.js new file mode 100644 index 0000000..5e27798 --- /dev/null +++ b/lib/do_instance/do_fwrule/do_list.js @@ -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; diff --git a/lib/do_instance/do_fwrule/index.js b/lib/do_instance/do_fwrule/index.js new file mode 100644 index 0000000..a2d974e --- /dev/null +++ b/lib/do_instance/do_fwrule/index.js @@ -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; diff --git a/lib/do_instance/do_fwrules.js b/lib/do_instance/do_fwrules.js index 00eac1f..62d9516 100644 --- a/lib/do_instance/do_fwrules.js +++ b/lib/do_instance/do_fwrules.js @@ -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; diff --git a/lib/do_instance/index.js b/lib/do_instance/index.js index 1165d22..f2554bd 100644 --- a/lib/do_instance/index.js +++ b/lib/do_instance/index.js @@ -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'); diff --git a/test/integration/cli-fwrules.test.js b/test/integration/cli-fwrules.test.js index 6aee9d4..6d93974 100644 --- a/test/integration/cli-fwrules.test.js +++ b/test/integration/cli-fwrules.test.js @@ -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) { diff --git a/test/integration/cli-subcommands.test.js b/test/integration/cli-subcommands.test.js index 4723668..3c83c37 100644 --- a/test/integration/cli-subcommands.test.js +++ b/test/integration/cli-subcommands.test.js @@ -72,6 +72,7 @@ var subs = [ ['package', 'pkg'], ['package get'], ['package list', 'packages', 'pkgs'], + ['fwrules'], ['fwrule'], ['fwrule create'], ['fwrule list', 'fwrule ls'],