Просмотр исходного кода

refactor: mgmt rm `generate_response/1`

JimMoen 3 лет назад
Родитель
Сommit
9e4b9c6689

+ 11 - 2
apps/emqx_authn/src/emqx_authn_api.erl

@@ -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),
-    emqx_mgmt_util:generate_response(Response).
+    case emqx_authentication:list_users(ChainName, AuthenticatorID, QueryString) of
+        {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, #{

+ 34 - 16
apps/emqx_authz/src/emqx_authz_api_mnesia.erl

@@ -405,14 +405,23 @@ fields(meta) ->
 %%--------------------------------------------------------------------
 %%--------------------------------------------------------------------
 
 
 users(get, #{query_string := QueryString}) ->
 users(get, #{query_string := QueryString}) ->
-    Response = emqx_mgmt_api:node_query(
-        node(),
-        QueryString,
-        ?ACL_TABLE,
-        ?ACL_USERNAME_QSCHEMA,
-        ?QUERY_USERNAME_FUN
-    ),
-    emqx_mgmt_util:generate_response(Response);
+    case
+        emqx_mgmt_api:node_query(
+            node(),
+            QueryString,
+            ?ACL_TABLE,
+            ?ACL_USERNAME_QSCHEMA,
+            ?QUERY_USERNAME_FUN
+        )
+    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(
-        node(),
-        QueryString,
-        ?ACL_TABLE,
-        ?ACL_CLIENTID_QSCHEMA,
-        ?QUERY_CLIENTID_FUN
-    ),
-    emqx_mgmt_util:generate_response(Response);
+    case
+        emqx_mgmt_api:node_query(
+            node(),
+            QueryString,
+            ?ACL_TABLE,
+            ?ACL_CLIENTID_QSCHEMA,
+            ?QUERY_CLIENTID_FUN
+        )
+    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}) ->

+ 31 - 22
apps/emqx_gateway/src/emqx_gateway_api_clients.erl

@@ -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
-            undefined ->
-                Response = emqx_mgmt_api:cluster_query(
-                    QString,
-                    TabName,
-                    ?CLIENT_QSCHEMA,
-                    ?QUERY_FUN
-                ),
-                emqx_mgmt_util:generate_response(Response);
-            Node1 ->
-                Node = binary_to_atom(Node1, utf8),
-                QStringWithoutNode = maps:without([<<"node">>], QString),
-                Response = emqx_mgmt_api:node_query(
-                    Node,
-                    QStringWithoutNode,
-                    TabName,
-                    ?CLIENT_QSCHEMA,
-                    ?QUERY_FUN
-                ),
-                emqx_mgmt_util:generate_response(Response)
+        Result =
+            case maps:get(<<"node">>, QString, undefined) of
+                undefined ->
+                    emqx_mgmt_api:cluster_query(
+                        QString,
+                        TabName,
+                        ?CLIENT_QSCHEMA,
+                        ?QUERY_FUN
+                    );
+                Node0 ->
+                    Node1 = binary_to_atom(Node0, utf8),
+                    QStringWithoutNode = maps:without([<<"node">>], QString),
+                    emqx_mgmt_api:node_query(
+                        Node1,
+                        QStringWithoutNode,
+                        TabName,
+                        ?CLIENT_QSCHEMA,
+                        ?QUERY_FUN
+                    )
+            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 := #{

+ 9 - 2
apps/emqx_management/src/emqx_mgmt_api_alarms.erl

@@ -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}),
-    emqx_mgmt_util:generate_response(Response);
+    case emqx_mgmt_api:cluster_query(QString, Table, [], {?MODULE, query}) 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;
 
 
 alarms(delete, _Params) ->
 alarms(delete, _Params) ->
     _ = emqx_mgmt:delete_all_deactivated_alarms(),
     _ = emqx_mgmt:delete_all_deactivated_alarms(),

+ 18 - 11
apps/emqx_management/src/emqx_mgmt_api_clients.erl

@@ -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
-        undefined ->
-            Response = emqx_mgmt_api:cluster_query(QString, ?CLIENT_QTAB,
-                                                   ?CLIENT_QSCHEMA, ?QUERY_FUN),
-            emqx_mgmt_util:generate_response(Response);
-        Node1 ->
-            Node = binary_to_atom(Node1, utf8),
-            QStringWithoutNode = maps:without([<<"node">>], QString),
-            Response = emqx_mgmt_api:node_query(Node, QStringWithoutNode,
-                                                ?CLIENT_QTAB, ?CLIENT_QSCHEMA, ?QUERY_FUN),
-            emqx_mgmt_util:generate_response(Response)
+    Result = case maps:get(<<"node">>, QString, undefined) of
+                 undefined ->
+                     emqx_mgmt_api:cluster_query(QString, ?CLIENT_QTAB,
+                                                 ?CLIENT_QSCHEMA, ?QUERY_FUN);
+                 Node0 ->
+                     Node1 = binary_to_atom(Node0, utf8),
+                     QStringWithoutNode = maps:without([<<"node">>], QString),
+                     emqx_mgmt_api:node_query(Node1, QStringWithoutNode,
+                                              ?CLIENT_QTAB, ?CLIENT_QSCHEMA, ?QUERY_FUN)
+             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.
 
 
 lookup(#{clientid := ClientID}) ->
 lookup(#{clientid := ClientID}) ->

+ 17 - 9
apps/emqx_management/src/emqx_mgmt_api_subscriptions.erl

@@ -113,15 +113,23 @@ parameters() ->
     ].
     ].
 
 
 subscriptions(get, #{query_string := QString}) ->
 subscriptions(get, #{query_string := QString}) ->
-    case maps:get(<<"node">>, QString, undefined) of
-        undefined ->
-            Response = emqx_mgmt_api:cluster_query(QString, ?SUBS_QTABLE,
-                                                   ?SUBS_QSCHEMA, ?QUERY_FUN),
-            emqx_mgmt_util:generate_response(Response);
-        Node ->
-            Response = emqx_mgmt_api:node_query(binary_to_atom(Node, utf8), QString,
-                                                ?SUBS_QTABLE, ?SUBS_QSCHEMA, ?QUERY_FUN),
-            emqx_mgmt_util:generate_response(Response)
+    Response =
+        case maps:get(<<"node">>, QString, undefined) of
+            undefined ->
+                emqx_mgmt_api:cluster_query(QString, ?SUBS_QTABLE,
+                                            ?SUBS_QSCHEMA, ?QUERY_FUN);
+            Node0 ->
+                emqx_mgmt_api:node_query(binary_to_atom(Node0, utf8), QString,
+                                         ?SUBS_QTABLE, ?SUBS_QSCHEMA, ?QUERY_FUN)
+        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) ->

+ 10 - 3
apps/emqx_management/src/emqx_mgmt_api_topics.erl

@@ -103,9 +103,16 @@ topic(get, #{bindings := Bindings}) ->
 %%%==============================================================================================
 %%%==============================================================================================
 %% api apply
 %% api apply
 do_list(Params) ->
 do_list(Params) ->
-    Response = emqx_mgmt_api:node_query(
-        node(), Params, emqx_route, ?TOPICS_QUERY_SCHEMA, {?MODULE, query}),
-    emqx_mgmt_util:generate_response(Response).
+    case emqx_mgmt_api:node_query(
+        node(), Params, emqx_route, ?TOPICS_QUERY_SCHEMA, {?MODULE, query}) 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.
 
 
 lookup(#{topic := Topic}) ->
 lookup(#{topic := Topic}) ->
     case emqx_router:lookup_routes(Topic) of
     case emqx_router:lookup_routes(Topic) of

+ 0 - 17
apps/emqx_management/src/emqx_mgmt_util.erl

@@ -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.