From 002171ea067fc76541ea5f384c8e697900e3987f Mon Sep 17 00:00:00 2001 From: Dave Eddy Date: Tue, 6 Mar 2018 15:15:06 -0500 Subject: [PATCH] TRITON-33 node-triton use common functions to cut down on code duplication Reviewed by: Trent Mick Approved by: Trent Mick --- lib/do_account/do_update.js | 38 ++++++++----------------------------- lib/do_fwrule/do_update.js | 37 +++++------------------------------- lib/do_key/do_add.js | 8 +------- lib/do_profile/do_create.js | 8 ++------ lib/do_rbac/do_key.js | 8 ++------ lib/do_rbac/do_policy.js | 8 ++------ lib/do_rbac/do_role.js | 8 ++------ lib/do_rbac/do_user.js | 8 ++------ 8 files changed, 24 insertions(+), 99 deletions(-) diff --git a/lib/do_account/do_update.js b/lib/do_account/do_update.js index 876debc..5e4d8c6 100644 --- a/lib/do_account/do_update.js +++ b/lib/do_account/do_update.js @@ -72,12 +72,8 @@ function do_update(subcmd, opts, args, callback) { next(); return; } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - process.stdin.on('end', function () { + + common.readStdin(function gotStdin(stdin) { try { ctx.data = JSON.parse(stdin); } catch (err) { @@ -92,36 +88,18 @@ function do_update(subcmd, opts, args, callback) { }, function validateIt(ctx, next) { - var keys = Object.keys(ctx.data); - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; - var value = ctx.data[key]; - var type = UPDATE_ACCOUNT_FIELDS[key]; - if (!type) { - next(new errors.UsageError(format('unknown or ' + - 'unupdateable field: %s (updateable fields are: %s)', - key, - Object.keys(UPDATE_ACCOUNT_FIELDS).sort().join(', ')))); - 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; - } + try { + common.validateObject(ctx.data, UPDATE_ACCOUNT_FIELDS); + } catch (e) { + next(e); + return; } + next(); }, function updateAway(ctx, next) { var keys = Object.keys(ctx.data); - if (keys.length === 0) { - console.log('No fields given for account update'); - next(); - return; - } tritonapi.cloudapi.updateAccount(ctx.data, function (err) { if (err) { diff --git a/lib/do_fwrule/do_update.js b/lib/do_fwrule/do_update.js index 9bad3e4..99f1304 100644 --- a/lib/do_fwrule/do_update.js +++ b/lib/do_fwrule/do_update.js @@ -84,14 +84,7 @@ function do_update(subcmd, opts, args, cb) { return; } - var stdin = ''; - - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - - process.stdin.on('end', function () { + common.readStdin(function gotStdin(stdin) { try { ctx.data = JSON.parse(stdin); } catch (err) { @@ -107,33 +100,13 @@ function do_update(subcmd, opts, args, cb) { }, function validateIt(ctx, next) { - var keys = Object.keys(ctx.data); - - if (keys.length === 0) { - console.log('No fields given for firewall rule update'); - next(); + try { + common.validateObject(ctx.data, UPDATE_FWRULE_FIELDS); + } catch (e) { + next(e); return; } - for (var i = 0; i < keys.length; i++) { - var key = keys[i]; - var value = ctx.data[key]; - var type = UPDATE_FWRULE_FIELDS[key]; - if (!type) { - next(new errors.UsageError(format('unknown or ' + - 'unupdateable field: %s (updateable fields are: %s)', - key, - Object.keys(UPDATE_FWRULE_FIELDS).sort().join(', ')))); - 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(); }, diff --git a/lib/do_key/do_add.js b/lib/do_key/do_add.js index b868b53..7f9e517 100644 --- a/lib/do_key/do_add.js +++ b/lib/do_key/do_add.js @@ -47,13 +47,7 @@ function do_add(subcmd, opts, args, cb) { return next(); } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - - process.stdin.on('end', function () { + common.readStdin(function gotStdin(stdin) { ctx.data = stdin; ctx.from = ''; next(); diff --git a/lib/do_profile/do_create.js b/lib/do_profile/do_create.js index 5a171ed..c97d403 100644 --- a/lib/do_profile/do_create.js +++ b/lib/do_profile/do_create.js @@ -93,12 +93,8 @@ function _createProfile(opts, cb) { next(); return; } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - process.stdin.on('end', function () { + + common.readStdin(function gotStdin(stdin) { try { data = JSON.parse(stdin); } catch (err) { diff --git a/lib/do_rbac/do_key.js b/lib/do_rbac/do_key.js index dbff62c..f9b658c 100644 --- a/lib/do_rbac/do_key.js +++ b/lib/do_rbac/do_key.js @@ -125,12 +125,8 @@ function _addUserKey(opts, cb) { if (opts.file !== '-') { return next(); } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - process.stdin.on('end', function () { + + common.readStdin(function gotStdin(stdin) { ctx.data = stdin; ctx.from = ''; next(); diff --git a/lib/do_rbac/do_policy.js b/lib/do_rbac/do_policy.js index 3e6b278..d1472a7 100644 --- a/lib/do_rbac/do_policy.js +++ b/lib/do_rbac/do_policy.js @@ -291,12 +291,8 @@ function _addPolicy(opts, cb) { if (opts.file !== '-') { return next(); } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - process.stdin.on('end', function () { + + common.readStdin(function gotStdin(stdin) { try { data = JSON.parse(stdin); } catch (err) { diff --git a/lib/do_rbac/do_role.js b/lib/do_rbac/do_role.js index 4fb56a7..7b8854e 100644 --- a/lib/do_rbac/do_role.js +++ b/lib/do_rbac/do_role.js @@ -287,12 +287,8 @@ function _addRole(opts, cb) { if (opts.file !== '-') { return next(); } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - process.stdin.on('end', function () { + + common.readStdin(function gotStdin(stdin) { try { data = JSON.parse(stdin); } catch (err) { diff --git a/lib/do_rbac/do_user.js b/lib/do_rbac/do_user.js index b3a755a..8dd8a40 100644 --- a/lib/do_rbac/do_user.js +++ b/lib/do_rbac/do_user.js @@ -282,12 +282,8 @@ function _addUser(opts, cb) { if (opts.file !== '-') { return next(); } - var stdin = ''; - process.stdin.resume(); - process.stdin.on('data', function (chunk) { - stdin += chunk; - }); - process.stdin.on('end', function () { + + common.readStdin(function gotStdin(stdin) { try { data = JSON.parse(stdin); } catch (err) {