feat(config): improve the API for resetting configs

This commit is contained in:
Shawn 2021-08-11 21:30:05 +08:00
parent 1c86bd6199
commit 8dbb14b668
1 changed files with 11 additions and 6 deletions

View File

@ -89,7 +89,7 @@ config_reset_api() ->
- For a config entry that has no default value, an error 400 will be returned">>, - For a config entry that has no default value, an error 400 will be returned">>,
parameters => ?PARAM_CONF_PATH, parameters => ?PARAM_CONF_PATH,
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_schema(<<"Remove configs successfully">>), <<"200">> => emqx_mgmt_util:response_schema(<<"Reset configs successfully">>),
<<"400">> => emqx_mgmt_util:response_error_schema( <<"400">> => emqx_mgmt_util:response_error_schema(
<<"It's not able to reset the config">>, ['INVALID_OPERATION']) <<"It's not able to reset the config">>, ['INVALID_OPERATION'])
} }
@ -100,7 +100,8 @@ config_reset_api() ->
%%%============================================================================================== %%%==============================================================================================
%% parameters trans %% parameters trans
config(get, Req) -> config(get, Req) ->
case emqx_config:find_raw(conf_path(Req)) of Path = conf_path(Req),
case emqx_map_lib:deep_find(Path, get_full_config()) of
{ok, Conf} -> {ok, Conf} ->
{200, Conf}; {200, Conf};
{not_found, _, _} -> {not_found, _, _} ->
@ -110,17 +111,21 @@ config(get, Req) ->
config(put, Req) -> config(put, Req) ->
Path = conf_path(Req), Path = conf_path(Req),
ok = emqx_config:update(Path, http_body(Req)), ok = emqx_config:update(Path, http_body(Req)),
{200, emqx_config:get_raw(Path)}. {200, emqx_map_lib:deep_get(Path, get_full_config())}.
config_reset(post, Req) -> config_reset(post, Req) ->
%% reset the config specified by the query string param 'conf_path' %% reset the config specified by the query string param 'conf_path'
Path = conf_path_reset(Req), Path = conf_path_reset(Req) ++ conf_path_from_querystr(Req),
case emqx_config:remove(Path ++ conf_path_from_querystr(Req)) of case emqx_config:reset(Path) of
ok -> {200, emqx_config:get_raw(Path)}; ok -> {200};
{error, Reason} -> {error, Reason} ->
{400, ?ERR_MSG(Reason)} {400, ?ERR_MSG(Reason)}
end. end.
get_full_config() ->
emqx_map_lib:jsonable_map(
emqx_config:fill_defaults(emqx_config:get_raw([]))).
conf_path_from_querystr(Req) -> conf_path_from_querystr(Req) ->
case proplists:get_value(<<"conf_path">>, cowboy_req:parse_qs(Req)) of case proplists:get_value(<<"conf_path">>, cowboy_req:parse_qs(Req)) of
undefined -> []; undefined -> [];