33
44const dbg = require ( '../util/debug_module' ) ( __filename ) ;
55const nb_native = require ( '../util/nb_native' ) ;
6- const { CONFIG_TYPES } = require ( '../sdk/config_fs' ) ;
76const native_fs_utils = require ( '../util/native_fs_utils' ) ;
87const ManageCLIError = require ( '../manage_nsfs/manage_nsfs_cli_errors' ) . ManageCLIError ;
98const NSFS_CLI_ERROR_EVENT_MAP = require ( '../manage_nsfs/manage_nsfs_cli_errors' ) . NSFS_CLI_ERROR_EVENT_MAP ;
109const ManageCLIResponse = require ( '../manage_nsfs/manage_nsfs_cli_responses' ) . ManageCLIResponse ;
1110const NSFS_CLI_SUCCESS_EVENT_MAP = require ( '../manage_nsfs/manage_nsfs_cli_responses' ) . NSFS_CLI_SUCCESS_EVENT_MAP ;
1211const { BOOLEAN_STRING_VALUES } = require ( '../manage_nsfs/manage_nsfs_constants' ) ;
1312const NoobaaEvent = require ( '../manage_nsfs/manage_nsfs_events_utils' ) . NoobaaEvent ;
14- const mongo_utils = require ( '../util/mongo_utils ' ) ;
13+ const { account_id_cache } = require ( '../sdk/accountspace_fs ' ) ;
1514
1615
1716function throw_cli_error ( error , detail , event_arg ) {
@@ -35,29 +34,43 @@ function write_stdout_response(response_code, detail, event_arg) {
3534}
3635
3736/**
38- * get_bucket_owner_account will return the account of the bucket_owner
37+ * get_bucket_owner_account_by_name will return the account of the bucket_owner
3938 * otherwise it would throw an error
4039 * @param {import('../sdk/config_fs').ConfigFS } config_fs
41- * @param {string } [bucket_owner]
42- * @param {string } [owner_account_id]
40+ * @param {string } bucket_owner
4341 */
44- async function get_bucket_owner_account ( config_fs , bucket_owner , owner_account_id ) {
42+ async function get_bucket_owner_account_by_name ( config_fs , bucket_owner ) {
4543 try {
46- const account = bucket_owner ?
47- await config_fs . get_account_by_name ( bucket_owner ) :
48- await config_fs . get_identity_by_id ( owner_account_id , CONFIG_TYPES . ACCOUNT ) ;
44+ const account = await config_fs . get_account_by_name ( bucket_owner ) ;
4945 return account ;
5046 } catch ( err ) {
5147 if ( err . code === 'ENOENT' ) {
52- const detail_msg = bucket_owner ?
53- `bucket owner name ${ bucket_owner } does not exists` :
54- `bucket owner id ${ owner_account_id } does not exists` ;
48+ const detail_msg = `bucket owner name ${ bucket_owner } does not exist` ;
5549 throw_cli_error ( ManageCLIError . BucketSetForbiddenBucketOwnerNotExists , detail_msg , { bucket_owner : bucket_owner } ) ;
5650 }
5751 throw err ;
5852 }
5953}
6054
55+ /**
56+ * get_bucket_owner_account_by_id will return the account of the owner_account id
57+ * otherwise it would throw an error
58+ * @param {import('../sdk/config_fs').ConfigFS } config_fs
59+ * @param {string } owner_account
60+ */
61+ async function get_bucket_owner_account_by_id ( config_fs , owner_account ) {
62+ try {
63+ const account = await account_id_cache . get_with_cache ( { _id : owner_account , config_fs } ) ;
64+ return account ;
65+ } catch ( err ) {
66+ if ( err . code === 'ENOENT' ) {
67+ const detail_msg = `bucket owner account id ${ owner_account } does not exist` ;
68+ throw_cli_error ( ManageCLIError . BucketSetForbiddenBucketOwnerNotExists , detail_msg , { owner_account : owner_account } ) ;
69+ }
70+ throw err ;
71+ }
72+ }
73+
6174/**
6275 * get_boolean_or_string_value will check if the value
6376 * 1. if the value is undefined - it returns false.
@@ -113,28 +126,6 @@ function set_debug_level(debug) {
113126 nb_native ( ) . fs . set_debug_level ( debug_level ) ;
114127}
115128
116- /**
117- * generate_id will generate an id that we use to identify entities (such as account, bucket, etc.).
118- */
119- // TODO:
120- // - reuse this function in NC NSFS where we used the mongo_utils module
121- // - this function implantation should be db_client.new_object_id(),
122- // but to align with manage nsfs we won't change it now
123- function generate_id ( ) {
124- return mongo_utils . mongoObjectId ( ) ;
125- }
126-
127- /**
128- * check_root_account_owns_user checks if an account is owned by root account
129- * @param {object } root_account
130- * @param {object } account
131- */
132- function check_root_account_owns_user ( root_account , account ) {
133- if ( account . owner === undefined ) return false ;
134- return root_account . _id === account . owner ;
135- }
136-
137-
138129/**
139130 * is_name_update returns true if a new_name flag was provided and it's not equal to
140131 * the current name
@@ -163,11 +154,10 @@ function is_access_key_update(data) {
163154exports . throw_cli_error = throw_cli_error ;
164155exports . write_stdout_response = write_stdout_response ;
165156exports . get_boolean_or_string_value = get_boolean_or_string_value ;
166- exports . get_bucket_owner_account = get_bucket_owner_account ;
157+ exports . get_bucket_owner_account_by_name = get_bucket_owner_account_by_name ;
158+ exports . get_bucket_owner_account_by_id = get_bucket_owner_account_by_id ;
167159exports . get_options_from_file = get_options_from_file ;
168160exports . has_access_keys = has_access_keys ;
169- exports . generate_id = generate_id ;
170161exports . set_debug_level = set_debug_level ;
171- exports . check_root_account_owns_user = check_root_account_owns_user ;
172162exports . is_name_update = is_name_update ;
173163exports . is_access_key_update = is_access_key_update ;
0 commit comments