Merge pull request #11436 from lafirest/feat/banned_clear

feat(banned): add a new API used to clear all banned data
This commit is contained in:
lafirest 2023-08-14 20:48:43 +08:00 committed by GitHub
commit 1bda8020f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 51 additions and 2 deletions

View File

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

View File

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

View File

@ -157,6 +157,30 @@ t_delete(_Config) ->
), ),
ok. 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() -> list_banned() ->
Path = emqx_mgmt_api_test_util:api_path(["banned"]), Path = emqx_mgmt_api_test_util:api_path(["banned"]),
case emqx_mgmt_api_test_util:request_api(get, Path) of 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]), DeletePath = emqx_mgmt_api_test_util:api_path(["banned", As, Who]),
emqx_mgmt_api_test_util:request_api(delete, DeletePath). 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) -> to_rfc3339(Sec) ->
list_to_binary(calendar:system_time_to_rfc3339(Sec)). list_to_binary(calendar:system_time_to_rfc3339(Sec)).

View File

@ -0,0 +1 @@
Add a new API endpoint `DELETE /banned` to clear all `banned` data.

View File

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