Merge pull request #7202 from DDDHuang/routes_honc

fix(api): routes api hocon support
This commit is contained in:
zhongwencool 2022-03-08 22:35:37 +08:00 committed by GitHub
commit 2ac5fb9223
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 68 additions and 51 deletions

View File

@ -17,80 +17,95 @@
-module(emqx_mgmt_api_routes). -module(emqx_mgmt_api_routes).
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
-include_lib("typerefl/include/types.hrl").
%% API %% API
-behaviour(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([ api_spec/0
, paths/0
, schema/1
, fields/1
]).
-export([ routes/2 -export([ routes/2
, route/2]). , route/2
]).
-export([query/4]). -export([ query/4]).
-define(TOPIC_NOT_FOUND, 'TOPIC_NOT_FOUND'). -define(TOPIC_NOT_FOUND, 'TOPIC_NOT_FOUND').
-define(ROUTES_QSCHEMA, [{<<"topic">>, binary}, {<<"node">>, atom}]). -define(ROUTES_QSCHEMA, [{<<"topic">>, binary}, {<<"node">>, atom}]).
-import(emqx_mgmt_util, [ object_schema/2
, object_array_schema/2
, error_schema/2
, properties/1
, page_params/0
, generate_response/1
]).
api_spec() -> api_spec() ->
{[routes_api(), route_api()], []}. emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}).
properties() -> paths() ->
properties([ ["/routes", "/routes/:topic"].
{topic, string},
{node, string}
]).
routes_api() -> schema("/routes") ->
Metadata = #{ #{
'operationId' => routes,
get => #{ get => #{
description => <<"EMQX routes">>, description => <<"EMQX Topics List">>,
parameters => [topic_param(query) , node_param()] ++ page_params(), parameters => [
topic_param(query),
node_param(),
hoconsc:ref(emqx_dashboard_swagger, page),
hoconsc:ref(emqx_dashboard_swagger, limit)
],
responses => #{ responses => #{
<<"200">> => object_array_schema(properties(), <<"List route info">>), 200 => [
<<"400">> => error_schema(<<"Invalid parameters">>, ['INVALID_PARAMETER']) {data, hoconsc:mk(hoconsc:array(hoconsc:ref(topic)), #{})},
{meta, hoconsc:mk(hoconsc:ref(meta), #{})}
]
} }
} }
}, };
{"/routes", Metadata, routes}. schema("/routes/:topic") ->
#{
route_api() -> 'operationId' => route,
Metadata = #{
get => #{ get => #{
description => <<"EMQX routes">>, description => <<"EMQX Topic List">>,
parameters => [topic_param(path)], parameters => [topic_param(path)],
responses => #{ responses => #{
<<"200">> => 200 => hoconsc:mk(hoconsc:ref(topic), #{}),
object_schema(properties(), <<"Route info">>), 404 =>
<<"404">> => emqx_dashboard_swagger:error_codes(['TOPIC_NOT_FOUND'],<<"Topic not found">>)
error_schema(<<"Topic not found">>, [?TOPIC_NOT_FOUND])
} }
} }
}, }.
{"/routes/:topic", Metadata, route}.
fields(topic) ->
[
{topic, hoconsc:mk(binary(), #{
desc => <<"Topic Name">>,
required => true})},
{node, hoconsc:mk(binary(), #{
desc => <<"Node">>,
required => true})}
];
fields(meta) ->
emqx_dashboard_swagger:fields(page) ++
emqx_dashboard_swagger:fields(limit) ++
[{count, hoconsc:mk(integer(), #{example => 1})}].
%%%============================================================================================== %%%==============================================================================================
%% parameters trans %% parameters trans
routes(get, #{query_string := QString}) -> routes(get, #{query_string := Qs}) ->
list(generate_topic(QString)). do_list(generate_topic(Qs)).
route(get, #{bindings := Bindings}) -> route(get, #{bindings := Bindings}) ->
lookup(generate_topic(Bindings)). lookup(generate_topic(Bindings)).
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply
list(QString) -> do_list(Params) ->
Response = emqx_mgmt_api:node_query( Response = emqx_mgmt_api:node_query(
node(), QString, emqx_route, ?ROUTES_QSCHEMA, {?MODULE, query}), node(), Params, emqx_route, ?ROUTES_QSCHEMA, {?MODULE, query}),
generate_response(Response). emqx_mgmt_util:generate_response(Response).
lookup(#{topic := Topic}) -> lookup(#{topic := Topic}) ->
case emqx_mgmt:lookup_routes(Topic) of case emqx_mgmt:lookup_routes(Topic) of
@ -124,19 +139,21 @@ format(#route{topic = Topic, dest = Node}) ->
#{topic => Topic, node => Node}. #{topic => Topic, node => Node}.
topic_param(In) -> topic_param(In) ->
#{ {
name => topic, topic, hoconsc:mk(binary(), #{
desc => <<"Topic Name">>,
in => In, in => In,
required => In == path, required => (In == path),
description => <<"Topic string, url encoding">>, example => <<"">>
schema => #{type => string} })
}. }.
node_param()-> node_param()->
#{ {
name => node, node, hoconsc:mk(binary(), #{
desc => <<"Node Name">>,
in => query, in => query,
required => false, required => false,
description => <<"Node">>, example => node()
schema => #{type => string} })
}. }.