chore: create_local/5 for emqx_resource_proto_v1

This commit is contained in:
zhongwencool 2024-07-17 12:45:37 +08:00
parent f29988ed8e
commit 2bb062d3a3
5 changed files with 56 additions and 34 deletions

View File

@ -187,7 +187,8 @@ do_create(
ResourceId, ResourceId,
?AUTHN_RESOURCE_GROUP, ?AUTHN_RESOURCE_GROUP,
emqx_authn_jwks_connector, emqx_authn_jwks_connector,
connector_opts(Config) connector_opts(Config),
#{}
), ),
{ok, #{ {ok, #{
jwk_resource => ResourceId, jwk_resource => ResourceId,

View File

@ -199,9 +199,13 @@ test_user_auth(#{
t_authenticate_disabled_prepared_statements(_Config) -> t_authenticate_disabled_prepared_statements(_Config) ->
ResConfig = maps:merge(pgsql_config(), #{disable_prepared_statements => true}), ResConfig = maps:merge(pgsql_config(), #{disable_prepared_statements => true}),
{ok, _} = emqx_resource:recreate_local(postgresql, ?PGSQL_RESOURCE, emqx_postgresql, ResConfig), {ok, _} = emqx_resource:recreate_local(
postgresql, ?PGSQL_RESOURCE, emqx_postgresql, ResConfig, #{}
),
on_exit(fun() -> on_exit(fun() ->
emqx_resource:recreate_local(postgresql, ?PGSQL_RESOURCE, emqx_postgresql, pgsql_config()) emqx_resource:recreate_local(
postgresql, ?PGSQL_RESOURCE, emqx_postgresql, pgsql_config(), #{}
)
end), end),
ok = lists:foreach( ok = lists:foreach(
fun(Sample0) -> fun(Sample0) ->

View File

@ -39,12 +39,10 @@
-export([ -export([
%% store the config and start the instance %% store the config and start the instance
create_local/5,
create_local/6, create_local/6,
create_dry_run_local/3, create_dry_run_local/3,
create_dry_run_local/4, create_dry_run_local/4,
create_dry_run_local/5, create_dry_run_local/5,
recreate_local/4,
recreate_local/5, recreate_local/5,
%% remove the config and stop the instance %% remove the config and stop the instance
remove_local/1, remove_local/1,
@ -142,6 +140,9 @@
-export([is_dry_run/1]). -export([is_dry_run/1]).
%% For emqx_resource_proto_v1 rpc only
-export([create_local/5, recreate_local/4, create_dry_run_local/2]).
-export_type([ -export_type([
query_mode/0, query_mode/0,
resource_id/0, resource_id/0,
@ -281,12 +282,6 @@ is_resource_mod(Module) ->
%% ================================================================================= %% =================================================================================
%% APIs for resource instances %% APIs for resource instances
%% ================================================================================= %% =================================================================================
-spec create_local(type(), resource_id(), resource_group(), resource_type(), resource_config()) ->
{ok, resource_data() | 'already_created'} | {error, Reason :: term()}.
create_local(Type, ResId, Group, ResourceType, Config) ->
create_local(Type, ResId, Group, ResourceType, Config, #{}).
-spec create_local( -spec create_local(
type(), type(),
resource_id(), resource_id(),
@ -320,11 +315,6 @@ when
create_dry_run_local(Type, ResId, ResourceType, Config, OnReadyCallback) -> create_dry_run_local(Type, ResId, ResourceType, Config, OnReadyCallback) ->
emqx_resource_manager:create_dry_run(Type, ResId, ResourceType, Config, OnReadyCallback). emqx_resource_manager:create_dry_run(Type, ResId, ResourceType, Config, OnReadyCallback).
-spec recreate_local(type(), resource_id(), resource_type(), resource_config()) ->
{ok, resource_data()} | {error, Reason :: term()}.
recreate_local(Type, ResId, ResourceType, Config) ->
recreate_local(Type, ResId, ResourceType, Config, #{}).
-spec recreate_local(type(), resource_id(), resource_type(), resource_config(), creation_opts()) -> -spec recreate_local(type(), resource_id(), resource_type(), resource_config(), creation_opts()) ->
{ok, resource_data()} | {error, Reason :: term()}. {ok, resource_data()} | {error, Reason :: term()}.
recreate_local(Type, ResId, ResourceType, Config, Opts) -> recreate_local(Type, ResId, ResourceType, Config, Opts) ->
@ -339,11 +329,15 @@ remove_local(ResId) ->
ok; ok;
Error -> Error ->
%% Only log, the ResId worker is always removed in manager's remove action. %% Only log, the ResId worker is always removed in manager's remove action.
?SLOG(warning, #{ ?SLOG(
msg => "remove_resource_failed", warning,
error => Error, #{
resource_id => ResId msg => "remove_resource_failed",
}), error => Error,
resource_id => ResId
},
#{tag => ?TAG}
),
ok ok
end. end.
@ -815,3 +809,18 @@ validate_name(Name, Opts) ->
-spec invalid_data(binary()) -> no_return(). -spec invalid_data(binary()) -> no_return().
invalid_data(Reason) -> throw(#{kind => validation_error, reason => Reason}). invalid_data(Reason) -> throw(#{kind => validation_error, reason => Reason}).
%% Those functions is only used in the emqx_resource_proto_v1
%% for versions that are less than version 5.6.0.
%% begin
-spec create_local(
resource_id(), resource_group(), resource_type(), resource_config(), creation_opts()
) ->
{ok, resource_data() | 'already_created'} | {error, Reason :: term()}.
create_local(ResId, Group, ResourceType, Config, Opts) ->
create_local(deprecated, ResId, Group, ResourceType, Config, Opts).
create_dry_run_local(ResourceType, Config) ->
create_dry_run_local(deprecated, ResourceType, Config).
recreate_local(ResId, ResourceType, Config, Opts) ->
recreate_local(deprecated, ResId, ResourceType, Config, Opts).
%% end

View File

@ -38,7 +38,7 @@
start(Name, Mod, Options) -> start(Name, Mod, Options) ->
case ecpool:start_sup_pool(Name, Mod, Options) of case ecpool:start_sup_pool(Name, Mod, Options) of
{ok, _} -> {ok, _} ->
?SLOG(info, #{msg => "start_ecpool_ok", pool_name => Name}), ?SLOG(info, #{msg => "start_ecpool_ok", pool_name => Name}, #{tag => ?TAG}),
ok; ok;
{error, {already_started, _Pid}} -> {error, {already_started, _Pid}} ->
stop(Name), stop(Name),
@ -46,27 +46,35 @@ start(Name, Mod, Options) ->
{error, Reason} -> {error, Reason} ->
NReason = parse_reason(Reason), NReason = parse_reason(Reason),
IsDryRun = emqx_resource:is_dry_run(Name), IsDryRun = emqx_resource:is_dry_run(Name),
?SLOG(?LOG_LEVEL(IsDryRun), #{ ?SLOG(
msg => "start_ecpool_error", ?LOG_LEVEL(IsDryRun),
resource_id => Name, #{
reason => NReason msg => "start_ecpool_error",
}), resource_id => Name,
reason => NReason
},
#{tag => ?TAG}
),
{error, {start_pool_failed, Name, NReason}} {error, {start_pool_failed, Name, NReason}}
end. end.
stop(Name) -> stop(Name) ->
case ecpool:stop_sup_pool(Name) of case ecpool:stop_sup_pool(Name) of
ok -> ok ->
?SLOG(info, #{msg => "stop_ecpool_ok", pool_name => Name}); ?SLOG(info, #{msg => "stop_ecpool_ok", pool_name => Name}, #{tag => ?TAG});
{error, not_found} -> {error, not_found} ->
ok; ok;
{error, Reason} -> {error, Reason} ->
IsDryRun = emqx_resource:is_dry_run(Name), IsDryRun = emqx_resource:is_dry_run(Name),
?SLOG(?LOG_LEVEL(IsDryRun), #{ ?SLOG(
msg => "stop_ecpool_failed", ?LOG_LEVEL(IsDryRun),
resource_id => Name, #{
reason => Reason msg => "stop_ecpool_failed",
}), resource_id => Name,
reason => Reason
},
#{tag => ?TAG}
),
error({stop_pool_failed, Name, Reason}) error({stop_pool_failed, Name, Reason})
end. end.

View File

@ -3506,7 +3506,7 @@ gauge_metric_set_fns() ->
]. ].
create(Id, Group, Type, Config) -> create(Id, Group, Type, Config) ->
emqx_resource:create_local(test, Id, Group, Type, Config). emqx_resource:create_local(test, Id, Group, Type, Config, #{}).
create(Id, Group, Type, Config, Opts) -> create(Id, Group, Type, Config, Opts) ->
emqx_resource:create_local(test, Id, Group, Type, Config, Opts). emqx_resource:create_local(test, Id, Group, Type, Config, Opts).