chore: move redis_type into paramaters in actions

This commit is contained in:
zhongwencool 2023-12-19 17:09:08 +08:00
parent f61f267ac4
commit 1c17121d44
5 changed files with 29 additions and 31 deletions

View File

@ -101,7 +101,14 @@ namespace() -> "bridge_redis".
roots() -> []. roots() -> [].
fields(action_parameters) -> fields(action_parameters) ->
[{command_template, fun command_template/1}]; [
command_template(),
{redis_type,
?HOCON(
?ENUM([single, sentinel, cluster]),
#{required => true, desc => ?DESC(redis_type)}
)}
];
fields("post_single") -> fields("post_single") ->
method_fields(post, redis_single); method_fields(post, redis_single);
fields("post_sentinel") -> fields("post_sentinel") ->
@ -147,8 +154,8 @@ method_fields(put, ConnectorType) ->
redis_bridge_common_fields(Type) -> redis_bridge_common_fields(Type) ->
emqx_bridge_schema:common_bridge_fields() ++ emqx_bridge_schema:common_bridge_fields() ++
[ [
{local_topic, mk(binary(), #{required => false, desc => ?DESC("desc_local_topic")})} {local_topic, mk(binary(), #{required => false, desc => ?DESC("desc_local_topic")})},
| fields(action_parameters) command_template()
] ++ ] ++
v1_resource_fields(Type). v1_resource_fields(Type).
@ -222,3 +229,6 @@ is_command_template_valid(CommandSegments) ->
"the value of the field 'command_template' should be a nonempty " "the value of the field 'command_template' should be a nonempty "
"list of strings (templates for Redis command and arguments)"} "list of strings (templates for Redis command and arguments)"}
end. end.
command_template() ->
{command_template, fun command_template/1}.

View File

@ -76,13 +76,7 @@ fields(redis_action) ->
) )
), ),
[ResOpts] = emqx_connector_schema:resource_opts_ref(?MODULE, action_resource_opts), [ResOpts] = emqx_connector_schema:resource_opts_ref(?MODULE, action_resource_opts),
RedisType = lists:keyreplace(resource_opts, 1, Schema, ResOpts);
{redis_type,
?HOCON(
?ENUM([single, sentinel, cluster]),
#{required => true, desc => ?DESC(redis_type)}
)},
[RedisType | lists:keyreplace(resource_opts, 1, Schema, ResOpts)];
fields(action_resource_opts) -> fields(action_resource_opts) ->
emqx_bridge_v2_schema:resource_opts_fields([ emqx_bridge_v2_schema:resource_opts_fields([
{batch_size, #{desc => ?DESC(batch_size)}}, {batch_size, #{desc => ?DESC(batch_size)}},
@ -130,7 +124,7 @@ resource_opts_converter(Conf, _Opts) ->
maps:map( maps:map(
fun(_Name, SubConf) -> fun(_Name, SubConf) ->
case SubConf of case SubConf of
#{<<"redis_type">> := <<"cluster">>} -> #{<<"parameters">> := #{<<"redis_type">> := <<"cluster">>}} ->
ResOpts = maps:get(<<"resource_opts">>, SubConf, #{}), ResOpts = maps:get(<<"resource_opts">>, SubConf, #{}),
%% cluster don't support batch %% cluster don't support batch
SubConf#{ SubConf#{
@ -218,12 +212,12 @@ action_example(RedisType, get) ->
); );
action_example(RedisType, put) -> action_example(RedisType, put) ->
#{ #{
redis_type => RedisType,
enable => true, enable => true,
connector => <<"my_connector_name">>, connector => <<"my_connector_name">>,
description => <<"My action">>, description => <<"My action">>,
parameters => #{ parameters => #{
command_template => [<<"LPUSH">>, <<"MSGS">>, <<"${payload}">>] command_template => [<<"LPUSH">>, <<"MSGS">>, <<"${payload}">>],
redis_type => RedisType
}, },
resource_opts => #{batch_size => 1} resource_opts => #{batch_size => 1}
}. }.

View File

@ -229,7 +229,10 @@ action_config(Name, Path, ConnectorId) ->
<<"enable">> => true, <<"enable">> => true,
<<"connector">> => ConnectorId, <<"connector">> => ConnectorId,
<<"parameters">> => <<"parameters">> =>
#{<<"command_template">> => [<<"RPUSH">>, <<"MSGS/${topic}">>, <<"${payload}">>]}, #{
<<"command_template">> => [<<"RPUSH">>, <<"MSGS/${topic}">>, <<"${payload}">>],
<<"redis_type">> => atom_to_binary(RedisType)
},
<<"local_topic">> => <<"t/redis">>, <<"local_topic">> => <<"t/redis">>,
<<"resource_opts">> => #{ <<"resource_opts">> => #{
<<"batch_size">> => 1, <<"batch_size">> => 1,
@ -246,18 +249,9 @@ action_config(Name, Path, ConnectorId) ->
<<"worker_pool_size">> => <<"1">> <<"worker_pool_size">> => <<"1">>
} }
}, },
PerTypeCfg = per_type_action_config(RedisType), InnerConfigMap = serde_roundtrip(CommonCfg),
InnerConfigMap0 = emqx_utils_maps:deep_merge(CommonCfg, PerTypeCfg),
InnerConfigMap = serde_roundtrip(InnerConfigMap0),
parse_and_check_bridge_config(InnerConfigMap, Name). parse_and_check_bridge_config(InnerConfigMap, Name).
per_type_action_config(single) ->
#{<<"redis_type">> => <<"single">>};
per_type_action_config(sentinel) ->
#{<<"redis_type">> => <<"sentinel">>};
per_type_action_config(cluster) ->
#{<<"redis_type">> => <<"cluster">>}.
%% check it serializes correctly %% check it serializes correctly
serde_roundtrip(InnerConfigMap0) -> serde_roundtrip(InnerConfigMap0) ->
IOList = hocon_pp:do(InnerConfigMap0, #{}), IOList = hocon_pp:do(InnerConfigMap0, #{}),

View File

@ -1,5 +1,12 @@
emqx_bridge_redis { emqx_bridge_redis {
redis_type.label:
"""Redis Type"""
redis_type.desc:
"""Single mode. Must be set to 'single' when Redis server is running in single mode.
Sentinel mode. Must be set to 'sentinel' when Redis server is running in sentinel mode.
Cluster mode. Must be set to 'cluster' when Redis server is running in clustered mode."""
command_template.desc: command_template.desc:
"""Redis command template used to export messages. Each list element stands for a command name or its argument. """Redis command template used to export messages. Each list element stands for a command name or its argument.
For example, to push payloads in a Redis list by key `msgs`, the elements should be the following: For example, to push payloads in a Redis list by key `msgs`, the elements should be the following:

View File

@ -10,13 +10,6 @@ producer_action.desc:
producer_action.label: producer_action.label:
"""Action Parameters""" """Action Parameters"""
redis_type.label:
"""Redis Type"""
redis_type.desc:
"""Single mode. Must be set to 'single' when Redis server is running in single mode.
Sentinel mode. Must be set to 'sentinel' when Redis server is running in sentinel mode.
Cluster mode. Must be set to 'cluster' when Redis server is running in clustered mode."""
batch_size.label: batch_size.label:
"""Batch Size""" """Batch Size"""
batch_size.desc: batch_size.desc: