fix: return `404` for unknown node names

This commit is contained in:
Stefan Strigler 2023-02-21 16:23:38 +01:00
parent bf978efc83
commit a0589d5b95
2 changed files with 21 additions and 30 deletions

View File

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

View File

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