Sfoglia il codice sorgente

Merge pull request #5742 from tigercl/chore/gen_id

chore(gen_id): using emqx_misc:gen_id/0, /1
tigercl 4 anni fa
parent
commit
fdbde321cd

+ 4 - 0
apps/emqx/test/emqx_misc_SUITE.erl

@@ -147,3 +147,7 @@ t_now_to_secs(_) ->
 t_now_to_ms(_) ->
 t_now_to_ms(_) ->
     ?assert(is_integer(emqx_misc:now_to_ms(os:timestamp()))).
     ?assert(is_integer(emqx_misc:now_to_ms(os:timestamp()))).
 
 
+t_gen_id(_) ->
+    ?assertEqual(10, length(emqx_misc:gen_id(10))),
+    ?assertEqual(20, length(emqx_misc:gen_id(20))).
+

+ 3 - 3
apps/emqx_authz/src/emqx_authz_api_sources.erl

@@ -450,21 +450,21 @@ write_cert(#{<<"ssl">> := #{<<"enable">> := true} = SSL} = Source) ->
     CertPath = filename:join([emqx:get_config([node, data_dir]), "certs"]),
     CertPath = filename:join([emqx:get_config([node, data_dir]), "certs"]),
     CaCert = case maps:is_key(<<"cacertfile">>, SSL) of
     CaCert = case maps:is_key(<<"cacertfile">>, SSL) of
                  true ->
                  true ->
-                     {ok, CaCertFile} = write_file(filename:join([CertPath, "cacert-" ++ emqx_plugin_libs_id:gen() ++".pem"]),
+                     {ok, CaCertFile} = write_file(filename:join([CertPath, "cacert-" ++ emqx_misc:gen_id() ++".pem"]),
                                                  maps:get(<<"cacertfile">>, SSL)),
                                                  maps:get(<<"cacertfile">>, SSL)),
                      CaCertFile;
                      CaCertFile;
                  false -> ""
                  false -> ""
              end,
              end,
     Cert =   case maps:is_key(<<"certfile">>, SSL) of
     Cert =   case maps:is_key(<<"certfile">>, SSL) of
                  true ->
                  true ->
-                     {ok, CertFile} = write_file(filename:join([CertPath, "cert-" ++ emqx_plugin_libs_id:gen() ++".pem"]),
+                     {ok, CertFile} = write_file(filename:join([CertPath, "cert-" ++ emqx_misc:gen_id() ++".pem"]),
                                                  maps:get(<<"certfile">>, SSL)),
                                                  maps:get(<<"certfile">>, SSL)),
                      CertFile;
                      CertFile;
                  false -> ""
                  false -> ""
              end,
              end,
     Key =    case maps:is_key(<<"keyfile">>, SSL) of
     Key =    case maps:is_key(<<"keyfile">>, SSL) of
                  true ->
                  true ->
-                     {ok, KeyFile}  = write_file(filename:join([CertPath, "key-" ++ emqx_plugin_libs_id:gen() ++".pem"]),
+                     {ok, KeyFile}  = write_file(filename:join([CertPath, "key-" ++ emqx_misc:gen_id() ++".pem"]),
                                                  maps:get(<<"keyfile">>, SSL)),
                                                  maps:get(<<"keyfile">>, SSL)),
                      KeyFile;
                      KeyFile;
                  false -> ""
                  false -> ""

+ 3 - 3
apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl

@@ -148,8 +148,8 @@ set_special_configs(_App) ->
     ok.
     ok.
 
 
 init_per_testcase(t_api, Config) ->
 init_per_testcase(t_api, Config) ->
-    meck:new(emqx_plugin_libs_id, [non_strict, passthrough, no_history, no_link]),
-    meck:expect(emqx_plugin_libs_id, gen, fun() -> "fake" end),
+    meck:new(emqx_misc, [non_strict, passthrough, no_history, no_link]),
+    meck:expect(emqx_misc, gen_id, fun() -> "fake" end),
 
 
     meck:new(emqx, [non_strict, passthrough, no_history, no_link]),
     meck:new(emqx, [non_strict, passthrough, no_history, no_link]),
     meck:expect(emqx, get_config, fun([node, data_dir]) ->
     meck:expect(emqx, get_config, fun([node, data_dir]) ->
@@ -162,7 +162,7 @@ init_per_testcase(t_api, Config) ->
 init_per_testcase(_, Config) -> Config.
 init_per_testcase(_, Config) -> Config.
 
 
 end_per_testcase(t_api, _Config) ->
 end_per_testcase(t_api, _Config) ->
-    meck:unload(emqx_plugin_libs_id),
+    meck:unload(emqx_misc),
     meck:unload(emqx),
     meck:unload(emqx),
     ok;
     ok;
 end_per_testcase(_, _Config) -> ok.
 end_per_testcase(_, _Config) -> ok.

+ 1 - 1
apps/emqx_connector/src/emqx_connector_mqtt.erl

@@ -214,7 +214,7 @@ bridge_name(Prefix, Id) ->
     list_to_atom(str(Prefix) ++ ":" ++ str(Id)).
     list_to_atom(str(Prefix) ++ ":" ++ str(Id)).
 
 
 clientid(Id) ->
 clientid(Id) ->
-    list_to_binary(str(Id) ++ ":" ++ emqx_plugin_libs_id:gen(16)).
+    list_to_binary(str(Id) ++ ":" ++ emqx_misc:gen_id(16)).
 
 
 str(A) when is_atom(A) ->
 str(A) when is_atom(A) ->
     atom_to_list(A);
     atom_to_list(A);

+ 0 - 57
apps/emqx_plugin_libs/src/emqx_plugin_libs_id.erl

@@ -1,57 +0,0 @@
-%%--------------------------------------------------------------------
-%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
-%%
-%% Licensed under the Apache License, Version 2.0 (the "License");
-%% you may not use this file except in compliance with the License.
-%% You may obtain a copy of the License at
-%%
-%%     http://www.apache.org/licenses/LICENSE-2.0
-%%
-%% Unless required by applicable law or agreed to in writing, software
-%% distributed under the License is distributed on an "AS IS" BASIS,
-%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-%% See the License for the specific language governing permissions and
-%% limitations under the License.
-%%--------------------------------------------------------------------
-
--module(emqx_plugin_libs_id).
-
--export([gen/0, gen/1]).
-
--define(SHORT, 8).
-
-%%--------------------------------------------------------------------
-%% APIs
-%%--------------------------------------------------------------------
--spec(gen() -> list()).
-gen() ->
-    gen(?SHORT).
-
--spec(gen(integer()) -> list()).
-gen(Len) ->
-    BitLen = Len * 4,
-    <<R:BitLen>> = crypto:strong_rand_bytes(Len div 2),
-    int_to_hex(R, Len).
-
-%%------------------------------------------------------------------------------
-%% Internal Functions
-%%------------------------------------------------------------------------------
-
-int_to_hex(I, N) when is_integer(I), I >= 0 ->
-    int_to_hex([], I, 1, N).
-
-int_to_hex(L, I, Count, N)
-    when I < 16 ->
-    pad([int_to_hex(I) | L], N - Count);
-int_to_hex(L, I, Count, N) ->
-    int_to_hex([int_to_hex(I rem 16) | L], I div 16, Count + 1, N).
-
-int_to_hex(I) when 0 =< I, I =< 9 ->
-    I + $0;
-int_to_hex(I) when 10 =< I, I =< 15 ->
-    (I - 10) + $a.
-
-pad(L, 0) ->
-    L;
-pad(L, Count) ->
-    pad([$0 | L], Count - 1).

+ 0 - 28
apps/emqx_plugin_libs/test/emqx_plugin_libs_id_SUITE.erl

@@ -1,28 +0,0 @@
-%%--------------------------------------------------------------------
-%% Copyright (c) 2020-2021 EMQ Technologies Co., Ltd. All Rights Reserved.
-%%
-%% Licensed under the Apache License, Version 2.0 (the "License");
-%% you may not use this file except in compliance with the License.
-%% You may obtain a copy of the License at
-%%
-%%     http://www.apache.org/licenses/LICENSE-2.0
-%%
-%% Unless required by applicable law or agreed to in writing, software
-%% distributed under the License is distributed on an "AS IS" BASIS,
-%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-%% See the License for the specific language governing permissions and
-%% limitations under the License.
-%%--------------------------------------------------------------------
-
--module(emqx_plugin_libs_id_SUITE).
-
--compile(export_all).
--compile(nowarn_export_all).
-
--include_lib("eunit/include/eunit.hrl").
-
-all() -> emqx_ct:all(?MODULE).
-
-t_gen(_) ->
-    ?assertEqual(10, length(emqx_plugin_libs_id:gen(10))),
-    ?assertEqual(20, length(emqx_plugin_libs_id:gen(20))).

+ 1 - 1
apps/emqx_resource/src/emqx_resource.erl

@@ -172,7 +172,7 @@ create_dry_run(ResourceType, Config) ->
 -spec create_dry_run_local(resource_type(), resource_config()) ->
 -spec create_dry_run_local(resource_type(), resource_config()) ->
     ok | {error, Reason :: term()}.
     ok | {error, Reason :: term()}.
 create_dry_run_local(ResourceType, Config) ->
 create_dry_run_local(ResourceType, Config) ->
-    InstId = emqx_plugin_libs_id:gen(16),
+    InstId = emqx_misc:gen_id(16),
     call_instance(InstId, {create_dry_run, InstId, ResourceType, Config}).
     call_instance(InstId, {create_dry_run, InstId, ResourceType, Config}).
 
 
 -spec recreate(instance_id(), resource_type(), resource_config(), term()) ->
 -spec recreate(instance_id(), resource_type(), resource_config(), term()) ->

+ 1 - 1
apps/emqx_rule_engine/src/emqx_rule_engine.erl

@@ -507,7 +507,7 @@ rule_id() ->
     gen_id("rule:", fun emqx_rule_registry:get_rule/1).
     gen_id("rule:", fun emqx_rule_registry:get_rule/1).
 
 
 gen_id(Prefix, TestFun) ->
 gen_id(Prefix, TestFun) ->
-    Id = iolist_to_binary([Prefix, emqx_plugin_libs_id:gen()]),
+    Id = iolist_to_binary([Prefix, emqx_misc:gen_id()]),
     case TestFun(Id) of
     case TestFun(Id) of
         not_found -> Id;
         not_found -> Id;
         _Res -> gen_id(Prefix, TestFun)
         _Res -> gen_id(Prefix, TestFun)

+ 2 - 2
apps/emqx_rule_engine/src/emqx_rule_sqltester.erl

@@ -48,8 +48,8 @@ test(#{<<"rawsql">> := Sql, <<"ctx">> := Context}) ->
     end.
     end.
 
 
 test_rule(Sql, Select, Context, EventTopics) ->
 test_rule(Sql, Select, Context, EventTopics) ->
-    RuleId = iolist_to_binary(["test_rule", emqx_plugin_libs_id:gen()]),
-    ActInstId = iolist_to_binary(["test_action", emqx_plugin_libs_id:gen()]),
+    RuleId = iolist_to_binary(["test_rule", emqx_misc:gen_id()]),
+    ActInstId = iolist_to_binary(["test_action", emqx_misc:gen_id()]),
     ok = emqx_rule_metrics:create_rule_metrics(RuleId),
     ok = emqx_rule_metrics:create_rule_metrics(RuleId),
     ok = emqx_rule_metrics:create_metrics(ActInstId),
     ok = emqx_rule_metrics:create_metrics(ActInstId),
     Rule = #rule{
     Rule = #rule{