emqx_modules_schema.erl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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([ namespace/0
  20. , roots/0
  21. , fields/1]).
  22. namespace() -> modules.
  23. roots() ->
  24. ["delayed",
  25. "telemetry",
  26. "event_message",
  27. array("rewrite"),
  28. array("topic_metrics")].
  29. fields("telemetry") ->
  30. [ {enable, hoconsc:mk(boolean(), #{default => false})}
  31. ];
  32. fields("delayed") ->
  33. [ {enable, hoconsc:mk(boolean(), #{default => false})}
  34. , {max_delayed_messages, sc(integer(), #{})}
  35. ];
  36. fields("rewrite") ->
  37. [ { action
  38. , sc( hoconsc:enum([subscribe, publish, all])
  39. , #{desc => <<"Action">>, example => publish})}
  40. , { source_topic
  41. , sc( binary()
  42. , #{desc => <<"Origin Topic">>, example => "x/#"})}
  43. , { dest_topic
  44. , sc( binary()
  45. , #{desc => <<"Destination Topic">>, example => "z/y/$1"})}
  46. , { re, fun regular_expression/1 }
  47. ];
  48. fields("event_message") ->
  49. Fields =
  50. [ { client_connected
  51. , sc( boolean()
  52. , #{desc => <<"Enable/disable client_connected event messages">>,
  53. default => false})}
  54. , { client_disconnected
  55. , sc(boolean()
  56. , #{desc => <<"Enable/disable client_disconnected event messages">>,
  57. default => false})}
  58. , { client_subscribed
  59. , sc( boolean()
  60. , #{desc => <<"Enable/disable client_subscribed event messages">>,
  61. default => false})}
  62. , { client_unsubscribed
  63. , sc( boolean()
  64. , #{desc => <<"Enable/disable client_unsubscribed event messages">>,
  65. default => false})}
  66. , { message_delivered
  67. , sc( boolean()
  68. , #{desc => <<"Enable/disable message_delivered event messages">>,
  69. default => false})}
  70. , { message_acked
  71. , sc( boolean()
  72. , #{desc => <<"Enable/disable message_acked event messages">>,
  73. default => false})}
  74. , { message_dropped
  75. , sc( boolean()
  76. , #{desc => <<"Enable/disable message_dropped event messages">>,
  77. default => false})}
  78. ],
  79. #{fields => Fields,
  80. desc => """
  81. Enable/Disable system event messages.
  82. The messages are plublished to '$event' prefixed topics.
  83. For example, if `client_disconnected` is set to `true`,
  84. a message is published to `$event/client_connected` topic
  85. whenver a client is connected.
  86. """};
  87. fields("topic_metrics") ->
  88. [{topic, sc(binary(), #{})}].
  89. regular_expression(type) -> binary();
  90. regular_expression(desc) -> "Regular expressions";
  91. regular_expression(example) -> "^x/y/(.+)$";
  92. regular_expression(validator) -> fun is_re/1;
  93. regular_expression(_) -> undefined.
  94. is_re(Bin) ->
  95. case re:compile(Bin) of
  96. {ok, _} -> ok;
  97. {error, Reason} -> {error, {Bin, Reason}}
  98. end.
  99. array(Name) -> {Name, hoconsc:array(hoconsc:ref(?MODULE, Name))}.
  100. sc(Type, Meta) -> hoconsc:mk(Type, Meta).