emqx_rewrite_api.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_rewrite_api).
  17. -behaviour(minirest_api).
  18. -include_lib("hocon/include/hoconsc.hrl").
  19. -include_lib("typerefl/include/types.hrl").
  20. -include("emqx_modules.hrl").
  21. -export([api_spec/0, paths/0, schema/1]).
  22. -export([topic_rewrite/2]).
  23. -define(MAX_RULES_LIMIT, 20).
  24. -define(EXCEED_LIMIT, 'EXCEED_LIMIT').
  25. -define(BAD_REQUEST, 'BAD_REQUEST').
  26. api_spec() ->
  27. emqx_dashboard_swagger:spec(?MODULE).
  28. paths() ->
  29. ["/mqtt/topic_rewrite"].
  30. schema("/mqtt/topic_rewrite") ->
  31. #{
  32. 'operationId' => topic_rewrite,
  33. get => #{
  34. tags => ?API_TAG_MQTT,
  35. description => ?DESC(list_topic_rewrite_api),
  36. responses => #{
  37. 200 => hoconsc:mk(
  38. hoconsc:array(hoconsc:ref(emqx_modules_schema, "rewrite")),
  39. #{desc => ?DESC(list_topic_rewrite_api)}
  40. )
  41. }
  42. },
  43. put => #{
  44. description => ?DESC(update_topic_rewrite_api),
  45. tags => ?API_TAG_MQTT,
  46. 'requestBody' => hoconsc:mk(
  47. hoconsc:array(
  48. hoconsc:ref(emqx_modules_schema, "rewrite")
  49. ),
  50. #{}
  51. ),
  52. responses => #{
  53. 200 => hoconsc:mk(
  54. hoconsc:array(hoconsc:ref(emqx_modules_schema, "rewrite")),
  55. #{desc => ?DESC(update_topic_rewrite_api)}
  56. ),
  57. 400 => emqx_dashboard_swagger:error_codes(
  58. [?BAD_REQUEST],
  59. ?DESC(update_topic_rewrite_api_response400)
  60. ),
  61. 413 => emqx_dashboard_swagger:error_codes(
  62. [?EXCEED_LIMIT],
  63. ?DESC(update_topic_rewrite_api_response413)
  64. )
  65. }
  66. }
  67. }.
  68. topic_rewrite(get, _Params) ->
  69. {200, emqx_rewrite:list()};
  70. topic_rewrite(put, #{body := Body}) ->
  71. case length(Body) < ?MAX_RULES_LIMIT of
  72. true ->
  73. try
  74. ok = emqx_rewrite:update(Body),
  75. {200, emqx_rewrite:list()}
  76. catch
  77. throw:#{
  78. kind := validation_error,
  79. reason := #{
  80. msg := "cannot_use_wildcard_for_destination_topic",
  81. invalid_topics := InvalidTopics
  82. }
  83. } ->
  84. Message = get_invalid_wildcard_topic_msg(InvalidTopics),
  85. {400, #{code => ?BAD_REQUEST, message => Message}}
  86. end;
  87. _ ->
  88. Message = iolist_to_binary(
  89. io_lib:format("Max rewrite rules count is ~p", [?MAX_RULES_LIMIT])
  90. ),
  91. {413, #{code => ?EXCEED_LIMIT, message => Message}}
  92. end.
  93. get_invalid_wildcard_topic_msg(InvalidTopics) ->
  94. iolist_to_binary(
  95. io_lib:format("Cannot use wildcard for destination topic. Invalid topics: ~p", [
  96. InvalidTopics
  97. ])
  98. ).