Skip to content
Permalink
Browse files

Check dependency when deleting resources

  • Loading branch information...
terry-xiaoyu committed Apr 23, 2019
1 parent a4d22b0 commit fa75b952efb7951bc57242adc8e953dbbba6b2ed
Showing with 26 additions and 8 deletions.
  1. +7 −2 src/emqx_rule_engine_api.erl
  2. +7 −2 src/emqx_rule_engine_cli.erl
  3. +12 −4 src/emqx_rule_registry.erl
@@ -225,8 +225,13 @@ show_resource(#{id := Id}, _Params) ->
reply_with(fun emqx_rule_registry:find_resource/1, Id).

delete_resource(#{id := Id}, _Params) ->
ok = emqx_rule_registry:remove_resource(Id),
return().
try
ok = emqx_rule_registry:remove_resource(Id),
return()
catch
_Error:Reason ->
return({error, 400, ?ERR_BADARGS(Reason)})
end.

%%------------------------------------------------------------------------------
%% Resource Types API
@@ -157,8 +157,13 @@ resources(["show", ResourceId]) ->
print_with(fun emqx_rule_registry:find_resource/1, list_to_binary(ResourceId));

resources(["delete", ResourceId]) ->
ok = emqx_rule_registry:remove_resource(list_to_binary(ResourceId)),
emqx_cli:print("ok~n");
try
ok = emqx_rule_registry:remove_resource(list_to_binary(ResourceId)),
emqx_cli:print("ok~n")
catch
_Error:Reason ->
emqx_cli:print("Cannot delete resource as ~p~n", [Reason])
end;

resources(_usage) ->
emqx_cli:usage([{"resources create", "Create a resource"},
@@ -294,14 +294,22 @@ find_resource(Id) ->

-spec(remove_resource(emqx_rule_engine:resource() | binary()) -> ok).
remove_resource(Resource) when is_record(Resource, resource) ->
trans(fun delete_resource/1, [Resource]);
trans(fun delete_resource/1, [Resource#resource.id]);

remove_resource(ResId) when is_binary(ResId) ->
trans(fun mnesia:delete/1, [{?RES_TAB, ResId}]).
trans(fun delete_resource/1, [ResId]).

%% @private
delete_resource(Resource) ->
mnesia:delete_object(?RES_TAB, Resource, write).
delete_resource(ResId) ->
try
[[?RAISE(not_found = find_resource(ResId1), {exists, Id})
|| #{params := #{<<"$resource">> := ResId1}} <- Actions]
|| #rule{id = Id, actions = Actions} <- get_rules()],
mnesia:delete(?RES_TAB, ResId, write)
catch
throw:{exists, Id} ->
throw({dependency_exists, {rule_id, Id}})
end.

%% @private
insert_resource(Resource) ->

0 comments on commit fa75b95

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