fix(rules): deny POST /rules with an existing Id
This commit is contained in:
parent
3544537906
commit
ae8f03a188
|
@ -222,6 +222,20 @@ test_rule_sql(Params) ->
|
||||||
do_create_rule(Params) ->
|
do_create_rule(Params) ->
|
||||||
case parse_rule_params(Params) of
|
case parse_rule_params(Params) of
|
||||||
{ok, ParsedParams} ->
|
{ok, ParsedParams} ->
|
||||||
|
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
|
case emqx_rule_engine:create_rule(ParsedParams) of
|
||||||
{ok, Rule} -> return({ok, record_to_map(Rule)});
|
{ok, Rule} -> return({ok, record_to_map(Rule)});
|
||||||
{error, {action_not_found, ActionName}} ->
|
{error, {action_not_found, ActionName}} ->
|
||||||
|
@ -229,10 +243,6 @@ do_create_rule(Params) ->
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
||||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||||
end;
|
|
||||||
{error, Reason} ->
|
|
||||||
?LOG(error, "~p failed: ~0p", [?FUNCTION_NAME, Reason]),
|
|
||||||
return({error, 400, ?ERR_BADARGS(Reason)})
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
update_rule(#{id := Id}, Params) ->
|
update_rule(#{id := Id}, Params) ->
|
||||||
|
|
|
@ -428,6 +428,14 @@ t_crud_rule_api(_Config) ->
|
||||||
{<<"params">>,[{<<"arg1">>,1}]}]]},
|
{<<"params">>,[{<<"arg1">>,1}]}]]},
|
||||||
{<<"description">>, <<"debug rule">>}]),
|
{<<"description">>, <<"debug rule">>}]),
|
||||||
RuleID = maps:get(id, 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]),
|
%ct:pal("RCreated : ~p", [Rule]),
|
||||||
|
|
||||||
{ok, #{code := 0, data := Rules}} = emqx_rule_engine_api:list_rules(#{}, []),
|
{ok, #{code := 0, data := Rules}} = emqx_rule_engine_api:list_rules(#{}, []),
|
||||||
|
|
Loading…
Reference in New Issue