refactor: rename plugins config api functions

This commit is contained in:
JimMoen 2024-04-26 18:06:28 +08:00
parent 00cab33fde
commit 5ff4e76904
No known key found for this signature in database
5 changed files with 42 additions and 43 deletions

View File

@ -488,7 +488,7 @@ update_plugin(put, #{bindings := #{name := Name, action := Action}}) ->
plugin_config(get, #{bindings := #{name := NameVsn}}) -> plugin_config(get, #{bindings := #{name := NameVsn}}) ->
case emqx_plugins:describe(NameVsn) of case emqx_plugins:describe(NameVsn) of
{ok, _} -> {ok, _} ->
case emqx_plugins:get_plugin_config(NameVsn) of case emqx_plugins:get_config(NameVsn) of
{ok, AvroJson} -> {ok, AvroJson} ->
{200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson}; {200, #{<<"content-type">> => <<"'application/json'">>}, AvroJson};
{error, _} -> {error, _} ->
@ -601,7 +601,7 @@ ensure_action(Name, restart) ->
%% for RPC plugin avro encoded config update %% for RPC plugin avro encoded config update
do_update_plugin_config(Name, AvroJsonMap, PluginConfigMap) -> do_update_plugin_config(Name, AvroJsonMap, PluginConfigMap) ->
%% TODO: maybe use `PluginConfigMap` to validate config %% TODO: maybe use `PluginConfigMap` to validate config
emqx_plugins:put_plugin_config(Name, AvroJsonMap, PluginConfigMap). emqx_plugins:put_config(Name, AvroJsonMap, PluginConfigMap).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Helper functions %% Helper functions

View File

@ -37,10 +37,10 @@ init_per_suite(Config) ->
ok = filelib:ensure_dir(WorkDir), ok = filelib:ensure_dir(WorkDir),
DemoShDir1 = string:replace(WorkDir, "emqx_mgmt_api_plugins", "emqx_plugins"), DemoShDir1 = string:replace(WorkDir, "emqx_mgmt_api_plugins", "emqx_plugins"),
DemoShDir = lists:flatten(string:replace(DemoShDir1, "emqx_management", "emqx_plugins")), DemoShDir = lists:flatten(string:replace(DemoShDir1, "emqx_management", "emqx_plugins")),
OrigInstallDir = emqx_plugins:get_config(install_dir, undefined), OrigInstallDir = emqx_plugins:get_config_interal(install_dir, undefined),
ok = filelib:ensure_dir(DemoShDir), ok = filelib:ensure_dir(DemoShDir),
emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_plugins]), emqx_mgmt_api_test_util:init_suite([emqx_conf, emqx_plugins]),
emqx_plugins:put_config(install_dir, DemoShDir), emqx_plugins:put_config_internal(install_dir, DemoShDir),
[{demo_sh_dir, DemoShDir}, {orig_install_dir, OrigInstallDir} | Config]. [{demo_sh_dir, DemoShDir}, {orig_install_dir, OrigInstallDir} | Config].
end_per_suite(Config) -> end_per_suite(Config) ->
@ -48,7 +48,7 @@ end_per_suite(Config) ->
%% restore config %% restore config
case proplists:get_value(orig_install_dir, Config) of case proplists:get_value(orig_install_dir, Config) of
undefined -> ok; undefined -> ok;
OrigInstallDir -> emqx_plugins:put_config(install_dir, OrigInstallDir) OrigInstallDir -> emqx_plugins:put_config_internal(install_dir, OrigInstallDir)
end, end,
emqx_mgmt_api_test_util:end_suite([emqx_plugins, emqx_conf]), emqx_mgmt_api_test_util:end_suite([emqx_plugins, emqx_conf]),
ok. ok.

View File

@ -56,11 +56,11 @@
%% Plugin config APIs %% Plugin config APIs
-export([ -export([
get_plugin_config/1, get_config/1,
get_plugin_config/2, get_config/2,
get_plugin_config/3, get_config/3,
get_plugin_config/4, get_config/4,
put_plugin_config/3 put_config/3
]). ]).
%% Package utils %% Package utils
@ -78,7 +78,7 @@
%% Internal export %% Internal export
-export([do_ensure_started/1]). -export([do_ensure_started/1]).
%% for test cases %% for test cases
-export([put_config/2]). -export([put_config_internal/2]).
-ifdef(TEST). -ifdef(TEST).
-compile(export_all). -compile(export_all).
@ -255,35 +255,34 @@ ensure_stopped(NameVsn) ->
end end
). ).
get_plugin_config(Name, Vsn, Options, Default) -> get_config(Name, Vsn, Options, Default) ->
get_plugin_config(make_name_vsn_string(Name, Vsn), Options, Default). get_config(make_name_vsn_string(Name, Vsn), Options, Default).
-spec get_plugin_config(name_vsn()) -> -spec get_config(name_vsn()) ->
{ok, plugin_config()} {ok, plugin_config()}
| {error, term()}. | {error, term()}.
get_plugin_config(NameVsn) -> get_config(NameVsn) ->
get_plugin_config(bin(NameVsn), #{format => ?CONFIG_FORMAT_MAP}). get_config(bin(NameVsn), #{format => ?CONFIG_FORMAT_MAP}).
-spec get_plugin_config(name_vsn(), Options :: map()) -> -spec get_config(name_vsn(), Options :: map()) ->
{ok, avro_binary() | plugin_config()} {ok, avro_binary() | plugin_config()}
| {error, term()}. | {error, term()}.
get_config(NameVsn, #{format := ?CONFIG_FORMAT_AVRO}) ->
get_plugin_config(NameVsn, #{format := ?CONFIG_FORMAT_AVRO}) ->
%% no default value when get raw binary config %% no default value when get raw binary config
case read_plugin_avro(NameVsn) of case read_plugin_avro(NameVsn) of
{ok, _AvroJson} = Res -> Res; {ok, _AvroJson} = Res -> Res;
{error, _Reason} = Err -> Err {error, _Reason} = Err -> Err
end; end;
get_plugin_config(NameVsn, Options = #{format := ?CONFIG_FORMAT_MAP}) -> get_config(NameVsn, Options = #{format := ?CONFIG_FORMAT_MAP}) ->
get_plugin_config(NameVsn, Options, #{}). get_config(NameVsn, Options, #{}).
get_plugin_config(NameVsn, #{format := ?CONFIG_FORMAT_MAP}, Default) -> get_config(NameVsn, #{format := ?CONFIG_FORMAT_MAP}, Default) ->
{ok, persistent_term:get(?PLUGIN_PERSIS_CONFIG_KEY(NameVsn), Default)}. {ok, persistent_term:get(?PLUGIN_PERSIS_CONFIG_KEY(NameVsn), Default)}.
%% @doc Update plugin's config. %% @doc Update plugin's config.
%% RPC call from Management API or CLI. %% RPC call from Management API or CLI.
%% the avro binary and plugin config ALWAYS be valid before calling this function. %% the avro Json Map and plugin config ALWAYS be valid before calling this function.
put_plugin_config(NameVsn, AvroJsonMap, _DecodedPluginConfig) -> put_config(NameVsn, AvroJsonMap, _DecodedPluginConfig) ->
AvroJsonBin = emqx_utils_json:encode(AvroJsonMap), AvroJsonBin = emqx_utils_json:encode(AvroJsonMap),
ok = write_avro_bin(NameVsn, AvroJsonBin), ok = write_avro_bin(NameVsn, AvroJsonBin),
ok = persistent_term:put(?PLUGIN_PERSIS_CONFIG_KEY(NameVsn), AvroJsonMap), ok = persistent_term:put(?PLUGIN_PERSIS_CONFIG_KEY(NameVsn), AvroJsonMap),
@ -328,13 +327,13 @@ decode_plugin_avro_config(NameVsn, AvroJsonBin) ->
{error, ReasonMap} -> {error, ReasonMap} {error, ReasonMap} -> {error, ReasonMap}
end. end.
get_config(Key, Default) when is_atom(Key) -> get_config_interal(Key, Default) when is_atom(Key) ->
get_config([Key], Default); get_config_interal([Key], Default);
get_config(Path, Default) -> get_config_interal(Path, Default) ->
emqx_conf:get([?CONF_ROOT | Path], Default). emqx_conf:get([?CONF_ROOT | Path], Default).
put_config(Key, Value) -> put_config_internal(Key, Value) ->
do_put_config(Key, Value, _ConfLocation = local). do_put_config_internal(Key, Value, _ConfLocation = local).
-spec get_tar(name_vsn()) -> {ok, binary()} | {error, any}. -spec get_tar(name_vsn()) -> {ok, binary()} | {error, any}.
get_tar(NameVsn) -> get_tar(NameVsn) ->
@ -950,16 +949,16 @@ is_needed_by(AppToStop, RunningApp) ->
undefined -> false undefined -> false
end. end.
do_put_config(Key, Value, ConfLocation) when is_atom(Key) -> do_put_config_internal(Key, Value, ConfLocation) when is_atom(Key) ->
do_put_config([Key], Value, ConfLocation); do_put_config_internal([Key], Value, ConfLocation);
do_put_config(Path, Values, _ConfLocation = local) when is_list(Path) -> do_put_config_internal(Path, Values, _ConfLocation = local) when is_list(Path) ->
Opts = #{rawconf_with_defaults => true, override_to => cluster}, Opts = #{rawconf_with_defaults => true, override_to => cluster},
%% Already in cluster_rpc, don't use emqx_conf:update, dead calls %% Already in cluster_rpc, don't use emqx_conf:update, dead calls
case emqx:update_config([?CONF_ROOT | Path], bin_key(Values), Opts) of case emqx:update_config([?CONF_ROOT | Path], bin_key(Values), Opts) of
{ok, _} -> ok; {ok, _} -> ok;
Error -> Error Error -> Error
end; end;
do_put_config(Path, Values, _ConfLocation = global) when is_list(Path) -> do_put_config_internal(Path, Values, _ConfLocation = global) when is_list(Path) ->
Opts = #{rawconf_with_defaults => true, override_to => cluster}, Opts = #{rawconf_with_defaults => true, override_to => cluster},
case emqx_conf:update([?CONF_ROOT | Path], bin_key(Values), Opts) of case emqx_conf:update([?CONF_ROOT | Path], bin_key(Values), Opts) of
{ok, _} -> ok; {ok, _} -> ok;
@ -995,16 +994,16 @@ enable_disable_plugin(_NameVsn, _Diff) ->
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
install_dir() -> install_dir() ->
get_config(install_dir, ""). get_config_interal(install_dir, "").
put_configured(Configured) -> put_configured(Configured) ->
put_configured(Configured, _ConfLocation = local). put_configured(Configured, _ConfLocation = local).
put_configured(Configured, ConfLocation) -> put_configured(Configured, ConfLocation) ->
ok = do_put_config(states, bin_key(Configured), ConfLocation). ok = do_put_config_internal(states, bin_key(Configured), ConfLocation).
configured() -> configured() ->
get_config(states, []). get_config_interal(states, []).
for_plugins(ActionFun) -> for_plugins(ActionFun) ->
case lists:flatmap(fun(I) -> for_plugin(I, ActionFun) end, configured()) of case lists:flatmap(fun(I) -> for_plugin(I, ActionFun) end, configured()) of

View File

@ -626,9 +626,9 @@ group_t_copy_plugin_to_a_new_node({init, Config}) ->
} }
), ),
[CopyFromNode] = emqx_cth_cluster:start([SpecCopyFrom#{join_to => undefined}]), [CopyFromNode] = emqx_cth_cluster:start([SpecCopyFrom#{join_to => undefined}]),
ok = rpc:call(CopyFromNode, emqx_plugins, put_config, [install_dir, FromInstallDir]), ok = rpc:call(CopyFromNode, emqx_plugins, put_config_internal, [install_dir, FromInstallDir]),
[CopyToNode] = emqx_cth_cluster:start([SpecCopyTo#{join_to => undefined}]), [CopyToNode] = emqx_cth_cluster:start([SpecCopyTo#{join_to => undefined}]),
ok = rpc:call(CopyToNode, emqx_plugins, put_config, [install_dir, ToInstallDir]), ok = rpc:call(CopyToNode, emqx_plugins, put_config_internal, [install_dir, ToInstallDir]),
NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX), NameVsn = filename:basename(Package, ?PACKAGE_SUFFIX),
ok = rpc:call(CopyFromNode, emqx_plugins, ensure_installed, [NameVsn]), ok = rpc:call(CopyFromNode, emqx_plugins, ensure_installed, [NameVsn]),
ok = rpc:call(CopyFromNode, emqx_plugins, ensure_started, [NameVsn]), ok = rpc:call(CopyFromNode, emqx_plugins, ensure_started, [NameVsn]),
@ -658,7 +658,7 @@ group_t_copy_plugin_to_a_new_node(Config) ->
CopyFromNode = proplists:get_value(copy_from_node, Config), CopyFromNode = proplists:get_value(copy_from_node, Config),
CopyToNode = proplists:get_value(copy_to_node, Config), CopyToNode = proplists:get_value(copy_to_node, Config),
CopyToDir = proplists:get_value(to_install_dir, Config), CopyToDir = proplists:get_value(to_install_dir, Config),
CopyFromPluginsState = rpc:call(CopyFromNode, emqx_plugins, get_config, [[states], []]), CopyFromPluginsState = rpc:call(CopyFromNode, emqx_plugins, get_config_interal, [[states], []]),
NameVsn = proplists:get_value(name_vsn, Config), NameVsn = proplists:get_value(name_vsn, Config),
PluginName = proplists:get_value(plugin_name, Config), PluginName = proplists:get_value(plugin_name, Config),
PluginApp = list_to_atom(PluginName), PluginApp = list_to_atom(PluginName),
@ -681,7 +681,7 @@ group_t_copy_plugin_to_a_new_node(Config) ->
), ),
ok = rpc:call(CopyToNode, ekka, join, [CopyFromNode]), ok = rpc:call(CopyToNode, ekka, join, [CopyFromNode]),
%% Mimic cluster-override conf copying %% Mimic cluster-override conf copying
ok = rpc:call(CopyToNode, emqx_plugins, put_config, [[states], CopyFromPluginsState]), ok = rpc:call(CopyToNode, emqx_plugins, put_config_internal, [[states], CopyFromPluginsState]),
%% Plugin copying is triggered upon app restart on a new node. %% Plugin copying is triggered upon app restart on a new node.
%% This is similar to emqx_conf, which copies cluster-override conf upon start, %% This is similar to emqx_conf, which copies cluster-override conf upon start,
%% see: emqx_conf_app:init_conf/0 %% see: emqx_conf_app:init_conf/0
@ -734,7 +734,7 @@ group_t_copy_plugin_to_a_new_node_single_node(Config) ->
%% successfully even if it's not extracted yet. Simply starting %% successfully even if it's not extracted yet. Simply starting
%% the node would crash if not working properly. %% the node would crash if not working properly.
ct:pal("~p config:\n ~p", [ ct:pal("~p config:\n ~p", [
CopyToNode, erpc:call(CopyToNode, emqx_plugins, get_config, [[], #{}]) CopyToNode, erpc:call(CopyToNode, emqx_plugins, get_config_interal, [[], #{}])
]), ]),
ct:pal("~p install_dir:\n ~p", [ ct:pal("~p install_dir:\n ~p", [
CopyToNode, erpc:call(CopyToNode, file, list_dir, [ToInstallDir]) CopyToNode, erpc:call(CopyToNode, file, list_dir, [ToInstallDir])

View File

@ -72,12 +72,12 @@ with_rand_install_dir(F) ->
TmpDir = integer_to_list(N), TmpDir = integer_to_list(N),
OriginalInstallDir = emqx_plugins:install_dir(), OriginalInstallDir = emqx_plugins:install_dir(),
ok = filelib:ensure_dir(filename:join([TmpDir, "foo"])), ok = filelib:ensure_dir(filename:join([TmpDir, "foo"])),
ok = emqx_plugins:put_config(install_dir, TmpDir), ok = emqx_plugins:put_config_internal(install_dir, TmpDir),
try try
F(TmpDir) F(TmpDir)
after after
file:del_dir_r(TmpDir), file:del_dir_r(TmpDir),
ok = emqx_plugins:put_config(install_dir, OriginalInstallDir) ok = emqx_plugins:put_config_internal(install_dir, OriginalInstallDir)
end. end.
write_file(Path, Content) -> write_file(Path, Content) ->