Add API emqx_portal:ensure_forward_absent
This commit is contained in:
parent
2903a810ce
commit
599f5c8d4f
|
@ -73,7 +73,7 @@
|
||||||
-export([connecting/3, connected/3]).
|
-export([connecting/3, connected/3]).
|
||||||
|
|
||||||
%% management APIs
|
%% management APIs
|
||||||
-export([get_forwards/1, ensure_forward_present/2]). %, del_forward/2]).
|
-export([get_forwards/1, ensure_forward_present/2, ensure_forward_absent/2]).
|
||||||
-export([get_subscriptions/1]). %, add_subscription/3, del_subscription/2]).
|
-export([get_subscriptions/1]). %, add_subscription/3, del_subscription/2]).
|
||||||
|
|
||||||
-export_type([config/0,
|
-export_type([config/0,
|
||||||
|
@ -143,8 +143,13 @@ handle_ack(Pid, Ref) when node() =:= node(Pid) ->
|
||||||
get_forwards(Id) -> gen_statem:call(id(Id), get_forwards, timer:seconds(1000)).
|
get_forwards(Id) -> gen_statem:call(id(Id), get_forwards, timer:seconds(1000)).
|
||||||
|
|
||||||
%% @doc Add a new forward (local topic subscription).
|
%% @doc Add a new forward (local topic subscription).
|
||||||
-spec ensure_forward_present(id(), topic()) -> ok | {error, any()}.
|
-spec ensure_forward_present(id(), topic()) -> ok.
|
||||||
ensure_forward_present(Id, Topic) -> gen_statem:call(id(Id), {ensure_forward_present, topic(Topic)}).
|
ensure_forward_present(Id, Topic) ->
|
||||||
|
gen_statem:call(id(Id), {ensure_forward_present, topic(Topic)}).
|
||||||
|
|
||||||
|
-spec ensure_forward_absent(id(), topic()) -> ok.
|
||||||
|
ensure_forward_absent(Id, Topic) ->
|
||||||
|
gen_statem:call(id(Id), {ensure_forward_absent, topic(Topic)}).
|
||||||
|
|
||||||
-spec get_subscriptions(id()) -> [{emqx_topic:topic(), qos()}].
|
-spec get_subscriptions(id()) -> [{emqx_topic:topic(), qos()}].
|
||||||
get_subscriptions(Id) -> gen_statem:call(id(Id), get_subscriptions).
|
get_subscriptions(Id) -> gen_statem:call(id(Id), get_subscriptions).
|
||||||
|
@ -290,6 +295,16 @@ common(_StateName, {call, From}, {ensure_forward_present, Topic},
|
||||||
{keep_state, State#{forwards := lists:usort([Topic | Forwards])},
|
{keep_state, State#{forwards := lists:usort([Topic | Forwards])},
|
||||||
[{reply, From, ok}]}
|
[{reply, From, ok}]}
|
||||||
end;
|
end;
|
||||||
|
common(_StateName, {call, From}, {ensure_forward_absent, Topic},
|
||||||
|
#{forwards := Forwards} = State) ->
|
||||||
|
case lists:member(Topic, Forwards) of
|
||||||
|
true ->
|
||||||
|
emqx_broker:unsubscribe(Topic),
|
||||||
|
{keep_state, State#{forwards := lists:delete(Topic, Forwards)},
|
||||||
|
[{reply, From, ok}]};
|
||||||
|
false ->
|
||||||
|
{keep_state_and_data, [{reply, From, ok}]}
|
||||||
|
end;
|
||||||
common(_StateName, {call, From}, get_subscriptions, #{subscriptions := Subs}) ->
|
common(_StateName, {call, From}, get_subscriptions, #{subscriptions := Subs}) ->
|
||||||
{keep_state_and_data, [{reply, From, Subs}]};
|
{keep_state_and_data, [{reply, From, Subs}]};
|
||||||
common(_StateName, info, {dispatch, _, Msg},
|
common(_StateName, info, {dispatch, _, Msg},
|
||||||
|
|
|
@ -39,7 +39,7 @@ init_per_suite(Config) ->
|
||||||
_ ->
|
_ ->
|
||||||
ok
|
ok
|
||||||
end,
|
end,
|
||||||
emqx_ct_broker_helpers:run_setup_steps(Config).
|
emqx_ct_broker_helpers:run_setup_steps([{log_leve, info} | Config]).
|
||||||
|
|
||||||
end_per_suite(_Config) ->
|
end_per_suite(_Config) ->
|
||||||
emqx_ct_broker_helpers:run_teardown_steps().
|
emqx_ct_broker_helpers:run_teardown_steps().
|
||||||
|
@ -59,6 +59,9 @@ t_forwards_mngr(Config) when is_list(Config) ->
|
||||||
?assertEqual(ok, emqx_portal:ensure_forward_present(Name, "mngr")),
|
?assertEqual(ok, emqx_portal:ensure_forward_present(Name, "mngr")),
|
||||||
?assertEqual(ok, emqx_portal:ensure_forward_present(Name, "mngr2")),
|
?assertEqual(ok, emqx_portal:ensure_forward_present(Name, "mngr2")),
|
||||||
?assertEqual([<<"mngr">>, <<"mngr2">>], emqx_portal:get_forwards(Pid)),
|
?assertEqual([<<"mngr">>, <<"mngr2">>], emqx_portal:get_forwards(Pid)),
|
||||||
|
?assertEqual(ok, emqx_portal:ensure_forward_absent(Name, "mngr2")),
|
||||||
|
?assertEqual(ok, emqx_portal:ensure_forward_absent(Name, "mngr3")),
|
||||||
|
?assertEqual([<<"mngr">>], emqx_portal:get_forwards(Pid)),
|
||||||
?assertEqual(Subs, emqx_portal:get_subscriptions(Pid))
|
?assertEqual(Subs, emqx_portal:get_subscriptions(Pid))
|
||||||
after
|
after
|
||||||
ok = emqx_portal:stop(Pid)
|
ok = emqx_portal:stop(Pid)
|
||||||
|
|
Loading…
Reference in New Issue