fix(resource_cli): update the CLIs for resources (#3978)
This commit is contained in:
parent
37974f7376
commit
a747cf16bd
|
@ -39,7 +39,6 @@
|
||||||
, get_resource_status/1
|
, get_resource_status/1
|
||||||
, get_resource_params/1
|
, get_resource_params/1
|
||||||
, delete_resource/1
|
, delete_resource/1
|
||||||
, ensure_resource_deleted/1
|
|
||||||
, update_resource/1
|
, update_resource/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
@ -322,18 +321,16 @@ delete_resource(ResId) ->
|
||||||
{ok, #resource{type = ResType}} ->
|
{ok, #resource{type = ResType}} ->
|
||||||
{ok, #resource_type{on_destroy = {ModD,Destroy}}}
|
{ok, #resource_type{on_destroy = {ModD,Destroy}}}
|
||||||
= emqx_rule_registry:find_resource_type(ResType),
|
= emqx_rule_registry:find_resource_type(ResType),
|
||||||
ok = emqx_rule_registry:remove_resource(ResId),
|
try
|
||||||
cluster_call(clear_resource, [ModD, Destroy, ResId]);
|
ok = emqx_rule_registry:remove_resource(ResId),
|
||||||
|
cluster_call(clear_resource, [ModD, Destroy, ResId])
|
||||||
|
catch
|
||||||
|
throw:Reason -> {error, Reason}
|
||||||
|
end;
|
||||||
not_found ->
|
not_found ->
|
||||||
{error, {resource_not_found, ResId}}
|
ok
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% @doc Ensure resource deleted. `resource_not_found` error is discarded.
|
|
||||||
-spec(ensure_resource_deleted(resource_id()) -> ok).
|
|
||||||
ensure_resource_deleted(ResId) ->
|
|
||||||
_ = delete_resource(ResId),
|
|
||||||
ok.
|
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Re-establish resources
|
%% Re-establish resources
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
|
|
|
@ -337,8 +337,11 @@ update_resource(#{id := Id}, Params) ->
|
||||||
end.
|
end.
|
||||||
|
|
||||||
delete_resource(#{id := Id}, _Params) ->
|
delete_resource(#{id := Id}, _Params) ->
|
||||||
ok = emqx_rule_engine:ensure_resource_deleted(Id),
|
case emqx_rule_engine:delete_resource(Id) of
|
||||||
return(ok).
|
ok -> return(ok);
|
||||||
|
{error, Reason} ->
|
||||||
|
return({error, 400, ?ERR_BADARGS(Reason)})
|
||||||
|
end.
|
||||||
|
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
%% Resource Types API
|
%% Resource Types API
|
||||||
|
|
|
@ -100,27 +100,21 @@ rules(["show", RuleId]) ->
|
||||||
|
|
||||||
rules(["create" | Params]) ->
|
rules(["create" | Params]) ->
|
||||||
with_opts(fun({Opts, _}) ->
|
with_opts(fun({Opts, _}) ->
|
||||||
try emqx_rule_engine:create_rule(make_rule(Opts)) of
|
case emqx_rule_engine:create_rule(make_rule(Opts)) of
|
||||||
{ok, #rule{id = RuleId}} ->
|
{ok, #rule{id = RuleId}} ->
|
||||||
emqx_ctl:print("Rule ~s created~n", [RuleId]);
|
emqx_ctl:print("Rule ~s created~n", [RuleId]);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
||||||
catch
|
|
||||||
throw:Error ->
|
|
||||||
emqx_ctl:print("Invalid options: ~0p~n", [Error])
|
|
||||||
end
|
end
|
||||||
end, Params, ?OPTSPEC_RULES_CREATE, {?FUNCTION_NAME, create});
|
end, Params, ?OPTSPEC_RULES_CREATE, {?FUNCTION_NAME, create});
|
||||||
|
|
||||||
rules(["update" | Params]) ->
|
rules(["update" | Params]) ->
|
||||||
with_opts(fun({Opts, _}) ->
|
with_opts(fun({Opts, _}) ->
|
||||||
try emqx_rule_engine:update_rule(make_updated_rule(Opts)) of
|
case emqx_rule_engine:update_rule(make_updated_rule(Opts)) of
|
||||||
{ok, #rule{id = RuleId}} ->
|
{ok, #rule{id = RuleId}} ->
|
||||||
emqx_ctl:print("Rule ~s updated~n", [RuleId]);
|
emqx_ctl:print("Rule ~s updated~n", [RuleId]);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
||||||
catch
|
|
||||||
throw:Error ->
|
|
||||||
emqx_ctl:print("Invalid options: ~0p~n", [Error])
|
|
||||||
end
|
end
|
||||||
end, Params, ?OPTSPEC_RULES_UPDATE, {?FUNCTION_NAME, update});
|
end, Params, ?OPTSPEC_RULES_UPDATE, {?FUNCTION_NAME, update});
|
||||||
|
|
||||||
|
@ -128,7 +122,7 @@ rules(["delete", RuleId]) ->
|
||||||
ok = emqx_rule_engine:delete_rule(list_to_binary(RuleId)),
|
ok = emqx_rule_engine:delete_rule(list_to_binary(RuleId)),
|
||||||
emqx_ctl:print("ok~n");
|
emqx_ctl:print("ok~n");
|
||||||
|
|
||||||
rules(_usage) ->
|
rules(_Usage) ->
|
||||||
emqx_ctl:usage([{"rules list", "List all rules"},
|
emqx_ctl:usage([{"rules list", "List all rules"},
|
||||||
{"rules show <RuleId>", "Show a rule"},
|
{"rules show <RuleId>", "Show a rule"},
|
||||||
{"rules create", "Create a rule"},
|
{"rules create", "Create a rule"},
|
||||||
|
@ -143,9 +137,10 @@ actions(["list"]) ->
|
||||||
print_all(get_actions());
|
print_all(get_actions());
|
||||||
|
|
||||||
actions(["show", ActionId]) ->
|
actions(["show", ActionId]) ->
|
||||||
print_with(fun emqx_rule_registry:find_action/1, ?RAISE(list_to_existing_atom(ActionId), {not_found, ActionId}));
|
print_with(fun emqx_rule_registry:find_action/1,
|
||||||
|
?RAISE(list_to_existing_atom(ActionId), {not_found, ActionId}));
|
||||||
|
|
||||||
actions(_usage) ->
|
actions(_Usage) ->
|
||||||
emqx_ctl:usage([{"rule-actions list", "List actions"},
|
emqx_ctl:usage([{"rule-actions list", "List actions"},
|
||||||
{"rule-actions show <ActionId>", "Show a rule action"}
|
{"rule-actions show <ActionId>", "Show a rule action"}
|
||||||
]).
|
]).
|
||||||
|
@ -155,27 +150,21 @@ actions(_usage) ->
|
||||||
%%------------------------------------------------------------------------------
|
%%------------------------------------------------------------------------------
|
||||||
resources(["create" | Params]) ->
|
resources(["create" | Params]) ->
|
||||||
with_opts(fun({Opts, _}) ->
|
with_opts(fun({Opts, _}) ->
|
||||||
try emqx_rule_engine:create_resource(make_resource(Opts)) of
|
case emqx_rule_engine:create_resource(make_resource(Opts)) of
|
||||||
{ok, #resource{id = ResId}} ->
|
{ok, #resource{id = ResId}} ->
|
||||||
emqx_ctl:print("Resource ~s created~n", [ResId]);
|
emqx_ctl:print("Resource ~s created~n", [ResId]);
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
||||||
catch
|
|
||||||
throw:Reason ->
|
|
||||||
emqx_ctl:print("Invalid options: ~0p~n", [Reason])
|
|
||||||
end
|
end
|
||||||
end, Params, ?OPTSPEC_RESOURCES_CREATE, {?FUNCTION_NAME, create});
|
end, Params, ?OPTSPEC_RESOURCES_CREATE, {?FUNCTION_NAME, create});
|
||||||
|
|
||||||
resources(["test" | Params]) ->
|
resources(["test" | Params]) ->
|
||||||
with_opts(fun({Opts, _}) ->
|
with_opts(fun({Opts, _}) ->
|
||||||
try emqx_rule_engine:test_resource(make_resource(Opts)) of
|
case emqx_rule_engine:test_resource(make_resource(Opts)) of
|
||||||
ok ->
|
ok ->
|
||||||
emqx_ctl:print("Test creating resource successfully (dry-run)~n");
|
emqx_ctl:print("Test creating resource successfully (dry-run)~n");
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
emqx_ctl:print("Test creating resource failed: ~0p~n", [Reason])
|
emqx_ctl:print("Test creating resource failed: ~0p~n", [Reason])
|
||||||
catch
|
|
||||||
throw:Reason ->
|
|
||||||
emqx_ctl:print("Test creating resource failed: ~0p~n", [Reason])
|
|
||||||
end
|
end
|
||||||
end, Params, ?OPTSPEC_RESOURCES_CREATE, {?FUNCTION_NAME, test});
|
end, Params, ?OPTSPEC_RESOURCES_CREATE, {?FUNCTION_NAME, test});
|
||||||
|
|
||||||
|
@ -192,15 +181,13 @@ resources(["show", ResourceId]) ->
|
||||||
print_with(fun emqx_rule_registry:find_resource/1, list_to_binary(ResourceId));
|
print_with(fun emqx_rule_registry:find_resource/1, list_to_binary(ResourceId));
|
||||||
|
|
||||||
resources(["delete", ResourceId]) ->
|
resources(["delete", ResourceId]) ->
|
||||||
try
|
case emqx_rule_engine:delete_resource(list_to_binary(ResourceId)) of
|
||||||
ok = emqx_rule_engine:delete_resource(list_to_binary(ResourceId)),
|
ok -> emqx_ctl:print("ok~n");
|
||||||
emqx_ctl:print("ok~n")
|
{error, Reason} ->
|
||||||
catch
|
|
||||||
_Error:Reason ->
|
|
||||||
emqx_ctl:print("Cannot delete resource as ~0p~n", [Reason])
|
emqx_ctl:print("Cannot delete resource as ~0p~n", [Reason])
|
||||||
end;
|
end;
|
||||||
|
|
||||||
resources(_usage) ->
|
resources(_Usage) ->
|
||||||
emqx_ctl:usage([{"resources create", "Create a resource"},
|
emqx_ctl:usage([{"resources create", "Create a resource"},
|
||||||
{"resources list [-t <ResourceType>]", "List resources"},
|
{"resources list [-t <ResourceType>]", "List resources"},
|
||||||
{"resources show <ResourceId>", "Show a resource"},
|
{"resources show <ResourceId>", "Show a resource"},
|
||||||
|
@ -216,7 +203,7 @@ resource_types(["list"]) ->
|
||||||
resource_types(["show", Name]) ->
|
resource_types(["show", Name]) ->
|
||||||
print_with(fun emqx_rule_registry:find_resource_type/1, list_to_atom(Name));
|
print_with(fun emqx_rule_registry:find_resource_type/1, list_to_atom(Name));
|
||||||
|
|
||||||
resource_types(_usage) ->
|
resource_types(_Usage) ->
|
||||||
emqx_ctl:usage([{"resource-types list", "List all resource-types"},
|
emqx_ctl:usage([{"resource-types list", "List all resource-types"},
|
||||||
{"resource-types show <Type>", "Show a resource-type"}
|
{"resource-types show <Type>", "Show a resource-type"}
|
||||||
]).
|
]).
|
||||||
|
@ -365,4 +352,4 @@ rmlf(Str) ->
|
||||||
re:replace(Str, "\n", "", [global]).
|
re:replace(Str, "\n", "", [global]).
|
||||||
|
|
||||||
untilde(Str) ->
|
untilde(Str) ->
|
||||||
re:replace(Str,"~","&&",[{return,list}, global]).
|
re:replace(Str, "~", "&&", [{return, list}, global]).
|
||||||
|
|
Loading…
Reference in New Issue