feat(emqx_config): return config maps when emqx_config:update/2,3

This commit is contained in:
Shawn 2021-08-13 18:48:45 +08:00
parent 26c0754732
commit b381b5d2b9
3 changed files with 42 additions and 24 deletions

View File

@ -65,7 +65,7 @@
, update/3
, remove/1
, remove/2
, reset/1
, reset/2
]).
-export([ get_raw/1
@ -184,27 +184,31 @@ put(KeyPath, Config) -> do_put(?CONF, KeyPath, Config).
-spec update(emqx_map_lib:config_key_path(), update_request()) ->
ok | {error, term()}.
update([RootName | _] = KeyPath, UpdateReq) ->
update(get_schema_mod(RootName), KeyPath, UpdateReq).
update(KeyPath, UpdateReq) ->
update(KeyPath, UpdateReq, #{}).
-spec update(module(), emqx_map_lib:config_key_path(), update_request()) ->
ok | {error, term()}.
update(SchemaMod, KeyPath, UpdateReq) ->
emqx_config_handler:update_config(SchemaMod, KeyPath, {update, UpdateReq}).
-spec update(emqx_map_lib:config_key_path(), update_request(),
emqx_config_handler:update_opts()) -> ok | {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 | {error, term()}.
remove([RootName | _] = KeyPath) ->
remove(get_schema_mod(RootName), KeyPath).
remove(KeyPath) ->
remove(KeyPath, #{}).
remove(SchemaMod, KeyPath) ->
emqx_config_handler:update_config(SchemaMod, KeyPath, remove).
-spec remove(emqx_map_lib:config_key_path(), emqx_config_handler: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()) -> ok | {error, term()}.
reset([RootName | _] = KeyPath) ->
-spec reset(emqx_map_lib:config_key_path(), emqx_config_handler:update_opts()) ->
ok | {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});
{{update, Default}, Opts});
{error, _} = Error ->
Error
end.

View File

@ -38,9 +38,15 @@
-define(MOD, {mod}).
-export_type([update_opts/0, update_cmd/0, update_args/0]).
-type handler_name() :: module().
-type handlers() :: #{emqx_config:config_key() => handlers(), ?MOD => handler_name()}.
-type update_args() :: {update, emqx_config:update_request()} | remove.
-type update_cmd() :: {update, emqx_config:update_request()} | remove.
-type update_opts() :: #{
%% fill the default values into the rawconf map
rawconf_with_defaults => boolean()
}.
-type update_args() :: {update_cmd(), Opts :: update_opts()}.
-optional_callbacks([ pre_config_update/2
, post_config_update/3
@ -61,7 +67,7 @@ start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, {}, []).
-spec update_config(module(), emqx_config:config_key_path(), update_args()) ->
ok | {error, term()}.
{ok, emqx_config:config(), emqx_config:raw_config()} | {error, term()}.
update_config(SchemaModule, ConfKeyPath, UpdateArgs) ->
gen_server:call(?MODULE, {change_config, SchemaModule, ConfKeyPath, UpdateArgs}).
@ -80,7 +86,7 @@ handle_call({add_child, ConfKeyPath, HandlerName}, _From,
{reply, ok, State#{handlers =>
emqx_map_lib:deep_put(ConfKeyPath, Handlers, #{?MOD => HandlerName})}};
handle_call({change_config, SchemaModule, ConfKeyPath, UpdateArgs}, _From,
handle_call({change_config, SchemaModule, ConfKeyPath, {_Cmd, Opts} = UpdateArgs}, _From,
#{handlers := Handlers} = State) ->
OldConf = emqx_config:get([]),
OldRawConf = emqx_config:get_raw([]),
@ -89,7 +95,10 @@ handle_call({change_config, SchemaModule, ConfKeyPath, UpdateArgs}, _From,
Handlers, UpdateArgs),
{AppEnvs, CheckedConf} = emqx_config:check_config(SchemaModule, NewRawConf),
_ = do_post_config_update(ConfKeyPath, Handlers, OldConf, CheckedConf, UpdateArgs),
emqx_config:save_configs(AppEnvs, CheckedConf, NewRawConf, OverrideConf)
case emqx_config:save_configs(AppEnvs, CheckedConf, NewRawConf, OverrideConf) of
ok -> {ok, emqx_config:get([]), return_rawconf(Opts)};
Err -> Err
end
catch Error:Reason:ST ->
?LOG(error, "change_config failed: ~p", [{Error, Reason, ST}]),
{error, Reason}
@ -112,12 +121,12 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
process_upadate_request(ConfKeyPath, OldRawConf, _Handlers, remove) ->
process_upadate_request(ConfKeyPath, OldRawConf, _Handlers, {remove, _Opts}) ->
BinKeyPath = bin_path(ConfKeyPath),
NewRawConf = emqx_map_lib:deep_remove(BinKeyPath, OldRawConf),
OverrideConf = emqx_map_lib:deep_remove(BinKeyPath, emqx_config:read_override_conf()),
{NewRawConf, OverrideConf};
process_upadate_request(ConfKeyPath, OldRawConf, Handlers, {update, UpdateReq}) ->
process_upadate_request(ConfKeyPath, OldRawConf, Handlers, {{update, UpdateReq}, _Opts}) ->
NewRawConf = do_update_config(ConfKeyPath, Handlers, OldRawConf, UpdateReq),
OverrideConf = update_override_config(NewRawConf),
{NewRawConf, OverrideConf}.
@ -172,8 +181,13 @@ update_override_config(RawConf) ->
OldConf = emqx_config:read_override_conf(),
maps:merge(OldConf, RawConf).
up_req(remove) -> '$remove';
up_req({update, Req}) -> Req.
up_req({remove, _Opts}) -> '$remove';
up_req({{update, Req}, _Opts}) -> Req.
return_rawconf(#{rawconf_with_defaults := true}) ->
emqx_config:fill_defaults(emqx_config:get_raw([]));
return_rawconf(_) ->
emqx_config:get_raw([]).
bin_path(ConfKeyPath) -> [bin(Key) || Key <- ConfKeyPath].

View File

@ -61,10 +61,10 @@ lookup(Id) ->
end.
move(Id, Position) ->
emqx_config:update(emqx_authz_schema, ?CONF_KEY_PATH, {move, Id, Position}).
emqx_config:update(?CONF_KEY_PATH, {move, Id, Position}).
update(Cmd, Rules) ->
emqx_config:update(emqx_authz_schema, ?CONF_KEY_PATH, {Cmd, Rules}).
emqx_config:update(?CONF_KEY_PATH, {Cmd, Rules}).
pre_config_update({move, Id, <<"top">>}, Conf) when is_list(Conf) ->
{Index, _} = find_rule_by_id(Id),