refactor(resource): rename the emqx_resource:update/4 to recreate/4

This commit is contained in:
Shawn 2021-09-14 11:36:20 +08:00
parent 75f528e08d
commit 304c5613ac
3 changed files with 29 additions and 28 deletions

View File

@ -392,7 +392,7 @@ gen_id(Type) ->
create_resource(#{type := DB,
annotations := #{id := ResourceID}} = Source) ->
case emqx_resource:update(ResourceID, connector_module(DB), Source, []) of
case emqx_resource:recreate(ResourceID, connector_module(DB), Source, []) of
{ok, _} -> ResourceID;
{error, Reason} -> {error, Reason}
end;

View File

@ -40,8 +40,8 @@
-export([ check_config/2
, check_and_create/3
, check_and_create_local/3
, check_and_update/4
, check_and_update_local/4
, check_and_recreate/4
, check_and_recreate_local/4
, resource_type_from_str/1
]).
@ -50,10 +50,10 @@
%% todo: replicate operations
-export([ create/3 %% store the config and start the instance
, create_local/3
, create_dry_run/3 %% run start/2, health_check/2 and stop/1 sequentially
, create_dry_run_local/3
, update/4 %% update the config, stop the old instance and start the new one
, update_local/4
, create_dry_run/2 %% run start/2, health_check/2 and stop/1 sequentially
, create_dry_run_local/2
, recreate/4 %% this will do create_dry_run, stop the old instance and start a new one
, recreate_local/4
, remove/1 %% remove the config and stop the instance
, remove_local/1
]).
@ -164,25 +164,26 @@ create(InstId, ResourceType, Config) ->
create_local(InstId, ResourceType, Config) ->
call_instance(InstId, {create, InstId, ResourceType, Config}).
-spec create_dry_run(instance_id(), resource_type(), resource_config()) ->
-spec create_dry_run(resource_type(), resource_config()) ->
ok | {error, Reason :: term()}.
create_dry_run(InstId, ResourceType, Config) ->
cluster_call(create_dry_run_local, [InstId, ResourceType, Config]).
create_dry_run(ResourceType, Config) ->
cluster_call(create_dry_run_local, [ResourceType, Config]).
-spec create_dry_run_local(instance_id(), resource_type(), resource_config()) ->
-spec create_dry_run_local(resource_type(), resource_config()) ->
ok | {error, Reason :: term()}.
create_dry_run_local(InstId, ResourceType, Config) ->
create_dry_run_local(ResourceType, Config) ->
InstId = emqx_plugin_libs_id:gen(16),
call_instance(InstId, {create_dry_run, InstId, ResourceType, Config}).
-spec update(instance_id(), resource_type(), resource_config(), term()) ->
-spec recreate(instance_id(), resource_type(), resource_config(), term()) ->
{ok, resource_data()} | {error, Reason :: term()}.
update(InstId, ResourceType, Config, Params) ->
cluster_call(update_local, [InstId, ResourceType, Config, Params]).
recreate(InstId, ResourceType, Config, Params) ->
cluster_call(recreate_local, [InstId, ResourceType, Config, Params]).
-spec update_local(instance_id(), resource_type(), resource_config(), term()) ->
-spec recreate_local(instance_id(), resource_type(), resource_config(), term()) ->
{ok, resource_data()} | {error, Reason :: term()}.
update_local(InstId, ResourceType, Config, Params) ->
call_instance(InstId, {update, InstId, ResourceType, Config, Params}).
recreate_local(InstId, ResourceType, Config, Params) ->
call_instance(InstId, {recreate, InstId, ResourceType, Config, Params}).
-spec remove(instance_id()) -> ok | {error, Reason :: term()}.
remove(InstId) ->
@ -296,17 +297,17 @@ check_and_create_local(InstId, ResourceType, RawConfig) ->
check_and_do(ResourceType, RawConfig,
fun(InstConf) -> create_local(InstId, ResourceType, InstConf) end).
-spec check_and_update(instance_id(), resource_type(), raw_resource_config(), term()) ->
-spec check_and_recreate(instance_id(), resource_type(), raw_resource_config(), term()) ->
{ok, resource_data()} | {error, term()}.
check_and_update(InstId, ResourceType, RawConfig, Params) ->
check_and_recreate(InstId, ResourceType, RawConfig, Params) ->
check_and_do(ResourceType, RawConfig,
fun(InstConf) -> update(InstId, ResourceType, InstConf, Params) end).
fun(InstConf) -> recreate(InstId, ResourceType, InstConf, Params) end).
-spec check_and_update_local(instance_id(), resource_type(), raw_resource_config(), term()) ->
-spec check_and_recreate_local(instance_id(), resource_type(), raw_resource_config(), term()) ->
{ok, resource_data()} | {error, term()}.
check_and_update_local(InstId, ResourceType, RawConfig, Params) ->
check_and_recreate_local(InstId, ResourceType, RawConfig, Params) ->
check_and_do(ResourceType, RawConfig,
fun(InstConf) -> update_local(InstId, ResourceType, InstConf, Params) end).
fun(InstConf) -> recreate_local(InstId, ResourceType, InstConf, Params) end).
check_and_do(ResourceType, RawConfig, Do) when is_function(Do) ->
case check_config(ResourceType, RawConfig) of

View File

@ -107,8 +107,8 @@ handle_call({create, InstId, ResourceType, Config}, _From, State) ->
handle_call({create_dry_run, InstId, ResourceType, Config}, _From, State) ->
{reply, do_create_dry_run(InstId, ResourceType, Config), State};
handle_call({update, InstId, ResourceType, Config, Params}, _From, State) ->
{reply, do_update(InstId, ResourceType, Config, Params), State};
handle_call({recreate, InstId, ResourceType, Config, Params}, _From, State) ->
{reply, do_recreate(InstId, ResourceType, Config, Params), State};
handle_call({remove, InstId}, _From, State) ->
{reply, do_remove(InstId), State};
@ -141,8 +141,8 @@ code_change(_OldVsn, State, _Extra) ->
%%------------------------------------------------------------------------------
%% suppress the race condition check, as these functions are protected in gproc workers
-dialyzer({nowarn_function, [do_update/4, do_create/3, do_restart/1, do_stop/1, do_health_check/1]}).
do_update(InstId, ResourceType, NewConfig, Params) ->
-dialyzer({nowarn_function, [do_recreate/4, do_create/3, do_restart/1, do_stop/1, do_health_check/1]}).
do_recreate(InstId, ResourceType, NewConfig, Params) ->
case lookup(InstId) of
{ok, #{mod := ResourceType, state := ResourceState, config := OldConfig}} ->
Config = emqx_resource:call_config_merge(ResourceType, OldConfig,