Skip to content

Commit 133cbc7

Browse files
authored
Merge pull request #8267 from shirady/nsfs-nc-remove-lodash-isundefined
NC | NSFS | Remove Condition Checks Using lodash `isUndefined`
2 parents 63dcad4 + 57ff216 commit 133cbc7

File tree

8 files changed

+47
-49
lines changed

8 files changed

+47
-49
lines changed

src/cmd/manage_nsfs.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async function fetch_bucket_data(action, user_input) {
9292
let data = {
9393
// added undefined values to keep the order the properties when printing the data object
9494
_id: undefined,
95-
name: _.isUndefined(user_input.name) ? undefined : String(user_input.name),
95+
name: user_input.name === undefined ? undefined : String(user_input.name),
9696
owner_account: undefined,
9797
system_owner: user_input.owner, // GAP - needs to be the system_owner (currently it is the account name)
9898
bucket_owner: user_input.owner,
@@ -101,9 +101,9 @@ async function fetch_bucket_data(action, user_input) {
101101
creation_date: action === ACTIONS.ADD ? new Date().toISOString() : undefined,
102102
path: user_input.path,
103103
should_create_underlying_storage: action === ACTIONS.ADD ? false : undefined,
104-
new_name: _.isUndefined(user_input.new_name) ? undefined : String(user_input.new_name),
105-
fs_backend: _.isUndefined(user_input.fs_backend) ? config.NSFS_NC_STORAGE_BACKEND : String(user_input.fs_backend),
106-
force_md5_etag: _.isUndefined(user_input.force_md5_etag) || user_input.force_md5_etag === '' ? user_input.force_md5_etag : get_boolean_or_string_value(user_input.force_md5_etag)
104+
new_name: user_input.new_name === undefined ? undefined : String(user_input.new_name),
105+
fs_backend: user_input.fs_backend === undefined ? config.NSFS_NC_STORAGE_BACKEND : String(user_input.fs_backend),
106+
force_md5_etag: user_input.force_md5_etag === undefined || user_input.force_md5_etag === '' ? user_input.force_md5_etag : get_boolean_or_string_value(user_input.force_md5_etag)
107107
};
108108

109109
if (user_input.bucket_policy !== undefined) {
@@ -297,14 +297,14 @@ async function fetch_account_data(action, user_input) {
297297
let data = {
298298
// added undefined values to keep the order the properties when printing the data object
299299
_id: undefined,
300-
name: _.isUndefined(user_input.name) ? undefined : String(user_input.name),
301-
email: _.isUndefined(user_input.name) ? undefined : String(user_input.name), // temp, keep the email internally
300+
name: user_input.name === undefined ? undefined : String(user_input.name),
301+
email: user_input.name === undefined ? undefined : String(user_input.name), // temp, keep the email internally
302302
creation_date: action === ACTIONS.ADD ? new Date().toISOString() : undefined,
303-
new_name: _.isUndefined(user_input.new_name) ? undefined : String(user_input.new_name),
303+
new_name: user_input.new_name === undefined ? undefined : String(user_input.new_name),
304304
new_access_key,
305305
access_keys,
306-
force_md5_etag: _.isUndefined(user_input.force_md5_etag) || user_input.force_md5_etag === '' ? user_input.force_md5_etag : get_boolean_or_string_value(user_input.force_md5_etag),
307-
iam_operate_on_root_account: _.isUndefined(user_input.iam_operate_on_root_account) ?
306+
force_md5_etag: user_input.force_md5_etag === undefined || user_input.force_md5_etag === '' ? user_input.force_md5_etag : get_boolean_or_string_value(user_input.force_md5_etag),
307+
iam_operate_on_root_account: user_input.iam_operate_on_root_account === undefined ?
308308
undefined : get_boolean_or_string_value(user_input.iam_operate_on_root_account),
309309
nsfs_account_config: {
310310
distinguished_name: user_input.user,
@@ -324,10 +324,10 @@ async function fetch_account_data(action, user_input) {
324324
// override values
325325
if (has_access_keys(data.access_keys)) {
326326
// access_key as SensitiveString
327-
data.access_keys[0].access_key = _.isUndefined(data.access_keys[0].access_key) ? undefined :
327+
data.access_keys[0].access_key = data.access_keys[0].access_key === undefined ? undefined :
328328
new SensitiveString(String(data.access_keys[0].access_key));
329329
// secret_key as SensitiveString
330-
data.access_keys[0].secret_key = _.isUndefined(data.access_keys[0].secret_key) ? undefined :
330+
data.access_keys[0].secret_key = data.access_keys[0].secret_key === undefined ? undefined :
331331
new SensitiveString(String(data.access_keys[0].secret_key));
332332
}
333333
if (data.new_access_key) data.new_access_key = new SensitiveString(data.new_access_key);
@@ -337,8 +337,8 @@ async function fetch_account_data(action, user_input) {
337337
data.nsfs_account_config.new_buckets_path = data.nsfs_account_config.new_buckets_path || undefined;
338338
// force_md5_etag deletion specified with empty string '' checked against user_input.force_md5_etag because data.force_md5_etag is boolean
339339
data.force_md5_etag = data.force_md5_etag === '' ? undefined : data.force_md5_etag;
340-
if (_.isUndefined(user_input.allow_bucket_creation)) {
341-
data.allow_bucket_creation = !_.isUndefined(data.nsfs_account_config.new_buckets_path);
340+
if (user_input.allow_bucket_creation === undefined) {
341+
data.allow_bucket_creation = data.nsfs_account_config.new_buckets_path !== undefined;
342342
} else if (typeof user_input.allow_bucket_creation === 'boolean') {
343343
data.allow_bucket_creation = Boolean(user_input.allow_bucket_creation);
344344
} else { // string of true or false
@@ -358,7 +358,7 @@ async function fetch_existing_account_data(action, target, decrypt_secret_key) {
358358
} catch (err) {
359359
dbg.log1('NSFS Manage command: Could not find account', target, err);
360360
if (err.code === 'ENOENT') {
361-
if (_.isUndefined(target.name)) {
361+
if (target.name === undefined) {
362362
throw_cli_error(ManageCLIError.NoSuchAccountAccessKey, target.access_keys[0].access_key);
363363
} else {
364364
throw_cli_error(ManageCLIError.NoSuchAccountName, target.name);
@@ -368,9 +368,9 @@ async function fetch_existing_account_data(action, target, decrypt_secret_key) {
368368
}
369369
const data = _.merge({}, source, target);
370370
if (action === ACTIONS.UPDATE) {
371-
const uid_update = !_.isUndefined(target.nsfs_account_config.uid);
372-
const gid_update = !_.isUndefined(target.nsfs_account_config.gid);
373-
const dn_update = !_.isUndefined(target.nsfs_account_config.distinguished_name);
371+
const uid_update = target.nsfs_account_config.uid !== undefined;
372+
const gid_update = target.nsfs_account_config.gid !== undefined;
373+
const dn_update = target.nsfs_account_config.distinguished_name !== undefined;
374374
const user_fs_permissions_change = uid_update || gid_update || dn_update;
375375
if (user_fs_permissions_change) {
376376
if (dn_update) {
@@ -486,13 +486,13 @@ async function get_account_status(data, show_secrets) {
486486
const options = { show_secrets, decrypt_secret_key: show_secrets };
487487

488488
try {
489-
const config_data = _.isUndefined(data.name) ?
489+
const config_data = data.name === undefined ?
490490
await config_fs.get_account_by_access_key(data.access_keys[0].access_key, options) :
491491
await config_fs.get_account_by_name(data.name, options);
492492
write_stdout_response(ManageCLIResponse.AccountStatus, config_data);
493493
} catch (err) {
494494
if (err.code !== 'ENOENT') throw err;
495-
if (_.isUndefined(data.name)) {
495+
if (data.name === undefined) {
496496
throw_cli_error(ManageCLIError.NoSuchAccountAccessKey, data.access_keys[0].access_key.unwrap());
497497
} else {
498498
throw_cli_error(ManageCLIError.NoSuchAccountName, data.name);

src/endpoint/iam/iam_utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function format_iam_xml_date(input) {
2727
*/
2828
function create_arn(account_id, username, iam_path) {
2929
const basic_structure = `arn:aws:iam::${account_id}:user`;
30-
if (_.isUndefined(username)) return `${basic_structure}/`;
30+
if (username === undefined) return `${basic_structure}/`;
3131
if (check_iam_path_was_set(iam_path)) {
3232
return `${basic_structure}${iam_path}${username}`;
3333
}
@@ -342,7 +342,7 @@ function validate_list_access_keys(params) {
342342
* @param {string} parameter_name
343343
*/
344344
function validate_iam_path(input_path, parameter_name = iam_constants.IAM_PARAMETER_NAME.IAM_PATH) {
345-
if (_.isUndefined(input_path)) return;
345+
if (input_path === undefined) return;
346346
// type check
347347
_type_check_input('string', input_path, parameter_name);
348348
// length check
@@ -370,7 +370,7 @@ function validate_iam_path(input_path, parameter_name = iam_constants.IAM_PARAME
370370
* @param {string} parameter_name
371371
*/
372372
function validate_username(input_username, parameter_name = iam_constants.IAM_PARAMETER_NAME.USERNAME) {
373-
if (_.isUndefined(input_username)) return;
373+
if (input_username === undefined) return;
374374
// type check
375375
_type_check_input('string', input_username, parameter_name);
376376
// length check
@@ -411,7 +411,7 @@ function validate_username(input_username, parameter_name = iam_constants.IAM_PA
411411
*/
412412
function validate_marker(input_marker) {
413413
const parameter_name = 'Marker';
414-
if (_.isUndefined(input_marker)) return;
414+
if (input_marker === undefined) return;
415415
// type check
416416
_type_check_input('string', input_marker, parameter_name);
417417
// length check
@@ -436,7 +436,7 @@ function validate_marker(input_marker) {
436436
*/
437437
function validate_max_items(input_max_items) {
438438
const parameter_name = 'MaxItems';
439-
if (_.isUndefined(input_max_items)) return;
439+
if (input_max_items === undefined) return;
440440
// type check
441441
_type_check_input('number', input_max_items, parameter_name);
442442
// value check
@@ -468,7 +468,7 @@ function validate_max_items(input_max_items) {
468468
*/
469469
function validate_access_key_id(input_access_key_id) {
470470
const parameter_name = 'AccessKeyId';
471-
if (_.isUndefined(input_access_key_id)) return;
471+
if (input_access_key_id === undefined) return;
472472
// type check
473473
_type_check_input('string', input_access_key_id, parameter_name);
474474
// length check
@@ -493,7 +493,7 @@ function validate_access_key_id(input_access_key_id) {
493493
*/
494494
function validate_status(input_status) {
495495
const parameter_name = 'Status';
496-
if (_.isUndefined(input_status)) return;
496+
if (input_status === undefined) return;
497497
if (input_status !== iam_constants.ACCESS_KEY_STATUS_ENUM.ACTIVE &&
498498
input_status !== iam_constants.ACCESS_KEY_STATUS_ENUM.INACTIVE) {
499499
const message_with_details = `Value ${input_status} at '${parameter_name}' ` +

src/manage_nsfs/manage_nsfs_cli_utils.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
'use strict';
33

44
const dbg = require('../util/debug_module')(__filename);
5-
const _ = require('lodash');
65
const nb_native = require('../util/nb_native');
76
const native_fs_utils = require('../util/native_fs_utils');
87
const ManageCLIError = require('../manage_nsfs/manage_nsfs_cli_errors').ManageCLIError;
@@ -61,7 +60,7 @@ async function get_bucket_owner_account(config_fs, bucket_owner) {
6160
* @param {boolean|string} value
6261
*/
6362
function get_boolean_or_string_value(value) {
64-
if (_.isUndefined(value)) {
63+
if (value === undefined) {
6564
return false;
6665
} else if (typeof value === 'string' && BOOLEAN_STRING_VALUES.includes(value.toLowerCase())) {
6766
return value.toLowerCase() === 'true';

src/manage_nsfs/manage_nsfs_validations.js

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
const config = require('../../config');
55
const dbg = require('../util/debug_module')(__filename);
6-
const _ = require('lodash');
76
const path = require('path');
87
const net = require('net');
98
const P = require('../util/promise');
@@ -92,7 +91,7 @@ function validate_type_and_action(type, action) {
9291
*/
9392
function validate_identifier(type, action, input_options, is_options_from_file) {
9493
// do not check identifier in the command of from_file (only in the file itself).
95-
if (!_.isUndefined(input_options.from_file) && !is_options_from_file) return;
94+
if (input_options.from_file !== undefined && !is_options_from_file) return;
9695

9796
if (type === TYPES.ACCOUNT) {
9897
validate_account_identifier(action, input_options);
@@ -206,7 +205,7 @@ function validate_min_flags_for_update(type, input_options_with_data) {
206205
const config_and_identifier_options = ['config_root', 'config_root_backend', 'name'];
207206

208207
// GAP - mandatory flags check should be earlier in the calls in general
209-
if (_.isUndefined(input_options_with_data.name)) {
208+
if (input_options_with_data.name === undefined) {
210209
if (type === TYPES.ACCOUNT && !input_options_with_data.anonymous) throw_cli_error(ManageCLIError.MissingAccountNameFlag);
211210
if (type === TYPES.BUCKET) throw_cli_error(ManageCLIError.MissingBucketNameFlag);
212211
}
@@ -305,7 +304,7 @@ function validate_account_name(type, action, input_options_with_data) {
305304
*/
306305
function validate_bucket_identifier(action, input_options) {
307306
if (action === ACTIONS.STATUS || action === ACTIONS.ADD || action === ACTIONS.UPDATE || action === ACTIONS.DELETE) {
308-
if (_.isUndefined(input_options.name)) throw_cli_error(ManageCLIError.MissingBucketNameFlag);
307+
if (input_options.name === undefined) throw_cli_error(ManageCLIError.MissingBucketNameFlag);
309308
}
310309
// in list there is no identifier
311310
}
@@ -319,8 +318,8 @@ if (action === ACTIONS.STATUS || action === ACTIONS.ADD || action === ACTIONS.UP
319318
async function validate_bucket_args(config_fs, data, action) {
320319
if (action === ACTIONS.ADD || action === ACTIONS.UPDATE) {
321320
if (action === ACTIONS.ADD) native_fs_utils.validate_bucket_creation({ name: data.name });
322-
if (action === ACTIONS.UPDATE && !_.isUndefined(data.new_name)) native_fs_utils.validate_bucket_creation({ name: data.new_name });
323-
if (action === ACTIONS.ADD && _.isUndefined(data.bucket_owner)) throw_cli_error(ManageCLIError.MissingBucketOwnerFlag);
321+
if ((action === ACTIONS.UPDATE) && (data.new_name !== undefined)) native_fs_utils.validate_bucket_creation({ name: data.new_name });
322+
if ((action === ACTIONS.ADD) && (data.bucket_owner === undefined)) throw_cli_error(ManageCLIError.MissingBucketOwnerFlag);
324323
if (!data.path) throw_cli_error(ManageCLIError.MissingBucketPathFlag);
325324
// fs_backend='' used for deletion of the fs_backend property
326325
if (data.fs_backend !== undefined && !['GPFS', 'CEPH_FS', 'NFSv4'].includes(data.fs_backend)) {
@@ -378,12 +377,12 @@ function validate_account_identifier(action, input_options) {
378377
if (get_boolean_or_string_value(input_options[ANONYMOUS])) return;
379378
if (action === ACTIONS.STATUS) {
380379
// in status we allow identifier as name or access_key
381-
if (_.isUndefined(input_options.access_key) && _.isUndefined(input_options.name)) {
380+
if ((input_options.access_key === undefined) && (input_options.name === undefined)) {
382381
throw_cli_error(ManageCLIError.MissingIdentifier);
383382
}
384383
} else if (action === ACTIONS.ADD || action === ACTIONS.UPDATE || action === ACTIONS.DELETE) {
385384
// in add, update and delete only name is an identifier
386-
if (_.isUndefined(input_options.name)) throw_cli_error(ManageCLIError.MissingAccountNameFlag);
385+
if (input_options.name === undefined) throw_cli_error(ManageCLIError.MissingAccountNameFlag);
387386
}
388387
// in list there is no identifier
389388
}
@@ -403,15 +402,15 @@ async function validate_account_args(config_fs, data, action, is_flag_iam_operat
403402
if (data.nsfs_account_config.uid && data.nsfs_account_config.gid === undefined) {
404403
throw_cli_error(ManageCLIError.MissingAccountNSFSConfigGID, data.nsfs_account_config);
405404
}
406-
if ((_.isUndefined(data.nsfs_account_config.distinguished_name) &&
407-
(data.nsfs_account_config.uid === undefined || data.nsfs_account_config.gid === undefined))) {
405+
if ((data.nsfs_account_config.distinguished_name === undefined) &&
406+
((data.nsfs_account_config.uid === undefined) || (data.nsfs_account_config.gid === undefined))) {
408407
throw_cli_error(ManageCLIError.InvalidAccountNSFSConfig, data.nsfs_account_config);
409408
}
410-
if (!_.isUndefined(data.nsfs_account_config.fs_backend) && !['GPFS', 'CEPH_FS', 'NFSv4'].includes(data.nsfs_account_config.fs_backend)) {
409+
if ((data.nsfs_account_config.fs_backend !== undefined) && !['GPFS', 'CEPH_FS', 'NFSv4'].includes(data.nsfs_account_config.fs_backend)) {
411410
throw_cli_error(ManageCLIError.InvalidFSBackend);
412411
}
413412

414-
if (_.isUndefined(data.nsfs_account_config.new_buckets_path)) {
413+
if (data.nsfs_account_config.new_buckets_path === undefined) {
415414
return;
416415
}
417416
// in case we have the fs_backend it changes the fs_context that we use for the new_buckets_path
@@ -459,10 +458,10 @@ async function validate_account_resources_before_deletion(config_fs, data) {
459458
*/
460459
function _validate_access_keys(access_key, secret_key) {
461460
// using the access_key flag requires also using the secret_key flag
462-
if (!_.isUndefined(access_key) && _.isUndefined(secret_key)) {
461+
if ((access_key !== undefined) && (secret_key === undefined)) {
463462
throw_cli_error(ManageCLIError.MissingAccountSecretKeyFlag);
464463
}
465-
if (!_.isUndefined(secret_key) && _.isUndefined(access_key)) {
464+
if ((secret_key !== undefined) && (access_key === undefined)) {
466465
throw_cli_error(ManageCLIError.MissingAccountAccessKeyFlag);
467466
}
468467

src/sdk/accountspace_fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ class AccountSpaceFS {
146146
{ show_secrets: true, decrypt_secret_key: true });
147147
this._check_if_requested_account_is_root_account_or_IAM_user(action, requesting_account, requested_account);
148148
this._check_if_requested_is_owned_by_root_account(action, requesting_account, requested_account);
149-
const is_username_update = !_.isUndefined(params.new_username) &&
149+
const is_username_update = params.new_username !== undefined &&
150150
params.new_username !== params.username;
151-
if (!_.isUndefined(params.new_iam_path)) requested_account.iam_path = params.new_iam_path;
151+
if (params.new_iam_path !== undefined) requested_account.iam_path = params.new_iam_path;
152152
if (is_username_update) {
153153
dbg.log1(`AccountSpaceFS.${action} username was updated, is_username_update`,
154154
is_username_update);

src/sdk/bucketspace_fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -719,8 +719,8 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
719719
const account = object_sdk.requesting_account;
720720
dbg.log1('_has_access_to_nsfs_dir: nsr: ', ns, 'account.nsfs_account_config: ', account && account.nsfs_account_config);
721721
// nsfs bucket
722-
if (!account || !account.nsfs_account_config || _.isUndefined(account.nsfs_account_config.uid) ||
723-
_.isUndefined(account.nsfs_account_config.gid)) return false;
722+
if (!account || !account.nsfs_account_config || (account.nsfs_account_config.uid === undefined) ||
723+
(account.nsfs_account_config.gid === undefined)) return false;
724724
try {
725725
dbg.log1('_has_access_to_nsfs_dir: checking access:', ns.write_resource, account.nsfs_account_config.uid, account.nsfs_account_config.gid);
726726
const path_to_check = path.join(ns.write_resource.resource.fs_root_path, ns.write_resource.path || '');

src/sdk/namespace_fs.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2574,11 +2574,11 @@ class NamespaceFS {
25742574
*/
25752575
_is_force_md5_enabled(object_sdk) {
25762576
// value defined for bucket
2577-
if (!_.isUndefined(this.force_md5_etag)) {
2577+
if (this.force_md5_etag !== undefined) {
25782578
return this.force_md5_etag;
25792579
}
25802580
// value defined for account
2581-
if (!_.isUndefined(object_sdk?.requesting_account?.force_md5_etag)) {
2581+
if (object_sdk?.requesting_account?.force_md5_etag !== undefined) {
25822582
return object_sdk?.requesting_account?.force_md5_etag;
25832583
}
25842584
// otherwise return global default

src/test/unit_tests/test_nc_nsfs_cli.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ mocha.describe('manage_nsfs cli', function() {
185185
assert_response(action, type, add_res, bucket_options);
186186
const bucket = await read_config_file(config_root, CONFIG_SUBDIRS.BUCKETS, name);
187187
assert_bucket(bucket, bucket_options);
188-
assert(!_.isUndefined(bucket._id));
188+
assert(bucket._id !== undefined);
189189
// make sure that the config file includes id and owner_account (account id)
190190
const account = await read_config_file(config_root, CONFIG_SUBDIRS.ACCOUNTS, account_name);
191191
assert(bucket.owner_account === account._id);

0 commit comments

Comments
 (0)