Merge pull request #10014 from sstrigler/EMQX-7822-5-0-get-api-v-5-monitor-nodes-node-must-return-404-when-node-is-not-responding

fix: return `404` for unknown node names
This commit is contained in:
Stefan Strigler 2023-02-21 17:09:03 +01:00 committed by GitHub
commit 967369075f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 30 deletions

View File

@ -55,7 +55,7 @@ schema("/monitor/nodes/:node") ->
parameters => [parameter_node(), parameter_latest()],
responses => #{
200 => hoconsc:mk(hoconsc:array(hoconsc:ref(sampler)), #{}),
400 => emqx_dashboard_swagger:error_codes(['BAD_RPC'], <<"Bad RPC">>)
404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"Node not found">>)
}
}
};
@ -79,7 +79,7 @@ schema("/monitor_current/nodes/:node") ->
parameters => [parameter_node()],
responses => #{
200 => hoconsc:mk(hoconsc:ref(sampler_current), #{}),
400 => emqx_dashboard_swagger:error_codes(['BAD_RPC'], <<"Bad RPC">>)
404 => emqx_dashboard_swagger:error_codes(['NOT_FOUND'], <<"Node not found">>)
}
}
}.
@ -122,38 +122,31 @@ fields(sampler_current) ->
monitor(get, #{query_string := QS, bindings := Bindings}) ->
Latest = maps:get(<<"latest">>, QS, infinity),
RawNode = maps:get(node, Bindings, all),
case emqx_misc:safe_to_existing_atom(RawNode, utf8) of
{ok, Node} ->
case emqx_dashboard_monitor:samplers(Node, Latest) of
{badrpc, {Node, Reason}} ->
Message = list_to_binary(
io_lib:format("Bad node ~p, rpc failed ~p", [Node, Reason])
),
{400, 'BAD_RPC', Message};
Samplers ->
{200, Samplers}
end;
_ ->
Message = list_to_binary(io_lib:format("Bad node ~p", [RawNode])),
{400, 'BAD_RPC', Message}
with_node(RawNode, dashboard_samplers_fun(Latest)).
dashboard_samplers_fun(Latest) ->
fun(NodeOrCluster) ->
case emqx_dashboard_monitor:samplers(NodeOrCluster, Latest) of
{badrpc, _} = Error -> Error;
Samplers -> {ok, Samplers}
end
end.
monitor_current(get, #{bindings := Bindings}) ->
RawNode = maps:get(node, Bindings, all),
with_node(RawNode, fun emqx_dashboard_monitor:current_rate/1).
with_node(RawNode, Fun) ->
case emqx_misc:safe_to_existing_atom(RawNode, utf8) of
{ok, NodeOrCluster} ->
case emqx_dashboard_monitor:current_rate(NodeOrCluster) of
{ok, CurrentRate} ->
{200, CurrentRate};
case Fun(NodeOrCluster) of
{badrpc, {Node, Reason}} ->
Message = list_to_binary(
io_lib:format("Bad node ~p, rpc failed ~p", [Node, Reason])
),
{400, 'BAD_RPC', Message}
{404, 'NOT_FOUND', io_lib:format("Node not found: ~p (~p)", [Node, Reason])};
{ok, Result} ->
{200, Result}
end;
{error, _} ->
Message = list_to_binary(io_lib:format("Bad node ~p", [RawNode])),
{400, 'BAD_RPC', Message}
_Error ->
{404, 'NOT_FOUND', io_lib:format("Node not found: ~p", [RawNode])}
end.
%% -------------------------------------------------------------------------------------------------

View File

@ -22,8 +22,6 @@
-import(emqx_dashboard_SUITE, [auth_header_/0]).
-include_lib("eunit/include/eunit.hrl").
-include_lib("common_test/include/ct.hrl").
-include_lib("emqx/include/emqx.hrl").
-include("emqx_dashboard.hrl").
-define(SERVER, "http://127.0.0.1:18083").
@ -114,9 +112,9 @@ t_monitor_reset(_) ->
ok.
t_monitor_api_error(_) ->
{error, {400, #{<<"code">> := <<"BAD_RPC">>}}} =
{error, {404, #{<<"code">> := <<"NOT_FOUND">>}}} =
request(["monitor", "nodes", 'emqx@127.0.0.2']),
{error, {400, #{<<"code">> := <<"BAD_RPC">>}}} =
{error, {404, #{<<"code">> := <<"NOT_FOUND">>}}} =
request(["monitor_current", "nodes", 'emqx@127.0.0.2']),
{error, {400, #{<<"code">> := <<"BAD_REQUEST">>}}} =
request(["monitor"], "latest=0"),