Skip to content

Commit

Permalink
Merge pull request #11436 from lafirest/feat/banned_clear
Browse files Browse the repository at this point in the history
feat(banned): add a new API used to clear all banned data
  • Loading branch information
lafirest committed Aug 14, 2023
2 parents 6bea894 + 81d2000 commit 1bda802
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
7 changes: 6 additions & 1 deletion apps/emqx/src/emqx_banned.erl
Expand Up @@ -38,7 +38,8 @@
delete/1,
info/1,
format/1,
parse/1
parse/1,
clear/0
]).

%% gen_server callbacks
Expand Down Expand Up @@ -226,6 +227,10 @@ delete(Who) ->
info(InfoKey) ->
mnesia:table_info(?BANNED_TAB, InfoKey).

clear() ->
_ = mria:clear_table(?BANNED_TAB),
ok.

%%--------------------------------------------------------------------
%% gen_server callbacks
%%--------------------------------------------------------------------
Expand Down
12 changes: 11 additions & 1 deletion apps/emqx_management/src/emqx_mgmt_api_banned.erl
Expand Up @@ -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") ->
Expand Down Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions apps/emqx_management/test/emqx_mgmt_api_banned_SUITE.erl
Expand Up @@ -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
Expand All @@ -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)).
1 change: 1 addition & 0 deletions changes/ce/feat-11436.en.md
@@ -0,0 +1 @@
Add a new API endpoint `DELETE /banned` to clear all `banned` data.
5 changes: 5 additions & 0 deletions rel/i18n/emqx_mgmt_api_banned.hocon
Expand Up @@ -57,4 +57,9 @@ who.desc:
who.label:
"""Ban Object"""

clear_banned_api.desc:
"""Clear all banned data."""
clear_banned_api.label:
"""Clear"""

}

0 comments on commit 1bda802

Please sign in to comment.