feat: pass along client attributes down to message transformation context
This commit is contained in:
parent
5629fe60c1
commit
6e0ef893f4
|
@ -685,20 +685,23 @@ process_publish(Packet = ?PUBLISH_PACKET(QoS, Topic, PacketId), Channel) ->
|
||||||
|
|
||||||
packet_to_message(Packet, #channel{
|
packet_to_message(Packet, #channel{
|
||||||
conninfo = #{proto_ver := ProtoVer},
|
conninfo = #{proto_ver := ProtoVer},
|
||||||
clientinfo = #{
|
clientinfo =
|
||||||
protocol := Protocol,
|
#{
|
||||||
clientid := ClientId,
|
protocol := Protocol,
|
||||||
username := Username,
|
clientid := ClientId,
|
||||||
peerhost := PeerHost,
|
username := Username,
|
||||||
mountpoint := MountPoint
|
peerhost := PeerHost,
|
||||||
}
|
mountpoint := MountPoint
|
||||||
|
} = ClientInfo
|
||||||
}) ->
|
}) ->
|
||||||
|
ClientAttrs = maps:get(client_attrs, ClientInfo, #{}),
|
||||||
emqx_mountpoint:mount(
|
emqx_mountpoint:mount(
|
||||||
MountPoint,
|
MountPoint,
|
||||||
emqx_packet:to_message(
|
emqx_packet:to_message(
|
||||||
Packet,
|
Packet,
|
||||||
ClientId,
|
ClientId,
|
||||||
#{
|
#{
|
||||||
|
client_attrs => ClientAttrs,
|
||||||
proto_ver => ProtoVer,
|
proto_ver => ProtoVer,
|
||||||
protocol => Protocol,
|
protocol => Protocol,
|
||||||
username => Username,
|
username => Username,
|
||||||
|
|
|
@ -45,6 +45,7 @@
|
||||||
-type rendered_value() :: qos() | boolean() | binary().
|
-type rendered_value() :: qos() | boolean() | binary().
|
||||||
|
|
||||||
-type eval_context() :: #{
|
-type eval_context() :: #{
|
||||||
|
client_attrs := map(),
|
||||||
payload := _,
|
payload := _,
|
||||||
qos := _,
|
qos := _,
|
||||||
retain := _,
|
retain := _,
|
||||||
|
@ -309,6 +310,7 @@ message_to_context(#message{} = Message, Payload, Transformation) ->
|
||||||
end,
|
end,
|
||||||
#{
|
#{
|
||||||
dirty => Dirty,
|
dirty => Dirty,
|
||||||
|
client_attrs => emqx_message:get_header(client_attrs, Message, #{}),
|
||||||
payload => Payload,
|
payload => Payload,
|
||||||
qos => Message#message.qos,
|
qos => Message#message.qos,
|
||||||
retain => emqx_message:get_flag(retain, Message, false),
|
retain => emqx_message:get_flag(retain, Message, false),
|
||||||
|
|
|
@ -250,7 +250,11 @@ connect(ClientId) ->
|
||||||
connect(ClientId, _IsPersistent = false).
|
connect(ClientId, _IsPersistent = false).
|
||||||
|
|
||||||
connect(ClientId, IsPersistent) ->
|
connect(ClientId, IsPersistent) ->
|
||||||
Properties = emqx_utils_maps:put_if(#{}, 'Session-Expiry-Interval', 30, IsPersistent),
|
connect(ClientId, IsPersistent, _Opts = #{}).
|
||||||
|
|
||||||
|
connect(ClientId, IsPersistent, Opts) ->
|
||||||
|
Properties0 = maps:get(properties, Opts, #{}),
|
||||||
|
Properties = emqx_utils_maps:put_if(Properties0, 'Session-Expiry-Interval', 30, IsPersistent),
|
||||||
{ok, Client} = emqtt:start_link([
|
{ok, Client} = emqtt:start_link([
|
||||||
{clean_start, true},
|
{clean_start, true},
|
||||||
{clientid, ClientId},
|
{clientid, ClientId},
|
||||||
|
@ -1441,3 +1445,37 @@ t_json_encode_decode_smoke_test(_Config) ->
|
||||||
[]
|
[]
|
||||||
),
|
),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
|
%% Simple smoke test for client attributes support.
|
||||||
|
t_client_attrs(_Config) ->
|
||||||
|
{ok, Compiled} = emqx_variform:compile(<<"user_property.tenant">>),
|
||||||
|
ok = emqx_config:put_zone_conf(default, [mqtt, client_attrs_init], [
|
||||||
|
#{
|
||||||
|
expression => Compiled,
|
||||||
|
set_as_attr => <<"tenant">>
|
||||||
|
}
|
||||||
|
]),
|
||||||
|
on_exit(fun() -> ok = emqx_config:put_zone_conf(default, [mqtt, client_attrs_init], []) end),
|
||||||
|
?check_trace(
|
||||||
|
begin
|
||||||
|
Name1 = <<"foo">>,
|
||||||
|
Operation1 = operation(topic, <<"concat([client_attrs.tenant, '/', topic])">>),
|
||||||
|
Transformation1 = transformation(Name1, [Operation1]),
|
||||||
|
{201, _} = insert(Transformation1),
|
||||||
|
|
||||||
|
Tenant = <<"mytenant">>,
|
||||||
|
C = connect(
|
||||||
|
<<"c1">>,
|
||||||
|
_IsPersistent = false,
|
||||||
|
#{properties => #{'User-Property' => [{<<"tenant">>, Tenant}]}}
|
||||||
|
),
|
||||||
|
{ok, _, [_]} = emqtt:subscribe(C, emqx_topic:join([Tenant, <<"#">>])),
|
||||||
|
|
||||||
|
ok = publish(C, <<"t/1">>, #{x => 1, y => 2}),
|
||||||
|
?assertReceive({publish, #{topic := <<"mytenant/t/1">>}}),
|
||||||
|
|
||||||
|
ok
|
||||||
|
end,
|
||||||
|
[]
|
||||||
|
),
|
||||||
|
ok.
|
||||||
|
|
Loading…
Reference in New Issue