From 566d6f8d74a5b2525fbe0de25b0b06f401d924fa Mon Sep 17 00:00:00 2001
From: Shawn <506895667@qq.com>
Date: Tue, 10 Aug 2021 12:24:50 +0800
Subject: [PATCH] refactor(config): create a dedicate API for config reset
---
.../src/emqx_mgmt_api_configs.erl | 41 +++++++++++--------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git a/apps/emqx_management/src/emqx_mgmt_api_configs.erl b/apps/emqx_management/src/emqx_mgmt_api_configs.erl
index 7e1c1af8e..15bc8af11 100644
--- a/apps/emqx_management/src/emqx_mgmt_api_configs.erl
+++ b/apps/emqx_management/src/emqx_mgmt_api_configs.erl
@@ -21,6 +21,7 @@
-export([api_spec/0]).
-export([ config/2
+ , config_reset/2
]).
-define(PARAM_CONF_PATH, [#{
@@ -41,6 +42,7 @@
}).
-define(PREFIX, "/configs").
+-define(PREFIX_RESET, "/configs_reset").
-define(MAX_DEPTH, 1).
@@ -81,51 +83,56 @@ config_api(ConfPath, Schema) ->
config_reset_api() ->
Metadata = #{
- delete => #{
- description => <<"Reset or remove the config entry specified by the query string parameter `conf_path`.
+ post => #{
+ description => <<"Reset the config entry specified by the query string parameter `conf_path`.
- 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,
responses => #{
<<"200">> => emqx_mgmt_util:response_schema(<<"Remove configs successfully">>),
<<"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
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} ->
{200, Conf};
{not_found, _, _} ->
{404, #{code => 'NOT_FOUND', message => <<"Config cannot found">>}}
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) ->
- Path = conf_path_from_http_path(Req),
+ Path = conf_path(Req),
ok = emqx_config:update(Path, http_body(Req)),
{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) ->
case proplists:get_value(<<"conf_path">>, cowboy_req:parse_qs(Req)) of
undefined -> [];
Path -> string:lexemes(Path, ". ")
end.
-conf_path_from_http_path(Req) ->
- <<"/api/v5", ?PREFIX, "/", Path/binary>> = cowboy_req:path(Req),
+conf_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, "/ ").
http_body(Req) ->