refactor(config): rename emqx_config:update_config/2 to emqx_config:update/2

refactor(config): rename emqx_config:update_config/2 to emqx_config:update/2
This commit is contained in:
turtleDeng 2021-07-26 09:14:13 +08:00 committed by GitHub
commit cafed47da6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 53 additions and 53 deletions

View File

@ -30,7 +30,7 @@
-boot_mnesia({mnesia, [boot]}).
-copy_mnesia({mnesia, [copy]}).
-export([handle_update_config/2]).
-export([pre_config_update/2]).
-export([ start_link/0
, stop/0
@ -153,10 +153,10 @@ get_alarms(activated) ->
get_alarms(deactivated) ->
gen_server:call(?MODULE, {get_alarms, deactivated}).
handle_update_config(#{<<"validity_period">> := Period0} = NewConf, OldConf) ->
pre_config_update(#{<<"validity_period">> := Period0} = NewConf, OldConf) ->
?MODULE ! {update_timer, hocon_postprocess:duration(Period0)},
maps:merge(OldConf, NewConf);
handle_update_config(NewConf, OldConf) ->
pre_config_update(NewConf, OldConf) ->
maps:merge(OldConf, NewConf).
format(#activated_alarm{name = Name, message = Message, activate_at = At, details = Details}) ->

View File

@ -46,7 +46,7 @@
, find_listener_conf/3
]).
-export([ update_config/2
-export([ update/2
]).
%% raw configs is the config that is now parsed and tranlated by hocon schema
@ -126,9 +126,9 @@ put(Config) ->
put(KeyPath, Config) ->
put(emqx_map_lib:deep_put(KeyPath, get(), Config)).
-spec update_config(emqx_map_lib:config_key_path(), update_request()) ->
-spec update(emqx_map_lib:config_key_path(), update_request()) ->
ok | {error, term()}.
update_config(ConfKeyPath, UpdateReq) ->
update(ConfKeyPath, UpdateReq) ->
emqx_config_handler:update_config(ConfKeyPath, UpdateReq, get_raw()).
-spec get_raw() -> map().

View File

@ -29,7 +29,7 @@
]).
%% emqx_config_handler callbacks
-export([ handle_update_config/2
-export([ pre_config_update/2
]).
%% gen_server callbacks
@ -45,14 +45,14 @@
-type handler_name() :: module().
-type handlers() :: #{emqx_config:config_key() => handlers(), ?MOD => handler_name()}.
-optional_callbacks([ handle_update_config/2
, post_update_config/2
-optional_callbacks([ pre_config_update/2
, post_config_update/2
]).
-callback handle_update_config(emqx_config:update_request(), emqx_config:raw_config()) ->
-callback pre_config_update(emqx_config:update_request(), emqx_config:raw_config()) ->
emqx_config:update_request().
-callback post_update_config(emqx_config:config(), emqx_config:config()) -> any().
-callback post_config_update(emqx_config:config(), emqx_config:config()) -> any().
-type state() :: #{
handlers := handlers(),
@ -88,7 +88,7 @@ handle_call({update_config, ConfKeyPath, UpdateReq, RawConf}, _From,
OldConf = emqx_config:get(),
try {RootKeys, Conf} = do_update_config(ConfKeyPath, Handlers, RawConf, UpdateReq),
Result = emqx_config:save_configs(Conf, #{overridden_keys => RootKeys}),
do_post_update_config(ConfKeyPath, Handlers, OldConf, emqx_config:get()),
do_post_config_update(ConfKeyPath, Handlers, OldConf, emqx_config:get()),
{reply, Result, State}
catch
Error : Reason : ST ->
@ -113,43 +113,43 @@ code_change(_OldVsn, State, _Extra) ->
{ok, State}.
do_update_config([], Handlers, OldRawConf, UpdateReq) ->
call_handle_update_config(Handlers, OldRawConf, UpdateReq);
call_pre_config_update(Handlers, OldRawConf, UpdateReq);
do_update_config([ConfKey | ConfKeyPath], Handlers, OldRawConf, UpdateReq) ->
SubOldRawConf = get_sub_config(bin(ConfKey), OldRawConf),
SubHandlers = maps:get(ConfKey, Handlers, #{}),
NewUpdateReq = do_update_config(ConfKeyPath, SubHandlers, SubOldRawConf, UpdateReq),
call_handle_update_config(Handlers, OldRawConf, #{bin(ConfKey) => NewUpdateReq}).
call_pre_config_update(Handlers, OldRawConf, #{bin(ConfKey) => NewUpdateReq}).
do_post_update_config([], Handlers, OldConf, NewConf) ->
call_post_update_config(Handlers, OldConf, NewConf);
do_post_update_config([ConfKey | ConfKeyPath], Handlers, OldConf, NewConf) ->
do_post_config_update([], Handlers, OldConf, NewConf) ->
call_post_config_update(Handlers, OldConf, NewConf);
do_post_config_update([ConfKey | ConfKeyPath], Handlers, OldConf, NewConf) ->
SubOldConf = get_sub_config(ConfKey, OldConf),
SubNewConf = get_sub_config(ConfKey, NewConf),
SubHandlers = maps:get(ConfKey, Handlers, #{}),
_ = do_post_update_config(ConfKeyPath, SubHandlers, SubOldConf, SubNewConf),
call_post_update_config(Handlers, OldConf, NewConf).
_ = do_post_config_update(ConfKeyPath, SubHandlers, SubOldConf, SubNewConf),
call_post_config_update(Handlers, OldConf, NewConf).
get_sub_config(ConfKey, Conf) when is_map(Conf) ->
maps:get(ConfKey, Conf, undefined);
get_sub_config(_, _Conf) -> %% the Conf is a primitive
undefined.
call_handle_update_config(Handlers, OldRawConf, UpdateReq) ->
call_pre_config_update(Handlers, OldRawConf, UpdateReq) ->
HandlerName = maps:get(?MOD, Handlers, undefined),
case erlang:function_exported(HandlerName, handle_update_config, 2) of
true -> HandlerName:handle_update_config(UpdateReq, OldRawConf);
case erlang:function_exported(HandlerName, pre_config_update, 2) of
true -> HandlerName:pre_config_update(UpdateReq, OldRawConf);
false -> merge_to_old_config(UpdateReq, OldRawConf)
end.
call_post_update_config(Handlers, OldConf, NewConf) ->
call_post_config_update(Handlers, OldConf, NewConf) ->
HandlerName = maps:get(?MOD, Handlers, undefined),
case erlang:function_exported(HandlerName, post_update_config, 2) of
true -> _ = HandlerName:post_update_config(NewConf, OldConf);
case erlang:function_exported(HandlerName, post_config_update, 2) of
true -> _ = HandlerName:post_config_update(NewConf, OldConf);
false -> ok
end.
%% callbacks for the top-level handler
handle_update_config(UpdateReq, OldConf) ->
pre_config_update(UpdateReq, OldConf) ->
FullRawConf = merge_to_old_config(UpdateReq, OldConf),
{maps:keys(UpdateReq), FullRawConf}.

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([]),
emqx_config:update_config([alarm], #{
emqx_config:update([alarm], #{
<<"size_limit">> => 2
}),
Config;
init_per_testcase(t_validity_period, Config) ->
emqx_ct_helpers:boot_modules(all),
emqx_ct_helpers:start_apps([]),
emqx_config:update_config([alarm], #{
emqx_config:update([alarm], #{
<<"validity_period">> => <<"1s">>
}),
Config;

View File

@ -31,7 +31,7 @@
, match/4
]).
-export([post_update_config/2, handle_update_config/2]).
-export([post_config_update/2, pre_config_update/2]).
-define(CONF_KEY_PATH, [emqx_authz, rules]).
@ -49,24 +49,24 @@ lookup() ->
emqx_config:get(?CONF_KEY_PATH, []).
update(Cmd, Rules) ->
emqx_config:update_config(?CONF_KEY_PATH, {Cmd, Rules}).
emqx_config:update(?CONF_KEY_PATH, {Cmd, Rules}).
%% For now we only support re-creating the entire rule list
handle_update_config({head, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
pre_config_update({head, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
[Rule | OldConf];
handle_update_config({tail, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
pre_config_update({tail, Rule}, OldConf) when is_map(Rule), is_list(OldConf) ->
OldConf ++ [Rule];
handle_update_config({_, NewConf}, _OldConf) ->
pre_config_update({_, NewConf}, _OldConf) ->
%% overwrite the entire config!
case is_list(NewConf) of
true -> NewConf;
false -> [NewConf]
end.
post_update_config(undefined, _OldConf) ->
post_config_update(undefined, _OldConf) ->
%_ = [release_rules(Rule) || Rule <- OldConf],
ok;
post_update_config(NewRules, _OldConf) ->
post_config_update(NewRules, _OldConf) ->
%_ = [release_rules(Rule) || Rule <- OldConf],
InitedRules = [init_rule(Rule) || Rule <- NewRules],
Action = find_action_in_hooks(),

View File

@ -30,8 +30,8 @@ groups() ->
init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]),
ok = emqx_config:update_config([zones, default, authorization, cache, enable], false),
ok = emqx_config:update_config([zones, default, authorization, enable], true),
ok = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true),
emqx_authz:update(replace, []),
Config.

View File

@ -36,8 +36,8 @@ init_per_suite(Config) ->
meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_resource, create, fun(_, _, _) -> {ok, meck_data} end ),
ok = emqx_ct_helpers:start_apps([emqx_authz]),
ok = emqx_config:update_config([zones, default, authorization, cache, enable], false),
ok = emqx_config:update_config([zones, default, authorization, enable], true),
ok = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"url">> => <<"https://fake.com:443/">>,
<<"headers">> => #{},

View File

@ -38,8 +38,8 @@ init_per_suite(Config) ->
ok = emqx_ct_helpers:start_apps([emqx_authz]),
ct:pal("---- emqx_hooks: ~p", [ets:tab2list(emqx_hooks)]),
ok = emqx_config:update_config([zones, default, authorization, cache, enable], false),
ok = emqx_config:update_config([zones, default, authorization, enable], true),
ok = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"mongo_type">> => <<"single">>,
<<"server">> => <<"127.0.0.1:27017">>,

View File

@ -36,8 +36,8 @@ init_per_suite(Config) ->
meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_resource, create, fun(_, _, _) -> {ok, meck_data} end ),
ok = emqx_ct_helpers:start_apps([emqx_authz]),
ok = emqx_config:update_config([zones, default, authorization, cache, enable], false),
ok = emqx_config:update_config([zones, default, authorization, enable], true),
ok = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1,

View File

@ -36,8 +36,8 @@ init_per_suite(Config) ->
meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_resource, create, fun(_, _, _) -> {ok, meck_data} end ),
ok = emqx_ct_helpers:start_apps([emqx_authz]),
ok = emqx_config:update_config([zones, default, authorization, cache, enable], false),
ok = emqx_config:update_config([zones, default, authorization, enable], true),
ok = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([zones, default, authorization, enable], true),
Rules = [#{ <<"config">> => #{
<<"server">> => <<"127.0.0.1:27017">>,
<<"pool_size">> => 1,

View File

@ -36,8 +36,8 @@ init_per_suite(Config) ->
meck:new(emqx_resource, [non_strict, passthrough, no_history, no_link]),
meck:expect(emqx_resource, create, fun(_, _, _) -> {ok, meck_data} end ),
ok = emqx_ct_helpers:start_apps([emqx_authz]),
ok = emqx_config:update_config([zones, default, authorization, cache, enable], false),
ok = emqx_config:update_config([zones, default, authorization, enable], true),
ok = emqx_config:update([zones, default, authorization, cache, enable], false),
ok = emqx_config:update([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(config_key_path(), ConfigReq).
emqx_config:update(config_key_path(), ConfigReq).

View File

@ -19,7 +19,7 @@
-behaviour(emqx_config_handler).
-export([start/2, stop/1, handle_update_config/2]).
-export([start/2, stop/1, pre_config_update/2]).
start(_StartType, _StartArgs) ->
{ok, Sup} = emqx_data_bridge_sup:start_link(),
@ -31,11 +31,11 @@ stop(_State) ->
ok.
%% internal functions
handle_update_config({update, Bridge = #{<<"name">> := Name}}, OldConf) ->
pre_config_update({update, Bridge = #{<<"name">> := Name}}, OldConf) ->
[Bridge | remove_bridge(Name, OldConf)];
handle_update_config({delete, Name}, OldConf) ->
pre_config_update({delete, Name}, OldConf) ->
remove_bridge(Name, OldConf);
handle_update_config(NewConf, _OldConf) when is_list(NewConf) ->
pre_config_update(NewConf, _OldConf) when is_list(NewConf) ->
%% overwrite the entire config!
NewConf.

View File

@ -92,7 +92,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_config([emqx_statsd], Params),
ok = emqx_config:update([emqx_statsd], Params),
enable_statsd(Enable).
enable_statsd(true) ->