fix(api): respond 404 on the deletion of nonexistent rule

This commit is contained in:
Ilya Averyanov 2023-05-11 22:37:50 +05:00
parent c4f2dba9c5
commit 49e9ace1c1
4 changed files with 26 additions and 13 deletions

View File

@ -2,7 +2,7 @@
{application, emqx_rule_engine, [ {application, emqx_rule_engine, [
{description, "EMQX Rule Engine"}, {description, "EMQX Rule Engine"},
% strict semver, bump manually! % strict semver, bump manually!
{vsn, "5.0.15"}, {vsn, "5.0.16"},
{modules, []}, {modules, []},
{registered, [emqx_rule_engine_sup, emqx_rule_engine]}, {registered, [emqx_rule_engine_sup, emqx_rule_engine]},
{applications, [kernel, stdlib, rulesql, getopt, emqx_ctl]}, {applications, [kernel, stdlib, rulesql, getopt, emqx_ctl]},

View File

@ -407,6 +407,8 @@ param_path_id() ->
{400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}} {400, #{code => 'BAD_REQUEST', message => ?ERR_BADARGS(Reason)}}
end; end;
'/rules/:id'(delete, #{bindings := #{id := Id}}) -> '/rules/:id'(delete, #{bindings := #{id := Id}}) ->
case emqx_rule_engine:get_rule(Id) of
{ok, _Rule} ->
ConfPath = emqx_rule_engine:config_key_path() ++ [Id], ConfPath = emqx_rule_engine:config_key_path() ++ [Id],
case emqx_conf:remove(ConfPath, #{override_to => cluster}) of case emqx_conf:remove(ConfPath, #{override_to => cluster}) of
{ok, _} -> {ok, _} ->
@ -418,6 +420,9 @@ param_path_id() ->
reason => Reason reason => Reason
}), }),
{500, #{code => 'INTERNAL_ERROR', message => ?ERR_BADARGS(Reason)}} {500, #{code => 'INTERNAL_ERROR', message => ?ERR_BADARGS(Reason)}}
end;
not_found ->
{404, #{code => 'NOT_FOUND', message => <<"Rule Id Not Found">>}}
end. end.
'/rules/:id/metrics'(get, #{bindings := #{id := Id}}) -> '/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( ?assertMatch(
{404, #{code := _, message := _Message}}, {404, #{code := _, message := _Message}},
emqx_rule_engine_api:'/rules/:id'(get, #{bindings => #{id => RuleId}}) 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.