refactor(config): create a dedicate API for config reset

This commit is contained in:
Shawn 2021-08-10 12:24:50 +08:00
parent a4b773b66f
commit 566d6f8d74
1 changed files with 24 additions and 17 deletions

View File

@ -21,6 +21,7 @@
-export([api_spec/0]). -export([api_spec/0]).
-export([ config/2 -export([ config/2
, config_reset/2
]). ]).
-define(PARAM_CONF_PATH, [#{ -define(PARAM_CONF_PATH, [#{
@ -41,6 +42,7 @@
}). }).
-define(PREFIX, "/configs"). -define(PREFIX, "/configs").
-define(PREFIX_RESET, "/configs_reset").
-define(MAX_DEPTH, 1). -define(MAX_DEPTH, 1).
@ -81,51 +83,56 @@ config_api(ConfPath, Schema) ->
config_reset_api() -> config_reset_api() ->
Metadata = #{ Metadata = #{
delete => #{ post => #{
description => <<"Reset or remove the config entry specified by the query string parameter `conf_path`.<br/> description => <<"Reset the config entry specified by the query string parameter `conf_path`.<br/>
- For a config entry that has default value, this resets it to the default value; - For a config entry that has default value, this resets it to the default value;
- For a config entry that is dynamic such as a listener Id, this will remove the config entry">>, - 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(<<"Remove configs successfully">>),
<<"400">> => emqx_mgmt_util:response_error_schema( <<"400">> => emqx_mgmt_util:response_error_schema(
<<"It's not able to remove the config">>, ['INVALID_OPERATION']) <<"It's not able to reset the config">>, ['INVALID_OPERATION'])
} }
} }
}, },
{?PREFIX, Metadata, config}. {?PREFIX_RESET, Metadata, config_reset}.
%%%============================================================================================== %%%==============================================================================================
%% parameters trans %% parameters trans
config(get, Req) -> config(get, Req) ->
case emqx_config:find_raw(conf_path_from_http_path(Req)) of case emqx_config:find_raw(conf_path(Req)) of
{ok, Conf} -> {ok, Conf} ->
{200, Conf}; {200, Conf};
{not_found, _, _} -> {not_found, _, _} ->
{404, #{code => 'NOT_FOUND', message => <<"Config cannot found">>}} {404, #{code => 'NOT_FOUND', message => <<"Config cannot found">>}}
end; end;
config(delete, Req) ->
%% remove the config specified by the query string param 'conf_path'
case emqx_config:remove(conf_path_from_http_path(Req) ++ conf_path_from_querystr(Req)) of
ok -> {200};
{error, Reason} ->
{400, ?ERR_MSG(Reason)}
end;
config(put, Req) -> config(put, Req) ->
Path = conf_path_from_http_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_config:get_raw(Path)}.
config_reset(post, Req) ->
%% reset the config specified by the query string param 'conf_path'
Path = conf_path_reset(Req),
case emqx_config:remove(Path ++ conf_path_from_querystr(Req)) of
ok -> {200, emqx_config:get_raw(Path)};
{error, Reason} ->
{400, ?ERR_MSG(Reason)}
end.
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 -> [];
Path -> string:lexemes(Path, ". ") Path -> string:lexemes(Path, ". ")
end. end.
conf_path_from_http_path(Req) -> conf_path(Req) ->
<<"/api/v5", ?PREFIX, "/", Path/binary>> = cowboy_req:path(Req), <<"/api/v5", ?PREFIX, Path/binary>> = cowboy_req:path(Req),
string:lexemes(Path, "/ ").
conf_path_reset(Req) ->
<<"/api/v5", ?PREFIX_RESET, Path/binary>> = cowboy_req:path(Req),
string:lexemes(Path, "/ "). string:lexemes(Path, "/ ").
http_body(Req) -> http_body(Req) ->