Skip to content
Permalink
Browse files

Fix deleting resource dependency

  • Loading branch information...
terry-xiaoyu committed May 31, 2019
1 parent 68a1bc8 commit 3265ffe10584f0edccc084e6f78ae035ba310c07
Showing with 29 additions and 12 deletions.
  1. +4 −9 src/emqx_rule_engine.erl
  2. +9 −2 src/emqx_rule_engine_api.erl
  3. +16 −1 src/emqx_rule_registry.erl
@@ -244,15 +244,10 @@ test_resource(#{type := Type, config := Config}) ->
delete_resource(ResId) ->
case emqx_rule_registry:find_resource(ResId) of
{ok, #resource{type = ResType}} ->
try
{ok, #resource_type{on_destroy = {ModD,Destroy}}}
= emqx_rule_registry:find_resource_type(ResType),
cluster_call(clear_resource, [ModD, Destroy, ResId]),
ok = emqx_rule_registry:remove_resource(ResId)
catch
Error:Reason ->
{error, {Error,Reason}}
end;
{ok, #resource_type{on_destroy = {ModD,Destroy}}}
= emqx_rule_registry:find_resource_type(ResType),
cluster_call(clear_resource, [ModD, Destroy, ResId]),
ok = emqx_rule_registry:remove_resource(ResId);
not_found ->
{error, {resource_not_found, ResId}}
end.
@@ -158,6 +158,8 @@ test_rule_sql(Params) ->
catch
throw:{invalid_hook, Hook} ->
return({error, 400, ?ERR_NO_HOOK(Hook)});
throw:Reason ->
return({error, 400, ?ERR_BADARGS(Reason)});
_:{parse_error,{unknown_column, Column}} ->
return({error, 400, ?ERR_UNKNOWN_COLUMN(Column)});
_Error:Reason:StackT ->
@@ -176,6 +178,8 @@ do_create_rule(Params) ->
return({error, 400, ?ERR_NO_RESOURCE(ResId)});
throw:{invalid_hook, Hook} ->
return({error, 400, ?ERR_NO_HOOK(Hook)});
throw:Reason ->
return({error, 400, ?ERR_BADARGS(Reason)});
_:{parse_error,{unknown_column, Column}} ->
return({error, 400, ?ERR_UNKNOWN_COLUMN(Column)});
_Error:Reason:StackT ->
@@ -232,6 +236,8 @@ do_create_resource(Create, Params) ->
catch
throw:{resource_type_not_found, Type} ->
return({error, 400, ?ERR_NO_RESOURCE_TYPE(Type)});
throw:Reason ->
return({error, 400, ?ERR_BADARGS(Reason)});
_Error:Reason:StackT ->
?LOG(error, "[RuleEngineAPI] ~p failed: ~0p", [?FUNCTION_NAME, {Reason, StackT}]),
return({error, 400, ?ERR_BADARGS(Reason)})
@@ -248,11 +254,12 @@ show_resource(#{id := Id}, _Params) ->

delete_resource(#{id := Id}, _Params) ->
try
ok = emqx_rule_engine:delete_resource(Id),
emqx_rule_engine:delete_resource(Id),
return(ok)
catch
_Error:{throw,Reason} ->
?LOG(error, "[RuleEngineAPI] ~p failed: ~0p", [?FUNCTION_NAME, Reason]),
return({error, 400, ?ERR_BADARGS(Reason)});
throw:Reason ->
return({error, 400, ?ERR_BADARGS(Reason)});
_Error:Reason:StackT ->
?LOG(error, "[RuleEngineAPI] ~p failed: ~0p", [?FUNCTION_NAME, {Reason, StackT}]),
@@ -64,6 +64,9 @@
, unregister_resource_types_of/1
]).

%% for debug purposes
-export([dump/0]).

%% gen_server Callbacks
-export([ init/1
, handle_call/3
@@ -154,6 +157,18 @@ mnesia(copy) ->
%% Copy hook_name -> rule_id table
ok = ekka_mnesia:copy_table(?RULE_HOOKS).

dump() ->
io:format("Rules: ~p~n"
"ActionInstParams: ~p~n"
"Resources: ~p~n"
"ResourceParams: ~p~n"
"Rule-Hook Mapping: ~p~n",
[ets:tab2list(?RULE_TAB),
ets:tab2list(?ACTION_INST_PARAMS_TAB),
ets:tab2list(?RES_TAB),
ets:tab2list(?RES_PARAMS_TAB),
ets:tab2list(?RULE_HOOKS)]).

%%------------------------------------------------------------------------------
%% Start the registry
%%------------------------------------------------------------------------------
@@ -350,7 +365,7 @@ remove_resource_params(ResId) ->
%% @private
delete_resource(ResId) ->
[[ResId =:= ResId1 andalso throw({dependency_exists, {rule, Id}})
|| #{params := #{<<"$resource">> := ResId1}} <- Actions]
|| #action_instance{args = #{<<"$resource">> := ResId1}} <- Actions]
|| #rule{id = Id, actions = Actions} <- get_rules()],
mnesia:delete(?RES_TAB, ResId, write).

0 comments on commit 3265ffe

Please sign in to comment.
You can’t perform that action at this time.