diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index d03dbc782..648086ec4 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -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] diff --git a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.appup.src b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.appup.src index 52b4e24fc..21fc2d8b1 100644 --- a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.appup.src +++ b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.appup.src @@ -31,4 +31,4 @@ ]}, {<<".*">>, []} ] -}. +}. \ No newline at end of file diff --git a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.erl b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.erl index 3bd12564c..ecb425226 100644 --- a/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.erl +++ b/apps/emqx_bridge_mqtt/src/emqx_bridge_mqtt.erl @@ -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; diff --git a/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_tests.erl b/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_tests.erl index 830fb1fe0..d37f1680b 100644 --- a/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_tests.erl +++ b/apps/emqx_bridge_mqtt/test/emqx_bridge_mqtt_tests.erl @@ -44,4 +44,15 @@ send_and_ack_test() -> ok = emqx_bridge_mqtt:stop(Conn) after meck:unload(emqtt) - end. \ No newline at end of file + 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).