emqx_modules_conf.erl 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2023 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. %% @doc The emqx-modules configuration interoperable interfaces
  17. -module(emqx_modules_conf).
  18. -behaviour(emqx_config_handler).
  19. %% Load/Unload
  20. -export([
  21. load/0,
  22. unload/0
  23. ]).
  24. -export([
  25. topic_metrics/0,
  26. add_topic_metrics/1,
  27. remove_topic_metrics/1
  28. ]).
  29. %% config handlers
  30. -export([
  31. pre_config_update/3,
  32. post_config_update/5
  33. ]).
  34. %%--------------------------------------------------------------------
  35. %% Load/Unload
  36. %%--------------------------------------------------------------------
  37. -spec load() -> ok.
  38. load() ->
  39. emqx_conf:add_handler([topic_metrics], ?MODULE).
  40. -spec unload() -> ok.
  41. unload() ->
  42. emqx_conf:remove_handler([topic_metrics]).
  43. %%--------------------------------------------------------------------
  44. %% Topic-Metrics
  45. %%--------------------------------------------------------------------
  46. -spec topic_metrics() -> [emqx_types:topic()].
  47. topic_metrics() ->
  48. lists:map(
  49. fun(#{topic := Topic}) -> Topic end,
  50. emqx:get_config([topic_metrics])
  51. ).
  52. -spec add_topic_metrics(emqx_types:topic()) ->
  53. {ok, emqx_types:topic()}
  54. | {error, term()}.
  55. add_topic_metrics(Topic) ->
  56. case cfg_update([topic_metrics], ?FUNCTION_NAME, Topic) of
  57. {ok, _} -> {ok, Topic};
  58. {error, Reason} -> {error, Reason}
  59. end.
  60. -spec remove_topic_metrics(emqx_types:topic()) ->
  61. ok
  62. | {error, term()}.
  63. remove_topic_metrics(Topic) ->
  64. case cfg_update([topic_metrics], ?FUNCTION_NAME, Topic) of
  65. {ok, _} -> ok;
  66. {error, Reason} -> {error, Reason}
  67. end.
  68. %%--------------------------------------------------------------------
  69. %% Config Handler
  70. %%--------------------------------------------------------------------
  71. -spec pre_config_update(
  72. list(atom()),
  73. emqx_config:update_request(),
  74. emqx_config:raw_config()
  75. ) ->
  76. {ok, emqx_config:update_request()} | {error, term()}.
  77. pre_config_update(_, {add_topic_metrics, Topic0}, RawConf) ->
  78. Topic = #{<<"topic">> => Topic0},
  79. case lists:member(Topic, RawConf) of
  80. true ->
  81. {error, already_existed};
  82. _ ->
  83. {ok, RawConf ++ [Topic]}
  84. end;
  85. pre_config_update(_, {remove_topic_metrics, Topic0}, RawConf) ->
  86. Topic = #{<<"topic">> => Topic0},
  87. case lists:member(Topic, RawConf) of
  88. true ->
  89. {ok, RawConf -- [Topic]};
  90. _ ->
  91. {error, not_found}
  92. end.
  93. -spec post_config_update(
  94. list(atom()),
  95. emqx_config:update_request(),
  96. emqx_config:config(),
  97. emqx_config:config(),
  98. emqx_config:app_envs()
  99. ) ->
  100. ok | {ok, Result :: any()} | {error, Reason :: term()}.
  101. post_config_update(
  102. _,
  103. {add_topic_metrics, Topic},
  104. _NewConfig,
  105. _OldConfig,
  106. _AppEnvs
  107. ) ->
  108. case emqx_topic_metrics:register(Topic) of
  109. ok -> ok;
  110. {error, Reason} -> {error, Reason}
  111. end;
  112. post_config_update(
  113. _,
  114. {remove_topic_metrics, Topic},
  115. _NewConfig,
  116. _OldConfig,
  117. _AppEnvs
  118. ) ->
  119. case emqx_topic_metrics:deregister(Topic) of
  120. ok -> ok;
  121. {error, Reason} -> {error, Reason}
  122. end.
  123. %%--------------------------------------------------------------------
  124. %% Private
  125. %%--------------------------------------------------------------------
  126. res({ok, Result}) -> {ok, Result};
  127. res({error, {pre_config_update, ?MODULE, Reason}}) -> {error, Reason};
  128. res({error, {post_config_update, ?MODULE, Reason}}) -> {error, Reason};
  129. res({error, Reason}) -> {error, Reason}.
  130. cfg_update(Path, Action, Params) ->
  131. res(
  132. emqx_conf:update(
  133. Path,
  134. {Action, Params},
  135. #{override_to => cluster}
  136. )
  137. ).