emqx_modules_schema.erl 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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_modules_schema).
  17. -include_lib("typerefl/include/types.hrl").
  18. -behaviour(hocon_schema).
  19. -export([
  20. namespace/0,
  21. roots/0,
  22. fields/1
  23. ]).
  24. namespace() -> modules.
  25. roots() ->
  26. [
  27. "delayed",
  28. "telemetry",
  29. "event_message",
  30. array("rewrite"),
  31. array("topic_metrics")
  32. ].
  33. fields("telemetry") ->
  34. [{enable, hoconsc:mk(boolean(), #{default => false})}];
  35. fields("delayed") ->
  36. [
  37. {enable, hoconsc:mk(boolean(), #{default => false})},
  38. {max_delayed_messages, sc(integer(), #{})}
  39. ];
  40. fields("rewrite") ->
  41. [
  42. {action,
  43. sc(
  44. hoconsc:enum([subscribe, publish, all]),
  45. #{desc => <<"Action">>, example => publish}
  46. )},
  47. {source_topic,
  48. sc(
  49. binary(),
  50. #{desc => <<"Origin Topic">>, example => "x/#"}
  51. )},
  52. {dest_topic,
  53. sc(
  54. binary(),
  55. #{desc => <<"Destination Topic">>, example => "z/y/$1"}
  56. )},
  57. {re, fun regular_expression/1}
  58. ];
  59. fields("event_message") ->
  60. Fields =
  61. [
  62. {client_connected,
  63. sc(
  64. boolean(),
  65. #{
  66. desc => <<"Enable/disable client_connected event messages">>,
  67. default => false
  68. }
  69. )},
  70. {client_disconnected,
  71. sc(
  72. boolean(),
  73. #{
  74. desc => <<"Enable/disable client_disconnected event messages">>,
  75. default => false
  76. }
  77. )},
  78. {client_subscribed,
  79. sc(
  80. boolean(),
  81. #{
  82. desc => <<"Enable/disable client_subscribed event messages">>,
  83. default => false
  84. }
  85. )},
  86. {client_unsubscribed,
  87. sc(
  88. boolean(),
  89. #{
  90. desc => <<"Enable/disable client_unsubscribed event messages">>,
  91. default => false
  92. }
  93. )},
  94. {message_delivered,
  95. sc(
  96. boolean(),
  97. #{
  98. desc => <<"Enable/disable message_delivered event messages">>,
  99. default => false
  100. }
  101. )},
  102. {message_acked,
  103. sc(
  104. boolean(),
  105. #{
  106. desc => <<"Enable/disable message_acked event messages">>,
  107. default => false
  108. }
  109. )},
  110. {message_dropped,
  111. sc(
  112. boolean(),
  113. #{
  114. desc => <<"Enable/disable message_dropped event messages">>,
  115. default => false
  116. }
  117. )}
  118. ],
  119. #{
  120. fields => Fields,
  121. desc =>
  122. "Enable/Disable system event messages.\n"
  123. "The messages are published to <code>$event</code> prefixed topics.\n"
  124. "For example, if `client_disconnected` is set to `true`,\n"
  125. "a message is published to <code>$event/client_connected</code> topic\n"
  126. "whenever a client is connected.\n"
  127. ""
  128. };
  129. fields("topic_metrics") ->
  130. [{topic, sc(binary(), #{})}].
  131. regular_expression(type) -> binary();
  132. regular_expression(desc) -> "Regular expressions";
  133. regular_expression(example) -> "^x/y/(.+)$";
  134. regular_expression(validator) -> fun is_re/1;
  135. regular_expression(_) -> undefined.
  136. is_re(Bin) ->
  137. case re:compile(Bin) of
  138. {ok, _} -> ok;
  139. {error, Reason} -> {error, {Bin, Reason}}
  140. end.
  141. array(Name) -> {Name, hoconsc:array(hoconsc:ref(?MODULE, Name))}.
  142. sc(Type, Meta) -> hoconsc:mk(Type, Meta).