@@ -21,7 +21,7 @@ const noobaa_cli_diagnose = require('../manage_nsfs/diagnose');
21
21
const nsfs_schema_utils = require ( '../manage_nsfs/nsfs_schema_utils' ) ;
22
22
const { print_usage } = require ( '../manage_nsfs/manage_nsfs_help_utils' ) ;
23
23
const { TYPES , ACTIONS , LIST_ACCOUNT_FILTERS , LIST_BUCKET_FILTERS , GLACIER_ACTIONS } = require ( '../manage_nsfs/manage_nsfs_constants' ) ;
24
- const { throw_cli_error, write_stdout_response, get_boolean_or_string_value, has_access_keys, set_debug_level } = require ( '../manage_nsfs/manage_nsfs_cli_utils' ) ;
24
+ const { throw_cli_error, get_bucket_owner_account , write_stdout_response, get_boolean_or_string_value, has_access_keys, set_debug_level } = require ( '../manage_nsfs/manage_nsfs_cli_utils' ) ;
25
25
const manage_nsfs_validations = require ( '../manage_nsfs/manage_nsfs_validations' ) ;
26
26
const nc_mkm = require ( '../manage_nsfs/nc_master_key_manager' ) . get_instance ( ) ;
27
27
@@ -97,7 +97,6 @@ async function fetch_bucket_data(action, user_input) {
97
97
_id : undefined ,
98
98
name : user_input . name === undefined ? undefined : String ( user_input . name ) ,
99
99
owner_account : undefined ,
100
- bucket_owner : user_input . owner ,
101
100
tag : undefined , // if we would add the option to tag a bucket using CLI, this should be changed
102
101
versioning : action === ACTIONS . ADD ? 'DISABLED' : undefined ,
103
102
creation_date : action === ACTIONS . ADD ? new Date ( ) . toISOString ( ) : undefined ,
@@ -126,6 +125,13 @@ async function fetch_bucket_data(action, user_input) {
126
125
data = await fetch_existing_bucket_data ( data ) ;
127
126
}
128
127
128
+ //if we're updating the owner, needs to override owner in file with the owner from user input.
129
+ //if we're adding a bucket, need to set its owner id field
130
+ if ( ( action === ACTIONS . UPDATE && user_input . owner ) || ( action === ACTIONS . ADD ) ) {
131
+ const account = await get_bucket_owner_account ( config_fs , String ( user_input . owner ) ) ;
132
+ data . owner_account = account . _id ;
133
+ }
134
+
129
135
// override values
130
136
// fs_backend deletion specified with empty string '' (but it is not part of the schema)
131
137
data . fs_backend = data . fs_backend || undefined ;
@@ -159,15 +165,17 @@ async function add_bucket(data) {
159
165
// for validating against the schema we need an object, hence we parse it back to object
160
166
nsfs_schema_utils . validate_bucket_schema ( JSON . parse ( data_json ) ) ;
161
167
await config_fs . create_bucket_config_file ( data . name , data_json ) ;
162
- write_stdout_response ( ManageCLIResponse . BucketCreated , data_json , { bucket : data . name } ) ;
168
+ await set_bucker_owner ( data ) ;
169
+ write_stdout_response ( ManageCLIResponse . BucketCreated , data , { bucket : data . name } ) ;
163
170
}
164
171
165
172
async function get_bucket_status ( data ) {
166
173
await manage_nsfs_validations . validate_bucket_args ( config_fs , data , ACTIONS . STATUS ) ;
167
174
168
175
try {
169
- const config_data = await config_fs . get_bucket_by_name ( data . name ) ;
170
- write_stdout_response ( ManageCLIResponse . BucketStatus , config_data ) ;
176
+ const bucket_data = await config_fs . get_bucket_by_name ( data . name ) ;
177
+ await set_bucker_owner ( bucket_data ) ;
178
+ write_stdout_response ( ManageCLIResponse . BucketStatus , bucket_data ) ;
171
179
} catch ( err ) {
172
180
const err_code = err . code === 'EACCES' ? ManageCLIError . AccessDenied : ManageCLIError . NoSuchBucket ;
173
181
throw_cli_error ( err_code , data . name ) ;
@@ -185,9 +193,11 @@ async function update_bucket(data) {
185
193
// We take an object that was stringify
186
194
// (it unwraps ths sensitive strings, creation_date to string and removes undefined parameters)
187
195
// for validating against the schema we need an object, hence we parse it back to object
188
- nsfs_schema_utils . validate_bucket_schema ( JSON . parse ( data ) ) ;
196
+ const parsed_data = JSON . parse ( data ) ;
197
+ nsfs_schema_utils . validate_bucket_schema ( parsed_data ) ;
189
198
await config_fs . update_bucket_config_file ( cur_name , data ) ;
190
- write_stdout_response ( ManageCLIResponse . BucketUpdated , data ) ;
199
+ await set_bucker_owner ( parsed_data ) ;
200
+ write_stdout_response ( ManageCLIResponse . BucketUpdated , parsed_data ) ;
191
201
return ;
192
202
}
193
203
@@ -203,6 +213,8 @@ async function update_bucket(data) {
203
213
nsfs_schema_utils . validate_bucket_schema ( JSON . parse ( data ) ) ;
204
214
await config_fs . create_bucket_config_file ( new_name , data ) ;
205
215
await config_fs . delete_bucket_config_file ( cur_name ) ;
216
+ data = JSON . parse ( data ) ;
217
+ await set_bucker_owner ( data ) ;
206
218
write_stdout_response ( ManageCLIResponse . BucketUpdated , data ) ;
207
219
}
208
220
@@ -577,7 +589,7 @@ function filter_bucket(bucket, filters) {
577
589
* @param {object } [filters]
578
590
*/
579
591
async function list_config_files ( type , wide , show_secrets , filters = { } ) {
580
- let entries = [ ] ;
592
+ let entry_names = [ ] ;
581
593
const should_filter = Object . keys ( filters ) . length > 0 ;
582
594
const is_filter_by_name = filters . name !== undefined ;
583
595
@@ -592,24 +604,35 @@ async function list_config_files(type, wide, show_secrets, filters = {}) {
592
604
// in case we have a filter by name, we don't need to read all the entries and iterate them
593
605
// instead we "mock" the entries array to have one entry and it is the name by the filter (we add it for performance)
594
606
if ( is_filter_by_name ) {
595
- entries = [ filters . name ] ;
607
+ entry_names = [ filters . name ] ;
596
608
} else if ( type === TYPES . ACCOUNT ) {
597
- entries = await config_fs . list_accounts ( ) ;
609
+ entry_names = await config_fs . list_accounts ( ) ;
598
610
} else if ( type === TYPES . BUCKET ) {
599
- entries = await config_fs . list_buckets ( ) ;
611
+ entry_names = await config_fs . list_buckets ( ) ;
600
612
}
601
613
602
- let config_files_list = await P . map_with_concurrency ( 10 , entries , async entry => {
614
+ // temporary cache for mapping bucker owner_account (id) -> bucket_owner (name)
615
+ const bucket_owners_map = { } ;
616
+ let config_files_list = await P . map_with_concurrency ( 10 , entry_names , async entry_name => {
603
617
if ( wide || should_filter ) {
604
618
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 ) ;
619
+ await config_fs . get_account_by_name ( entry_name , options ) :
620
+ await config_fs . get_bucket_by_name ( entry_name , options ) ;
607
621
if ( ! data ) return undefined ;
608
622
if ( should_filter && ! filter_list_item ( type , data , filters ) ) return undefined ;
609
623
// remove secrets on !show_secrets && should filter
610
- return wide ? _ . omit ( data , show_secrets ? [ ] : [ 'access_keys' ] ) : { name : entry } ;
624
+ if ( ! wide ) return { name : entry_name } ;
625
+ if ( type === TYPES . ACCOUNT ) return _ . omit ( data , show_secrets ? [ ] : [ 'access_keys' ] ) ;
626
+ if ( type === TYPES . BUCKET ) {
627
+ data . bucket_owner = bucket_owners_map [ data . owner_account ] ;
628
+ if ( ! data . bucket_owner ) {
629
+ await set_bucker_owner ( data ) ;
630
+ bucket_owners_map [ data . owner_account ] = data . bucket_owner ;
631
+ }
632
+ return data ;
633
+ }
611
634
} else {
612
- return { name : entry } ;
635
+ return { name : entry_name } ;
613
636
}
614
637
} ) ;
615
638
// it inserts undefined for the entry '.noobaa-config-nsfs' and we wish to remove it
@@ -646,6 +669,15 @@ function get_access_keys(action, user_input) {
646
669
return { access_keys, new_access_key } ;
647
670
}
648
671
672
+ /**
673
+ * set_bucker_owner gets bucket owner from cache by its id and sets bucket_owner name on the bucket data
674
+ * @param {object } bucket_data
675
+ */
676
+ async function set_bucker_owner ( bucket_data ) {
677
+ const account_data = await config_fs . get_identity_by_id ( bucket_data . owner_account , TYPES . ACCOUNT , { silent_if_missing : true } ) ;
678
+ bucket_data . bucket_owner = account_data ?. name ;
679
+ }
680
+
649
681
async function whitelist_ips_management ( args ) {
650
682
const ips = args . ips ;
651
683
manage_nsfs_validations . validate_whitelist_arg ( ips ) ;
0 commit comments