|
|
@@ -37,7 +37,8 @@
|
|
|
|
|
|
%% APIs for instances
|
|
|
|
|
|
--export([ parse_config/2
|
|
|
+-export([ check_config/2
|
|
|
+ , check_and_load_instance/3
|
|
|
, resource_type_from_str/1
|
|
|
]).
|
|
|
|
|
|
@@ -73,9 +74,6 @@
|
|
|
, list_instances_verbose/0 %% list all the instances
|
|
|
, get_instance/1 %% return the data of the instance
|
|
|
, get_instance_by_type/1 %% return all the instances of the same resource type
|
|
|
- , load_instances_from_dir/1 %% load instances from a directory
|
|
|
- , load_instance_from_file/1 %% load an instance from a config file
|
|
|
- , load_instance_from_config/1 %% load an instance from a map or json-string config
|
|
|
% , dependents/1
|
|
|
% , inc_counter/2 %% increment the counter of the instance
|
|
|
% , inc_counter/3 %% increment the counter by a given integer
|
|
|
@@ -215,18 +213,6 @@ list_instances_verbose() ->
|
|
|
get_instance_by_type(ResourceType) ->
|
|
|
emqx_resource_instance:lookup_by_type(ResourceType).
|
|
|
|
|
|
--spec load_instances_from_dir(Dir :: string()) -> ok.
|
|
|
-load_instances_from_dir(Dir) ->
|
|
|
- emqx_resource_instance:load_dir(Dir).
|
|
|
-
|
|
|
--spec load_instance_from_file(File :: string()) -> ok.
|
|
|
-load_instance_from_file(File) ->
|
|
|
- emqx_resource_instance:load_file(File).
|
|
|
-
|
|
|
--spec load_instance_from_config(binary() | map()) -> ok.
|
|
|
-load_instance_from_config(Config) ->
|
|
|
- emqx_resource_instance:load_config(Config).
|
|
|
-
|
|
|
-spec call_start(instance_id(), module(), resource_config()) ->
|
|
|
{ok, resource_state()} | {error, Reason :: term()}.
|
|
|
call_start(InstId, Mod, Config) ->
|
|
|
@@ -267,24 +253,30 @@ call_api_reply_format(Mod, Data) ->
|
|
|
true -> ?SAFE_CALL(Mod:on_api_reply_format(Data))
|
|
|
end.
|
|
|
|
|
|
--spec parse_config(resource_type(), binary() | term()) ->
|
|
|
+-spec check_config(resource_type(), binary() | term()) ->
|
|
|
{ok, resource_config()} | {error, term()}.
|
|
|
-parse_config(ResourceType, RawConfig) when is_binary(RawConfig) ->
|
|
|
+check_config(ResourceType, RawConfig) when is_binary(RawConfig) ->
|
|
|
case hocon:binary(RawConfig, #{format => richmap}) of
|
|
|
{ok, MapConfig} ->
|
|
|
- do_parse_config(ResourceType, MapConfig);
|
|
|
+ do_check_config(ResourceType, MapConfig);
|
|
|
Error -> Error
|
|
|
end;
|
|
|
-parse_config(ResourceType, RawConfigTerm) ->
|
|
|
- parse_config(ResourceType, jsx:encode(#{config => RawConfigTerm})).
|
|
|
+check_config(ResourceType, RawConfigTerm) ->
|
|
|
+ check_config(ResourceType, jsx:encode(#{config => RawConfigTerm})).
|
|
|
|
|
|
--spec do_parse_config(resource_type(), map()) -> {ok, resource_config()} | {error, term()}.
|
|
|
-do_parse_config(ResourceType, MapConfig) ->
|
|
|
- case ?SAFE_CALL(hocon_schema:generate(ResourceType, MapConfig)) of
|
|
|
+-spec do_check_config(resource_type(), map()) -> {ok, resource_config()} | {error, term()}.
|
|
|
+do_check_config(ResourceType, MapConfig) ->
|
|
|
+ case ?SAFE_CALL(hocon_schema:check(ResourceType, MapConfig)) of
|
|
|
{error, Reason} -> {error, Reason};
|
|
|
- Config ->
|
|
|
- InstConf = maps:from_list(proplists:get_value(config, Config)),
|
|
|
- {ok, InstConf}
|
|
|
+ Config -> {ok, maps:get(<<"config">>, hocon:richmap_to_map(Config))}
|
|
|
+ end.
|
|
|
+
|
|
|
+-spec check_and_load_instance(instance_id(), resource_type(), binary() | term()) ->
|
|
|
+ {ok, resource_data()} | {error, term()}.
|
|
|
+check_and_load_instance(InstId, ResourceType, Config) ->
|
|
|
+ case check_config(ResourceType, Config) of
|
|
|
+ {ok, InstConf} -> emqx_resource_instance:create_local(InstId, ResourceType, InstConf);
|
|
|
+ Error -> Error
|
|
|
end.
|
|
|
|
|
|
%% =================================================================================
|