emqx_utils_api.erl 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_utils_api).
  17. -export([
  18. with_node/2,
  19. with_node_or_cluster/2
  20. ]).
  21. -include("emqx_utils_api.hrl").
  22. -define(NODE_NOT_FOUND(NODE), ?NOT_FOUND(<<"Node not found: ", NODE/binary>>)).
  23. %%--------------------------------------------------------------------
  24. %% exported API
  25. %%--------------------------------------------------------------------
  26. -spec with_node(binary() | atom(), fun((atom()) -> {ok, term()} | {error, term()})) ->
  27. ?OK(term()) | ?NOT_FOUND(binary()) | ?BAD_REQUEST(term()).
  28. with_node(Node0, Fun) ->
  29. case lookup_node(Node0) of
  30. {ok, Node} ->
  31. handle_result(Fun(Node));
  32. not_found ->
  33. ?NODE_NOT_FOUND(Node0)
  34. end.
  35. -spec with_node_or_cluster(binary() | atom(), fun((atom()) -> {ok, term()} | {error, term()})) ->
  36. ?OK(term()) | ?NOT_FOUND(iolist()) | ?BAD_REQUEST(term()).
  37. with_node_or_cluster(<<"all">>, Fun) ->
  38. handle_result(Fun(all));
  39. with_node_or_cluster(Node, Fun) ->
  40. with_node(Node, Fun).
  41. %%--------------------------------------------------------------------
  42. %% Internal
  43. %%--------------------------------------------------------------------
  44. -spec lookup_node(atom() | binary()) -> {ok, atom()} | not_found.
  45. lookup_node(BinNode) when is_binary(BinNode) ->
  46. case emqx_utils:safe_to_existing_atom(BinNode, utf8) of
  47. {ok, Node} ->
  48. is_running_node(Node);
  49. _Error ->
  50. not_found
  51. end;
  52. lookup_node(Node) when is_atom(Node) ->
  53. is_running_node(Node).
  54. -spec is_running_node(atom()) -> {ok, atom()} | not_found.
  55. is_running_node(Node) ->
  56. case lists:member(Node, mria:running_nodes()) of
  57. true ->
  58. {ok, Node};
  59. false ->
  60. not_found
  61. end.
  62. handle_result({ok, Result}) ->
  63. ?OK(Result);
  64. handle_result({error, Reason}) ->
  65. ?BAD_REQUEST(Reason);
  66. handle_result({HTTPCode, Content}) when is_integer(HTTPCode) ->
  67. {HTTPCode, Content}.