emqx_mgmt_api_plugins.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 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_mgmt_api_plugins).
  17. -include("emqx_mgmt.hrl").
  18. -include_lib("emqx/include/emqx.hrl").
  19. -import(minirest, [return/1]).
  20. -rest_api(#{name => list_all_plugins,
  21. method => 'GET',
  22. path => "/plugins/",
  23. func => list,
  24. descr => "List all plugins in the cluster"}).
  25. -rest_api(#{name => list_node_plugins,
  26. method => 'GET',
  27. path => "/nodes/:atom:node/plugins/",
  28. func => list,
  29. descr => "List all plugins on a node"}).
  30. -rest_api(#{name => load_node_plugin,
  31. method => 'PUT',
  32. path => "/nodes/:atom:node/plugins/:atom:plugin/load",
  33. func => load,
  34. descr => "Load a plugin"}).
  35. -rest_api(#{name => unload_node_plugin,
  36. method => 'PUT',
  37. path => "/nodes/:atom:node/plugins/:atom:plugin/unload",
  38. func => unload,
  39. descr => "Unload a plugin"}).
  40. -rest_api(#{name => reload_node_plugin,
  41. method => 'PUT',
  42. path => "/nodes/:atom:node/plugins/:atom:plugin/reload",
  43. func => reload,
  44. descr => "Reload a plugin"}).
  45. -rest_api(#{name => unload_plugin,
  46. method => 'PUT',
  47. path => "/plugins/:atom:plugin/unload",
  48. func => unload,
  49. descr => "Unload a plugin in the cluster"}).
  50. -rest_api(#{name => reload_plugin,
  51. method => 'PUT',
  52. path => "/plugins/:atom:plugin/reload",
  53. func => reload,
  54. descr => "Reload a plugin in the cluster"}).
  55. -export([ list/2
  56. , load/2
  57. , unload/2
  58. , reload/2
  59. ]).
  60. list(#{node := Node}, _Params) ->
  61. return({ok, [format(Plugin) || Plugin <- emqx_mgmt:list_plugins(Node)]});
  62. list(_Bindings, _Params) ->
  63. return({ok, [format({Node, Plugins}) || {Node, Plugins} <- emqx_mgmt:list_plugins()]}).
  64. load(#{node := Node, plugin := Plugin}, _Params) ->
  65. return(emqx_mgmt:load_plugin(Node, Plugin)).
  66. unload(#{node := Node, plugin := Plugin}, _Params) ->
  67. return(emqx_mgmt:unload_plugin(Node, Plugin));
  68. unload(#{plugin := Plugin}, _Params) ->
  69. Results = [emqx_mgmt:unload_plugin(Node, Plugin) || {Node, _Info} <- emqx_mgmt:list_nodes()],
  70. case lists:filter(fun(Item) -> Item =/= ok end, Results) of
  71. [] ->
  72. return(ok);
  73. Errors ->
  74. return(lists:last(Errors))
  75. end.
  76. reload(#{node := Node, plugin := Plugin}, _Params) ->
  77. return(emqx_mgmt:reload_plugin(Node, Plugin));
  78. reload(#{plugin := Plugin}, _Params) ->
  79. Results = [emqx_mgmt:reload_plugin(Node, Plugin) || {Node, _Info} <- emqx_mgmt:list_nodes()],
  80. case lists:filter(fun(Item) -> Item =/= ok end, Results) of
  81. [] ->
  82. return(ok);
  83. Errors ->
  84. return(lists:last(Errors))
  85. end.
  86. format({Node, Plugins}) ->
  87. #{node => Node, plugins => [format(Plugin) || Plugin <- Plugins]};
  88. format(#plugin{name = Name,
  89. descr = Descr,
  90. active = Active,
  91. type = Type}) ->
  92. #{name => Name,
  93. description => iolist_to_binary(Descr),
  94. active => Active,
  95. type => Type}.