|
3 | 3 |
|
4 | 4 | const dbg = require('../util/debug_module')(__filename);
|
5 | 5 | const _ = require('lodash');
|
6 |
| -const path = require('path'); |
7 | 6 | const minimist = require('minimist');
|
8 | 7 | const config = require('../../config');
|
9 | 8 | const P = require('../util/promise');
|
@@ -717,22 +716,38 @@ async function set_bucker_owner(bucket_data) {
|
717 | 716 | ////////////////////
|
718 | 717 | //// IP WHITELIST //
|
719 | 718 | ////////////////////
|
| 719 | +/** |
| 720 | + * @returns {Promise<[boolean, object]>} - [config_exists, config_data] - config_exists is a boolean that indicates if the config.json exists |
| 721 | + * and config_data is the parsed data from the config.json file. returns empty object for config_data if config.json does not exist. |
| 722 | + */ |
| 723 | +async function _get_config_json_if_exist() { |
| 724 | + try { |
| 725 | + const config_json = await config_fs.get_config_json(); |
| 726 | + return [true, config_json]; |
| 727 | + } catch (err) { |
| 728 | + if (err.code !== 'ENOENT') throw err; |
| 729 | + return [false, {}]; |
| 730 | + } |
| 731 | +} |
720 | 732 |
|
721 | 733 | async function whitelist_ips_management(args) {
|
722 | 734 | const ips = args.ips;
|
723 | 735 | manage_nsfs_validations.validate_whitelist_arg(ips);
|
724 | 736 |
|
725 | 737 | const whitelist_ips = JSON.parse(ips);
|
726 | 738 | manage_nsfs_validations.validate_whitelist_ips(whitelist_ips);
|
727 |
| - const config_path = path.join(config_fs.config_root, 'config.json'); |
728 | 739 | try {
|
729 |
| - const config_data = require(config_path); |
| 740 | + const [config_exists, config_data] = await _get_config_json_if_exist(); |
730 | 741 | config_data.S3_SERVER_IP_WHITELIST = whitelist_ips;
|
731 | 742 | const data = JSON.stringify(config_data);
|
732 |
| - await config_fs.update_config_json_file(data); |
| 743 | + if (config_exists) { |
| 744 | + await config_fs.update_config_json_file(data); |
| 745 | + } else { |
| 746 | + await config_fs.create_config_json_file(data); |
| 747 | + } |
733 | 748 | } catch (err) {
|
734 |
| - dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, path ' + config_path, err); |
735 |
| - throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed, config_path); |
| 749 | + dbg.error('manage_nsfs.whitelist_ips_management: Error while updation config.json, path:', config_fs.config_json_path, err); |
| 750 | + throw_cli_error(ManageCLIError.WhiteListIPUpdateFailed); |
736 | 751 | }
|
737 | 752 | write_stdout_response(ManageCLIResponse.WhiteListIPUpdated, ips);
|
738 | 753 | }
|
|
0 commit comments