Skip to content

Commit fea26a8

Browse files
dumbbellmergify[bot]
authored andcommitted
rabbit_plugins: Add which_plugin/1 to query which plugin provides a module
[Why] This will be used in a later commit to find the auth backend plugin that provides a configured auth backend module. [How] We go through the list of available plugins, regardless if they are enabled or not, then look up the given module in the list of modules associated with each plugin's application. (cherry picked from commit a8bef77)
1 parent eeb35fb commit fea26a8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

deps/rabbit/src/rabbit_plugins.erl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
-export([is_strictly_plugin/1, strictly_plugins/2, strictly_plugins/1]).
1919
-export([plugins_dir/0, plugin_names/1, plugins_expand_dir/0, enabled_plugins_file/0]).
2020
-export([is_enabled/1, is_enabled_on_node/2]).
21+
-export([which_plugin/1]).
2122

2223
% Export for testing purpose.
2324
-export([is_version_supported/2, validate_plugins/2]).
@@ -290,6 +291,56 @@ running_plugins() ->
290291
ActivePlugins = active(),
291292
{ok, [{App, Vsn} || {App, _ , Vsn} <- rabbit_misc:which_applications(), lists:member(App, ActivePlugins)]}.
292293

294+
-spec which_plugin(Module) -> Ret when
295+
Module :: module(),
296+
Ret :: {ok, PluginName} | {error, Reason},
297+
PluginName :: atom(),
298+
Reason :: no_provider.
299+
%% @doc Returns the name of the plugin that provides the given module.
300+
%%
301+
%% If no plugin provides the module, `{error, no_provider}' is returned.
302+
%%
303+
%% The returned plugin might not be enabled, thus using the given module might
304+
%% not work until the plugin is enabled.
305+
%%
306+
%% @returns An `{ok, PluginName}' tuple with the name of the plugin providing
307+
%% the module, or `{error, no_provider}'.
308+
309+
which_plugin(Module) ->
310+
Plugins = list(),
311+
which_plugin(Plugins, Module).
312+
313+
which_plugin([#plugin{name = Name} | Rest], Module) ->
314+
%% Get the list of modules belonging to this plugin.
315+
ModulesKey = case application:get_key(Name, modules) of
316+
{ok, _} = Ret ->
317+
Ret;
318+
undefined ->
319+
%% The plugin application might not be loaded. Load
320+
%% it temporarily and try again.
321+
case application:load(Name) of
322+
ok ->
323+
Ret = application:get_key(Name, modules),
324+
_ = application:unload(Name),
325+
Ret;
326+
{error, _Reason} ->
327+
undefined
328+
end
329+
end,
330+
case ModulesKey of
331+
{ok, Modules} ->
332+
case lists:member(Module, Modules) of
333+
true ->
334+
{ok, Name};
335+
false ->
336+
which_plugin(Rest, Module)
337+
end;
338+
undefined ->
339+
which_plugin(Rest, Module)
340+
end;
341+
which_plugin([], _Module) ->
342+
{error, no_provider}.
343+
293344
%%----------------------------------------------------------------------------
294345

295346
prepare_plugins(Enabled) ->

0 commit comments

Comments
 (0)