Merge pull request #7612 from terry-xiaoyu/post_existing_rules
fix(rules): deny POST /rules with an existing Id
This commit is contained in:
commit
a89f6f4b95
|
@ -222,19 +222,29 @@ test_rule_sql(Params) ->
|
|||
do_create_rule(Params) ->
|
||||
case parse_rule_params(Params) of
|
||||
{ok, ParsedParams} ->
|
||||
case emqx_rule_engine:create_rule(ParsedParams) of
|
||||
{ok, Rule} -> return({ok, record_to_map(Rule)});
|
||||
{error, {action_not_found, ActionName}} ->
|
||||
return({error, 400, ?ERR_NO_ACTION(ActionName)});
|
||||
{error, Reason} ->
|
||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||
case maps:find(id, ParsedParams) of
|
||||
{ok, RuleId} ->
|
||||
case emqx_rule_registry:get_rule(RuleId) of
|
||||
{ok, _} -> return({error, 400, <<"Already Exists">>});
|
||||
not_found -> do_create_rule2(ParsedParams)
|
||||
end;
|
||||
error -> do_create_rule2(ParsedParams)
|
||||
end;
|
||||
{error, Reason} ->
|
||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||
end.
|
||||
|
||||
do_create_rule2(ParsedParams) ->
|
||||
case emqx_rule_engine:create_rule(ParsedParams) of
|
||||
{ok, Rule} -> return({ok, record_to_map(Rule)});
|
||||
{error, {action_not_found, ActionName}} ->
|
||||
return({error, 400, ?ERR_NO_ACTION(ActionName)});
|
||||
{error, Reason} ->
|
||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||
end.
|
||||
|
||||
update_rule(#{id := Id}, Params) ->
|
||||
case parse_rule_params(Params, #{id => Id}) of
|
||||
{ok, ParsedParams} ->
|
||||
|
|
|
@ -428,6 +428,14 @@ t_crud_rule_api(_Config) ->
|
|||
{<<"params">>,[{<<"arg1">>,1}]}]]},
|
||||
{<<"description">>, <<"debug rule">>}]),
|
||||
RuleID = maps:get(id, Rule),
|
||||
{ok, #{code := 400, message := <<"Already Exists">>}} =
|
||||
emqx_rule_engine_api:create_rule(#{},
|
||||
[{<<"name">>, <<"debug-rule">>},
|
||||
{<<"id">>, RuleID},
|
||||
{<<"rawsql">>, <<"select * from \"t/a\"">>},
|
||||
{<<"actions">>, [[{<<"name">>,<<"inspect">>},
|
||||
{<<"params">>,[{<<"arg1">>,1}]}]]},
|
||||
{<<"description">>, <<"debug rule">>}]),
|
||||
%ct:pal("RCreated : ~p", [Rule]),
|
||||
|
||||
{ok, #{code := 0, data := Rules}} = emqx_rule_engine_api:list_rules(#{}, []),
|
||||
|
|
Loading…
Reference in New Issue