feat(rule-engine): change init resource to test resource

This commit is contained in:
wwhai 2021-01-25 19:43:44 +08:00
parent 6a6a94f99e
commit 81da182e1b
3 changed files with 44 additions and 41 deletions

View File

@ -249,13 +249,13 @@ create_resource(#{type := Type, config := Config0} = Params) ->
update_resource(ResId, NewParams) -> update_resource(ResId, NewParams) ->
try try
lists:foreach(fun(#rule{id = RuleId, enabled = Enabled, actions = Actions}) -> lists:foreach(fun(#rule{id = RuleId, enabled = Enabled, actions = Actions}) ->
lists:foreach( lists:foreach(
fun (#action_instance{args = #{<<"$resource">> := ResId1}}) fun (#action_instance{args = #{<<"$resource">> := ResId1}})
when ResId =:= ResId1, Enabled == true -> when ResId =:= ResId1, Enabled == true ->
throw({dependency_exists, RuleId}); throw({dependency_exists, RuleId});
(_) -> ok (_) -> ok
end, Actions) end, Actions)
end, ets:tab2list(?RULE_TAB)), end, ets:tab2list(?RULE_TAB)),
do_update_resource_check(ResId, NewParams) do_update_resource_check(ResId, NewParams)
catch _ : Reason -> catch _ : Reason ->
{error, Reason} {error, Reason}
@ -267,37 +267,40 @@ do_update_resource_check(Id, NewParams) ->
type = Type, type = Type,
config = OldConfig, config = OldConfig,
description = OldDescription} = _OldResource} -> description = OldDescription} = _OldResource} ->
try try
do_update_resource(#{id => Id, do_update_resource(#{id => Id,
config => case maps:find(<<"config">>, NewParams) of config => case maps:find(<<"config">>, NewParams) of
{ok, NewConfig} -> NewConfig; {ok, NewConfig} -> NewConfig;
error -> OldConfig error -> OldConfig
end, end,
type => Type, type => Type,
description => case maps:find(<<"description">>, NewParams) of description => case maps:find(<<"description">>, NewParams) of
{ok, NewDescription} -> NewDescription; {ok, NewDescription} -> NewDescription;
error -> OldDescription error -> OldDescription
end}), end}),
ok ok
catch _ : Reason -> catch _ : Reason ->
{error, Reason} {error, Reason}
end; end;
_Other -> _Other ->
{error, not_found} {error, not_found}
end. end.
do_update_resource(#{id := Id, type := Type, description:= NewDescription, config:= NewConfig}) -> do_update_resource(#{id := Id, type := Type, description:= NewDescription, config:= NewConfig}) ->
case emqx_rule_registry:find_resource_type(Type) of case emqx_rule_registry:find_resource_type(Type) of
{ok, #resource_type{on_create = {Module, Create}, {ok, #resource_type{params_spec = ParamSpec}} ->
on_destroy = {Module, Destroy},
params_spec = ParamSpec}} ->
Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec), Config = emqx_rule_validator:validate_params(NewConfig, ParamSpec),
cluster_call(init_resource, [Module, Create, Id, Config]), case test_resource(#{type => Type, config => NewConfig}) of
emqx_rule_registry:add_resource(#resource{id = Id, ok ->
type = Type, Resource = #resource{id = Id,
config = Config, type = Type,
description = NewDescription, config = Config,
created_at = erlang:system_time(millisecond)}) description = NewDescription,
created_at = erlang:system_time(millisecond)},
emqx_rule_registry:add_resource(Resource);
{error, Reason} ->
{error, Reason}
end
end. end.
-spec(start_resource(resource_id()) -> ok | {error, Reason :: term()}). -spec(start_resource(resource_id()) -> ok | {error, Reason :: term()}).

View File

@ -329,13 +329,13 @@ start_resource(#{id := Id}, _Params) ->
update_resource(#{id := Id}, NewParams) -> update_resource(#{id := Id}, NewParams) ->
P1 = case proplists:get_value(<<"description">>, NewParams) of P1 = case proplists:get_value(<<"description">>, NewParams) of
undefined -> #{}; undefined -> #{};
Value -> #{<<"description">> => Value} Value -> #{<<"description">> => Value}
end, end,
P2 = case proplists:get_value(<<"config">>, NewParams) of P2 = case proplists:get_value(<<"config">>, NewParams) of
undefined -> #{}; undefined -> #{};
<<"{}">> -> #{}; <<"{}">> -> #{};
Map -> #{<<"config">> => ?RAISE(maps:from_list(Map), {invalid_config, Map})} Map -> #{<<"config">> => ?RAISE(maps:from_list(Map), {invalid_config, Map})}
end, end,
case emqx_rule_engine:update_resource(Id, maps:merge(P1, P2)) of case emqx_rule_engine:update_resource(Id, maps:merge(P1, P2)) of
ok -> ok ->

View File

@ -167,7 +167,7 @@ resources(["create" | Params]) ->
resources(["update" | Params]) -> resources(["update" | Params]) ->
with_opts(fun({Opts, _}) -> with_opts(fun({Opts, _}) ->
Id = maps:get(id, maps:from_list(Opts)), Id = proplists:get_value(id, Opts),
Maps = make_updated_resource(Opts), Maps = make_updated_resource(Opts),
case emqx_rule_engine:update_resource(Id, Maps) of case emqx_rule_engine:update_resource(Id, Maps) of
ok -> ok ->
@ -324,12 +324,12 @@ make_resource(Opts) ->
make_updated_resource(Opts) -> make_updated_resource(Opts) ->
P1 = case proplists:get_value(description, Opts) of P1 = case proplists:get_value(description, Opts) of
undefined -> #{}; undefined -> #{};
Value -> #{<<"description">> => Value} Value -> #{<<"description">> => Value}
end, end,
P2 = case proplists:get_value(config, Opts) of P2 = case proplists:get_value(config, Opts) of
undefined -> #{}; undefined -> #{};
Map -> #{<<"config">> => ?RAISE((emqx_json:decode(Map, [return_maps])), {invalid_config, Map})} Map -> #{<<"config">> => ?RAISE((emqx_json:decode(Map, [return_maps])), {invalid_config, Map})}
end, end,
maps:merge(P1, P2). maps:merge(P1, P2).