Przeglądaj źródła

Merge pull request #11436 from lafirest/feat/banned_clear

feat(banned): add a new API used to clear all banned data
lafirest 2 lat temu
rodzic
commit
1bda8020f5

+ 6 - 1
apps/emqx/src/emqx_banned.erl

@@ -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
 %%--------------------------------------------------------------------

+ 11 - 1
apps/emqx_management/src/emqx_mgmt_api_banned.erl

@@ -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

+ 28 - 0
apps/emqx_management/test/emqx_mgmt_api_banned_SUITE.erl

@@ -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)).

+ 1 - 0
changes/ce/feat-11436.en.md

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

+ 5 - 0
rel/i18n/emqx_mgmt_api_banned.hocon

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