fix: bad auto_subscribe api schema
This commit is contained in:
parent
e67657d6d9
commit
5415e341fb
|
@ -51,8 +51,21 @@ max_limit() ->
|
||||||
list() ->
|
list() ->
|
||||||
format(emqx_conf:get([auto_subscribe, topics], [])).
|
format(emqx_conf:get([auto_subscribe, topics], [])).
|
||||||
|
|
||||||
update(Topics) ->
|
update(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE ->
|
||||||
update_(Topics).
|
case
|
||||||
|
emqx_conf:update(
|
||||||
|
[auto_subscribe, topics],
|
||||||
|
Topics,
|
||||||
|
#{rawconf_with_defaults => true, override_to => cluster}
|
||||||
|
)
|
||||||
|
of
|
||||||
|
{ok, #{raw_config := NewTopics}} ->
|
||||||
|
{ok, NewTopics};
|
||||||
|
{error, Reason} ->
|
||||||
|
{error, Reason}
|
||||||
|
end;
|
||||||
|
update(_Topics) ->
|
||||||
|
{error, quota_exceeded}.
|
||||||
|
|
||||||
post_config_update(_KeyPath, _Req, NewTopics, _OldConf, _AppEnvs) ->
|
post_config_update(_KeyPath, _Req, NewTopics, _OldConf, _AppEnvs) ->
|
||||||
Config = emqx_conf:get([auto_subscribe], #{}),
|
Config = emqx_conf:get([auto_subscribe], #{}),
|
||||||
|
@ -95,22 +108,6 @@ format(Rule = #{topic := Topic}) when is_map(Rule) ->
|
||||||
nl => maps:get(nl, Rule, 0)
|
nl => maps:get(nl, Rule, 0)
|
||||||
}.
|
}.
|
||||||
|
|
||||||
update_(Topics) when length(Topics) =< ?MAX_AUTO_SUBSCRIBE ->
|
|
||||||
case
|
|
||||||
emqx_conf:update(
|
|
||||||
[auto_subscribe, topics],
|
|
||||||
Topics,
|
|
||||||
#{rawconf_with_defaults => true, override_to => cluster}
|
|
||||||
)
|
|
||||||
of
|
|
||||||
{ok, #{raw_config := NewTopics}} ->
|
|
||||||
{ok, NewTopics};
|
|
||||||
{error, Reason} ->
|
|
||||||
{error, Reason}
|
|
||||||
end;
|
|
||||||
update_(_Topics) ->
|
|
||||||
{error, quota_exceeded}.
|
|
||||||
|
|
||||||
update_hook() ->
|
update_hook() ->
|
||||||
update_hook(emqx_conf:get([auto_subscribe], #{})).
|
update_hook(emqx_conf:get([auto_subscribe], #{})).
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@
|
||||||
-include_lib("emqx/include/emqx_placeholder.hrl").
|
-include_lib("emqx/include/emqx_placeholder.hrl").
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
emqx_dashboard_swagger:spec(?MODULE).
|
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
||||||
|
|
||||||
paths() ->
|
paths() ->
|
||||||
["/mqtt/auto_subscribe"].
|
["/mqtt/auto_subscribe"].
|
||||||
|
@ -46,15 +46,15 @@ schema("/mqtt/auto_subscribe") ->
|
||||||
description => ?DESC(list_auto_subscribe_api),
|
description => ?DESC(list_auto_subscribe_api),
|
||||||
tags => [<<"Auto Subscribe">>],
|
tags => [<<"Auto Subscribe">>],
|
||||||
responses => #{
|
responses => #{
|
||||||
200 => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe")
|
200 => topics()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
put => #{
|
put => #{
|
||||||
description => ?DESC(update_auto_subscribe_api),
|
description => ?DESC(update_auto_subscribe_api),
|
||||||
tags => [<<"Auto Subscribe">>],
|
tags => [<<"Auto Subscribe">>],
|
||||||
'requestBody' => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe"),
|
'requestBody' => topics(),
|
||||||
responses => #{
|
responses => #{
|
||||||
200 => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe"),
|
200 => topics(),
|
||||||
409 => emqx_dashboard_swagger:error_codes(
|
409 => emqx_dashboard_swagger:error_codes(
|
||||||
[?EXCEED_LIMIT],
|
[?EXCEED_LIMIT],
|
||||||
?DESC(update_auto_subscribe_api_response409)
|
?DESC(update_auto_subscribe_api_response409)
|
||||||
|
@ -63,14 +63,17 @@ schema("/mqtt/auto_subscribe") ->
|
||||||
}
|
}
|
||||||
}.
|
}.
|
||||||
|
|
||||||
|
topics() ->
|
||||||
|
Fields = emqx_auto_subscribe_schema:fields("auto_subscribe"),
|
||||||
|
{topics, Topics} = lists:keyfind(topics, 1, Fields),
|
||||||
|
Topics.
|
||||||
|
|
||||||
%%%==============================================================================================
|
%%%==============================================================================================
|
||||||
%% api apply
|
%% api apply
|
||||||
auto_subscribe(get, _) ->
|
auto_subscribe(get, _) ->
|
||||||
{200, emqx_auto_subscribe:list()};
|
{200, emqx_auto_subscribe:list()};
|
||||||
auto_subscribe(put, #{body := #{}}) ->
|
auto_subscribe(put, #{body := Topics}) when is_list(Topics) ->
|
||||||
{400, #{code => ?BAD_REQUEST, message => <<"Request body required">>}};
|
case emqx_auto_subscribe:update(Topics) of
|
||||||
auto_subscribe(put, #{body := Params}) ->
|
|
||||||
case emqx_auto_subscribe:update(Params) of
|
|
||||||
{error, quota_exceeded} ->
|
{error, quota_exceeded} ->
|
||||||
Message = list_to_binary(
|
Message = list_to_binary(
|
||||||
io_lib:format(
|
io_lib:format(
|
||||||
|
|
|
@ -151,6 +151,32 @@ t_update(_) ->
|
||||||
ResponseMap = emqx_json:decode(Response, [return_maps]),
|
ResponseMap = emqx_json:decode(Response, [return_maps]),
|
||||||
?assertEqual(1, erlang:length(ResponseMap)),
|
?assertEqual(1, erlang:length(ResponseMap)),
|
||||||
|
|
||||||
|
BadBody1 = #{topic => ?TOPIC_S},
|
||||||
|
?assertMatch(
|
||||||
|
{error, {"HTTP/1.1", 400, "Bad Request"}},
|
||||||
|
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody1)
|
||||||
|
),
|
||||||
|
BadBody2 = [#{topic => ?TOPIC_S, qos => 3}],
|
||||||
|
?assertMatch(
|
||||||
|
{error, {"HTTP/1.1", 400, "Bad Request"}},
|
||||||
|
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody2)
|
||||||
|
),
|
||||||
|
BadBody3 = [#{topic => ?TOPIC_S, rh => 10}],
|
||||||
|
?assertMatch(
|
||||||
|
{error, {"HTTP/1.1", 400, "Bad Request"}},
|
||||||
|
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody3)
|
||||||
|
),
|
||||||
|
BadBody4 = [#{topic => ?TOPIC_S, rap => -1}],
|
||||||
|
?assertMatch(
|
||||||
|
{error, {"HTTP/1.1", 400, "Bad Request"}},
|
||||||
|
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody4)
|
||||||
|
),
|
||||||
|
BadBody5 = [#{topic => ?TOPIC_S, nl => -1}],
|
||||||
|
?assertMatch(
|
||||||
|
{error, {"HTTP/1.1", 400, "Bad Request"}},
|
||||||
|
emqx_mgmt_api_test_util:request_api(put, Path, "", Auth, BadBody5)
|
||||||
|
),
|
||||||
|
|
||||||
{ok, Client} = emqtt:start_link(#{username => ?CLIENT_USERNAME, clientid => ?CLIENT_ID}),
|
{ok, Client} = emqtt:start_link(#{username => ?CLIENT_USERNAME, clientid => ?CLIENT_ID}),
|
||||||
{ok, _} = emqtt:connect(Client),
|
{ok, _} = emqtt:connect(Client),
|
||||||
timer:sleep(100),
|
timer:sleep(100),
|
||||||
|
|
|
@ -705,7 +705,7 @@ typename_to_spec("service_account_json()", _Mod) ->
|
||||||
typename_to_spec("#{" ++ _, Mod) ->
|
typename_to_spec("#{" ++ _, Mod) ->
|
||||||
typename_to_spec("map()", Mod);
|
typename_to_spec("map()", Mod);
|
||||||
typename_to_spec("qos()", _Mod) ->
|
typename_to_spec("qos()", _Mod) ->
|
||||||
#{type => string, enum => [0, 1, 2]};
|
#{type => integer, minimum => 0, maximum => 2, example => 0};
|
||||||
typename_to_spec("{binary(), binary()}", _Mod) ->
|
typename_to_spec("{binary(), binary()}", _Mod) ->
|
||||||
#{type => object, example => #{}};
|
#{type => object, example => #{}};
|
||||||
typename_to_spec("comma_separated_list()", _Mod) ->
|
typename_to_spec("comma_separated_list()", _Mod) ->
|
||||||
|
|
Loading…
Reference in New Issue