chore(gen_id): using emqx_misc:gen_id/0, /1
This commit is contained in:
parent
deac54c847
commit
43ce727632
|
@ -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))).
|
||||||
|
|
||||||
|
|
|
@ -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 -> ""
|
||||||
|
|
|
@ -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);
|
||||||
|
|
|
@ -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).
|
|
|
@ -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))).
|
|
|
@ -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)
|
||||||
|
|
|
@ -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()]),
|
RuleId = iolist_to_binary(["test_rule", emqx_misc:gen_id()]),
|
||||||
ActInstId = iolist_to_binary(["test_action", emqx_plugin_libs_id:gen()]),
|
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{
|
||||||
|
|
Loading…
Reference in New Issue