Merge pull request #7653 from JimMoen/rm-generate-response

refactor: mgmt rm `generate_response/1`
This commit is contained in:
JimMoen 2022-04-19 09:39:32 +08:00 committed by GitHub
commit ec29b8381c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 130 additions and 82 deletions

View File

@ -1162,8 +1162,17 @@ delete_user(ChainName, AuthenticatorID, UserID) ->
end. end.
list_users(ChainName, AuthenticatorID, QueryString) -> list_users(ChainName, AuthenticatorID, QueryString) ->
Response = emqx_authentication:list_users(ChainName, AuthenticatorID, QueryString), case emqx_authentication:list_users(ChainName, AuthenticatorID, QueryString) of
emqx_mgmt_util:generate_response(Response). {error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Reason} ->
{400, #{
code => <<"INVALID_PARAMETER">>,
message => list_to_binary(io_lib:format("Reason ~p", [Reason]))
}};
Result ->
{200, Result}
end.
update_config(Path, ConfigRequest) -> update_config(Path, ConfigRequest) ->
emqx_conf:update(Path, ConfigRequest, #{ emqx_conf:update(Path, ConfigRequest, #{

View File

@ -405,14 +405,23 @@ fields(meta) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
users(get, #{query_string := QueryString}) -> users(get, #{query_string := QueryString}) ->
Response = emqx_mgmt_api:node_query( case
node(), emqx_mgmt_api:node_query(
QueryString, node(),
?ACL_TABLE, QueryString,
?ACL_USERNAME_QSCHEMA, ?ACL_TABLE,
?QUERY_USERNAME_FUN ?ACL_USERNAME_QSCHEMA,
), ?QUERY_USERNAME_FUN
emqx_mgmt_util:generate_response(Response); )
of
{error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Result ->
{200, Result}
end;
users(post, #{body := Body}) when is_list(Body) -> users(post, #{body := Body}) when is_list(Body) ->
lists:foreach( lists:foreach(
fun(#{<<"username">> := Username, <<"rules">> := Rules}) -> fun(#{<<"username">> := Username, <<"rules">> := Rules}) ->
@ -423,14 +432,23 @@ users(post, #{body := Body}) when is_list(Body) ->
{204}. {204}.
clients(get, #{query_string := QueryString}) -> clients(get, #{query_string := QueryString}) ->
Response = emqx_mgmt_api:node_query( case
node(), emqx_mgmt_api:node_query(
QueryString, node(),
?ACL_TABLE, QueryString,
?ACL_CLIENTID_QSCHEMA, ?ACL_TABLE,
?QUERY_CLIENTID_FUN ?ACL_CLIENTID_QSCHEMA,
), ?QUERY_CLIENTID_FUN
emqx_mgmt_util:generate_response(Response); )
of
{error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Result ->
{200, Result}
end;
clients(post, #{body := Body}) when is_list(Body) -> clients(post, #{body := Body}) when is_list(Body) ->
lists:foreach( lists:foreach(
fun(#{<<"clientid">> := ClientID, <<"rules">> := Rules}) -> fun(#{<<"clientid">> := ClientID, <<"rules">> := Rules}) ->

View File

@ -101,30 +101,39 @@ clients(get, #{
bindings := #{name := Name0}, bindings := #{name := Name0},
query_string := QString query_string := QString
}) -> }) ->
with_gateway(Name0, fun(GwName, _) -> Fun = fun(GwName, _) ->
TabName = emqx_gateway_cm:tabname(info, GwName), TabName = emqx_gateway_cm:tabname(info, GwName),
case maps:get(<<"node">>, QString, undefined) of Result =
undefined -> case maps:get(<<"node">>, QString, undefined) of
Response = emqx_mgmt_api:cluster_query( undefined ->
QString, emqx_mgmt_api:cluster_query(
TabName, QString,
?CLIENT_QSCHEMA, TabName,
?QUERY_FUN ?CLIENT_QSCHEMA,
), ?QUERY_FUN
emqx_mgmt_util:generate_response(Response); );
Node1 -> Node0 ->
Node = binary_to_atom(Node1, utf8), Node1 = binary_to_atom(Node0, utf8),
QStringWithoutNode = maps:without([<<"node">>], QString), QStringWithoutNode = maps:without([<<"node">>], QString),
Response = emqx_mgmt_api:node_query( emqx_mgmt_api:node_query(
Node, Node1,
QStringWithoutNode, QStringWithoutNode,
TabName, TabName,
?CLIENT_QSCHEMA, ?CLIENT_QSCHEMA,
?QUERY_FUN ?QUERY_FUN
), )
emqx_mgmt_util:generate_response(Response) end,
case Result of
{error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Response ->
{200, Response}
end end
end). end,
with_gateway(Name0, Fun).
clients_insta(get, #{ clients_insta(get, #{
bindings := #{ bindings := #{

View File

@ -91,8 +91,15 @@ alarms(get, #{query_string := QString}) ->
true -> ?ACTIVATED_ALARM; true -> ?ACTIVATED_ALARM;
false -> ?DEACTIVATED_ALARM false -> ?DEACTIVATED_ALARM
end, end,
Response = emqx_mgmt_api:cluster_query(QString, Table, [], {?MODULE, query}), case emqx_mgmt_api:cluster_query(QString, Table, [], {?MODULE, query}) of
emqx_mgmt_util:generate_response(Response); {error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Response ->
{200, Response}
end;
alarms(delete, _Params) -> alarms(delete, _Params) ->
_ = emqx_mgmt:delete_all_deactivated_alarms(), _ = emqx_mgmt:delete_all_deactivated_alarms(),

View File

@ -454,17 +454,24 @@ set_keepalive(put, #{bindings := #{clientid := ClientID}, body := Body}) ->
%% api apply %% api apply
list_clients(QString) -> list_clients(QString) ->
case maps:get(<<"node">>, QString, undefined) of Result = case maps:get(<<"node">>, QString, undefined) of
undefined -> undefined ->
Response = emqx_mgmt_api:cluster_query(QString, ?CLIENT_QTAB, emqx_mgmt_api:cluster_query(QString, ?CLIENT_QTAB,
?CLIENT_QSCHEMA, ?QUERY_FUN), ?CLIENT_QSCHEMA, ?QUERY_FUN);
emqx_mgmt_util:generate_response(Response); Node0 ->
Node1 -> Node1 = binary_to_atom(Node0, utf8),
Node = binary_to_atom(Node1, utf8), QStringWithoutNode = maps:without([<<"node">>], QString),
QStringWithoutNode = maps:without([<<"node">>], QString), emqx_mgmt_api:node_query(Node1, QStringWithoutNode,
Response = emqx_mgmt_api:node_query(Node, QStringWithoutNode, ?CLIENT_QTAB, ?CLIENT_QSCHEMA, ?QUERY_FUN)
?CLIENT_QTAB, ?CLIENT_QSCHEMA, ?QUERY_FUN), end,
emqx_mgmt_util:generate_response(Response) case Result of
{error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Response ->
{200, Response}
end. end.
lookup(#{clientid := ClientID}) -> lookup(#{clientid := ClientID}) ->

View File

@ -113,15 +113,23 @@ parameters() ->
]. ].
subscriptions(get, #{query_string := QString}) -> subscriptions(get, #{query_string := QString}) ->
case maps:get(<<"node">>, QString, undefined) of Response =
undefined -> case maps:get(<<"node">>, QString, undefined) of
Response = emqx_mgmt_api:cluster_query(QString, ?SUBS_QTABLE, undefined ->
?SUBS_QSCHEMA, ?QUERY_FUN), emqx_mgmt_api:cluster_query(QString, ?SUBS_QTABLE,
emqx_mgmt_util:generate_response(Response); ?SUBS_QSCHEMA, ?QUERY_FUN);
Node -> Node0 ->
Response = emqx_mgmt_api:node_query(binary_to_atom(Node, utf8), QString, emqx_mgmt_api:node_query(binary_to_atom(Node0, utf8), QString,
?SUBS_QTABLE, ?SUBS_QSCHEMA, ?QUERY_FUN), ?SUBS_QTABLE, ?SUBS_QSCHEMA, ?QUERY_FUN)
emqx_mgmt_util:generate_response(Response) end,
case Response of
{error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Result ->
{200, Result}
end. end.
format(Items) when is_list(Items) -> format(Items) when is_list(Items) ->

View File

@ -103,9 +103,16 @@ topic(get, #{bindings := Bindings}) ->
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply
do_list(Params) -> do_list(Params) ->
Response = emqx_mgmt_api:node_query( case emqx_mgmt_api:node_query(
node(), Params, emqx_route, ?TOPICS_QUERY_SCHEMA, {?MODULE, query}), node(), Params, emqx_route, ?TOPICS_QUERY_SCHEMA, {?MODULE, query}) of
emqx_mgmt_util:generate_response(Response). {error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Response ->
{200, Response}
end.
lookup(#{topic := Topic}) -> lookup(#{topic := Topic}) ->
case emqx_router:lookup_routes(Topic) of case emqx_router:lookup_routes(Topic) of

View File

@ -43,9 +43,6 @@
, batch_schema/1 , batch_schema/1
]). ]).
-export([generate_response/1]).
-export([urldecode/1]). -export([urldecode/1]).
-define(KB, 1024). -define(KB, 1024).
@ -262,17 +259,3 @@ bad_request() ->
bad_request(<<"Bad Request">>). bad_request(<<"Bad Request">>).
bad_request(Desc) -> bad_request(Desc) ->
object_schema(properties([{message, string}, {code, string}]), Desc). object_schema(properties([{message, string}, {code, string}]), Desc).
%%%==============================================================================================
%% Response util
generate_response(QueryResult) ->
case QueryResult of
{error, page_limit_invalid} ->
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
{error, Node, {badrpc, R}} ->
Message = list_to_binary(io_lib:format("bad rpc call ~p, Reason ~p", [Node, R])),
{500, #{code => <<"NODE_DOWN">>, message => Message}};
Response ->
{200, Response}
end.