Merge pull request #10677 from savonarola/0511-fix-rule-api

fix(api): respond 404 on the deletion of nonexistent rule
This commit is contained in:
Ilya Averyanov 2023-05-16 16:50:30 +03:00 committed by GitHub
commit c113a8ac6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 12 deletions

View File

@ -407,17 +407,22 @@ param_path_id() ->
{400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}}
end;
'/rules/:id'(delete, #{bindings := #{id := Id}}) ->
ConfPath = emqx_rule_engine:config_key_path() ++ [Id],
case emqx_conf:remove(ConfPath, #{override_to => cluster}) of
{ok, _} ->
{204};
{error, Reason} ->
?SLOG(error, #{
msg => "delete_rule_failed",
id => Id,
reason => Reason
}),
{500, #{code => 'INTERNAL_ERROR', message => ?ERR_BADARGS(Reason)}}
case emqx_rule_engine:get_rule(Id) of
{ok, _Rule} ->
ConfPath = emqx_rule_engine:config_key_path() ++ [Id],
case emqx_conf:remove(ConfPath, #{override_to => cluster}) of
{ok, _} ->
{204};
{error, Reason} ->
?SLOG(error, #{
msg => "delete_rule_failed",
id => Id,
reason => Reason
}),
{500, #{code => 'INTERNAL_ERROR', message => ?ERR_BADARGS(Reason)}}
end;
not_found ->
{404, #{code => 'NOT_FOUND', message => <<"Rule Id Not Found">>}}
end.
'/rules/:id/metrics'(get, #{bindings := #{id := Id}}) ->

View File

@ -120,7 +120,14 @@ t_crud_rule_api(_Config) ->
)
),
%ct:pal("Show After Deleted: ~p", [NotFound]),
?assertMatch(
{404, #{code := 'NOT_FOUND'}},
emqx_rule_engine_api:'/rules/:id'(
delete,
#{bindings => #{id => RuleId}}
)
),
?assertMatch(
{404, #{code := _, message := _Message}},
emqx_rule_engine_api:'/rules/:id'(get, #{bindings => #{id => RuleId}})

View File

@ -0,0 +1 @@
In Rule API, reapond with 404 HTTP error code when trying to delete a rule that does not exist.