Merge pull request #5300 from DDDHuang/api_format

chore: api code format; fix: atom refname; update minirest version
This commit is contained in:
DDDHuang 2021-07-23 17:52:27 +08:00 committed by GitHub
commit f278d170a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 140 additions and 151 deletions

View File

@ -16,7 +16,7 @@
-module(emqx_mgmt_api_alarms). -module(emqx_mgmt_api_alarms).
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -58,7 +58,7 @@ alarm_schema() ->
alarms_api() -> alarms_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "EMQ X alarms", description => <<"EMQ X alarms">>,
parameters => [#{ parameters => [#{
name => activated, name => activated,
in => query, in => query,
@ -68,9 +68,9 @@ alarms_api() ->
}], }],
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_array_schema(<<"List all alarms">>, <<"alarm">>)}}, emqx_mgmt_util:response_array_schema(<<"List all alarms">>, alarm)}},
delete => #{ delete => #{
description => "Remove all deactivated alarms", description => <<"Remove all deactivated alarms">>,
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_schema(<<"Remove all deactivated alarms ok">>)}}}, emqx_mgmt_util:response_schema(<<"Remove all deactivated alarms ok">>)}}},

View File

@ -16,7 +16,7 @@
-module(emqx_mgmt_api_apps). -module(emqx_mgmt_api_apps).
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -76,17 +76,17 @@ app_without_secret_schema() ->
apps_api() -> apps_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "List EMQ X apps", description => <<"List EMQ X apps">>,
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_array_schema(<<"All apps">>, emqx_mgmt_util:response_array_schema(<<"All apps">>,
app_without_secret_schema())}}, app_without_secret_schema())}},
post => #{ post => #{
description => "EMQ X create apps", description => <<"EMQ X create apps">>,
'requestBody' => emqx_mgmt_util:request_body_schema(<<"app">>), 'requestBody' => emqx_mgmt_util:request_body_schema(<<"app">>),
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_schema(<<"Create apps">>, <<"app_secret">>), emqx_mgmt_util:response_schema(<<"Create apps">>, app_secret),
<<"400">> => <<"400">> =>
emqx_mgmt_util:response_error_schema(<<"App ID already exist">>, [?BAD_APP_ID])}}}, emqx_mgmt_util:response_error_schema(<<"App ID already exist">>, [?BAD_APP_ID])}}},
{"/apps", Metadata, apps}. {"/apps", Metadata, apps}.
@ -94,30 +94,29 @@ apps_api() ->
app_api() -> app_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "EMQ X apps", description => <<"EMQ X apps">>,
parameters => [#{ parameters => [#{
name => app_id, name => app_id,
in => path, in => path,
required => true, required => true,
schema => #{type => string}, schema => #{type => string}}],
example => <<"admin">>}],
responses => #{ responses => #{
<<"404">> => <<"404">> =>
emqx_mgmt_util:response_error_schema(<<"App id not found">>), emqx_mgmt_util:response_error_schema(<<"App id not found">>),
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_schema("Get App", app_without_secret_schema())}}, emqx_mgmt_util:response_schema(<<"Get App">>, app_without_secret_schema())}},
delete => #{ delete => #{
description => "EMQ X apps", description => <<"EMQ X apps">>,
parameters => [#{ parameters => [#{
name => app_id, name => app_id,
in => path, in => path,
required => true, required => true,
schema => #{type => string}, schema => #{type => string}
example => <<"admin">>}], }],
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_schema("Remove app ok")}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Remove app ok">>)}},
put => #{ put => #{
description => "EMQ X update apps", description => <<"EMQ X update apps">>,
parameters => [#{ parameters => [#{
name => app_id, name => app_id,
in => path, in => path,
@ -175,23 +174,19 @@ app(put, Request) ->
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply
list(_) -> list(_) ->
Data = [format_without_app_secret(Apps) || Apps <- emqx_mgmt_auth:list_apps()], {200, [format_without_app_secret(Apps) || Apps <- emqx_mgmt_auth:list_apps()]}.
Response = emqx_json:encode(Data),
{200, Response}.
create(#{app_id := AppID, name := Name, secret := Secret, create(#{app_id := AppID, name := Name, secret := Secret,
desc := Desc, status := Status, expired := Expired}) -> desc := Desc, status := Status, expired := Expired}) ->
case emqx_mgmt_auth:add_app(AppID, Name, Secret, Desc, Status, Expired) of case emqx_mgmt_auth:add_app(AppID, Name, Secret, Desc, Status, Expired) of
{ok, AppSecret} -> {ok, AppSecret} ->
Response = emqx_json:encode(#{secret => AppSecret}), {200, #{secret => AppSecret}};
{200, Response};
{error, alread_existed} -> {error, alread_existed} ->
Message = list_to_binary(io_lib:format("appid ~p already existed", [AppID])), Message = list_to_binary(io_lib:format("appid ~p already existed", [AppID])),
{400, #{code => 'BAD_APP_ID', reason => Message}}; {400, #{code => 'BAD_APP_ID', message => Message}};
{error, Reason} -> {error, Reason} ->
Data = #{code => 'UNKNOW_ERROR', Response = #{code => 'UNKNOW_ERROR',
reason => list_to_binary(io_lib:format("~p", [Reason]))}, message => list_to_binary(io_lib:format("~p", [Reason]))},
Response = emqx_json:encode(Data),
{500, Response} {500, Response}
end. end.
@ -200,8 +195,7 @@ lookup(#{app_id := AppID}) ->
undefined -> undefined ->
{404, ?APP_ID_NOT_FOUND}; {404, ?APP_ID_NOT_FOUND};
App -> App ->
Data = format_with_app_secret(App), Response = format_with_app_secret(App),
Response = emqx_json:encode(Data),
{200, Response} {200, Response}
end. end.
@ -216,8 +210,7 @@ update(App = #{app_id := AppID, name := Name, desc := Desc, status := Status, ex
{error, not_found} -> {error, not_found} ->
{404, ?APP_ID_NOT_FOUND}; {404, ?APP_ID_NOT_FOUND};
{error, Reason} -> {error, Reason} ->
Data = #{code => 'UNKNOW_ERROR', reason => list_to_binary(io_lib:format("~p", [Reason]))}, Response = #{code => 'UNKNOW_ERROR', message => list_to_binary(io_lib:format("~p", [Reason]))},
Response = emqx_json:encode(Data),
{500, Response} {500, Response}
end. end.

View File

@ -16,7 +16,7 @@
-module(emqx_mgmt_api_clients). -module(emqx_mgmt_api_clients).
-behavior(minirest_api). -behaviour(minirest_api).
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
@ -214,112 +214,106 @@ schemas() ->
clients_api() -> clients_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "List clients", description => <<"List clients">>,
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_array_schema(<<"List clients 200 OK">>, <<"client">>)}}}, <<"200">> => emqx_mgmt_util:response_array_schema(<<"List clients 200 OK">>, client)}}},
{"/clients", Metadata, clients}. {"/clients", Metadata, clients}.
client_api() -> client_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Get clients info by client ID", description => <<"Get clients info by client ID">>,
parameters => [#{ parameters => [#{
name => clientid, name => clientid,
in => path, in => path,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => 123456}], }],
responses => #{ responses => #{
<<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>),
<<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, <<"client">>)}}, <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, client)}},
delete => #{ delete => #{
description => "Kick out client by client ID", description => <<"Kick out client by client ID">>,
parameters => [#{ parameters => [#{
name => clientid, name => clientid,
in => path, in => path,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => 123456}], }],
responses => #{ responses => #{
<<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>),
<<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, <<"client">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, client)}}},
{"/clients/:clientid", Metadata, client}. {"/clients/:clientid", Metadata, client}.
clients_acl_cache_api() -> clients_acl_cache_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Get client acl cache", description => <<"Get client acl cache">>,
parameters => [#{ parameters => [#{
name => clientid, name => clientid,
in => path, in => path,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => 123456}], }],
responses => #{ responses => #{
<<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>),
<<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, <<"acl_cache">>)}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Get client acl cache">>, acl_cache)}},
delete => #{ delete => #{
description => "Clean client acl cache", description => <<"Clean client acl cache">>,
parameters => [#{ parameters => [#{
name => clientid, name => clientid,
in => path, in => path,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => 123456}], }],
responses => #{ responses => #{
<<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>),
<<"200">> => emqx_mgmt_util:response_schema(<<"Delete clients 200 OK">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Delete clients acl cache OK">>)}}},
{"/clients/:clientid/acl_cache", Metadata, acl_cache}. {"/clients/:clientid/acl_cache", Metadata, acl_cache}.
subscribe_api() -> subscribe_api() ->
Metadata = #{ Metadata = #{
post => #{ post => #{
description => "subscribe", description => <<"Subscribe">>,
parameters => [ parameters => [#{
#{
name => clientid, name => clientid,
in => path, in => path,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => 123456 }],
}
],
'requestBody' => emqx_mgmt_util:request_body_schema(#{ 'requestBody' => emqx_mgmt_util:request_body_schema(#{
type => object, type => object,
properties => #{ properties => #{
<<"topic">> => #{ topic => #{
type => string, type => string,
example => <<"topic_1">>,
description => <<"Topic">>}, description => <<"Topic">>},
<<"qos">> => #{ qos => #{
type => integer, type => integer,
enum => [0, 1, 2], enum => [0, 1, 2],
example => 0, example => 0,
description => <<"QoS">>}}}), description => <<"QoS">>}}}),
responses => #{ responses => #{
<<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>),
<<"200">> => emqx_mgmt_util:response_schema(<<"subscribe ok">>)}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Subscribe ok">>)}},
delete => #{ delete => #{
description => "unsubscribe", description => <<"Unsubscribe">>,
parameters => [ parameters => [
#{ #{
name => clientid, name => clientid,
in => path, in => path,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => 123456
}, },
#{ #{
name => topic, name => topic,
in => query, in => query,
schema => #{type => string}, schema => #{type => string},
required => true, required => true
example => <<"topic_1">>
} }
], ],
responses => #{ responses => #{
<<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>),
<<"200">> => emqx_mgmt_util:response_schema(<<"unsubscribe ok">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Unsubscribe ok">>)}}},
{"/clients/:clientid/subscribe", Metadata, subscribe}. {"/clients/:clientid/subscribe", Metadata, subscribe}.
%%%============================================================================================== %%%==============================================================================================
@ -373,17 +367,15 @@ subscribe_batch(post, Request) ->
%% api apply %% api apply
list(Params) -> list(Params) ->
Data = emqx_mgmt_api:cluster_query(maps:to_list(Params), ?CLIENT_QS_SCHEMA, ?query_fun), Response = emqx_mgmt_api:cluster_query(maps:to_list(Params), ?CLIENT_QS_SCHEMA, ?query_fun),
Body = emqx_json:encode(Data), {200, Response}.
{200, Body}.
lookup(#{clientid := ClientID}) -> lookup(#{clientid := ClientID}) ->
case emqx_mgmt:lookup_client({clientid, ClientID}, ?format_fun) of case emqx_mgmt:lookup_client({clientid, ClientID}, ?format_fun) of
[] -> [] ->
{404, ?CLIENT_ID_NOT_FOUND}; {404, ?CLIENT_ID_NOT_FOUND};
ClientInfo -> ClientInfo ->
Response = emqx_json:encode(hd(ClientInfo)), {200, hd(ClientInfo)}
{200, Response}
end. end.
kickout(#{clientid := ClientID}) -> kickout(#{clientid := ClientID}) ->
@ -395,9 +387,10 @@ get_acl_cache(#{clientid := ClientID})->
{error, not_found} -> {error, not_found} ->
{404, ?CLIENT_ID_NOT_FOUND}; {404, ?CLIENT_ID_NOT_FOUND};
{error, Reason} -> {error, Reason} ->
{500, #{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}}; Message = list_to_binary(io_lib:format("~p", [Reason])),
{500, #{code => <<"UNKNOW_ERROR">>, message => Message}};
Caches -> Caches ->
Response = emqx_json:encode([format_acl_cache(Cache) || Cache <- Caches]), Response = [format_acl_cache(Cache) || Cache <- Caches],
{200, Response} {200, Response}
end. end.
@ -408,7 +401,8 @@ clean_acl_cache(#{clientid := ClientID}) ->
{error, not_found} -> {error, not_found} ->
{404, ?CLIENT_ID_NOT_FOUND}; {404, ?CLIENT_ID_NOT_FOUND};
{error, Reason} -> {error, Reason} ->
{500, #{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}} Message = list_to_binary(io_lib:format("~p", [Reason])),
{500, #{code => <<"UNKNOW_ERROR">>, message => Message}}
end. end.
subscribe(#{clientid := ClientID, topic := Topic, qos := Qos}) -> subscribe(#{clientid := ClientID, topic := Topic, qos := Qos}) ->
@ -416,8 +410,8 @@ subscribe(#{clientid := ClientID, topic := Topic, qos := Qos}) ->
{error, channel_not_found} -> {error, channel_not_found} ->
{404, ?CLIENT_ID_NOT_FOUND}; {404, ?CLIENT_ID_NOT_FOUND};
{error, Reason} -> {error, Reason} ->
Body = emqx_json:encode(#{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}), Message = list_to_binary(io_lib:format("~p", [Reason])),
{500, Body}; {500, #{code => <<"UNKNOW_ERROR">>, message => Message}};
ok -> ok ->
{200} {200}
end. end.
@ -427,8 +421,8 @@ unsubscribe(#{clientid := ClientID, topic := Topic}) ->
{error, channel_not_found} -> {error, channel_not_found} ->
{404, ?CLIENT_ID_NOT_FOUND}; {404, ?CLIENT_ID_NOT_FOUND};
{error, Reason} -> {error, Reason} ->
Body = emqx_json:encode(#{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}), Message = list_to_binary(io_lib:format("~p", [Reason])),
{500, Body}; {500, #{code => <<"UNKNOW_ERROR">>, message => Message}};
{unsubscribe, [{Topic, #{}}]} -> {unsubscribe, [{Topic, #{}}]} ->
{200} {200}
end. end.

View File

@ -16,7 +16,7 @@
-module(emqx_mgmt_api_listeners). -module(emqx_mgmt_api_listeners).
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -78,28 +78,28 @@ listener_schema() ->
listeners_api() -> listeners_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "List listeners in cluster", description => <<"List listeners in cluster">>,
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_array_schema(<<"List all listeners">>, <<"listener">>)}}}, emqx_mgmt_util:response_array_schema(<<"List all listeners">>, listener)}}},
{"/listeners", Metadata, listeners}. {"/listeners", Metadata, listeners}.
restart_listeners_api() -> restart_listeners_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "List listeners by listener ID", description => <<"List listeners by listener ID">>,
parameters => [param_path_identifier()], parameters => [param_path_identifier()],
responses => #{ responses => #{
<<"404">> => <<"404">> =>
emqx_mgmt_util:response_error_schema(<<"Listener id not found">>, ['BAD_LISTENER_ID']), emqx_mgmt_util:response_error_schema(<<"Listener id not found">>, ['BAD_LISTENER_ID']),
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_array_schema(<<"List listener info ok">>, <<"listener">>)}}}, emqx_mgmt_util:response_array_schema(<<"List listener info ok">>, listener)}}},
{"/listeners/:identifier", Metadata, listener}. {"/listeners/:identifier", Metadata, listener}.
manage_listeners_api() -> manage_listeners_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Restart listeners in cluster", description => <<"Restart listeners in cluster">>,
parameters => [ parameters => [
param_path_identifier(), param_path_identifier(),
param_path_operation()], param_path_operation()],
@ -119,7 +119,7 @@ manage_listeners_api() ->
manage_nodes_listeners_api() -> manage_nodes_listeners_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Restart listeners in cluster", description => <<"Restart listeners in cluster">>,
parameters => [ parameters => [
param_path_node(), param_path_node(),
param_path_identifier(), param_path_identifier(),
@ -140,26 +140,26 @@ manage_nodes_listeners_api() ->
nodes_listeners_api() -> nodes_listeners_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Get listener info in one node", description => <<"Get listener info in one node">>,
parameters => [param_path_node(), param_path_identifier()], parameters => [param_path_node(), param_path_identifier()],
responses => #{ responses => #{
<<"404">> => <<"404">> =>
emqx_mgmt_util:response_error_schema(<<"Node name or listener id not found">>, emqx_mgmt_util:response_error_schema(<<"Node name or listener id not found">>,
['BAD_NODE_NAME', 'BAD_LISTENER_ID']), ['BAD_NODE_NAME', 'BAD_LISTENER_ID']),
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_schema(<<"Get listener info ok">>, <<"listener">>)}}}, emqx_mgmt_util:response_schema(<<"Get listener info ok">>, listener)}}},
{"/nodes/:node/listeners/:identifier", Metadata, node_listener}. {"/nodes/:node/listeners/:identifier", Metadata, node_listener}.
nodes_listener_api() -> nodes_listener_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "List listeners in one node", description => <<"List listeners in one node">>,
parameters => [param_path_node()], parameters => [param_path_node()],
responses => #{ responses => #{
<<"404">> => <<"404">> =>
emqx_mgmt_util:response_error_schema(<<"Listener id not found">>), emqx_mgmt_util:response_error_schema(<<"Listener id not found">>),
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_schema(<<"Get listener info ok">>, <<"listener">>)}}}, emqx_mgmt_util:response_schema(<<"Get listener info ok">>, listener)}}},
{"/nodes/:node/listeners", Metadata, node_listeners}. {"/nodes/:node/listeners", Metadata, node_listeners}.
%%%============================================================================================== %%%==============================================================================================
%% parameters %% parameters

View File

@ -16,7 +16,7 @@
-module(emqx_mgmt_api_metrics). -module(emqx_mgmt_api_metrics).
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -283,13 +283,12 @@ metrics_schema() ->
metrics_api() -> metrics_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "EMQ X metrics", description => <<"EMQ X metrics">>,
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_schema(<<"List all metrics">>, <<"metrics">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"List all metrics">>, metrics)}}},
{"/metrics", Metadata, list}. {"/metrics", Metadata, list}.
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply
list(get, _) -> list(get, _) ->
Response = emqx_json:encode(emqx_mgmt:get_metrics()), {200, emqx_mgmt:get_metrics()}.
{200, Response}.

View File

@ -15,7 +15,7 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-module(emqx_mgmt_api_nodes). -module(emqx_mgmt_api_nodes).
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -103,15 +103,15 @@ node_schema() ->
nodes_api() -> nodes_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "List EMQ X nodes", description => <<"List EMQ X nodes">>,
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_array_schema(<<"List EMQ X Nodes">>, <<"node">>)}}}, <<"200">> => emqx_mgmt_util:response_array_schema(<<"List EMQ X Nodes">>, node)}}},
{"/nodes", Metadata, nodes}. {"/nodes", Metadata, nodes}.
node_api() -> node_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Get node info", description => <<"Get node info">>,
parameters => [#{ parameters => [#{
name => node_name, name => node_name,
in => path, in => path,
@ -121,13 +121,13 @@ node_api() ->
example => node()}], example => node()}],
responses => #{ responses => #{
<<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']), <<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']),
<<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Nodes info by name">>, <<"node">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Nodes info by name">>, node)}}},
{"/nodes/:node_name", Metadata, node}. {"/nodes/:node_name", Metadata, node}.
node_metrics_api() -> node_metrics_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Get node metrics", description => <<"Get node metrics">>,
parameters => [#{ parameters => [#{
name => node_name, name => node_name,
in => path, in => path,
@ -137,13 +137,13 @@ node_metrics_api() ->
example => node()}], example => node()}],
responses => #{ responses => #{
<<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']), <<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']),
<<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Metrics">>, <<"metrics">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Metrics">>, metrics)}}},
{"/nodes/:node_name/metrics", Metadata, node_metrics}. {"/nodes/:node_name/metrics", Metadata, node_metrics}.
node_stats_api() -> node_stats_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "Get node stats", description => <<"Get node stats">>,
parameters => [#{ parameters => [#{
name => node_name, name => node_name,
in => path, in => path,
@ -153,7 +153,7 @@ node_stats_api() ->
example => node()}], example => node()}],
responses => #{ responses => #{
<<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']), <<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']),
<<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Stats">>, <<"stats">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Stats">>, stats)}}},
{"/nodes/:node_name/stats", Metadata, node_metrics}. {"/nodes/:node_name/stats", Metadata, node_metrics}.
%%%============================================================================================== %%%==============================================================================================
@ -177,32 +177,30 @@ node_stats(get, Request) ->
%% api apply %% api apply
list(#{}) -> list(#{}) ->
NodesInfo = [format(Node, NodeInfo) || {Node, NodeInfo} <- emqx_mgmt:list_nodes()], NodesInfo = [format(Node, NodeInfo) || {Node, NodeInfo} <- emqx_mgmt:list_nodes()],
Response = emqx_json:encode(NodesInfo), {200, NodesInfo}.
{200, Response}.
get_node(#{node := Node}) -> get_node(#{node := Node}) ->
case emqx_mgmt:lookup_node(Node) of case emqx_mgmt:lookup_node(Node) of
#{node_status := 'ERROR'} -> #{node_status := 'ERROR'} ->
{400, emqx_json:encode(#{code => 'SOURCE_ERROR', reason => <<"rpc_failed">>})}; {400, #{code => 'SOURCE_ERROR', message => <<"rpc_failed">>}};
NodeInfo -> NodeInfo ->
Response = emqx_json:encode(format(Node, NodeInfo)), {200, format(Node, NodeInfo)}
{200, Response}
end. end.
get_metrics(#{node := Node}) -> get_metrics(#{node := Node}) ->
case emqx_mgmt:get_metrics(Node) of case emqx_mgmt:get_metrics(Node) of
{error, _} -> {error, _} ->
{400, emqx_json:encode(#{code => 'SOURCE_ERROR', reason => <<"rpc_failed">>})}; {400, #{code => 'SOURCE_ERROR', message => <<"rpc_failed">>}};
Metrics -> Metrics ->
{200, emqx_json:encode(Metrics)} {200, Metrics}
end. end.
get_stats(#{node := Node}) -> get_stats(#{node := Node}) ->
case emqx_mgmt:get_stats(Node) of case emqx_mgmt:get_stats(Node) of
{error, _} -> {error, _} ->
{400, emqx_json:encode(#{code => 'SOURCE_ERROR', reason => <<"rpc_failed">>})}; {400, #{code => 'SOURCE_ERROR', message => <<"rpc_failed">>}};
Stats -> Stats ->
{200, emqx_json:encode(Stats)} {200, Stats}
end. end.
%%============================================================================================================ %%============================================================================================================

View File

@ -17,7 +17,7 @@
%% API %% API
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -33,7 +33,7 @@ api_spec() ->
publish_api() -> publish_api() ->
MeteData = #{ MeteData = #{
post => #{ post => #{
description => "publish", description => <<"Publish">>,
'requestBody' => #{ 'requestBody' => #{
content => #{ content => #{
'application/json' => #{ 'application/json' => #{
@ -41,13 +41,13 @@ publish_api() ->
type => object, type => object,
properties => maps:with([id], message_properties())}}}}, properties => maps:with([id], message_properties())}}}},
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_schema(<<"publish ok">>, <<"message">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"publish ok">>, message)}}},
{"/publish", MeteData, publish}. {"/publish", MeteData, publish}.
publish_batch_api() -> publish_batch_api() ->
MeteData = #{ MeteData = #{
post => #{ post => #{
description => "publish", description => <<"publish">>,
'requestBody' => #{ 'requestBody' => #{
content => #{ content => #{
'application/json' => #{ 'application/json' => #{
@ -57,8 +57,8 @@ publish_batch_api() ->
type => object, type => object,
properties => maps:with([id], message_properties())}}}}}, properties => maps:with([id], message_properties())}}}}},
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_array_schema(<<"publish ok">>, <<"message">>)}}}, <<"200">> => emqx_mgmt_util:response_array_schema(<<"publish ok">>, message)}}},
{"/publish_batch", MeteData, publish_batch}. {"/publish/bulk", MeteData, publish_batch}.
message_schema() -> message_schema() ->
#{ #{
@ -110,14 +110,13 @@ publish(post, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
Message = message(emqx_json:decode(Body, [return_maps])), Message = message(emqx_json:decode(Body, [return_maps])),
_ = emqx_mgmt:publish(Message), _ = emqx_mgmt:publish(Message),
{200, emqx_json:encode(format_message(Message))}. {200, format_message(Message)}.
publish_batch(post, Request) -> publish_batch(post, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
Messages = messages(emqx_json:decode(Body, [return_maps])), Messages = messages(emqx_json:decode(Body, [return_maps])),
_ = [emqx_mgmt:publish(Message) || Message <- Messages], _ = [emqx_mgmt:publish(Message) || Message <- Messages],
ResponseBody = emqx_json:encode(format_message(Messages)), {200, format_message(Messages)}.
{200, ResponseBody}.
message(Map) -> message(Map) ->
From = maps:get(<<"from">>, Map, http_api), From = maps:get(<<"from">>, Map, http_api),

View File

@ -19,7 +19,7 @@
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
%% API %% API
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -48,7 +48,7 @@ route_schema() ->
routes_api() -> routes_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "EMQ X routes", description => <<"EMQ X routes">>,
parameters => [ parameters => [
#{ #{
name => page, name => page,
@ -64,13 +64,13 @@ routes_api() ->
}], }],
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_array_schema("List route info", <<"route">>)}}}, emqx_mgmt_util:response_array_schema("List route info", route)}}},
{"/routes", Metadata, routes}. {"/routes", Metadata, routes}.
route_api() -> route_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "EMQ X routes", description => <<"EMQ X routes">>,
parameters => [#{ parameters => [#{
name => topic, name => topic,
in => path, in => path,
@ -80,7 +80,7 @@ route_api() ->
}], }],
responses => #{ responses => #{
<<"200">> => <<"200">> =>
emqx_mgmt_util:response_schema(<<"Route info">>, <<"route">>), emqx_mgmt_util:response_schema(<<"Route info">>, route),
<<"404">> => <<"404">> =>
emqx_mgmt_util:response_error_schema(<<"Topic not found">>, [?TOPIC_NOT_FOUND]) emqx_mgmt_util:response_error_schema(<<"Topic not found">>, [?TOPIC_NOT_FOUND])
}}}, }}},
@ -99,20 +99,15 @@ route(get, Request) ->
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply
list(Params) -> list(Params) ->
Data = emqx_mgmt_api:paginate(emqx_route, Params, fun format/1), Response = emqx_mgmt_api:paginate(emqx_route, Params, fun format/1),
Response = emqx_json:encode(Data),
{200, Response}. {200, Response}.
lookup(#{topic := Topic}) -> lookup(#{topic := Topic}) ->
case emqx_mgmt:lookup_routes(Topic) of case emqx_mgmt:lookup_routes(Topic) of
[] -> [] ->
NotFound = #{code => ?TOPIC_NOT_FOUND, reason => <<"Topic not found">>}, {404, #{code => ?TOPIC_NOT_FOUND, message => <<"Topic not found">>}};
Response = emqx_json:encode(NotFound),
{404, Response};
[Route] -> [Route] ->
Data = format(Route), {200, format(Route)}
Response = emqx_json:encode(Data),
{200, Response}
end. end.
%%%============================================================================================== %%%==============================================================================================

View File

@ -15,7 +15,7 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-module(emqx_mgmt_api_stats). -module(emqx_mgmt_api_stats).
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).
@ -96,13 +96,12 @@ stats_schema() ->
stats_api() -> stats_api() ->
Metadata = #{ Metadata = #{
get => #{ get => #{
description => "EMQ X stats", description => <<"EMQ X stats">>,
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_schema(<<"List stats ok">>, <<"stats">>)}}}, <<"200">> => emqx_mgmt_util:response_schema(<<"List stats ok">>, stats)}}},
{"/stats", Metadata, list}. {"/stats", Metadata, list}.
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply
list(get, _Request) -> list(get, _Request) ->
Response = emqx_json:encode(emqx_mgmt:get_stats()), {200, emqx_mgmt:get_stats()}.
{200, Response}.

View File

@ -15,7 +15,7 @@
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-module(emqx_mgmt_api_status). -module(emqx_mgmt_api_status).
%% API %% API
-behavior(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([api_spec/0]).

View File

@ -16,7 +16,7 @@
-module(emqx_mgmt_api_subscriptions). -module(emqx_mgmt_api_subscriptions).
-behavior(minirest_api). -behaviour(minirest_api).
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
@ -47,7 +47,7 @@ api_spec() ->
subscriptions_api() -> subscriptions_api() ->
MetaData = #{ MetaData = #{
get => #{ get => #{
description => "List subscriptions", description => <<"List subscriptions">>,
parameters => [ parameters => [
#{ #{
name => page, name => page,
@ -93,7 +93,7 @@ subscriptions_api() ->
} }
], ],
responses => #{ responses => #{
<<"200">> => emqx_mgmt_util:response_page_schema(<<"subscription">>)}}}, <<"200">> => emqx_mgmt_util:response_page_schema(subscription)}}},
{"/subscriptions", MetaData, subscriptions}. {"/subscriptions", MetaData, subscriptions}.
subscription_schema() -> subscription_schema() ->

View File

@ -93,16 +93,22 @@ urldecode(S) ->
request_body_array_schema(Schema) when is_map(Schema) -> request_body_array_schema(Schema) when is_map(Schema) ->
json_content_schema("", #{type => array, items => Schema}); json_content_schema("", #{type => array, items => Schema});
request_body_array_schema(Ref) when is_atom(Ref) ->
request_body_array_schema(atom_to_binary(Ref, utf8));
request_body_array_schema(Ref) when is_binary(Ref) -> request_body_array_schema(Ref) when is_binary(Ref) ->
json_content_schema("", #{type => array, items => minirest:ref(Ref)}). json_content_schema("", #{type => array, items => minirest:ref(Ref)}).
request_body_schema(Schema) when is_map(Schema) -> request_body_schema(Schema) when is_map(Schema) ->
json_content_schema("", Schema); json_content_schema("", Schema);
request_body_schema(Ref) when is_atom(Ref) ->
request_body_schema(atom_to_binary(Ref));
request_body_schema(Ref) when is_binary(Ref) -> request_body_schema(Ref) when is_binary(Ref) ->
json_content_schema("", minirest:ref(Ref)). json_content_schema("", minirest:ref(Ref)).
response_array_schema(Description, Schema) when is_map(Schema) -> response_array_schema(Description, Schema) when is_map(Schema) ->
json_content_schema(Description, #{type => array, items => Schema}); json_content_schema(Description, #{type => array, items => Schema});
response_array_schema(Description, Ref) when is_atom(Ref) ->
response_array_schema(Description, atom_to_binary(Ref, utf8));
response_array_schema(Description, Ref) when is_binary(Ref) -> response_array_schema(Description, Ref) when is_binary(Ref) ->
json_content_schema(Description, #{type => array, items => minirest:ref(Ref)}). json_content_schema(Description, #{type => array, items => minirest:ref(Ref)}).
@ -111,6 +117,8 @@ response_schema(Description) ->
response_schema(Description, Schema) when is_map(Schema) -> response_schema(Description, Schema) when is_map(Schema) ->
json_content_schema(Description, Schema); json_content_schema(Description, Schema);
response_schema(Description, Ref) when is_atom(Ref) ->
response_schema(Description, atom_to_binary(Ref, utf8));
response_schema(Description, Ref) when is_binary(Ref) -> response_schema(Description, Ref) when is_binary(Ref) ->
json_content_schema(Description, minirest:ref(Ref)). json_content_schema(Description, minirest:ref(Ref)).
@ -125,10 +133,12 @@ response_error_schema(Description, Enum) ->
code => #{ code => #{
type => string, type => string,
enum => Enum}, enum => Enum},
reason => #{ message => #{
type => string}}}, type => string}}},
json_content_schema(Description, Schema). json_content_schema(Description, Schema).
response_page_schema(Def) when is_atom(Def) ->
response_page_schema(atom_to_binary(Def, utf8));
response_page_schema(Def) when is_binary(Def) -> response_page_schema(Def) when is_binary(Def) ->
Schema = #{ Schema = #{
type => object, type => object,
@ -147,6 +157,8 @@ response_page_schema(Def) when is_binary(Def) ->
items => minirest:ref(Def)}}}, items => minirest:ref(Def)}}},
json_content_schema("", Schema). json_content_schema("", Schema).
response_batch_schema(DefName) when is_atom(DefName) ->
response_batch_schema(atom_to_binary(DefName, utf8));
response_batch_schema(DefName) when is_binary(DefName) -> response_batch_schema(DefName) when is_binary(DefName) ->
Schema = #{ Schema = #{
type => object, type => object,

View File

@ -83,7 +83,7 @@ t_clients(_) ->
%% delete /clients/:clientid kickout %% delete /clients/:clientid kickout
Client2Path = emqx_mgmt_api_test_util:api_path(["clients", binary_to_list(ClientId2)]), Client2Path = emqx_mgmt_api_test_util:api_path(["clients", binary_to_list(ClientId2)]),
{ok, _} = emqx_mgmt_api_test_util:request_api(delete, Client2Path), {ok, _} = emqx_mgmt_api_test_util:request_api(delete, Client2Path),
timer:sleep(100), timer:sleep(300),
AfterKickoutResponse = emqx_mgmt_api_test_util:request_api(get, Client2Path), AfterKickoutResponse = emqx_mgmt_api_test_util:request_api(get, Client2Path),
?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, AfterKickoutResponse), ?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, AfterKickoutResponse),

View File

@ -60,13 +60,13 @@ t_publish_api(_) ->
?assertEqual(receive_assert(?TOPIC1, 0, Payload), ok), ?assertEqual(receive_assert(?TOPIC1, 0, Payload), ok),
emqtt:disconnect(Client). emqtt:disconnect(Client).
t_publish_batch_api(_) -> t_publish_bulk_api(_) ->
{ok, Client} = emqtt:start_link(#{username => <<"api_username">>, clientid => <<"api_clientid">>}), {ok, Client} = emqtt:start_link(#{username => <<"api_username">>, clientid => <<"api_clientid">>}),
{ok, _} = emqtt:connect(Client), {ok, _} = emqtt:connect(Client),
{ok, _, [0]} = emqtt:subscribe(Client, ?TOPIC1), {ok, _, [0]} = emqtt:subscribe(Client, ?TOPIC1),
{ok, _, [0]} = emqtt:subscribe(Client, ?TOPIC2), {ok, _, [0]} = emqtt:subscribe(Client, ?TOPIC2),
Payload = <<"hello">>, Payload = <<"hello">>,
Path = emqx_mgmt_api_test_util:api_path(["publish_batch"]), Path = emqx_mgmt_api_test_util:api_path(["publish", "bulk"]),
Auth = emqx_mgmt_api_test_util:auth_header_(), Auth = emqx_mgmt_api_test_util:auth_header_(),
Body =[#{topic => ?TOPIC1, payload => Payload}, #{topic => ?TOPIC2, payload => Payload}], Body =[#{topic => ?TOPIC1, payload => Payload}, #{topic => ?TOPIC2, payload => Payload}],
{ok, Response} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body), {ok, Response} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body),

View File

@ -50,7 +50,7 @@
, {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.8.2"}}} , {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.8.2"}}}
, {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.10.4"}}} , {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.10.4"}}}
, {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.5.1"}}} , {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.5.1"}}}
, {minirest, {git, "https://github.com/emqx/minirest", {tag, "1.1.2"}}} , {minirest, {git, "https://github.com/emqx/minirest", {tag, "1.1.4"}}}
, {ecpool, {git, "https://github.com/emqx/ecpool", {tag, "0.5.1"}}} , {ecpool, {git, "https://github.com/emqx/ecpool", {tag, "0.5.1"}}}
, {replayq, {git, "https://github.com/emqx/replayq", {tag, "0.3.2"}}} , {replayq, {git, "https://github.com/emqx/replayq", {tag, "0.3.2"}}}
, {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}} , {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}