2016-02-04 15:39:50 +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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright 2016 Joyent, Inc.
|
|
|
|
*
|
|
|
|
* `triton fwrule update ...`
|
|
|
|
*/
|
|
|
|
|
|
|
|
var assert = require('assert-plus');
|
|
|
|
var format = require('util').format;
|
|
|
|
var fs = require('fs');
|
|
|
|
var vasync = require('vasync');
|
|
|
|
|
|
|
|
var common = require('../common');
|
|
|
|
var errors = require('../errors');
|
2016-03-02 02:46:06 +02:00
|
|
|
var UPDATE_FWRULE_FIELDS
|
|
|
|
= require('../cloudapi2').CloudApi.prototype.UPDATE_FWRULE_FIELDS;
|
2016-02-04 15:39:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
function do_update(subcmd, opts, args, cb) {
|
|
|
|
if (opts.help) {
|
|
|
|
this.do_help('help', {}, [subcmd], cb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var log = this.log;
|
|
|
|
var tritonapi = this.top.tritonapi;
|
|
|
|
|
|
|
|
if (args.length === 0) {
|
2016-02-16 15:13:29 +02:00
|
|
|
cb(new errors.UsageError('missing <fwrule-id> argument'));
|
2016-02-04 15:39:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-02 02:46:06 +02:00
|
|
|
var id = args.shift();
|
2016-02-04 15:39:50 +02:00
|
|
|
|
|
|
|
vasync.pipeline({arg: {}, funcs: [
|
|
|
|
function gatherDataArgs(ctx, next) {
|
|
|
|
if (opts.file) {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.data = common.objFromKeyValueArgs(args, {
|
|
|
|
disableDotted: true,
|
2016-03-02 02:46:06 +02:00
|
|
|
typeHintFromKey: UPDATE_FWRULE_FIELDS
|
2016-02-04 15:39:50 +02:00
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
|
|
|
function gatherDataFile(ctx, next) {
|
|
|
|
if (!opts.file || opts.file === '-') {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var input = fs.readFileSync(opts.file, 'utf8');
|
|
|
|
|
|
|
|
try {
|
|
|
|
ctx.data = JSON.parse(input);
|
|
|
|
} catch (err) {
|
|
|
|
next(new errors.TritonError(format(
|
|
|
|
'invalid JSON for firewall rule update in "%s": %s',
|
|
|
|
opts.file, err)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
|
|
|
function gatherDataStdin(ctx, next) {
|
|
|
|
if (opts.file !== '-') {
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var stdin = '';
|
|
|
|
|
|
|
|
process.stdin.resume();
|
|
|
|
process.stdin.on('data', function (chunk) {
|
|
|
|
stdin += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
process.stdin.on('end', function () {
|
|
|
|
try {
|
|
|
|
ctx.data = JSON.parse(stdin);
|
|
|
|
} catch (err) {
|
|
|
|
log.trace({stdin: stdin},
|
|
|
|
'invalid firewall rule update JSON on stdin');
|
|
|
|
next(new errors.TritonError(format(
|
|
|
|
'invalid JSON for firewall rule update on stdin: %s',
|
|
|
|
err)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
function validateIt(ctx, next) {
|
|
|
|
var keys = Object.keys(ctx.data);
|
2016-02-05 14:54:13 +02:00
|
|
|
|
|
|
|
if (keys.length === 0) {
|
|
|
|
console.log('No fields given for firewall rule update');
|
|
|
|
next();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-04 15:39:50 +02:00
|
|
|
for (var i = 0; i < keys.length; i++) {
|
|
|
|
var key = keys[i];
|
|
|
|
var value = ctx.data[key];
|
2016-03-02 02:46:06 +02:00
|
|
|
var type = UPDATE_FWRULE_FIELDS[key];
|
2016-02-04 15:39:50 +02:00
|
|
|
if (!type) {
|
|
|
|
next(new errors.UsageError(format('unknown or ' +
|
|
|
|
'unupdateable field: %s (updateable fields are: %s)',
|
|
|
|
key,
|
2016-03-02 02:46:06 +02:00
|
|
|
Object.keys(UPDATE_FWRULE_FIELDS).sort().join(', '))));
|
2016-02-04 15:39:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof (value) !== type) {
|
|
|
|
next(new errors.UsageError(format('field "%s" must be ' +
|
|
|
|
'of type "%s", but got a value of type "%s"', key,
|
|
|
|
type, typeof (value))));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
},
|
|
|
|
|
2016-02-05 14:54:13 +02:00
|
|
|
function updateAway(ctx, next) {
|
|
|
|
var data = ctx.data;
|
2016-02-16 15:13:29 +02:00
|
|
|
data.id = id;
|
2016-02-04 15:39:50 +02:00
|
|
|
|
2016-02-16 15:13:29 +02:00
|
|
|
tritonapi.updateFirewallRule(data, function (err) {
|
2016-02-04 15:39:50 +02:00
|
|
|
if (err) {
|
|
|
|
next(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-05 14:54:13 +02:00
|
|
|
delete data.id;
|
2016-02-04 15:39:50 +02:00
|
|
|
console.log('Updated firewall rule %s (fields: %s)', id,
|
2016-02-05 14:54:13 +02:00
|
|
|
Object.keys(data).join(', '));
|
2016-02-04 15:39:50 +02:00
|
|
|
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
]}, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
do_update.options = [
|
|
|
|
{
|
|
|
|
names: ['help', 'h'],
|
|
|
|
type: 'bool',
|
|
|
|
help: 'Show this help.'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
names: ['file', 'f'],
|
|
|
|
type: 'string',
|
2016-02-16 15:13:29 +02:00
|
|
|
helpArg: '<json-file>',
|
2016-02-04 15:39:50 +02:00
|
|
|
help: 'A file holding a JSON file of updates, or "-" to read ' +
|
|
|
|
'JSON from stdin.'
|
|
|
|
}
|
|
|
|
];
|
|
|
|
do_update.help = [
|
|
|
|
'Update a firewall rule',
|
|
|
|
'',
|
|
|
|
'Usage:',
|
2016-03-02 02:46:06 +02:00
|
|
|
' {{name}} update <fwrule-id> [FIELD=VALUE ...]',
|
2016-02-16 15:13:29 +02:00
|
|
|
' {{name}} update -f <json-file> <fwrule-id>',
|
2016-02-04 15:39:50 +02:00
|
|
|
'',
|
|
|
|
'{{options}}',
|
|
|
|
|
|
|
|
'Updateable fields:',
|
2016-03-02 02:46:06 +02:00
|
|
|
' ' + Object.keys(UPDATE_FWRULE_FIELDS).sort().map(function (f) {
|
|
|
|
return f + ' (' + UPDATE_FWRULE_FIELDS[f] + ')';
|
2016-02-04 15:39:50 +02:00
|
|
|
}).join('\n '),
|
|
|
|
''
|
|
|
|
].join('\n');
|
|
|
|
|
2016-03-02 02:46:06 +02:00
|
|
|
do_update.completionArgtypes = ['tritonfwrule', 'tritonupdatefwrulefield'];
|
2016-02-04 15:39:50 +02:00
|
|
|
|
|
|
|
module.exports = do_update;
|