From bd8263e3243f172f317cba4d93c541e7d828992f Mon Sep 17 00:00:00 2001 From: Shawn <506895667@qq.com> Date: Tue, 17 Aug 2021 11:01:20 +0800 Subject: [PATCH] refactor(config): move APIs for config update,remove,reset to emqx Move the emqx_config:update,remove,reset APIs to emqx, to remove the circular dependency between the modules emqx_config and emqx_config_handler. After this change the dependency among these modules will be: ``` emqx ---> emqx_config | ^ | | + ---> emqx_conifg_handler ``` --- apps/emqx/src/emqx.erl | 41 +++++++++++++++++++ apps/emqx/src/emqx_config.erl | 39 ------------------ apps/emqx/test/emqx_alarm_SUITE.erl | 4 +- apps/emqx_authz/src/emqx_authz.erl | 4 +- apps/emqx_authz/test/emqx_authz_SUITE.erl | 4 +- apps/emqx_authz/test/emqx_authz_api_SUITE.erl | 4 +- .../emqx_authz/test/emqx_authz_http_SUITE.erl | 4 +- .../test/emqx_authz_mongo_SUITE.erl | 4 +- .../test/emqx_authz_mysql_SUITE.erl | 4 +- .../test/emqx_authz_pgsql_SUITE.erl | 4 +- .../test/emqx_authz_redis_SUITE.erl | 4 +- .../emqx_data_bridge/src/emqx_data_bridge.erl | 2 +- .../src/emqx_mgmt_api_configs.erl | 4 +- .../src/emqx_prometheus_api.erl | 2 +- apps/emqx_statsd/src/emqx_statsd_api.erl | 2 +- 15 files changed, 64 insertions(+), 62 deletions(-) diff --git a/apps/emqx/src/emqx.erl b/apps/emqx/src/emqx.erl index 3a5935a8b..ce9036389 100644 --- a/apps/emqx/src/emqx.erl +++ b/apps/emqx/src/emqx.erl @@ -55,6 +55,13 @@ -export([ set_debug_secret/1 ]). +-export([ update_config/2 + , update_config/3 + , remove_config/1 + , remove_config/2 + , reset_config/2 + ]). + -define(APP, ?MODULE). %% @hidden Path to the file which has debug_info encryption secret in it. @@ -184,3 +191,37 @@ run_hook(HookPoint, Args) -> -spec(run_fold_hook(emqx_hooks:hookpoint(), list(any()), any()) -> any()). run_fold_hook(HookPoint, Args, Acc) -> emqx_hooks:run_fold(HookPoint, Args, Acc). + +-spec update_config(emqx_map_lib:config_key_path(), emqx_config:update_request()) -> + {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. +update_config(KeyPath, UpdateReq) -> + update_config(KeyPath, UpdateReq, #{}). + +-spec update_config(emqx_map_lib:config_key_path(), emqx_config:update_request(), + emqx_config:update_opts()) -> + {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. +update_config([RootName | _] = KeyPath, UpdateReq, Opts) -> + emqx_config_handler:update_config(emqx_config:get_schema_mod(RootName), KeyPath, + {{update, UpdateReq}, Opts}). + +-spec remove_config(emqx_map_lib:config_key_path()) -> + {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. +remove_config(KeyPath) -> + remove_config(KeyPath, #{}). + +-spec remove_config(emqx_map_lib:config_key_path(), emqx_config:update_opts()) -> + ok | {error, term()}. +remove_config([RootName | _] = KeyPath, Opts) -> + emqx_config_handler:update_config(emqx_config:get_schema_mod(RootName), + KeyPath, {remove, Opts}). + +-spec reset_config(emqx_map_lib:config_key_path(), emqx_config:update_opts()) -> + {ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}. +reset_config([RootName | _] = KeyPath, Opts) -> + case emqx_config:get_default_value(KeyPath) of + {ok, Default} -> + emqx_config_handler:update_config(emqx_config:get_schema_mod(RootName), KeyPath, + {{update, Default}, Opts}); + {error, _} = Error -> + Error + end. diff --git a/apps/emqx/src/emqx_config.erl b/apps/emqx/src/emqx_config.erl index a86d4b309..1815ad66d 100644 --- a/apps/emqx/src/emqx_config.erl +++ b/apps/emqx/src/emqx_config.erl @@ -61,13 +61,6 @@ , find_listener_conf/3 ]). --export([ update/2 - , update/3 - , remove/1 - , remove/2 - , reset/2 - ]). - -export([ get_raw/1 , get_raw/2 , put_raw/1 @@ -191,38 +184,6 @@ put(Config) -> -spec put(emqx_map_lib:config_key_path(), term()) -> ok. put(KeyPath, Config) -> do_put(?CONF, KeyPath, Config). --spec update(emqx_map_lib:config_key_path(), update_request()) -> - {ok, config(), raw_config()} | {error, term()}. -update(KeyPath, UpdateReq) -> - update(KeyPath, UpdateReq, #{}). - --spec update(emqx_map_lib:config_key_path(), update_request(), - update_opts()) -> - {ok, config(), raw_config()} | {error, term()}. -update([RootName | _] = KeyPath, UpdateReq, Opts) -> - emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath, - {{update, UpdateReq}, Opts}). - --spec remove(emqx_map_lib:config_key_path()) -> {ok, config(), raw_config()} | {error, term()}. -remove(KeyPath) -> - remove(KeyPath, #{}). - --spec remove(emqx_map_lib:config_key_path(), update_opts()) -> - ok | {error, term()}. -remove([RootName | _] = KeyPath, Opts) -> - emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath, {remove, Opts}). - --spec reset(emqx_map_lib:config_key_path(), update_opts()) -> - {ok, config(), raw_config()} | {error, term()}. -reset([RootName | _] = KeyPath, Opts) -> - case get_default_value(KeyPath) of - {ok, Default} -> - emqx_config_handler:update_config(get_schema_mod(RootName), KeyPath, - {{update, Default}, Opts}); - {error, _} = Error -> - Error - end. - -spec get_default_value(emqx_map_lib:config_key_path()) -> {ok, term()} | {error, term()}. get_default_value([RootName | _] = KeyPath) -> BinKeyPath = [bin(Key) || Key <- KeyPath], diff --git a/apps/emqx/test/emqx_alarm_SUITE.erl b/apps/emqx/test/emqx_alarm_SUITE.erl index e797a91d2..b8af41232 100644 --- a/apps/emqx/test/emqx_alarm_SUITE.erl +++ b/apps/emqx/test/emqx_alarm_SUITE.erl @@ -28,14 +28,14 @@ all() -> emqx_ct:all(?MODULE). init_per_testcase(t_size_limit, Config) -> emqx_ct_helpers:boot_modules(all), emqx_ct_helpers:start_apps([]), - {ok, _, _} = emqx_config:update([alarm], #{ + {ok, _, _} = emqx:update_config([alarm], #{ <<"size_limit">> => 2 }), Config; init_per_testcase(t_validity_period, Config) -> emqx_ct_helpers:boot_modules(all), emqx_ct_helpers:start_apps([]), - {ok, _, _} = emqx_config:update([alarm], #{ + {ok, _, _} = emqx:update_config([alarm], #{ <<"validity_period">> => <<"1s">> }), Config; diff --git a/apps/emqx_authz/src/emqx_authz.erl b/apps/emqx_authz/src/emqx_authz.erl index d4399b82d..3f8177c51 100644 --- a/apps/emqx_authz/src/emqx_authz.erl +++ b/apps/emqx_authz/src/emqx_authz.erl @@ -61,10 +61,10 @@ lookup(Id) -> end. move(Id, Position) -> - emqx_config:update(?CONF_KEY_PATH, {move, Id, Position}). + emqx:update_config(?CONF_KEY_PATH, {move, Id, Position}). update(Cmd, Rules) -> - emqx_config:update(?CONF_KEY_PATH, {Cmd, Rules}). + emqx:update_config(?CONF_KEY_PATH, {Cmd, Rules}). pre_config_update({move, Id, <<"top">>}, Conf) when is_list(Conf) -> {Index, _} = find_rule_by_id(Id), diff --git a/apps/emqx_authz/test/emqx_authz_SUITE.erl b/apps/emqx_authz/test/emqx_authz_SUITE.erl index e509542e6..85ef400f7 100644 --- a/apps/emqx_authz/test/emqx_authz_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_SUITE.erl @@ -33,8 +33,8 @@ groups() -> init_per_suite(Config) -> ok = emqx_config:init_load(emqx_authz_schema, ?CONF_DEFAULT), ok = emqx_ct_helpers:start_apps([emqx_authz]), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Config. end_per_suite(_Config) -> diff --git a/apps/emqx_authz/test/emqx_authz_api_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_SUITE.erl index b5d5902d7..23bcac31c 100644 --- a/apps/emqx_authz/test/emqx_authz_api_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_SUITE.erl @@ -76,8 +76,8 @@ init_per_suite(Config) -> ekka_mnesia:start(), emqx_mgmt_auth:mnesia(boot), ok = emqx_ct_helpers:start_apps([emqx_management, emqx_authz], fun set_special_configs/1), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Config. diff --git a/apps/emqx_authz/test/emqx_authz_http_SUITE.erl b/apps/emqx_authz/test/emqx_authz_http_SUITE.erl index 59fdb1eb2..ff04148d9 100644 --- a/apps/emqx_authz/test/emqx_authz_http_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_http_SUITE.erl @@ -35,8 +35,8 @@ init_per_suite(Config) -> ok = emqx_ct_helpers:start_apps([emqx_authz]), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Rules = [#{ <<"config">> => #{ <<"url">> => <<"https://fake.com:443/">>, <<"headers">> => #{}, diff --git a/apps/emqx_authz/test/emqx_authz_mongo_SUITE.erl b/apps/emqx_authz/test/emqx_authz_mongo_SUITE.erl index 8b74d5ec0..5f179c0b0 100644 --- a/apps/emqx_authz/test/emqx_authz_mongo_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_mongo_SUITE.erl @@ -34,8 +34,8 @@ init_per_suite(Config) -> meck:expect(emqx_resource, remove, fun(_) -> ok end ), ok = emqx_ct_helpers:start_apps([emqx_authz]), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Rules = [#{ <<"config">> => #{ <<"mongo_type">> => <<"single">>, <<"server">> => <<"127.0.0.1:27017">>, diff --git a/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl b/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl index cfe64e2fa..61e738041 100644 --- a/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_mysql_SUITE.erl @@ -35,8 +35,8 @@ init_per_suite(Config) -> ok = emqx_ct_helpers:start_apps([emqx_authz]), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Rules = [#{ <<"config">> => #{ <<"server">> => <<"127.0.0.1:27017">>, <<"pool_size">> => 1, diff --git a/apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl b/apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl index a6f62322c..85e8a360a 100644 --- a/apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl @@ -35,8 +35,8 @@ init_per_suite(Config) -> ok = emqx_ct_helpers:start_apps([emqx_authz]), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Rules = [#{ <<"config">> => #{ <<"server">> => <<"127.0.0.1:27017">>, <<"pool_size">> => 1, diff --git a/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl b/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl index 17947e7d9..cf0694449 100644 --- a/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_redis_SUITE.erl @@ -35,8 +35,8 @@ init_per_suite(Config) -> ok = emqx_ct_helpers:start_apps([emqx_authz]), - {ok, _, _} = emqx_config:update([zones, default, authorization, cache, enable], false), - {ok, _, _} = emqx_config:update([zones, default, authorization, enable], true), + {ok, _, _} = emqx:update_config([zones, default, authorization, cache, enable], false), + {ok, _, _} = emqx:update_config([zones, default, authorization, enable], true), Rules = [#{ <<"config">> => #{ <<"server">> => <<"127.0.0.1:27017">>, <<"pool_size">> => 1, diff --git a/apps/emqx_data_bridge/src/emqx_data_bridge.erl b/apps/emqx_data_bridge/src/emqx_data_bridge.erl index 9c27ff8d5..17527ca3a 100644 --- a/apps/emqx_data_bridge/src/emqx_data_bridge.erl +++ b/apps/emqx_data_bridge/src/emqx_data_bridge.erl @@ -60,4 +60,4 @@ config_key_path() -> [emqx_data_bridge, bridges]. update_config(ConfigReq) -> - emqx_config:update(config_key_path(), ConfigReq). + emqx:update_config(config_key_path(), ConfigReq). diff --git a/apps/emqx_management/src/emqx_mgmt_api_configs.erl b/apps/emqx_management/src/emqx_mgmt_api_configs.erl index f8aab5ddd..9a647e174 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_configs.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_configs.erl @@ -113,14 +113,14 @@ config(get, Req) -> config(put, Req) -> Path = conf_path(Req), - {ok, _, RawConf} = emqx_config:update(Path, http_body(Req), + {ok, _, RawConf} = emqx:update_config(Path, http_body(Req), #{rawconf_with_defaults => true}), {200, emqx_map_lib:deep_get(Path, emqx_map_lib:jsonable_map(RawConf))}. config_reset(post, Req) -> %% reset the config specified by the query string param 'conf_path' Path = conf_path_reset(Req) ++ conf_path_from_querystr(Req), - case emqx_config:reset(Path, #{}) of + case emqx:reset_config(Path, #{}) of {ok, _, _} -> {200}; {error, Reason} -> {400, ?ERR_MSG(Reason)} diff --git a/apps/emqx_prometheus/src/emqx_prometheus_api.erl b/apps/emqx_prometheus/src/emqx_prometheus_api.erl index 555311cdc..15762f5de 100644 --- a/apps/emqx_prometheus/src/emqx_prometheus_api.erl +++ b/apps/emqx_prometheus/src/emqx_prometheus_api.erl @@ -113,7 +113,7 @@ prometheus(put, Request) -> {ok, Body, _} = cowboy_req:read_body(Request), Params = emqx_json:decode(Body, [return_maps]), Enable = maps:get(<<"enable">>, Params), - {ok, _, _} = emqx_config:update([prometheus], Params), + {ok, _, _} = emqx:update_config([prometheus], Params), enable_prometheus(Enable). % stats(_Bindings, Params) -> diff --git a/apps/emqx_statsd/src/emqx_statsd_api.erl b/apps/emqx_statsd/src/emqx_statsd_api.erl index d749e26e4..71e72d924 100644 --- a/apps/emqx_statsd/src/emqx_statsd_api.erl +++ b/apps/emqx_statsd/src/emqx_statsd_api.erl @@ -91,7 +91,7 @@ statsd(put, Request) -> {ok, Body, _} = cowboy_req:read_body(Request), Params = emqx_json:decode(Body, [return_maps]), Enable = maps:get(<<"enable">>, Params), - {ok, _, _} = emqx_config:update([statsd], Params), + {ok, _, _} = emqx:update_config([statsd], Params), enable_statsd(Enable). enable_statsd(true) ->