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
```
This commit is contained in:
Shawn 2021-08-17 11:01:20 +08:00
parent a2067c3bf3
commit bd8263e324
15 changed files with 64 additions and 62 deletions

View File

@ -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.

View File

@ -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],

View File

@ -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;

View File

@ -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),

View File

@ -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) ->

View File

@ -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.

View File

@ -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">> => #{},

View File

@ -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">>,

View File

@ -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,

View File

@ -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,

View File

@ -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,

View File

@ -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).

View File

@ -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)}

View File

@ -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) ->

View File

@ -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) ->