fix(mgmt): add subscribe options into the result of the /subscriptions API

This commit is contained in:
firest 2022-04-27 11:34:57 +08:00
parent 5457e5551f
commit 6b84016259
2 changed files with 43 additions and 22 deletions

View File

@ -72,7 +72,10 @@ fields(subscription) ->
{node, hoconsc:mk(binary(), #{desc => <<"Access type">>})}, {node, hoconsc:mk(binary(), #{desc => <<"Access type">>})},
{topic, hoconsc:mk(binary(), #{desc => <<"Topic name">>})}, {topic, hoconsc:mk(binary(), #{desc => <<"Topic name">>})},
{clientid, hoconsc:mk(binary(), #{desc => <<"Client identifier">>})}, {clientid, hoconsc:mk(binary(), #{desc => <<"Client identifier">>})},
{qos, hoconsc:mk(emqx_schema:qos(), #{desc => <<"QoS">>})} {qos, hoconsc:mk(emqx_schema:qos(), #{desc => <<"QoS">>})},
{nl, hoconsc:mk(integer(), #{desc => <<"No Local">>})},
{rap, hoconsc:mk(integer(), #{desc => <<"Retain as Published">>})},
{rh, hoconsc:mk(integer(), #{desc => <<"Retain Handling">>})}
]. ].
parameters() -> parameters() ->
@ -163,22 +166,20 @@ format(Items) when is_list(Items) ->
[format(Item) || Item <- Items]; [format(Item) || Item <- Items];
format({{Subscriber, Topic}, Options}) -> format({{Subscriber, Topic}, Options}) ->
format({Subscriber, Topic, Options}); format({Subscriber, Topic, Options});
format({_Subscriber, Topic, Options = #{share := Group}}) ->
QoS = maps:get(qos, Options),
#{
topic => filename:join([<<"$share">>, Group, Topic]),
clientid => maps:get(subid, Options),
qos => QoS,
node => node()
};
format({_Subscriber, Topic, Options}) -> format({_Subscriber, Topic, Options}) ->
QoS = maps:get(qos, Options), maps:merge(
#{ #{
topic => Topic, topic => get_topic(Topic, Options),
clientid => maps:get(subid, Options), clientid => maps:get(subid, Options),
qos => QoS, node => node()
node => node() },
}. maps:with([qos, nl, rap, rh], Options)
).
get_topic(Topic, #{share := Group}) ->
filename:join([<<"$share">>, Group, Topic]);
get_topic(Topic, _) ->
Topic.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Query Function %% Query Function

View File

@ -25,6 +25,10 @@
%% notice: integer topic for sort response %% notice: integer topic for sort response
-define(TOPIC1, <<"t/0000">>). -define(TOPIC1, <<"t/0000">>).
-define(TOPIC1RH, 1).
-define(TOPIC1RAP, false).
-define(TOPIC1NL, false).
-define(TOPIC1QOS, 1).
-define(TOPIC2, <<"$share/test_group/t/0001">>). -define(TOPIC2, <<"$share/test_group/t/0001">>).
-define(TOPIC2_TOPIC_ONLY, <<"t/0001">>). -define(TOPIC2_TOPIC_ONLY, <<"t/0001">>).
@ -41,9 +45,13 @@ end_per_suite(_) ->
emqx_mgmt_api_test_util:end_suite(). emqx_mgmt_api_test_util:end_suite().
t_subscription_api(_) -> t_subscription_api(_) ->
{ok, Client} = emqtt:start_link(#{username => ?USERNAME, clientid => ?CLIENTID}), {ok, Client} = emqtt:start_link(#{username => ?USERNAME, clientid => ?CLIENTID, proto_ver => v5}),
{ok, _} = emqtt:connect(Client), {ok, _} = emqtt:connect(Client),
{ok, _, _} = emqtt:subscribe(Client, ?TOPIC1), {ok, _, _} = emqtt:subscribe(
Client, [
{?TOPIC1, [{rh, ?TOPIC1RH}, {rap, ?TOPIC1RAP}, {nl, ?TOPIC1NL}, {qos, ?TOPIC1QOS}]}
]
),
{ok, _, _} = emqtt:subscribe(Client, ?TOPIC2), {ok, _, _} = emqtt:subscribe(Client, ?TOPIC2),
Path = emqx_mgmt_api_test_util:api_path(["subscriptions"]), Path = emqx_mgmt_api_test_util:api_path(["subscriptions"]),
{ok, Response} = emqx_mgmt_api_test_util:request_api(get, Path), {ok, Response} = emqx_mgmt_api_test_util:request_api(get, Path),
@ -59,9 +67,21 @@ t_subscription_api(_) ->
maps:get(T1, ?TOPIC_SORT) =< maps:get(T2, ?TOPIC_SORT) maps:get(T1, ?TOPIC_SORT) =< maps:get(T2, ?TOPIC_SORT)
end, end,
[Subscriptions1, Subscriptions2] = lists:sort(Sort, Subscriptions), [Subscriptions1, Subscriptions2] = lists:sort(Sort, Subscriptions),
?assertEqual(maps:get(<<"topic">>, Subscriptions1), ?TOPIC1),
?assertMatch(
#{
<<"topic">> := ?TOPIC1,
<<"qos">> := ?TOPIC1QOS,
<<"nl">> := _,
<<"rap">> := _,
<<"rh">> := ?TOPIC1RH,
<<"clientid">> := ?CLIENTID,
<<"node">> := _
},
Subscriptions1
),
?assertEqual(maps:get(<<"topic">>, Subscriptions2), ?TOPIC2), ?assertEqual(maps:get(<<"topic">>, Subscriptions2), ?TOPIC2),
?assertEqual(maps:get(<<"clientid">>, Subscriptions1), ?CLIENTID),
?assertEqual(maps:get(<<"clientid">>, Subscriptions2), ?CLIENTID), ?assertEqual(maps:get(<<"clientid">>, Subscriptions2), ?CLIENTID),
QS = uri_string:compose_query([ QS = uri_string:compose_query([
@ -94,8 +114,8 @@ t_subscription_api(_) ->
MatchMeta = maps:get(<<"meta">>, MatchData), MatchMeta = maps:get(<<"meta">>, MatchData),
?assertEqual(1, maps:get(<<"page">>, MatchMeta)), ?assertEqual(1, maps:get(<<"page">>, MatchMeta)),
?assertEqual(emqx_mgmt:max_row_limit(), maps:get(<<"limit">>, MatchMeta)), ?assertEqual(emqx_mgmt:max_row_limit(), maps:get(<<"limit">>, MatchMeta)),
?assertEqual(2, maps:get(<<"count">>, MatchMeta)), ?assertEqual(1, maps:get(<<"count">>, MatchMeta)),
MatchSubs = maps:get(<<"data">>, MatchData), MatchSubs = maps:get(<<"data">>, MatchData),
?assertEqual(length(MatchSubs), 2), ?assertEqual(1, length(MatchSubs)),
emqtt:disconnect(Client). emqtt:disconnect(Client).