Merge pull request #7014 from gsychev/bridge_config_topic_node_template_43

Bridge config topic node template
This commit is contained in:
gsychev 2022-02-25 11:56:23 +00:00 committed by GitHub
commit 3af36378dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 7 deletions

View File

@ -24,6 +24,8 @@ File format:
* Refactor the ExProto so that anonymous clients can also be displayed on the dashboard [#6983]
* Force shutdown of processe that cannot answer takeover event [#7026]
* `topic` parameter in bridge configuration can have `${node}` substitution (just like in `clientid` parameter)
### Bug fixes
* Fix the `{error,eexist}` error when do release upgrade again if last run failed. [#7121]

View File

@ -37,6 +37,11 @@
, handle_disconnected/2
]).
%% for testing
-ifdef(TEST).
-export([ replvar/1 ]).
-endif.
-include_lib("emqx/include/logger.hrl").
-include_lib("emqx/include/emqx_mqtt.hrl").
@ -176,13 +181,13 @@ subscribe_remote_topics(ClientPid, Subscriptions) ->
end
end, Subscriptions).
replvar(Options) ->
replvar([topic, clientid, max_inflight], Options).
%%--------------------------------------------------------------------
%% Internal funcs
%%--------------------------------------------------------------------
replvar(Options) ->
replvar([clientid, max_inflight], Options).
replvar([], Options) ->
Options;
replvar([Key|More], Options) ->
@ -194,8 +199,8 @@ replvar([Key|More], Options) ->
end.
%% ${node} => node()
feedvar(clientid, ClientId, _) ->
iolist_to_binary(re:replace(ClientId, "\\${node}", atom_to_list(node())));
feedvar(Key, Value, _) when Key =:= topic; Key =:= clientid ->
iolist_to_binary(re:replace(Value, "\\${node}", atom_to_list(node())));
feedvar(max_inflight, 0, _) ->
infinity;

View File

@ -45,3 +45,14 @@ send_and_ack_test() ->
after
meck:unload(emqtt)
end.
replvar_test() ->
Node = atom_to_list(node()),
Config = #{clientid => <<"Hey ${node}">>, topic => <<"topic ${node}">>, other => <<"other">>},
ReplacedConfig = emqx_bridge_mqtt:replvar(Config),
ExpectedConfig = #{clientid => iolist_to_binary("Hey " ++ Node),
topic => iolist_to_binary("topic " ++ Node),
other => <<"other">>},
?assertEqual(ExpectedConfig, ReplacedConfig).