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,40 +65,37 @@ 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]).
case mnesia:read(?APP, Name, write) of
[] -> do_update(Name, Enable, ExpiredAt, Desc) ->
mnesia:abort(not_found); case mnesia:read(?APP, Name, write) of
[App0 = #?APP{enable = Enable0, desc = Desc0}] -> [] ->
App = mnesia:abort(not_found);
App0#?APP{ [App0 = #?APP{enable = Enable0, desc = Desc0}] ->
expired_at = ExpiredAt, App =
enable = ensure_not_undefined(Enable, Enable0), App0#?APP{
desc = ensure_not_undefined(Desc, Desc0) expired_at = ExpiredAt,
}, enable = ensure_not_undefined(Enable, Enable0),
ok = mnesia:write(App), desc = ensure_not_undefined(Desc, Desc0)
to_map(App) },
end ok = mnesia:write(App),
end, to_map(App)
trans(Fun). end.
delete(Name) -> delete(Name) ->
Fun = fun() -> trans(fun ?MODULE:do_delete/1, [Name]).
case mnesia:read(?APP, Name) of
[] -> mnesia:abort(not_found); do_delete(Name) ->
[_App] -> mnesia:delete({?APP, Name}) case mnesia:read(?APP, Name) of
end [] -> mnesia:abort(not_found);
end, [_App] -> mnesia:delete({?APP, Name})
trans(Fun). end.
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,23 +167,24 @@ 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]).
case mnesia:read(?APP, Name) of
[_] ->
mnesia:abort(name_already_existed);
[] ->
case mnesia:match_object(?APP, #?APP{api_key = ApiKey, _ = '_'}, read) of
[] ->
ok = mnesia:write(App),
to_map(App);
_ ->
mnesia:abort(api_key_already_existed)
end
end
end).
trans(Fun) -> do_create_app(App, ApiKey, Name) ->
case mria:transaction(?COMMON_SHARD, Fun) of case mnesia:read(?APP, Name) of
[_] ->
mnesia:abort(name_already_existed);
[] ->
case mnesia:match_object(?APP, #?APP{api_key = ApiKey, _ = '_'}, read) of
[] ->
ok = mnesia:write(App),
to_map(App);
_ ->
mnesia:abort(api_key_already_existed)
end
end.
trans(Fun, Args) ->
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.