feat(banned): add a new API used to clear all banned data

This commit is contained in:
firest 2023-08-11 17:12:02 +08:00
parent bab05b6613
commit 2fc0468e0c
4 changed files with 50 additions and 2 deletions

View File

@ -38,7 +38,8 @@
delete/1,
info/1,
format/1,
parse/1
parse/1,
clear/0
]).
%% gen_server callbacks
@ -226,6 +227,10 @@ delete(Who) ->
info(InfoKey) ->
mnesia:table_info(?BANNED_TAB, InfoKey).
clear() ->
_ = mria:clear_table(?BANNED_TAB),
ok.
%%--------------------------------------------------------------------
%% gen_server callbacks
%%--------------------------------------------------------------------

View File

@ -79,6 +79,13 @@ schema("/banned") ->
?DESC(create_banned_api_response400)
)
}
},
delete => #{
description => ?DESC(clear_banned_api),
tags => ?TAGS,
parameters => [],
'requestBody' => [],
responses => #{204 => <<"No Content">>}
}
};
schema("/banned/:as/:who") ->
@ -168,7 +175,10 @@ banned(post, #{body := Body}) ->
OldBannedFormat = emqx_utils_json:encode(format(Old)),
{400, 'ALREADY_EXISTS', OldBannedFormat}
end
end.
end;
banned(delete, _) ->
emqx_banned:clear(),
{204}.
delete_banned(delete, #{bindings := Params}) ->
case emqx_banned:look_up(Params) of

View File

@ -157,6 +157,30 @@ t_delete(_Config) ->
),
ok.
t_clear(_Config) ->
Now = erlang:system_time(second),
At = emqx_banned:to_rfc3339(Now),
Until = emqx_banned:to_rfc3339(Now + 3),
Who = <<"TestClient-"/utf8>>,
By = <<"banned suite 中"/utf8>>,
Reason = <<"test测试"/utf8>>,
As = <<"clientid">>,
Banned = #{
as => clientid,
who => Who,
by => By,
reason => Reason,
at => At,
until => Until
},
{ok, _} = create_banned(Banned),
?assertMatch({ok, _}, clear_banned()),
?assertMatch(
{error, {"HTTP/1.1", 404, "Not Found"}},
delete_banned(binary_to_list(As), binary_to_list(Who))
),
ok.
list_banned() ->
Path = emqx_mgmt_api_test_util:api_path(["banned"]),
case emqx_mgmt_api_test_util:request_api(get, Path) of
@ -176,5 +200,9 @@ delete_banned(As, Who) ->
DeletePath = emqx_mgmt_api_test_util:api_path(["banned", As, Who]),
emqx_mgmt_api_test_util:request_api(delete, DeletePath).
clear_banned() ->
ClearPath = emqx_mgmt_api_test_util:api_path(["banned"]),
emqx_mgmt_api_test_util:request_api(delete, ClearPath).
to_rfc3339(Sec) ->
list_to_binary(calendar:system_time_to_rfc3339(Sec)).

View File

@ -57,4 +57,9 @@ who.desc:
who.label:
"""Ban Object"""
clear_banned_api.desc:
"""Clear all banned data."""
clear_banned_api.label:
"""Clear"""
}