Przeglądaj źródła

refactor: rename plugins config api functions

JimMoen 1 rok temu
rodzic
commit
5ff4e76904

+ 2 - 2
apps/emqx_management/src/emqx_mgmt_api_plugins.erl

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

+ 3 - 3
apps/emqx_management/test/emqx_mgmt_api_plugins_SUITE.erl

@@ -37,10 +37,10 @@ init_per_suite(Config) ->
     ok = filelib:ensure_dir(WorkDir),
     DemoShDir1 = string:replace(WorkDir, "emqx_mgmt_api_plugins", "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),
     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].
 
 end_per_suite(Config) ->
@@ -48,7 +48,7 @@ end_per_suite(Config) ->
     %% restore config
     case proplists:get_value(orig_install_dir, Config) of
         undefined -> ok;
-        OrigInstallDir -> emqx_plugins:put_config(install_dir, OrigInstallDir)
+        OrigInstallDir -> emqx_plugins:put_config_internal(install_dir, OrigInstallDir)
     end,
     emqx_mgmt_api_test_util:end_suite([emqx_plugins, emqx_conf]),
     ok.

+ 30 - 31
apps/emqx_plugins/src/emqx_plugins.erl

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

+ 5 - 5
apps/emqx_plugins/test/emqx_plugins_SUITE.erl

@@ -626,9 +626,9 @@ group_t_copy_plugin_to_a_new_node({init, Config}) ->
             }
         ),
     [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}]),
-    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),
     ok = rpc:call(CopyFromNode, emqx_plugins, ensure_installed, [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),
     CopyToNode = proplists:get_value(copy_to_node, 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),
     PluginName = proplists:get_value(plugin_name, Config),
     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]),
     %% 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.
     %% This is similar to emqx_conf, which copies cluster-override conf upon start,
     %% 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
     %% the node would crash if not working properly.
     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", [
         CopyToNode, erpc:call(CopyToNode, file, list_dir, [ToInstallDir])

+ 2 - 2
apps/emqx_plugins/test/emqx_plugins_tests.erl

@@ -72,12 +72,12 @@ with_rand_install_dir(F) ->
     TmpDir = integer_to_list(N),
     OriginalInstallDir = emqx_plugins:install_dir(),
     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
         F(TmpDir)
     after
         file:del_dir_r(TmpDir),
-        ok = emqx_plugins:put_config(install_dir, OriginalInstallDir)
+        ok = emqx_plugins:put_config_internal(install_dir, OriginalInstallDir)
     end.
 
 write_file(Path, Content) ->