Skip to content

Commit 7e5d01a

Browse files
committed
change functions order
Signed-off-by: Romy <[email protected]>
1 parent 351167d commit 7e5d01a

File tree

1 file changed

+128
-131
lines changed

1 file changed

+128
-131
lines changed

src/sdk/config_fs.js

Lines changed: 128 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,40 @@ class ConfigFS {
106106
await native_fs_utils.update_config_file(this.root_fs_context, this.config_root, this.config_json_path, data);
107107
}
108108

109+
/**
110+
* get_config_data reads a config file and returns its content
111+
* while omitting secrets if show_secrets flag was not provided
112+
* and decrypts the account's secret_key if decrypt_secret_key is true
113+
* @param {string} config_file_path
114+
* @param {boolean} [show_secrets]
115+
* @param {boolean} [decrypt_secret_key]
116+
*/
117+
async get_config_data(config_file_path, show_secrets = false, decrypt_secret_key = false) {
118+
const { data } = await nb_native().fs.readFile(this.root_fs_context, config_file_path);
119+
const config_data = _.omit(JSON.parse(data.toString()), show_secrets ? [] : ['access_keys']);
120+
if (decrypt_secret_key) config_data.access_keys = await nc_mkm.decrypt_access_keys(config_data);
121+
return config_data;
122+
}
123+
124+
/**
125+
* get_config_data_if_exists will read a config file and return its content
126+
* while omitting secrets if show_secrets flag was not provided
127+
* and decrypts the account's secret_key if decrypt_secret_key is true
128+
* if the config file was deleted (encounter ENOENT error) - continue (returns undefined)
129+
* @param {string} config_file_path
130+
* @param {boolean} [show_secrets]
131+
* @param {boolean} [decrypt_secret_key]
132+
*/
133+
async get_config_data_if_exists(config_file_path, show_secrets = false, decrypt_secret_key = false) {
134+
try {
135+
const config_data = await this.get_config_data(config_file_path, show_secrets, decrypt_secret_key);
136+
return config_data;
137+
} catch (err) {
138+
dbg.warn('get_config_data_if_exists: with config_file_path', config_file_path, 'got an error', err);
139+
if (err.code !== 'ENOENT') throw err;
140+
}
141+
}
142+
109143
///////////////////////////////////////
110144
////// ACCOUNT CONFIG DIR FUNCS //////
111145
///////////////////////////////////////
@@ -133,64 +167,25 @@ class ConfigFS {
133167
return path.join(this.accounts_dir_path, this.json(account_name));
134168
}
135169

136-
137-
/**
138-
* read_root_accounts returns the root accounts array exists under the config dir
139-
* @returns {Promise<Dirent[]>}
140-
*/
141-
async read_root_accounts() {
142-
return nb_native().fs.readdir(this.root_fs_context, this.accounts_dir_path);
143-
}
144-
145170
/**
146171
* get_iam_account_path_by_name returns the full account path by name
147172
* @param {string} account_name
148173
* @returns {string}
149-
*/
174+
*/
150175
get_iam_account_path_by_name(account_name, owner_root_account_name) {
151-
// TODO - change to this.symlink(account_name) on identities/ PR;
152-
// update IAM user location identities/root_id_number/iam_account_name.symlink
153-
return path.join(this.accounts_dir_path, this.json(account_name));
176+
// TODO - change to this.symlink(account_name) on identities/ PR;
177+
// update IAM user location identities/root_id_number/iam_account_name.symlink
178+
return path.join(this.accounts_dir_path, this.json(account_name));
154179
}
155180

156181
/**
157182
* get_account_relative_path_by_name returns the full account path by name
158183
* @param {string} account_name
159184
* @returns {string}
160-
*/
185+
*/
161186
get_account_relative_path_by_name(account_name) {
162-
// TODO - change to this.symlink(account_name) on identities/ PR;
163-
return path.join('../', CONFIG_SUBDIRS.ACCOUNTS, this.json(account_name));
164-
}
165-
166-
/**
167-
* is_account_exists returns true if account config path exists in config dir
168-
* it can be determined by any account identifier - name, access_key and in the future id
169-
* @param {{ name?: string, access_key?: string }} identifier_object
170-
* @returns {Promise<boolean>}
171-
*/
172-
async is_account_exists(identifier_object) {
173-
let path_to_check;
174-
let use_lstat = false;
175-
if (identifier_object.name) {
176-
// TODO - when users/ move to be symlink we need to use_lstat by name, id is not using lstat
177-
path_to_check = this.get_account_path_by_name(identifier_object.name);
178-
} else if (identifier_object.access_key) {
179-
path_to_check = this.get_account_path_by_access_key(identifier_object.access_key);
180-
use_lstat = true;
181-
}
182-
// TODO - add is_account_exists by id when idenetities/ added
183-
// else if (identifier_object.id) {}
184-
return native_fs_utils.is_path_exists(this.root_fs_context, path_to_check, use_lstat);
185-
}
186-
187-
/**
188-
* get_old_account_path_by_name returns the full account path by name based on old config dir structure
189-
* @param {string} account_name
190-
* @returns {string}
191-
*/
192-
get_old_account_path_by_name(account_name) {
193-
return path.join(this.accounts_dir_path, this.json(account_name));
187+
// TODO - change to this.symlink(account_name) on identities/ PR;
188+
return path.join('../', CONFIG_SUBDIRS.ACCOUNTS, this.json(account_name));
194189
}
195190

196191
/**
@@ -203,37 +198,33 @@ class ConfigFS {
203198
}
204199

205200
/**
206-
* get_config_data reads a config file and returns its content
207-
* while omitting secrets if show_secrets flag was not provided
208-
* and decrypts the account's secret_key if decrypt_secret_key is true
209-
* @param {string} config_file_path
210-
* @param {boolean} [show_secrets]
211-
* @param {boolean} [decrypt_secret_key]
212-
*/
213-
async get_config_data(config_file_path, show_secrets = false, decrypt_secret_key = false) {
214-
const { data } = await nb_native().fs.readFile(this.root_fs_context, config_file_path);
215-
const config_data = _.omit(JSON.parse(data.toString()), show_secrets ? [] : ['access_keys']);
216-
if (decrypt_secret_key) config_data.access_keys = await nc_mkm.decrypt_access_keys(config_data);
217-
return config_data;
201+
* get_old_account_path_by_name returns the full account path by name based on old config dir structure
202+
* @param {string} account_name
203+
* @returns {string}
204+
*/
205+
get_old_account_path_by_name(account_name) {
206+
return path.join(this.accounts_dir_path, this.json(account_name));
218207
}
219208

220209
/**
221-
* get_config_data_if_exists will read a config file and return its content
222-
* while omitting secrets if show_secrets flag was not provided
223-
* and decrypts the account's secret_key if decrypt_secret_key is true
224-
* if the config file was deleted (encounter ENOENT error) - continue (returns undefined)
225-
* @param {string} config_file_path
226-
* @param {boolean} [show_secrets]
227-
* @param {boolean} [decrypt_secret_key]
228-
*/
229-
async get_config_data_if_exists(config_file_path, show_secrets = false, decrypt_secret_key = false) {
230-
try {
231-
const config_data = await this.get_config_data(config_file_path, show_secrets, decrypt_secret_key);
232-
return config_data;
233-
} catch (err) {
234-
dbg.warn('get_config_data_if_exists: with config_file_path', config_file_path, 'got an error', err);
235-
if (err.code !== 'ENOENT') throw err;
210+
* is_account_exists returns true if account config path exists in config dir
211+
* it can be determined by any account identifier - name, access_key and in the future id
212+
* @param {{ name?: string, access_key?: string }} identifier_object
213+
* @returns {Promise<boolean>}
214+
*/
215+
async is_account_exists(identifier_object) {
216+
let path_to_check;
217+
let use_lstat = false;
218+
if (identifier_object.name) {
219+
// TODO - when users/ move to be symlink we need to use_lstat by name, id is not using lstat
220+
path_to_check = this.get_account_path_by_name(identifier_object.name);
221+
} else if (identifier_object.access_key) {
222+
path_to_check = this.get_account_path_by_access_key(identifier_object.access_key);
223+
use_lstat = true;
236224
}
225+
// TODO - add is_account_exists by id when idenetities/ added
226+
// else if (identifier_object.id) {}
227+
return native_fs_utils.is_path_exists(this.root_fs_context, path_to_check, use_lstat);
237228
}
238229

239230
/**
@@ -262,6 +253,14 @@ class ConfigFS {
262253
return account;
263254
}
264255

256+
/**
257+
* read_root_accounts returns the root accounts array exists under the config dir
258+
* @returns {Promise<Dirent[]>}
259+
*/
260+
async read_root_accounts() {
261+
return nb_native().fs.readdir(this.root_fs_context, this.accounts_dir_path);
262+
}
263+
265264
/**
266265
* create_account_config_file creates account config file
267266
* if account_data.access_keys is an array that contains at least 1 item -
@@ -292,34 +291,6 @@ class ConfigFS {
292291
}
293292
}
294293

295-
/**
296-
* delete_account_config_file deletes account config file
297-
* if access_keys_to_delete is an array that contains at least 1 item -
298-
* unlink all item in access_keys_to_delete
299-
* @param {string} account_name
300-
* @param {Object[]} access_keys_to_delete
301-
* @returns {Promise<void>}
302-
*/
303-
async delete_account_config_file(account_name, access_keys_to_delete = []) {
304-
const account_config_path = this.get_account_path_by_name(account_name);
305-
await native_fs_utils.delete_config_file(this.root_fs_context, this.accounts_dir_path, account_config_path);
306-
for (const access_keys of access_keys_to_delete) {
307-
const access_key_config_path = this.get_account_path_by_access_key(access_keys.access_key);
308-
await nb_native().fs.unlink(this.root_fs_context, access_key_config_path);
309-
}
310-
}
311-
312-
/**
313-
* delete_access_key_config_file unlinks the access key from the file system
314-
* @param {string} access_key
315-
* @returns {Promise<void>}
316-
*/
317-
async delete_access_key_config_file(access_key) {
318-
const acces_key_path = this.get_account_path_by_access_key(access_key);
319-
await nb_native().fs.unlink(this.root_fs_context, acces_key_path);
320-
}
321-
322-
323294
/**
324295
* update_account_config_file updates account config file
325296
* if old_access_keys is an array that contains at least 1 item -
@@ -351,6 +322,33 @@ class ConfigFS {
351322
}
352323
}
353324

325+
/**
326+
* delete_account_config_file deletes account config file
327+
* if access_keys_to_delete is an array that contains at least 1 item -
328+
* unlink all item in access_keys_to_delete
329+
* @param {string} account_name
330+
* @param {Object[]} access_keys_to_delete
331+
* @returns {Promise<void>}
332+
*/
333+
async delete_account_config_file(account_name, access_keys_to_delete = []) {
334+
const account_config_path = this.get_account_path_by_name(account_name);
335+
await native_fs_utils.delete_config_file(this.root_fs_context, this.accounts_dir_path, account_config_path);
336+
for (const access_keys of access_keys_to_delete) {
337+
const access_key_config_path = this.get_account_path_by_access_key(access_keys.access_key);
338+
await nb_native().fs.unlink(this.root_fs_context, access_key_config_path);
339+
}
340+
}
341+
342+
/**
343+
* delete_access_key_config_file unlinks the access key from the file system
344+
* @param {string} access_key
345+
* @returns {Promise<void>}
346+
*/
347+
async delete_access_key_config_file(access_key) {
348+
const acces_key_path = this.get_account_path_by_access_key(access_key);
349+
await nb_native().fs.unlink(this.root_fs_context, acces_key_path);
350+
}
351+
354352
//////////////////////////////////////
355353
////// BUCKET CONFIG DIR FUNCS //////
356354
//////////////////////////////////////
@@ -374,6 +372,16 @@ class ConfigFS {
374372
// TODO
375373
}
376374

375+
/**
376+
* is_bucket_exists returns true if bucket config path exists in config dir
377+
* @param {string} bucket_name
378+
* @returns {Promise<boolean>}
379+
*/
380+
async is_bucket_exists(bucket_name) {
381+
const path_to_check = this.get_bucket_path_by_name(bucket_name);
382+
return native_fs_utils.is_path_exists(this.root_fs_context, path_to_check);
383+
}
384+
377385
/**
378386
* get_bucket_by_name returns the full bucket info by name
379387
* @param {string} bucket_name
@@ -385,6 +393,25 @@ class ConfigFS {
385393
return bucket;
386394
}
387395

396+
/**
397+
* get_bucket_by_name returns the full bucket info by name
398+
* @param {string} bucket_name
399+
* @returns {Promise<any>}
400+
*/
401+
async get_bucket_if_exists(bucket_name) {
402+
const bucket_path = this.get_bucket_path_by_name(bucket_name);
403+
const bucket = await this.get_config_data_if_exists(bucket_path);
404+
return bucket;
405+
}
406+
407+
/**
408+
* read_buckets returns the buckets array exists under the config dir
409+
* @returns {Promise<Dirent[]>}
410+
*/
411+
async read_buckets() {
412+
return nb_native().fs.readdir(this.root_fs_context, this.buckets_dir_path);
413+
}
414+
388415
/**
389416
* create_bucket_config_file creates bucket config file
390417
* @param {string} bucket_name
@@ -416,36 +443,6 @@ class ConfigFS {
416443
const bucket_config_path = this.get_bucket_path_by_name(bucket_name);
417444
await native_fs_utils.delete_config_file(this.root_fs_context, this.buckets_dir_path, bucket_config_path);
418445
}
419-
420-
/**
421-
* read_buckets returns the buckets array exists under the config dir
422-
* @returns {Promise<Dirent[]>}
423-
*/
424-
async read_buckets() {
425-
return nb_native().fs.readdir(this.root_fs_context, this.buckets_dir_path);
426-
}
427-
428-
/**
429-
* get_bucket_by_name returns the full bucket info by name
430-
* @param {string} bucket_name
431-
* @returns {Promise<any>}
432-
*/
433-
434-
async get_bucket_if_exists(bucket_name) {
435-
const bucket_path = this.get_bucket_path_by_name(bucket_name);
436-
const bucket = await this.get_config_data_if_exists(bucket_path);
437-
return bucket;
438-
}
439-
440-
/**
441-
* is_bucket_exists returns true if bucket config path exists in config dir
442-
* @param {string} bucket_name
443-
* @returns {Promise<boolean>}
444-
*/
445-
async is_bucket_exists(bucket_name) {
446-
const path_to_check = this.get_bucket_path_by_name(bucket_name);
447-
return native_fs_utils.is_path_exists(this.root_fs_context, path_to_check);
448-
}
449446
}
450447

451448
// EXPORTS

0 commit comments

Comments
 (0)