@@ -8,7 +8,7 @@ const minimist = require('minimist');
8
8
const config = require ( '../../config' ) ;
9
9
const P = require ( '../util/promise' ) ;
10
10
const nb_native = require ( '../util/nb_native' ) ;
11
- const { ConfigFS, JSON_SUFFIX } = require ( '../sdk/config_fs' ) ;
11
+ const { ConfigFS } = require ( '../sdk/config_fs' ) ;
12
12
const cloud_utils = require ( '../util/cloud_utils' ) ;
13
13
const native_fs_utils = require ( '../util/native_fs_utils' ) ;
14
14
const mongo_utils = require ( '../util/mongo_utils' ) ;
@@ -387,8 +387,8 @@ async function add_account(data) {
387
387
await manage_nsfs_validations . validate_account_args ( config_fs , data , ACTIONS . ADD , undefined ) ;
388
388
389
389
const access_key = has_access_keys ( data . access_keys ) ? data . access_keys [ 0 ] . access_key : undefined ;
390
- const name_exists = await config_fs . is_account_exists ( { name : data . name } ) ;
391
- const access_key_exists = access_key && await config_fs . is_account_exists ( { access_key } ) ;
390
+ const name_exists = await config_fs . is_account_exists_by_name ( data . name ) ;
391
+ const access_key_exists = access_key && await config_fs . is_account_exists_by_access_key ( access_key ) ;
392
392
393
393
const event_arg = data . name ? data . name : access_key ;
394
394
if ( name_exists || access_key_exists ) {
@@ -405,7 +405,7 @@ async function add_account(data) {
405
405
// for validating against the schema we need an object, hence we parse it back to object
406
406
const account = encrypted_data ? JSON . parse ( encrypted_data ) : data ;
407
407
nsfs_schema_utils . validate_account_schema ( account ) ;
408
- await config_fs . create_account_config_file ( data . name , account , true ) ;
408
+ await config_fs . create_account_config_file ( account ) ;
409
409
write_stdout_response ( ManageCLIResponse . AccountCreated , data , { account : event_arg } ) ;
410
410
}
411
411
@@ -434,7 +434,7 @@ async function update_account(data, is_flag_iam_operate_on_root_account) {
434
434
// for validating against the schema we need an object, hence we parse it back to object
435
435
const account = encrypted_data ? JSON . parse ( encrypted_data ) : data ;
436
436
nsfs_schema_utils . validate_account_schema ( account ) ;
437
- await config_fs . update_account_config_file ( data . name , account , undefined , undefined ) ;
437
+ await config_fs . update_account_config_file ( account ) ;
438
438
write_stdout_response ( ManageCLIResponse . AccountUpdated , data ) ;
439
439
return ;
440
440
}
@@ -446,9 +446,9 @@ async function update_account(data, is_flag_iam_operate_on_root_account) {
446
446
secret_key : data . access_keys [ 0 ] . secret_key ,
447
447
} ;
448
448
449
- const name_exists = update_name && await config_fs . is_account_exists ( { name : data . name } ) ;
449
+ const name_exists = update_name && await config_fs . is_account_exists_by_name ( data . name , undefined ) ;
450
450
const access_key_exists = update_access_key &&
451
- await config_fs . is_account_exists ( { access_key : data . access_keys [ 0 ] . access_key . unwrap ( ) } ) ;
451
+ await config_fs . is_account_exists_by_access_key ( data . access_keys [ 0 ] . access_key . unwrap ( ) ) ;
452
452
453
453
if ( name_exists || access_key_exists ) {
454
454
const err_code = name_exists ? ManageCLIError . AccountNameAlreadyExists : ManageCLIError . AccountAccessKeyAlreadyExists ;
@@ -465,18 +465,17 @@ async function update_account(data, is_flag_iam_operate_on_root_account) {
465
465
// for validating against the schema we need an object, hence we parse it back to object
466
466
const parsed_data = JSON . parse ( encrypted_data ) ;
467
467
nsfs_schema_utils . validate_account_schema ( parsed_data ) ;
468
- if ( update_name ) {
469
- await config_fs . create_account_config_file ( new_name , parsed_data , true , [ cur_access_key ] ) ;
470
- await config_fs . delete_account_config_file ( cur_name , data . access_keys ) ;
471
- } else if ( update_access_key ) {
472
- await config_fs . update_account_config_file ( cur_name , parsed_data , parsed_data . access_keys , [ cur_access_key ] ) ;
473
- }
468
+ await config_fs . update_account_config_file ( parsed_data , {
469
+ old_name : update_name && cur_name ,
470
+ new_access_keys_to_link : update_access_key && parsed_data . access_keys ,
471
+ access_keys_to_delete : update_access_key && [ { access_key : cur_access_key } ]
472
+ } ) ;
474
473
write_stdout_response ( ManageCLIResponse . AccountUpdated , data ) ;
475
474
}
476
475
477
476
async function delete_account ( data ) {
478
477
await manage_nsfs_validations . validate_account_args ( config_fs , data , ACTIONS . DELETE , undefined ) ;
479
- await config_fs . delete_account_config_file ( data . name , data . access_keys ) ;
478
+ await config_fs . delete_account_config_file ( data ) ;
480
479
write_stdout_response ( ManageCLIResponse . AccountDeleted , '' , { account : data . name } ) ;
481
480
}
482
481
@@ -578,19 +577,10 @@ function filter_bucket(bucket, filters) {
578
577
* @param {object } [filters]
579
578
*/
580
579
async function list_config_files ( type , wide , show_secrets , filters = { } ) {
581
- let entries ;
582
- // in case we have a filter by name, we don't need to read all the entries and iterate them
583
- // instead we "mock" the entries array to have one entry and it is the name by the filter (we add it for performance)
580
+ let entries = [ ] ;
581
+ const should_filter = Object . keys ( filters ) . length > 0 ;
584
582
const is_filter_by_name = filters . name !== undefined ;
585
- if ( is_filter_by_name ) {
586
- entries = [ { 'name' : filters . name + JSON_SUFFIX } ] ;
587
- } else {
588
- entries = type === TYPES . ACCOUNT ?
589
- await config_fs . list_root_accounts ( ) :
590
- await config_fs . list_buckets ( ) ;
591
- }
592
583
593
- const should_filter = Object . keys ( filters ) . length > 0 ;
594
584
// decryption causing mkm initalization
595
585
// decrypt only if data has access_keys and show_secrets = true (no need to decrypt if show_secrets = false but should_filter = true)
596
586
const options = {
@@ -599,19 +589,27 @@ async function list_config_files(type, wide, show_secrets, filters = {}) {
599
589
silent_if_missing : true
600
590
} ;
601
591
592
+ // in case we have a filter by name, we don't need to read all the entries and iterate them
593
+ // instead we "mock" the entries array to have one entry and it is the name by the filter (we add it for performance)
594
+ if ( is_filter_by_name ) {
595
+ entries = [ filters . name ] ;
596
+ } else if ( type === TYPES . ACCOUNT ) {
597
+ entries = await config_fs . list_accounts ( ) ;
598
+ } else if ( type === TYPES . BUCKET ) {
599
+ entries = await config_fs . list_buckets ( ) ;
600
+ }
601
+
602
602
let config_files_list = await P . map_with_concurrency ( 10 , entries , async entry => {
603
- if ( entry . name . endsWith ( JSON_SUFFIX ) ) {
604
- if ( wide || should_filter ) {
605
- const data = type === TYPES . ACCOUNT ?
606
- await config_fs . get_account_by_name ( entry . name , options ) :
607
- await config_fs . get_bucket_by_name ( entry . name , options ) ;
608
- if ( ! data ) return undefined ;
609
- if ( should_filter && ! filter_list_item ( type , data , filters ) ) return undefined ;
610
- // remove secrets on !show_secrets && should filter
611
- return wide ? _ . omit ( data , show_secrets ? [ ] : [ 'access_keys' ] ) : { name : entry . name . slice ( 0 , entry . name . indexOf ( JSON_SUFFIX ) ) } ;
612
- } else {
613
- return { name : entry . name . slice ( 0 , entry . name . indexOf ( JSON_SUFFIX ) ) } ;
614
- }
603
+ if ( wide || should_filter ) {
604
+ const data = type === TYPES . ACCOUNT ?
605
+ await config_fs . get_account_by_name ( entry , options ) :
606
+ await config_fs . get_bucket_by_name ( entry , options ) ;
607
+ if ( ! data ) return undefined ;
608
+ if ( should_filter && ! filter_list_item ( type , data , filters ) ) return undefined ;
609
+ // remove secrets on !show_secrets && should filter
610
+ return wide ? _ . omit ( data , show_secrets ? [ ] : [ 'access_keys' ] ) : { name : entry } ;
611
+ } else {
612
+ return { name : entry } ;
615
613
}
616
614
} ) ;
617
615
// it inserts undefined for the entry '.noobaa-config-nsfs' and we wish to remove it
0 commit comments