Merge pull request #8777 from ieQu1/master

refactor(emqx_management): Export transactions
This commit is contained in:
ieQu1 2022-08-23 10:57:11 +02:00 committed by GitHub
commit 7fca34c11a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 48 deletions

View File

@ -30,6 +30,13 @@
-export([authorize/3]). -export([authorize/3]).
%% Internal exports (RPC)
-export([
do_update/4,
do_delete/1,
do_create_app/3
]).
-define(APP, emqx_app). -define(APP, emqx_app).
-record(?APP, { -record(?APP, {
@ -58,16 +65,15 @@ create(Name, Enable, ExpiredAt, Desc) ->
end. end.
read(Name) -> read(Name) ->
Fun = fun() -> case mnesia:dirty_read(?APP, Name) of
case mnesia:read(?APP, Name) of [App] -> {ok, to_map(App)};
[] -> mnesia:abort(not_found); [] -> {error, not_found}
[App] -> to_map(App) end.
end
end,
trans(Fun).
update(Name, Enable, ExpiredAt, Desc) -> update(Name, Enable, ExpiredAt, Desc) ->
Fun = fun() -> trans(fun ?MODULE:do_update/4, [Name, Enable, ExpiredAt, Desc]).
do_update(Name, Enable, ExpiredAt, Desc) ->
case mnesia:read(?APP, Name, write) of case mnesia:read(?APP, Name, write) of
[] -> [] ->
mnesia:abort(not_found); mnesia:abort(not_found);
@ -80,18 +86,16 @@ update(Name, Enable, ExpiredAt, Desc) ->
}, },
ok = mnesia:write(App), ok = mnesia:write(App),
to_map(App) to_map(App)
end end.
end,
trans(Fun).
delete(Name) -> delete(Name) ->
Fun = fun() -> trans(fun ?MODULE:do_delete/1, [Name]).
do_delete(Name) ->
case mnesia:read(?APP, Name) of case mnesia:read(?APP, Name) of
[] -> mnesia:abort(not_found); [] -> mnesia:abort(not_found);
[_App] -> mnesia:delete({?APP, Name}) [_App] -> mnesia:delete({?APP, Name})
end end.
end,
trans(Fun).
list() -> list() ->
to_map(ets:match_object(?APP, #?APP{_ = '_'})). to_map(ets:match_object(?APP, #?APP{_ = '_'})).
@ -118,8 +122,8 @@ authorize(_Path, ApiKey, ApiSecret) ->
find_by_api_key(ApiKey) -> find_by_api_key(ApiKey) ->
Fun = fun() -> mnesia:match_object(#?APP{api_key = ApiKey, _ = '_'}) end, Fun = fun() -> mnesia:match_object(#?APP{api_key = ApiKey, _ = '_'}) end,
case trans(Fun) of case mria:ro_transaction(?COMMON_SHARD, Fun) of
{ok, [#?APP{api_secret_hash = SecretHash, enable = Enable, expired_at = ExpiredAt}]} -> {atomic, [#?APP{api_secret_hash = SecretHash, enable = Enable, expired_at = ExpiredAt}]} ->
{ok, Enable, ExpiredAt, SecretHash}; {ok, Enable, ExpiredAt, SecretHash};
_ -> _ ->
{error, "not_found"} {error, "not_found"}
@ -163,7 +167,9 @@ create_app(Name, Enable, ExpiredAt, Desc) ->
end. end.
create_app(App = #?APP{api_key = ApiKey, name = Name}) -> create_app(App = #?APP{api_key = ApiKey, name = Name}) ->
trans(fun() -> trans(fun ?MODULE:do_create_app/3, [App, ApiKey, Name]).
do_create_app(App, ApiKey, Name) ->
case mnesia:read(?APP, Name) of case mnesia:read(?APP, Name) of
[_] -> [_] ->
mnesia:abort(name_already_existed); mnesia:abort(name_already_existed);
@ -175,11 +181,10 @@ create_app(App = #?APP{api_key = ApiKey, name = Name}) ->
_ -> _ ->
mnesia:abort(api_key_already_existed) mnesia:abort(api_key_already_existed)
end end
end end.
end).
trans(Fun) -> trans(Fun, Args) ->
case mria:transaction(?COMMON_SHARD, Fun) of case mria:transaction(?COMMON_SHARD, Fun, Args) of
{atomic, Res} -> {ok, Res}; {atomic, Res} -> {ok, Res};
{aborted, Error} -> {error, Error} {aborted, Error} -> {error, Error}
end. end.