Skip to content

NC | NSFS | Remove Condition Checks Using lodash isUndefined #8267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions src/cmd/manage_nsfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ async function fetch_bucket_data(action, user_input) {
let data = {
// added undefined values to keep the order the properties when printing the data object
_id: undefined,
name: _.isUndefined(user_input.name) ? undefined : String(user_input.name),
name: user_input.name === undefined ? undefined : String(user_input.name),
owner_account: undefined,
system_owner: user_input.owner, // GAP - needs to be the system_owner (currently it is the account name)
bucket_owner: user_input.owner,
Expand All @@ -101,9 +101,9 @@ async function fetch_bucket_data(action, user_input) {
creation_date: action === ACTIONS.ADD ? new Date().toISOString() : undefined,
path: user_input.path,
should_create_underlying_storage: action === ACTIONS.ADD ? false : undefined,
new_name: _.isUndefined(user_input.new_name) ? undefined : String(user_input.new_name),
fs_backend: _.isUndefined(user_input.fs_backend) ? config.NSFS_NC_STORAGE_BACKEND : String(user_input.fs_backend),
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)
new_name: user_input.new_name === undefined ? undefined : String(user_input.new_name),
fs_backend: user_input.fs_backend === undefined ? config.NSFS_NC_STORAGE_BACKEND : String(user_input.fs_backend),
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)
};

if (user_input.bucket_policy !== undefined) {
Expand Down Expand Up @@ -297,14 +297,14 @@ async function fetch_account_data(action, user_input) {
let data = {
// added undefined values to keep the order the properties when printing the data object
_id: undefined,
name: _.isUndefined(user_input.name) ? undefined : String(user_input.name),
email: _.isUndefined(user_input.name) ? undefined : String(user_input.name), // temp, keep the email internally
name: user_input.name === undefined ? undefined : String(user_input.name),
email: user_input.name === undefined ? undefined : String(user_input.name), // temp, keep the email internally
creation_date: action === ACTIONS.ADD ? new Date().toISOString() : undefined,
new_name: _.isUndefined(user_input.new_name) ? undefined : String(user_input.new_name),
new_name: user_input.new_name === undefined ? undefined : String(user_input.new_name),
new_access_key,
access_keys,
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),
iam_operate_on_root_account: _.isUndefined(user_input.iam_operate_on_root_account) ?
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),
iam_operate_on_root_account: user_input.iam_operate_on_root_account === undefined ?
undefined : get_boolean_or_string_value(user_input.iam_operate_on_root_account),
nsfs_account_config: {
distinguished_name: user_input.user,
Expand All @@ -324,10 +324,10 @@ async function fetch_account_data(action, user_input) {
// override values
if (has_access_keys(data.access_keys)) {
// access_key as SensitiveString
data.access_keys[0].access_key = _.isUndefined(data.access_keys[0].access_key) ? undefined :
data.access_keys[0].access_key = data.access_keys[0].access_key === undefined ? undefined :
new SensitiveString(String(data.access_keys[0].access_key));
// secret_key as SensitiveString
data.access_keys[0].secret_key = _.isUndefined(data.access_keys[0].secret_key) ? undefined :
data.access_keys[0].secret_key = data.access_keys[0].secret_key === undefined ? undefined :
new SensitiveString(String(data.access_keys[0].secret_key));
}
if (data.new_access_key) data.new_access_key = new SensitiveString(data.new_access_key);
Expand All @@ -337,8 +337,8 @@ async function fetch_account_data(action, user_input) {
data.nsfs_account_config.new_buckets_path = data.nsfs_account_config.new_buckets_path || undefined;
// force_md5_etag deletion specified with empty string '' checked against user_input.force_md5_etag because data.force_md5_etag is boolean
data.force_md5_etag = data.force_md5_etag === '' ? undefined : data.force_md5_etag;
if (_.isUndefined(user_input.allow_bucket_creation)) {
data.allow_bucket_creation = !_.isUndefined(data.nsfs_account_config.new_buckets_path);
if (user_input.allow_bucket_creation === undefined) {
data.allow_bucket_creation = data.nsfs_account_config.new_buckets_path !== undefined;
} else if (typeof user_input.allow_bucket_creation === 'boolean') {
data.allow_bucket_creation = Boolean(user_input.allow_bucket_creation);
} else { // string of true or false
Expand All @@ -358,7 +358,7 @@ async function fetch_existing_account_data(action, target, decrypt_secret_key) {
} catch (err) {
dbg.log1('NSFS Manage command: Could not find account', target, err);
if (err.code === 'ENOENT') {
if (_.isUndefined(target.name)) {
if (target.name === undefined) {
throw_cli_error(ManageCLIError.NoSuchAccountAccessKey, target.access_keys[0].access_key);
} else {
throw_cli_error(ManageCLIError.NoSuchAccountName, target.name);
Expand All @@ -368,9 +368,9 @@ async function fetch_existing_account_data(action, target, decrypt_secret_key) {
}
const data = _.merge({}, source, target);
if (action === ACTIONS.UPDATE) {
const uid_update = !_.isUndefined(target.nsfs_account_config.uid);
const gid_update = !_.isUndefined(target.nsfs_account_config.gid);
const dn_update = !_.isUndefined(target.nsfs_account_config.distinguished_name);
const uid_update = target.nsfs_account_config.uid !== undefined;
const gid_update = target.nsfs_account_config.gid !== undefined;
const dn_update = target.nsfs_account_config.distinguished_name !== undefined;
const user_fs_permissions_change = uid_update || gid_update || dn_update;
if (user_fs_permissions_change) {
if (dn_update) {
Expand Down Expand Up @@ -486,13 +486,13 @@ async function get_account_status(data, show_secrets) {
const options = { show_secrets, decrypt_secret_key: show_secrets };

try {
const config_data = _.isUndefined(data.name) ?
const config_data = data.name === undefined ?
await config_fs.get_account_by_access_key(data.access_keys[0].access_key, options) :
await config_fs.get_account_by_name(data.name, options);
write_stdout_response(ManageCLIResponse.AccountStatus, config_data);
} catch (err) {
if (err.code !== 'ENOENT') throw err;
if (_.isUndefined(data.name)) {
if (data.name === undefined) {
throw_cli_error(ManageCLIError.NoSuchAccountAccessKey, data.access_keys[0].access_key.unwrap());
} else {
throw_cli_error(ManageCLIError.NoSuchAccountName, data.name);
Expand Down
14 changes: 7 additions & 7 deletions src/endpoint/iam/iam_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function format_iam_xml_date(input) {
*/
function create_arn(account_id, username, iam_path) {
const basic_structure = `arn:aws:iam::${account_id}:user`;
if (_.isUndefined(username)) return `${basic_structure}/`;
if (username === undefined) return `${basic_structure}/`;
if (check_iam_path_was_set(iam_path)) {
return `${basic_structure}${iam_path}${username}`;
}
Expand Down Expand Up @@ -342,7 +342,7 @@ function validate_list_access_keys(params) {
* @param {string} parameter_name
*/
function validate_iam_path(input_path, parameter_name = iam_constants.IAM_PARAMETER_NAME.IAM_PATH) {
if (_.isUndefined(input_path)) return;
if (input_path === undefined) return;
// type check
_type_check_input('string', input_path, parameter_name);
// length check
Expand Down Expand Up @@ -370,7 +370,7 @@ function validate_iam_path(input_path, parameter_name = iam_constants.IAM_PARAME
* @param {string} parameter_name
*/
function validate_username(input_username, parameter_name = iam_constants.IAM_PARAMETER_NAME.USERNAME) {
if (_.isUndefined(input_username)) return;
if (input_username === undefined) return;
// type check
_type_check_input('string', input_username, parameter_name);
// length check
Expand Down Expand Up @@ -411,7 +411,7 @@ function validate_username(input_username, parameter_name = iam_constants.IAM_PA
*/
function validate_marker(input_marker) {
const parameter_name = 'Marker';
if (_.isUndefined(input_marker)) return;
if (input_marker === undefined) return;
// type check
_type_check_input('string', input_marker, parameter_name);
// length check
Expand All @@ -436,7 +436,7 @@ function validate_marker(input_marker) {
*/
function validate_max_items(input_max_items) {
const parameter_name = 'MaxItems';
if (_.isUndefined(input_max_items)) return;
if (input_max_items === undefined) return;
// type check
_type_check_input('number', input_max_items, parameter_name);
// value check
Expand Down Expand Up @@ -468,7 +468,7 @@ function validate_max_items(input_max_items) {
*/
function validate_access_key_id(input_access_key_id) {
const parameter_name = 'AccessKeyId';
if (_.isUndefined(input_access_key_id)) return;
if (input_access_key_id === undefined) return;
// type check
_type_check_input('string', input_access_key_id, parameter_name);
// length check
Expand All @@ -493,7 +493,7 @@ function validate_access_key_id(input_access_key_id) {
*/
function validate_status(input_status) {
const parameter_name = 'Status';
if (_.isUndefined(input_status)) return;
if (input_status === undefined) return;
if (input_status !== iam_constants.ACCESS_KEY_STATUS_ENUM.ACTIVE &&
input_status !== iam_constants.ACCESS_KEY_STATUS_ENUM.INACTIVE) {
const message_with_details = `Value ${input_status} at '${parameter_name}' ` +
Expand Down
3 changes: 1 addition & 2 deletions src/manage_nsfs/manage_nsfs_cli_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
'use strict';

const dbg = require('../util/debug_module')(__filename);
const _ = require('lodash');
const nb_native = require('../util/nb_native');
const native_fs_utils = require('../util/native_fs_utils');
const ManageCLIError = require('../manage_nsfs/manage_nsfs_cli_errors').ManageCLIError;
Expand Down Expand Up @@ -61,7 +60,7 @@ async function get_bucket_owner_account(config_fs, bucket_owner) {
* @param {boolean|string} value
*/
function get_boolean_or_string_value(value) {
if (_.isUndefined(value)) {
if (value === undefined) {
return false;
} else if (typeof value === 'string' && BOOLEAN_STRING_VALUES.includes(value.toLowerCase())) {
return value.toLowerCase() === 'true';
Expand Down
27 changes: 13 additions & 14 deletions src/manage_nsfs/manage_nsfs_validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

const config = require('../../config');
const dbg = require('../util/debug_module')(__filename);
const _ = require('lodash');
const path = require('path');
const net = require('net');
const P = require('../util/promise');
Expand Down Expand Up @@ -92,7 +91,7 @@ function validate_type_and_action(type, action) {
*/
function validate_identifier(type, action, input_options, is_options_from_file) {
// do not check identifier in the command of from_file (only in the file itself).
if (!_.isUndefined(input_options.from_file) && !is_options_from_file) return;
if (input_options.from_file !== undefined && !is_options_from_file) return;

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

// GAP - mandatory flags check should be earlier in the calls in general
if (_.isUndefined(input_options_with_data.name)) {
if (input_options_with_data.name === undefined) {
if (type === TYPES.ACCOUNT && !input_options_with_data.anonymous) throw_cli_error(ManageCLIError.MissingAccountNameFlag);
if (type === TYPES.BUCKET) throw_cli_error(ManageCLIError.MissingBucketNameFlag);
}
Expand Down Expand Up @@ -305,7 +304,7 @@ function validate_account_name(type, action, input_options_with_data) {
*/
function validate_bucket_identifier(action, input_options) {
if (action === ACTIONS.STATUS || action === ACTIONS.ADD || action === ACTIONS.UPDATE || action === ACTIONS.DELETE) {
if (_.isUndefined(input_options.name)) throw_cli_error(ManageCLIError.MissingBucketNameFlag);
if (input_options.name === undefined) throw_cli_error(ManageCLIError.MissingBucketNameFlag);
}
// in list there is no identifier
}
Expand All @@ -319,8 +318,8 @@ if (action === ACTIONS.STATUS || action === ACTIONS.ADD || action === ACTIONS.UP
async function validate_bucket_args(config_fs, data, action) {
if (action === ACTIONS.ADD || action === ACTIONS.UPDATE) {
if (action === ACTIONS.ADD) native_fs_utils.validate_bucket_creation({ name: data.name });
if (action === ACTIONS.UPDATE && !_.isUndefined(data.new_name)) native_fs_utils.validate_bucket_creation({ name: data.new_name });
if (action === ACTIONS.ADD && _.isUndefined(data.bucket_owner)) throw_cli_error(ManageCLIError.MissingBucketOwnerFlag);
if ((action === ACTIONS.UPDATE) && (data.new_name !== undefined)) native_fs_utils.validate_bucket_creation({ name: data.new_name });
if ((action === ACTIONS.ADD) && (data.bucket_owner === undefined)) throw_cli_error(ManageCLIError.MissingBucketOwnerFlag);
if (!data.path) throw_cli_error(ManageCLIError.MissingBucketPathFlag);
// fs_backend='' used for deletion of the fs_backend property
if (data.fs_backend !== undefined && !['GPFS', 'CEPH_FS', 'NFSv4'].includes(data.fs_backend)) {
Expand Down Expand Up @@ -378,12 +377,12 @@ function validate_account_identifier(action, input_options) {
if (get_boolean_or_string_value(input_options[ANONYMOUS])) return;
if (action === ACTIONS.STATUS) {
// in status we allow identifier as name or access_key
if (_.isUndefined(input_options.access_key) && _.isUndefined(input_options.name)) {
if ((input_options.access_key === undefined) && (input_options.name === undefined)) {
throw_cli_error(ManageCLIError.MissingIdentifier);
}
} else if (action === ACTIONS.ADD || action === ACTIONS.UPDATE || action === ACTIONS.DELETE) {
// in add, update and delete only name is an identifier
if (_.isUndefined(input_options.name)) throw_cli_error(ManageCLIError.MissingAccountNameFlag);
if (input_options.name === undefined) throw_cli_error(ManageCLIError.MissingAccountNameFlag);
}
// in list there is no identifier
}
Expand All @@ -403,15 +402,15 @@ async function validate_account_args(config_fs, data, action, is_flag_iam_operat
if (data.nsfs_account_config.uid && data.nsfs_account_config.gid === undefined) {
throw_cli_error(ManageCLIError.MissingAccountNSFSConfigGID, data.nsfs_account_config);
}
if ((_.isUndefined(data.nsfs_account_config.distinguished_name) &&
(data.nsfs_account_config.uid === undefined || data.nsfs_account_config.gid === undefined))) {
if ((data.nsfs_account_config.distinguished_name === undefined) &&
((data.nsfs_account_config.uid === undefined) || (data.nsfs_account_config.gid === undefined))) {
throw_cli_error(ManageCLIError.InvalidAccountNSFSConfig, data.nsfs_account_config);
}
if (!_.isUndefined(data.nsfs_account_config.fs_backend) && !['GPFS', 'CEPH_FS', 'NFSv4'].includes(data.nsfs_account_config.fs_backend)) {
if ((data.nsfs_account_config.fs_backend !== undefined) && !['GPFS', 'CEPH_FS', 'NFSv4'].includes(data.nsfs_account_config.fs_backend)) {
throw_cli_error(ManageCLIError.InvalidFSBackend);
}

if (_.isUndefined(data.nsfs_account_config.new_buckets_path)) {
if (data.nsfs_account_config.new_buckets_path === undefined) {
return;
}
// in case we have the fs_backend it changes the fs_context that we use for the new_buckets_path
Expand Down Expand Up @@ -459,10 +458,10 @@ async function validate_account_resources_before_deletion(config_fs, data) {
*/
function _validate_access_keys(access_key, secret_key) {
// using the access_key flag requires also using the secret_key flag
if (!_.isUndefined(access_key) && _.isUndefined(secret_key)) {
if ((access_key !== undefined) && (secret_key === undefined)) {
throw_cli_error(ManageCLIError.MissingAccountSecretKeyFlag);
}
if (!_.isUndefined(secret_key) && _.isUndefined(access_key)) {
if ((secret_key !== undefined) && (access_key === undefined)) {
throw_cli_error(ManageCLIError.MissingAccountAccessKeyFlag);
}

Expand Down
4 changes: 2 additions & 2 deletions src/sdk/accountspace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@ class AccountSpaceFS {
{ show_secrets: true, decrypt_secret_key: true });
this._check_if_requested_account_is_root_account_or_IAM_user(action, requesting_account, requested_account);
this._check_if_requested_is_owned_by_root_account(action, requesting_account, requested_account);
const is_username_update = !_.isUndefined(params.new_username) &&
const is_username_update = params.new_username !== undefined &&
params.new_username !== params.username;
if (!_.isUndefined(params.new_iam_path)) requested_account.iam_path = params.new_iam_path;
if (params.new_iam_path !== undefined) requested_account.iam_path = params.new_iam_path;
if (is_username_update) {
dbg.log1(`AccountSpaceFS.${action} username was updated, is_username_update`,
is_username_update);
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/bucketspace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,8 @@ class BucketSpaceFS extends BucketSpaceSimpleFS {
const account = object_sdk.requesting_account;
dbg.log1('_has_access_to_nsfs_dir: nsr: ', ns, 'account.nsfs_account_config: ', account && account.nsfs_account_config);
// nsfs bucket
if (!account || !account.nsfs_account_config || _.isUndefined(account.nsfs_account_config.uid) ||
_.isUndefined(account.nsfs_account_config.gid)) return false;
if (!account || !account.nsfs_account_config || (account.nsfs_account_config.uid === undefined) ||
(account.nsfs_account_config.gid === undefined)) return false;
try {
dbg.log1('_has_access_to_nsfs_dir: checking access:', ns.write_resource, account.nsfs_account_config.uid, account.nsfs_account_config.gid);
const path_to_check = path.join(ns.write_resource.resource.fs_root_path, ns.write_resource.path || '');
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/namespace_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2574,11 +2574,11 @@ class NamespaceFS {
*/
_is_force_md5_enabled(object_sdk) {
// value defined for bucket
if (!_.isUndefined(this.force_md5_etag)) {
if (this.force_md5_etag !== undefined) {
return this.force_md5_etag;
}
// value defined for account
if (!_.isUndefined(object_sdk?.requesting_account?.force_md5_etag)) {
if (object_sdk?.requesting_account?.force_md5_etag !== undefined) {
return object_sdk?.requesting_account?.force_md5_etag;
}
// otherwise return global default
Expand Down
2 changes: 1 addition & 1 deletion src/test/unit_tests/test_nc_nsfs_cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ mocha.describe('manage_nsfs cli', function() {
assert_response(action, type, add_res, bucket_options);
const bucket = await read_config_file(config_root, CONFIG_SUBDIRS.BUCKETS, name);
assert_bucket(bucket, bucket_options);
assert(!_.isUndefined(bucket._id));
assert(bucket._id !== undefined);
// make sure that the config file includes id and owner_account (account id)
const account = await read_config_file(config_root, CONFIG_SUBDIRS.ACCOUNTS, account_name);
assert(bucket.owner_account === account._id);
Expand Down