Skip to content

Commit 4f4756f

Browse files
committed
add support for uls deletion
Signed-off-by: Utkarsh Srivastava <srivastavautkarsh8097@gmail.com>
1 parent 0557aa1 commit 4f4756f

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/cmd/manage_nsfs.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ async function fetch_bucket_data(action, user_input) {
131131
versioning: action === ACTIONS.ADD ? 'DISABLED' : undefined,
132132
creation_date: action === ACTIONS.ADD ? new Date().toISOString() : undefined,
133133
path: user_input.path,
134-
should_create_underlying_storage: action === ACTIONS.ADD ? false : undefined,
134+
should_create_underlying_storage: action === ACTIONS.ADD ? Boolean(user_input.should_create_underlying_storage) : undefined,
135135
new_name: user_input.new_name === undefined ? undefined : String(user_input.new_name),
136136
fs_backend: user_input.fs_backend === undefined ? config.NSFS_NC_STORAGE_BACKEND : String(user_input.fs_backend),
137137
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),
@@ -221,9 +221,25 @@ async function merge_new_and_existing_config_data(user_input_bucket_data) {
221221
async function add_bucket(data) {
222222
data._id = mongo_utils.mongoObjectId();
223223
const parsed_bucket_data = await config_fs.create_bucket_config_file(data);
224-
await set_bucker_owner(parsed_bucket_data);
224+
225+
const account = await account_id_cache.get_with_cache({ _id: parsed_bucket_data.owner_account, config_fs });
226+
await set_bucker_owner(parsed_bucket_data, account);
225227

226228
const [reserved_tag_event_args] = BucketSpaceFS._generate_reserved_tag_event_args({}, data.tag);
229+
if (parsed_bucket_data.should_create_underlying_storage) {
230+
try {
231+
await nb_native().fs.mkdir(
232+
await native_fs_utils.get_fs_context(account.nsfs_account_config, parsed_bucket_data.fs_backend),
233+
data.path,
234+
native_fs_utils.get_umasked_mode(config.BASE_MODE_DIR),
235+
);
236+
} catch (error) {
237+
new NoobaaEvent(NoobaaEvent.BUCKET_DIR_CREATION_FAILED)
238+
.create_event(data.name, { bucket: data.name, path: data.path }, error);
239+
await config_fs.delete_bucket_config_file(data.name);
240+
throw error;
241+
}
242+
}
227243

228244
return {
229245
code: ManageCLIResponse.BucketCreated,
@@ -295,6 +311,13 @@ async function delete_bucket(data, force) {
295311

296312
await native_fs_utils.folder_delete(bucket_temp_dir_path, fs_context_fs_backend, true);
297313
await config_fs.delete_bucket_config_file(data.name);
314+
if (data.should_create_underlying_storage) {
315+
try {
316+
await nb_native().fs.rmdir(config_fs.fs_context, data.path);
317+
} catch (error) {
318+
dbg.warn('failed to delete underlying storage directory:', error);
319+
}
320+
}
298321
return { code: ManageCLIResponse.BucketDeleted, detail: { name: data.name }, event_arg: { bucket: data.name } };
299322
} catch (err) {
300323
if (err.code === 'ENOENT') throw_cli_error(ManageCLIError.NoSuchBucket, data.name);
@@ -784,11 +807,13 @@ function get_access_keys(action, user_input) {
784807
/**
785808
* set_bucker_owner gets bucket owner from cache by its id and sets bucket_owner name on the bucket data
786809
* @param {object} bucket_data
810+
* @param {*} [account_data]
787811
*/
788-
async function set_bucker_owner(bucket_data) {
789-
let account_data;
812+
async function set_bucker_owner(bucket_data, account_data) {
790813
try {
791-
account_data = await account_id_cache.get_with_cache({ _id: bucket_data.owner_account, config_fs });
814+
if (!account_data) {
815+
account_data = await account_id_cache.get_with_cache({ _id: bucket_data.owner_account, config_fs });
816+
}
792817
} catch (err) {
793818
dbg.warn(`set_bucker_owner.couldn't find bucket owner data by id ${bucket_data.owner_account}`);
794819
}

src/manage_nsfs/manage_nsfs_constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ const VALID_OPTIONS_ANONYMOUS_ACCOUNT = {
6161
};
6262

6363
const VALID_OPTIONS_BUCKET = {
64-
'add': new Set(['name', 'owner', 'path', 'bucket_policy', 'fs_backend', 'force_md5_etag', 'notifications', FROM_FILE, ...CLI_MUTUAL_OPTIONS]),
64+
'add': new Set(['name', 'owner', 'path', 'bucket_policy', 'fs_backend', 'force_md5_etag', 'notifications', 'should_create_underlying_storage', FROM_FILE, ...CLI_MUTUAL_OPTIONS]),
6565
'update': new Set(['name', 'owner', 'path', 'bucket_policy', 'fs_backend', 'new_name', 'force_md5_etag', 'notifications', 'tag', 'merge_tag', ...CLI_MUTUAL_OPTIONS]),
6666
'delete': new Set(['name', 'force', ...CLI_MUTUAL_OPTIONS]),
6767
'list': new Set(['wide', 'name', ...CLI_MUTUAL_OPTIONS]),
@@ -143,6 +143,7 @@ const OPTION_TYPE = {
143143
force: 'boolean',
144144
anonymous: 'boolean',
145145
default_connection: 'string',
146+
should_create_underlying_storage: 'boolean',
146147
// health options
147148
deployment_type: 'string',
148149
all_account_details: 'boolean',

src/manage_nsfs/manage_nsfs_help_utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ Flags:
243243
--fs_backend <none | GPFS | CEPH_FS | NFSv4> (optional) Set the filesystem type (default config.NSFS_NC_STORAGE_BACKEND)
244244
--force_md5_etag <true | false> (optional) Set the bucket to force md5 etag calculation (unset with '') (will override default config.NSFS_NC_STORAGE_BACKEND)
245245
--from_file <string> (optional) Use details from the JSON file, there is no need to mention all the properties individually in the CLI
246-
246+
--should_create_underlying_storage <true | false> (optional) Force creation of bucket's underlying storage directory
247247
`;
248248

249249
const BUCKET_FLAGS_UPDATE = `

src/manage_nsfs/manage_nsfs_validations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ async function validate_bucket_args(config_fs, data, action) {
455455
await check_new_name_exists(TYPES.BUCKET, config_fs, action, data);
456456
// in case we have the fs_backend it changes the fs_context that we use for the path
457457
const fs_context_fs_backend = native_fs_utils.get_process_fs_context(data.fs_backend);
458-
if (!config.NC_DISABLE_ACCESS_CHECK) {
458+
if (!data.should_create_underlying_storage && !config.NC_DISABLE_ACCESS_CHECK) {
459459
const exists = await native_fs_utils.is_path_exists(fs_context_fs_backend, data.path);
460460
if (!exists) {
461461
throw_cli_error(ManageCLIError.InvalidStoragePath, data.path);
@@ -467,7 +467,7 @@ async function validate_bucket_args(config_fs, data, action) {
467467

468468
const account_fs_context = await native_fs_utils.get_fs_context(owner_account_data.nsfs_account_config,
469469
owner_account_data.nsfs_account_config.fs_backend);
470-
if (!config.NC_DISABLE_ACCESS_CHECK) {
470+
if (!data.should_create_underlying_storage && !config.NC_DISABLE_ACCESS_CHECK) {
471471
const accessible = await native_fs_utils.is_dir_accessible(account_fs_context, data.path);
472472
if (!accessible) {
473473
throw_cli_error(ManageCLIError.InaccessibleStoragePath, data.path);

0 commit comments

Comments
 (0)