From 527ea23ec8cbc62c12e582c4758c3986160a4d81 Mon Sep 17 00:00:00 2001 From: firest Date: Mon, 29 Aug 2022 14:20:02 +0800 Subject: [PATCH 1/5] fix(gateway): change the `/gateway` API path to plural form --- apps/emqx_gateway/src/coap/emqx_coap_api.erl | 2 +- apps/emqx_gateway/src/emqx_gateway_api.erl | 8 +- .../src/emqx_gateway_api_authn.erl | 12 +- .../emqx_gateway_api_authn_user_import.erl | 8 +- .../src/emqx_gateway_api_clients.erl | 16 +- .../src/emqx_gateway_api_listeners.erl | 20 +- .../emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl | 2 +- .../emqx_gateway/test/emqx_coap_api_SUITE.erl | 2 +- .../test/emqx_gateway_api_SUITE.erl | 190 +++++++++--------- .../test/emqx_lwm2m_api_SUITE.erl | 4 +- 10 files changed, 132 insertions(+), 132 deletions(-) diff --git a/apps/emqx_gateway/src/coap/emqx_coap_api.erl b/apps/emqx_gateway/src/coap/emqx_coap_api.erl index 9d20ac6dc..beb32b2cf 100644 --- a/apps/emqx_gateway/src/coap/emqx_coap_api.erl +++ b/apps/emqx_gateway/src/coap/emqx_coap_api.erl @@ -28,7 +28,7 @@ -export([request/2]). --define(PREFIX, "/gateway/coap/clients/:clientid"). +-define(PREFIX, "/gateways/coap/clients/:clientid"). -import(hoconsc, [mk/2, enum/1]). -import(emqx_dashboard_swagger, [error_codes/2]). diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 609a02149..6a2f2313c 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -62,8 +62,8 @@ api_spec() -> paths() -> [ - "/gateway", - "/gateway/:name" + "/gateways", + "/gateways/:name" ]. %%-------------------------------------------------------------------- @@ -159,7 +159,7 @@ gateway_insta(put, #{ %% Swagger defines %%-------------------------------------------------------------------- -schema("/gateway") -> +schema("/gateways") -> #{ 'operationId' => gateway, get => @@ -185,7 +185,7 @@ schema("/gateway") -> ?STANDARD_RESP(#{201 => schema_gateways_conf()}) } }; -schema("/gateway/:name") -> +schema("/gateways/:name") -> #{ 'operationId' => gateway_insta, get => diff --git a/apps/emqx_gateway/src/emqx_gateway_api_authn.erl b/apps/emqx_gateway/src/emqx_gateway_api_authn.erl index c49b69e1c..e1cd39c7c 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_authn.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_authn.erl @@ -61,9 +61,9 @@ api_spec() -> paths() -> [ - "/gateway/:name/authentication", - "/gateway/:name/authentication/users", - "/gateway/:name/authentication/users/:uid" + "/gateways/:name/authentication", + "/gateways/:name/authentication/users", + "/gateways/:name/authentication/users/:uid" ]. %%-------------------------------------------------------------------- @@ -176,7 +176,7 @@ parse_qstring(Qs) -> %% Swagger defines %%-------------------------------------------------------------------- -schema("/gateway/:name/authentication") -> +schema("/gateways/:name/authentication") -> #{ 'operationId' => authn, get => @@ -215,7 +215,7 @@ schema("/gateway/:name/authentication") -> ?STANDARD_RESP(#{204 => <<"Deleted">>}) } }; -schema("/gateway/:name/authentication/users") -> +schema("/gateways/:name/authentication/users") -> #{ 'operationId' => users, get => @@ -253,7 +253,7 @@ schema("/gateway/:name/authentication/users") -> ) } }; -schema("/gateway/:name/authentication/users/:uid") -> +schema("/gateways/:name/authentication/users/:uid") -> #{ 'operationId' => users_insta, get => diff --git a/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl b/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl index c324262ee..09482c593 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl @@ -54,8 +54,8 @@ api_spec() -> paths() -> [ - "/gateway/:name/authentication/import_users", - "/gateway/:name/listeners/:id/authentication/import_users" + "/gateways/:name/authentication/import_users", + "/gateways/:name/listeners/:id/authentication/import_users" ]. %%-------------------------------------------------------------------- @@ -117,7 +117,7 @@ import_listener_users(post, #{ %% Swagger defines %%-------------------------------------------------------------------- -schema("/gateway/:name/authentication/import_users") -> +schema("/gateways/:name/authentication/import_users") -> #{ 'operationId' => import_users, post => @@ -129,7 +129,7 @@ schema("/gateway/:name/authentication/import_users") -> ?STANDARD_RESP(#{204 => <<"Imported">>}) } }; -schema("/gateway/:name/listeners/:id/authentication/import_users") -> +schema("/gateways/:name/listeners/:id/authentication/import_users") -> #{ 'operationId' => import_listener_users, post => diff --git a/apps/emqx_gateway/src/emqx_gateway_api_clients.erl b/apps/emqx_gateway/src/emqx_gateway_api_clients.erl index ac0e72c83..cb6618fd1 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_clients.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_clients.erl @@ -68,10 +68,10 @@ api_spec() -> paths() -> [ - "/gateway/:name/clients", - "/gateway/:name/clients/:clientid", - "/gateway/:name/clients/:clientid/subscriptions", - "/gateway/:name/clients/:clientid/subscriptions/:topic" + "/gateways/:name/clients", + "/gateways/:name/clients/:clientid", + "/gateways/:name/clients/:clientid/subscriptions", + "/gateways/:name/clients/:clientid/subscriptions/:topic" ]. -define(CLIENT_QSCHEMA, [ @@ -462,7 +462,7 @@ conn_state_to_connected(_) -> false. %% Swagger defines %%-------------------------------------------------------------------- -schema("/gateway/:name/clients") -> +schema("/gateways/:name/clients") -> #{ 'operationId' => clients, get => @@ -473,7 +473,7 @@ schema("/gateway/:name/clients") -> ?STANDARD_RESP(#{200 => schema_client_list()}) } }; -schema("/gateway/:name/clients/:clientid") -> +schema("/gateways/:name/clients/:clientid") -> #{ 'operationId' => clients_insta, get => @@ -491,7 +491,7 @@ schema("/gateway/:name/clients/:clientid") -> ?STANDARD_RESP(#{204 => <<"Kicked">>}) } }; -schema("/gateway/:name/clients/:clientid/subscriptions") -> +schema("/gateways/:name/clients/:clientid/subscriptions") -> #{ 'operationId' => subscriptions, get => @@ -527,7 +527,7 @@ schema("/gateway/:name/clients/:clientid/subscriptions") -> ) } }; -schema("/gateway/:name/clients/:clientid/subscriptions/:topic") -> +schema("/gateways/:name/clients/:clientid/subscriptions/:topic") -> #{ 'operationId' => subscriptions, delete => diff --git a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl index 79734bfc0..bdf0c4c02 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl @@ -69,11 +69,11 @@ api_spec() -> paths() -> [ - "/gateway/:name/listeners", - "/gateway/:name/listeners/:id", - "/gateway/:name/listeners/:id/authentication", - "/gateway/:name/listeners/:id/authentication/users", - "/gateway/:name/listeners/:id/authentication/users/:uid" + "/gateways/:name/listeners", + "/gateways/:name/listeners/:id", + "/gateways/:name/listeners/:id/authentication", + "/gateways/:name/listeners/:id/authentication/users", + "/gateways/:name/listeners/:id/authentication/users/:uid" ]. %%-------------------------------------------------------------------- @@ -353,7 +353,7 @@ bind2str(Listener = #{<<"bind">> := Bind}) -> %% Swagger defines %%-------------------------------------------------------------------- -schema("/gateway/:name/listeners") -> +schema("/gateways/:name/listeners") -> #{ 'operationId' => listeners, get => @@ -391,7 +391,7 @@ schema("/gateway/:name/listeners") -> ) } }; -schema("/gateway/:name/listeners/:id") -> +schema("/gateways/:name/listeners/:id") -> #{ 'operationId' => listeners_insta, get => @@ -437,7 +437,7 @@ schema("/gateway/:name/listeners/:id") -> ) } }; -schema("/gateway/:name/listeners/:id/authentication") -> +schema("/gateways/:name/listeners/:id/authentication") -> #{ 'operationId' => listeners_insta_authn, get => @@ -480,7 +480,7 @@ schema("/gateway/:name/listeners/:id/authentication") -> ?STANDARD_RESP(#{200 => <<"Deleted">>}) } }; -schema("/gateway/:name/listeners/:id/authentication/users") -> +schema("/gateways/:name/listeners/:id/authentication/users") -> #{ 'operationId' => users, get => @@ -519,7 +519,7 @@ schema("/gateway/:name/listeners/:id/authentication/users") -> ) } }; -schema("/gateway/:name/listeners/:id/authentication/users/:uid") -> +schema("/gateways/:name/listeners/:id/authentication/users/:uid") -> #{ 'operationId' => users_insta, get => diff --git a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl index d8cb6b0a4..3e0a9f9b7 100644 --- a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl +++ b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl @@ -25,7 +25,7 @@ -export([lookup/2, observe/2, read/2, write/2]). --define(PATH(Suffix), "/gateway/lwm2m/clients/:clientid" Suffix). +-define(PATH(Suffix), "/gateways/lwm2m/clients/:clientid" Suffix). -define(DATA_TYPE, ['Integer', 'Float', 'Time', 'String', 'Boolean', 'Opaque', 'Objlnk']). -import(hoconsc, [mk/2, ref/1, ref/2]). diff --git a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl index 0f6ca22bb..577da1a26 100644 --- a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl @@ -71,7 +71,7 @@ end_per_suite(Config) -> t_send_request_api(_) -> ClientId = start_client(), timer:sleep(200), - Path = emqx_mgmt_api_test_util:api_path(["gateway/coap/clients/client1/request"]), + Path = emqx_mgmt_api_test_util:api_path(["gateways/coap/clients/client1/request"]), Token = <<"atoken">>, Payload = <<"simple echo this">>, Req = #{ diff --git a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl index aac140d3e..d873ad1a4 100644 --- a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl @@ -60,23 +60,23 @@ end_per_suite(Conf) -> %%-------------------------------------------------------------------- t_gateway(_) -> - {200, Gateways} = request(get, "/gateway"), + {200, Gateways} = request(get, "/gateways"), lists:foreach(fun assert_gw_unloaded/1, Gateways), - {400, BadReq} = request(get, "/gateway/uname_gateway"), + {400, BadReq} = request(get, "/gateways/uname_gateway"), assert_bad_request(BadReq), - {201, _} = request(post, "/gateway", #{name => <<"stomp">>}), - {200, StompGw1} = request(get, "/gateway/stomp"), + {201, _} = request(post, "/gateways", #{name => <<"stomp">>}), + {200, StompGw1} = request(get, "/gateways/stomp"), assert_feilds_apperence( [name, status, enable, created_at, started_at], StompGw1 ), - {204, _} = request(delete, "/gateway/stomp"), - {200, StompGw2} = request(get, "/gateway/stomp"), + {204, _} = request(delete, "/gateways/stomp"), + {200, StompGw2} = request(get, "/gateways/stomp"), assert_gw_unloaded(StompGw2), ok. t_gateway_stomp(_) -> - {200, Gw} = request(get, "/gateway/stomp"), + {200, Gw} = request(get, "/gateways/stomp"), assert_gw_unloaded(Gw), %% post GwConf = #{ @@ -90,18 +90,18 @@ t_gateway_stomp(_) -> #{name => <<"def">>, type => <<"tcp">>, bind => <<"61613">>} ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/stomp"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/stomp"), assert_confs(GwConf, ConfResp), %% put GwConf2 = emqx_map_lib:deep_merge(GwConf, #{frame => #{max_headers => 10}}), - {200, _} = request(put, "/gateway/stomp", maps:without([name, listeners], GwConf2)), - {200, ConfResp2} = request(get, "/gateway/stomp"), + {200, _} = request(put, "/gateways/stomp", maps:without([name, listeners], GwConf2)), + {200, ConfResp2} = request(get, "/gateways/stomp"), assert_confs(GwConf2, ConfResp2), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp"). t_gateway_mqttsn(_) -> - {200, Gw} = request(get, "/gateway/mqttsn"), + {200, Gw} = request(get, "/gateways/mqttsn"), assert_gw_unloaded(Gw), %% post GwConf = #{ @@ -114,18 +114,18 @@ t_gateway_mqttsn(_) -> #{name => <<"def">>, type => <<"udp">>, bind => <<"1884">>} ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/mqttsn"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/mqttsn"), assert_confs(GwConf, ConfResp), %% put GwConf2 = emqx_map_lib:deep_merge(GwConf, #{predefined => []}), - {200, _} = request(put, "/gateway/mqttsn", maps:without([name, listeners], GwConf2)), - {200, ConfResp2} = request(get, "/gateway/mqttsn"), + {200, _} = request(put, "/gateways/mqttsn", maps:without([name, listeners], GwConf2)), + {200, ConfResp2} = request(get, "/gateways/mqttsn"), assert_confs(GwConf2, ConfResp2), - {204, _} = request(delete, "/gateway/mqttsn"). + {204, _} = request(delete, "/gateways/mqttsn"). t_gateway_coap(_) -> - {200, Gw} = request(get, "/gateway/coap"), + {200, Gw} = request(get, "/gateways/coap"), assert_gw_unloaded(Gw), %% post GwConf = #{ @@ -136,18 +136,18 @@ t_gateway_coap(_) -> #{name => <<"def">>, type => <<"udp">>, bind => <<"5683">>} ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/coap"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/coap"), assert_confs(GwConf, ConfResp), %% put GwConf2 = emqx_map_lib:deep_merge(GwConf, #{heartbeat => <<"10s">>}), - {200, _} = request(put, "/gateway/coap", maps:without([name, listeners], GwConf2)), - {200, ConfResp2} = request(get, "/gateway/coap"), + {200, _} = request(put, "/gateways/coap", maps:without([name, listeners], GwConf2)), + {200, ConfResp2} = request(get, "/gateways/coap"), assert_confs(GwConf2, ConfResp2), - {204, _} = request(delete, "/gateway/coap"). + {204, _} = request(delete, "/gateways/coap"). t_gateway_lwm2m(_) -> - {200, Gw} = request(get, "/gateway/lwm2m"), + {200, Gw} = request(get, "/gateways/lwm2m"), assert_gw_unloaded(Gw), %% post GwConf = #{ @@ -168,18 +168,18 @@ t_gateway_lwm2m(_) -> #{name => <<"def">>, type => <<"udp">>, bind => <<"5783">>} ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/lwm2m"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/lwm2m"), assert_confs(GwConf, ConfResp), %% put GwConf2 = emqx_map_lib:deep_merge(GwConf, #{qmode_time_window => <<"10s">>}), - {200, _} = request(put, "/gateway/lwm2m", maps:without([name, listeners], GwConf2)), - {200, ConfResp2} = request(get, "/gateway/lwm2m"), + {200, _} = request(put, "/gateways/lwm2m", maps:without([name, listeners], GwConf2)), + {200, ConfResp2} = request(get, "/gateways/lwm2m"), assert_confs(GwConf2, ConfResp2), - {204, _} = request(delete, "/gateway/lwm2m"). + {204, _} = request(delete, "/gateways/lwm2m"). t_gateway_exproto(_) -> - {200, Gw} = request(get, "/gateway/exproto"), + {200, Gw} = request(get, "/gateways/exproto"), assert_gw_unloaded(Gw), %% post GwConf = #{ @@ -190,18 +190,18 @@ t_gateway_exproto(_) -> #{name => <<"def">>, type => <<"tcp">>, bind => <<"7993">>} ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/exproto"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/exproto"), assert_confs(GwConf, ConfResp), %% put GwConf2 = emqx_map_lib:deep_merge(GwConf, #{server => #{bind => <<"9200">>}}), - {200, _} = request(put, "/gateway/exproto", maps:without([name, listeners], GwConf2)), - {200, ConfResp2} = request(get, "/gateway/exproto"), + {200, _} = request(put, "/gateways/exproto", maps:without([name, listeners], GwConf2)), + {200, ConfResp2} = request(get, "/gateways/exproto"), assert_confs(GwConf2, ConfResp2), - {204, _} = request(delete, "/gateway/exproto"). + {204, _} = request(delete, "/gateways/exproto"). t_gateway_exproto_with_ssl(_) -> - {200, Gw} = request(get, "/gateway/exproto"), + {200, Gw} = request(get, "/gateways/exproto"), assert_gw_unloaded(Gw), SslSvrOpts = ssl_server_opts(), @@ -221,8 +221,8 @@ t_gateway_exproto_with_ssl(_) -> #{name => <<"def">>, type => <<"tcp">>, bind => <<"7993">>} ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/exproto"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/exproto"), assert_confs(GwConf, ConfResp), %% put GwConf2 = emqx_map_lib:deep_merge(GwConf, #{ @@ -231,50 +231,50 @@ t_gateway_exproto_with_ssl(_) -> ssl_options => SslCliOpts } }), - {200, _} = request(put, "/gateway/exproto", maps:without([name, listeners], GwConf2)), - {200, ConfResp2} = request(get, "/gateway/exproto"), + {200, _} = request(put, "/gateways/exproto", maps:without([name, listeners], GwConf2)), + {200, ConfResp2} = request(get, "/gateways/exproto"), assert_confs(GwConf2, ConfResp2), - {204, _} = request(delete, "/gateway/exproto"). + {204, _} = request(delete, "/gateways/exproto"). t_authn(_) -> GwConf = #{name => <<"stomp">>}, - {201, _} = request(post, "/gateway", GwConf), + {201, _} = request(post, "/gateways", GwConf), ct:sleep(500), - {204, _} = request(get, "/gateway/stomp/authentication"), + {204, _} = request(get, "/gateways/stomp/authentication"), AuthConf = #{ mechanism => <<"password_based">>, backend => <<"built_in_database">>, user_id_type => <<"clientid">> }, - {201, _} = request(post, "/gateway/stomp/authentication", AuthConf), - {200, ConfResp} = request(get, "/gateway/stomp/authentication"), + {201, _} = request(post, "/gateways/stomp/authentication", AuthConf), + {200, ConfResp} = request(get, "/gateways/stomp/authentication"), assert_confs(AuthConf, ConfResp), AuthConf2 = maps:merge(AuthConf, #{user_id_type => <<"username">>}), - {200, _} = request(put, "/gateway/stomp/authentication", AuthConf2), + {200, _} = request(put, "/gateways/stomp/authentication", AuthConf2), - {200, ConfResp2} = request(get, "/gateway/stomp/authentication"), + {200, ConfResp2} = request(get, "/gateways/stomp/authentication"), assert_confs(AuthConf2, ConfResp2), - {204, _} = request(delete, "/gateway/stomp/authentication"), - {204, _} = request(get, "/gateway/stomp/authentication"), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp/authentication"), + {204, _} = request(get, "/gateways/stomp/authentication"), + {204, _} = request(delete, "/gateways/stomp"). t_authn_data_mgmt(_) -> GwConf = #{name => <<"stomp">>}, - {201, _} = request(post, "/gateway", GwConf), + {201, _} = request(post, "/gateways", GwConf), ct:sleep(500), - {204, _} = request(get, "/gateway/stomp/authentication"), + {204, _} = request(get, "/gateways/stomp/authentication"), AuthConf = #{ mechanism => <<"password_based">>, backend => <<"built_in_database">>, user_id_type => <<"clientid">> }, - {201, _} = request(post, "/gateway/stomp/authentication", AuthConf), + {201, _} = request(post, "/gateways/stomp/authentication", AuthConf), ct:sleep(500), - {200, ConfResp} = request(get, "/gateway/stomp/authentication"), + {200, ConfResp} = request(get, "/gateways/stomp/authentication"), assert_confs(AuthConf, ConfResp), User1 = #{ @@ -282,19 +282,19 @@ t_authn_data_mgmt(_) -> password => <<"123456">>, is_superuser => false }, - {201, _} = request(post, "/gateway/stomp/authentication/users", User1), - {200, #{data := [UserRespd1]}} = request(get, "/gateway/stomp/authentication/users"), + {201, _} = request(post, "/gateways/stomp/authentication/users", User1), + {200, #{data := [UserRespd1]}} = request(get, "/gateways/stomp/authentication/users"), assert_confs(UserRespd1, User1), {200, UserRespd2} = request( get, - "/gateway/stomp/authentication/users/test" + "/gateways/stomp/authentication/users/test" ), assert_confs(UserRespd2, User1), {200, UserRespd3} = request( put, - "/gateway/stomp/authentication/users/test", + "/gateways/stomp/authentication/users/test", #{ password => <<"654321">>, is_superuser => true @@ -304,19 +304,19 @@ t_authn_data_mgmt(_) -> {200, UserRespd4} = request( get, - "/gateway/stomp/authentication/users/test" + "/gateways/stomp/authentication/users/test" ), assert_confs(UserRespd4, User1#{is_superuser => true}), - {204, _} = request(delete, "/gateway/stomp/authentication/users/test"), + {204, _} = request(delete, "/gateways/stomp/authentication/users/test"), {200, #{data := []}} = request( get, - "/gateway/stomp/authentication/users" + "/gateways/stomp/authentication/users" ), ImportUri = emqx_dashboard_api_test_helpers:uri( - ["gateway", "stomp", "authentication", "import_users"] + ["gateways", "stomp", "authentication", "import_users"] ), Dir = code:lib_dir(emqx_authn, test), @@ -332,38 +332,38 @@ t_authn_data_mgmt(_) -> {filename, "user-credentials.csv", CSVData} ]), - {204, _} = request(delete, "/gateway/stomp/authentication"), - {204, _} = request(get, "/gateway/stomp/authentication"), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp/authentication"), + {204, _} = request(get, "/gateways/stomp/authentication"), + {204, _} = request(delete, "/gateways/stomp"). t_listeners_tcp(_) -> GwConf = #{name => <<"stomp">>}, - {201, _} = request(post, "/gateway", GwConf), - {404, _} = request(get, "/gateway/stomp/listeners"), + {201, _} = request(post, "/gateways", GwConf), + {404, _} = request(get, "/gateways/stomp/listeners"), LisConf = #{ name => <<"def">>, type => <<"tcp">>, bind => <<"127.0.0.1:61613">> }, - {201, _} = request(post, "/gateway/stomp/listeners", LisConf), - {200, ConfResp} = request(get, "/gateway/stomp/listeners"), + {201, _} = request(post, "/gateways/stomp/listeners", LisConf), + {200, ConfResp} = request(get, "/gateways/stomp/listeners"), assert_confs([LisConf], ConfResp), - {200, ConfResp1} = request(get, "/gateway/stomp/listeners/stomp:tcp:def"), + {200, ConfResp1} = request(get, "/gateways/stomp/listeners/stomp:tcp:def"), assert_confs(LisConf, ConfResp1), LisConf2 = maps:merge(LisConf, #{bind => <<"127.0.0.1:61614">>}), {200, _} = request( put, - "/gateway/stomp/listeners/stomp:tcp:def", + "/gateways/stomp/listeners/stomp:tcp:def", LisConf2 ), - {200, ConfResp2} = request(get, "/gateway/stomp/listeners/stomp:tcp:def"), + {200, ConfResp2} = request(get, "/gateways/stomp/listeners/stomp:tcp:def"), assert_confs(LisConf2, ConfResp2), - {204, _} = request(delete, "/gateway/stomp/listeners/stomp:tcp:def"), - {404, _} = request(get, "/gateway/stomp/listeners/stomp:tcp:def"), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp/listeners/stomp:tcp:def"), + {404, _} = request(get, "/gateways/stomp/listeners/stomp:tcp:def"), + {204, _} = request(delete, "/gateways/stomp"). t_listeners_authn(_) -> GwConf = #{ @@ -376,9 +376,9 @@ t_listeners_authn(_) -> } ] }, - {201, _} = request(post, "/gateway", GwConf), + {201, _} = request(post, "/gateways", GwConf), ct:sleep(500), - {200, ConfResp} = request(get, "/gateway/stomp"), + {200, ConfResp} = request(get, "/gateways/stomp"), assert_confs(GwConf, ConfResp), AuthConf = #{ @@ -386,7 +386,7 @@ t_listeners_authn(_) -> backend => <<"built_in_database">>, user_id_type => <<"clientid">> }, - Path = "/gateway/stomp/listeners/stomp:tcp:def/authentication", + Path = "/gateways/stomp/listeners/stomp:tcp:def/authentication", {201, _} = request(post, Path, AuthConf), {200, ConfResp2} = request(get, Path), assert_confs(AuthConf, ConfResp2), @@ -400,7 +400,7 @@ t_listeners_authn(_) -> {204, _} = request(delete, Path), %% FIXME: 204? {204, _} = request(get, Path), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp"). t_listeners_authn_data_mgmt(_) -> GwConf = #{ @@ -413,8 +413,8 @@ t_listeners_authn_data_mgmt(_) -> } ] }, - {201, _} = request(post, "/gateway", GwConf), - {200, ConfResp} = request(get, "/gateway/stomp"), + {201, _} = request(post, "/gateways", GwConf), + {200, ConfResp} = request(get, "/gateways/stomp"), assert_confs(GwConf, ConfResp), AuthConf = #{ @@ -422,7 +422,7 @@ t_listeners_authn_data_mgmt(_) -> backend => <<"built_in_database">>, user_id_type => <<"clientid">> }, - Path = "/gateway/stomp/listeners/stomp:tcp:def/authentication", + Path = "/gateways/stomp/listeners/stomp:tcp:def/authentication", {201, _} = request(post, Path, AuthConf), {200, ConfResp2} = request(get, Path), assert_confs(AuthConf, ConfResp2), @@ -434,7 +434,7 @@ t_listeners_authn_data_mgmt(_) -> }, {201, _} = request( post, - "/gateway/stomp/listeners/stomp:tcp:def/authentication/users", + "/gateways/stomp/listeners/stomp:tcp:def/authentication/users", User1 ), @@ -474,7 +474,7 @@ t_listeners_authn_data_mgmt(_) -> ), ImportUri = emqx_dashboard_api_test_helpers:uri( - ["gateway", "stomp", "listeners", "stomp:tcp:def", "authentication", "import_users"] + ["gateways", "stomp", "listeners", "stomp:tcp:def", "authentication", "import_users"] ), Dir = code:lib_dir(emqx_authn, test), @@ -490,31 +490,31 @@ t_listeners_authn_data_mgmt(_) -> {filename, "user-credentials.csv", CSVData} ]), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp"). t_authn_fuzzy_search(_) -> GwConf = #{name => <<"stomp">>}, - {201, _} = request(post, "/gateway", GwConf), - {204, _} = request(get, "/gateway/stomp/authentication"), + {201, _} = request(post, "/gateways", GwConf), + {204, _} = request(get, "/gateways/stomp/authentication"), AuthConf = #{ mechanism => <<"password_based">>, backend => <<"built_in_database">>, user_id_type => <<"clientid">> }, - {201, _} = request(post, "/gateway/stomp/authentication", AuthConf), - {200, ConfResp} = request(get, "/gateway/stomp/authentication"), + {201, _} = request(post, "/gateways/stomp/authentication", AuthConf), + {200, ConfResp} = request(get, "/gateways/stomp/authentication"), assert_confs(AuthConf, ConfResp), Checker = fun({User, Fuzzy}) -> {200, #{data := [UserRespd]}} = request( - get, "/gateway/stomp/authentication/users", Fuzzy + get, "/gateways/stomp/authentication/users", Fuzzy ), assert_confs(UserRespd, User) end, Create = fun(User) -> - {201, _} = request(post, "/gateway/stomp/authentication/users", User) + {201, _} = request(post, "/gateways/stomp/authentication/users", User) end, UserDatas = [ @@ -535,9 +535,9 @@ t_authn_fuzzy_search(_) -> lists:foreach(Create, UserDatas), lists:foreach(Checker, lists:zip(UserDatas, FuzzyDatas)), - {204, _} = request(delete, "/gateway/stomp/authentication"), - {204, _} = request(get, "/gateway/stomp/authentication"), - {204, _} = request(delete, "/gateway/stomp"). + {204, _} = request(delete, "/gateways/stomp/authentication"), + {204, _} = request(get, "/gateways/stomp/authentication"), + {204, _} = request(delete, "/gateways/stomp"). %%-------------------------------------------------------------------- %% Asserts diff --git a/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl b/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl index 671b4bae9..8c011c54a 100644 --- a/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl @@ -338,7 +338,7 @@ t_observe(Config) -> %%% Internal Functions %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% call_lookup_api(ClientId, Path, Action) -> - ApiPath = emqx_mgmt_api_test_util:api_path(["gateway/lwm2m/clients", ClientId, "lookup"]), + ApiPath = emqx_mgmt_api_test_util:api_path(["gateways/lwm2m/clients", ClientId, "lookup"]), Auth = emqx_mgmt_api_test_util:auth_header_(), Query = io_lib:format("path=~ts&action=~ts", [Path, Action]), {ok, Response} = emqx_mgmt_api_test_util:request_api(get, ApiPath, Query, Auth), @@ -346,7 +346,7 @@ call_lookup_api(ClientId, Path, Action) -> Response. call_send_api(ClientId, Cmd, Query) -> - ApiPath = emqx_mgmt_api_test_util:api_path(["gateway/lwm2m/clients", ClientId, Cmd]), + ApiPath = emqx_mgmt_api_test_util:api_path(["gateways/lwm2m/clients", ClientId, Cmd]), Auth = emqx_mgmt_api_test_util:auth_header_(), {ok, Response} = emqx_mgmt_api_test_util:request_api(post, ApiPath, Query, Auth), ?LOGT("rest api response:~ts~n", [Response]), From 81a86e3202040ca3f29cf67be00b058f09cffc4a Mon Sep 17 00:00:00 2001 From: firest Date: Mon, 29 Aug 2022 15:27:11 +0800 Subject: [PATCH 2/5] fix(gateway): fix test case error --- apps/emqx_gateway/test/emqx_coap_SUITE.erl | 20 +++++++++---------- .../test/emqx_gateway_auth_ct.erl | 4 ++-- apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl | 16 +++++++-------- .../test/emqx_sn_protocol_SUITE.erl | 14 ++++++------- apps/emqx_gateway/test/emqx_stomp_SUITE.erl | 6 +++--- 5 files changed, 30 insertions(+), 30 deletions(-) diff --git a/apps/emqx_gateway/test/emqx_coap_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_SUITE.erl index 774776468..eac23a202 100644 --- a/apps/emqx_gateway/test/emqx_coap_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_SUITE.erl @@ -255,37 +255,37 @@ t_clients_api(_) -> Fun = fun(_Channel, _Token) -> ClientId = <<"client1">>, %% list - {200, #{data := [Client1]}} = request(get, "/gateway/coap/clients"), + {200, #{data := [Client1]}} = request(get, "/gateways/coap/clients"), #{clientid := ClientId} = Client1, %% searching {200, #{data := [Client2]}} = request( get, - "/gateway/coap/clients", + "/gateways/coap/clients", [{<<"clientid">>, ClientId}] ), {200, #{data := [Client3]}} = request( get, - "/gateway/coap/clients", + "/gateways/coap/clients", [{<<"like_clientid">>, <<"cli">>}] ), %% lookup {200, Client4} = - request(get, "/gateway/coap/clients/client1"), + request(get, "/gateways/coap/clients/client1"), %% assert Client1 = Client2 = Client3 = Client4, %% kickout {204, _} = - request(delete, "/gateway/coap/clients/client1"), + request(delete, "/gateways/coap/clients/client1"), timer:sleep(200), - {200, #{data := []}} = request(get, "/gateway/coap/clients") + {200, #{data := []}} = request(get, "/gateways/coap/clients") end, with_connection(Fun). t_clients_subscription_api(_) -> Fun = fun(_Channel, _Token) -> - Path = "/gateway/coap/clients/client1/subscriptions", + Path = "/gateways/coap/clients/client1/subscriptions", %% list {200, []} = request(get, Path), %% create @@ -312,7 +312,7 @@ t_clients_subscription_api(_) -> t_clients_get_subscription_api(_) -> Fun = fun(Channel, Token) -> - Path = "/gateway/coap/clients/client1/subscriptions", + Path = "/gateways/coap/clients/client1/subscriptions", %% list {200, []} = request(get, Path), @@ -333,14 +333,14 @@ t_on_offline_event(_) -> emqx_hooks:add('client.connected', {emqx_sys, on_client_connected, []}, 1000), emqx_hooks:add('client.disconnected', {emqx_sys, on_client_disconnected, []}, 1000), - ConnectedSub = <<"$SYS/brokers/+/gateway/coap/clients/+/connected">>, + ConnectedSub = <<"$SYS/brokers/+/gateways/coap/clients/+/connected">>, emqx_broker:subscribe(ConnectedSub), timer:sleep(100), Token = connection(Channel), ?assertMatch(#message{}, receive_deliver(500)), - DisconnectedSub = <<"$SYS/brokers/+/gateway/coap/clients/+/disconnected">>, + DisconnectedSub = <<"$SYS/brokers/+/gateways/coap/clients/+/disconnected">>, emqx_broker:subscribe(DisconnectedSub), timer:sleep(100), diff --git a/apps/emqx_gateway/test/emqx_gateway_auth_ct.erl b/apps/emqx_gateway/test/emqx_gateway_auth_ct.erl index e24764030..d9d2e0dca 100644 --- a/apps/emqx_gateway/test/emqx_gateway_auth_ct.erl +++ b/apps/emqx_gateway/test/emqx_gateway_auth_ct.erl @@ -141,7 +141,7 @@ on_start_auth(authn_http) -> %% set authn for gateway Setup = fun(Gateway) -> - Path = io_lib:format("/gateway/~ts/authentication", [Gateway]), + Path = io_lib:format("/gateways/~ts/authentication", [Gateway]), {204, _} = request(delete, Path), timer:sleep(200), {201, _} = request(post, Path, http_authn_config()), @@ -198,7 +198,7 @@ on_start_auth(authz_http) -> on_stop_auth(authn_http) -> Delete = fun(Gateway) -> - Path = io_lib:format("/gateway/~ts/authentication", [Gateway]), + Path = io_lib:format("/gateways/~ts/authentication", [Gateway]), {204, _} = request(delete, Path) end, lists:foreach(Delete, ?GATEWAYS), diff --git a/apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl b/apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl index 2f5d71a43..1365200d0 100644 --- a/apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_lwm2m_SUITE.erl @@ -2353,18 +2353,18 @@ case100_clients_api(Config) -> std_register(UdpSock, Epn, ObjectList, MsgId1, RespTopic), %% list - {200, #{data := [Client1]}} = request(get, "/gateway/lwm2m/clients"), + {200, #{data := [Client1]}} = request(get, "/gateways/lwm2m/clients"), %% searching {200, #{data := [Client2]}} = request( get, - "/gateway/lwm2m/clients", + "/gateways/lwm2m/clients", [{<<"endpoint_name">>, list_to_binary(Epn)}] ), {200, #{data := [Client3]}} = request( get, - "/gateway/lwm2m/clients", + "/gateways/lwm2m/clients", [ {<<"like_endpoint_name">>, list_to_binary(Epn)}, {<<"gte_lifetime">>, <<"1">>} @@ -2373,14 +2373,14 @@ case100_clients_api(Config) -> %% lookup ClientId = maps:get(clientid, Client1), {200, Client4} = - request(get, "/gateway/lwm2m/clients/" ++ binary_to_list(ClientId)), + request(get, "/gateways/lwm2m/clients/" ++ binary_to_list(ClientId)), %% assert Client1 = Client2 = Client3 = Client4, %% kickout {204, _} = - request(delete, "/gateway/lwm2m/clients/" ++ binary_to_list(ClientId)), + request(delete, "/gateways/lwm2m/clients/" ++ binary_to_list(ClientId)), timer:sleep(100), - {200, #{data := []}} = request(get, "/gateway/lwm2m/clients"). + {200, #{data := []}} = request(get, "/gateways/lwm2m/clients"). case100_subscription_api(Config) -> Epn = "urn:oma:lwm2m:oma:3", @@ -2390,10 +2390,10 @@ case100_subscription_api(Config) -> RespTopic = list_to_binary("lwm2m/" ++ Epn ++ "/up/resp"), std_register(UdpSock, Epn, ObjectList, MsgId1, RespTopic), - {200, #{data := [Client1]}} = request(get, "/gateway/lwm2m/clients"), + {200, #{data := [Client1]}} = request(get, "/gateways/lwm2m/clients"), ClientId = maps:get(clientid, Client1), Path = - "/gateway/lwm2m/clients/" ++ + "/gateways/lwm2m/clients/" ++ binary_to_list(ClientId) ++ "/subscriptions", diff --git a/apps/emqx_gateway/test/emqx_sn_protocol_SUITE.erl b/apps/emqx_gateway/test/emqx_sn_protocol_SUITE.erl index 084fd9f57..2c8a98c03 100644 --- a/apps/emqx_gateway/test/emqx_sn_protocol_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_sn_protocol_SUITE.erl @@ -2198,15 +2198,15 @@ t_clients_api(_) -> send_connect_msg(Socket, ClientId), ?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)), %% list - {200, #{data := [Client1]}} = request(get, "/gateway/mqttsn/clients"), + {200, #{data := [Client1]}} = request(get, "/gateways/mqttsn/clients"), #{clientid := ClientId} = Client1, %% searching {200, #{data := [Client2]}} = - request(get, "/gateway/mqttsn/clients", [{<<"clientid">>, ClientId}]), + request(get, "/gateways/mqttsn/clients", [{<<"clientid">>, ClientId}]), {200, #{data := [Client3]}} = request( get, - "/gateway/mqttsn/clients", + "/gateways/mqttsn/clients", [ {<<"like_clientid">>, <<"test1">>}, {<<"proto_ver">>, <<"1.2">>}, @@ -2218,21 +2218,21 @@ t_clients_api(_) -> ), %% lookup {200, Client4} = - request(get, "/gateway/mqttsn/clients/client_id_test1"), + request(get, "/gateways/mqttsn/clients/client_id_test1"), %% assert Client1 = Client2 = Client3 = Client4, %% kickout {204, _} = - request(delete, "/gateway/mqttsn/clients/client_id_test1"), + request(delete, "/gateways/mqttsn/clients/client_id_test1"), timer:sleep(100), - {200, #{data := []}} = request(get, "/gateway/mqttsn/clients"), + {200, #{data := []}} = request(get, "/gateways/mqttsn/clients"), send_disconnect_msg(Socket, undefined), gen_udp:close(Socket). t_clients_subscription_api(_) -> ClientId = <<"client_id_test1">>, - Path = "/gateway/mqttsn/clients/client_id_test1/subscriptions", + Path = "/gateways/mqttsn/clients/client_id_test1/subscriptions", {ok, Socket} = gen_udp:open(0, [binary]), send_connect_msg(Socket, ClientId), ?assertEqual(<<3, ?SN_CONNACK, 0>>, receive_response(Socket)), diff --git a/apps/emqx_gateway/test/emqx_stomp_SUITE.erl b/apps/emqx_gateway/test/emqx_stomp_SUITE.erl index 3e821042d..7299159ab 100644 --- a/apps/emqx_gateway/test/emqx_stomp_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_stomp_SUITE.erl @@ -721,12 +721,12 @@ t_rest_clienit_info(_) -> _, _} = parse(Data), %% client lists - {200, Clients} = request(get, "/gateway/stomp/clients"), + {200, Clients} = request(get, "/gateways/stomp/clients"), ?assertEqual(1, length(maps:get(data, Clients))), StompClient = lists:nth(1, maps:get(data, Clients)), ClientId = maps:get(clientid, StompClient), ClientPath = - "/gateway/stomp/clients/" ++ + "/gateways/stomp/clients/" ++ binary_to_list(ClientId), {200, StompClient1} = request(get, ClientPath), ?assertEqual(StompClient, StompClient1), @@ -811,7 +811,7 @@ t_rest_clienit_info(_) -> % sync ignored = gen_server:call(emqx_cm, ignore, infinity), ok = emqx_pool:flush_async_tasks(), - {200, Clients2} = request(get, "/gateway/stomp/clients"), + {200, Clients2} = request(get, "/gateways/stomp/clients"), ?assertEqual(0, length(maps:get(data, Clients2))) end). From bc12899ef8700b14dc205a747921e302d6c1542c Mon Sep 17 00:00:00 2001 From: firest Date: Mon, 29 Aug 2022 16:04:42 +0800 Subject: [PATCH 3/5] fix(gateway): fix coap test case error --- apps/emqx_gateway/test/emqx_coap_SUITE.erl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/emqx_gateway/test/emqx_coap_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_SUITE.erl index eac23a202..e672e2d59 100644 --- a/apps/emqx_gateway/test/emqx_coap_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_SUITE.erl @@ -333,14 +333,14 @@ t_on_offline_event(_) -> emqx_hooks:add('client.connected', {emqx_sys, on_client_connected, []}, 1000), emqx_hooks:add('client.disconnected', {emqx_sys, on_client_disconnected, []}, 1000), - ConnectedSub = <<"$SYS/brokers/+/gateways/coap/clients/+/connected">>, + ConnectedSub = <<"$SYS/brokers/+/gateway/coap/clients/+/connected">>, emqx_broker:subscribe(ConnectedSub), timer:sleep(100), Token = connection(Channel), ?assertMatch(#message{}, receive_deliver(500)), - DisconnectedSub = <<"$SYS/brokers/+/gateways/coap/clients/+/disconnected">>, + DisconnectedSub = <<"$SYS/brokers/+/gateway/coap/clients/+/disconnected">>, emqx_broker:subscribe(DisconnectedSub), timer:sleep(100), From 1233d3c8df19c2c5fed854800c0e2d6a9f1c9c60 Mon Sep 17 00:00:00 2001 From: firest Date: Wed, 31 Aug 2022 10:53:13 +0800 Subject: [PATCH 4/5] fix(gateway): compatible with the old "/gateway" form API --- apps/emqx_gateway/src/coap/emqx_coap_api.erl | 6 ++- apps/emqx_gateway/src/emqx_gateway_api.erl | 8 +-- .../src/emqx_gateway_api_authn.erl | 9 ++-- .../emqx_gateway_api_authn_user_import.erl | 9 ++-- .../src/emqx_gateway_api_clients.erl | 8 +-- .../src/emqx_gateway_api_listeners.erl | 9 ++-- apps/emqx_gateway/src/emqx_gateway_utils.erl | 37 +++++++++++++- .../emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl | 8 ++- .../emqx_gateway/test/emqx_coap_api_SUITE.erl | 49 ++++++++++--------- .../test/emqx_gateway_api_SUITE.erl | 16 ++++++ .../test/emqx_lwm2m_api_SUITE.erl | 10 +++- 11 files changed, 122 insertions(+), 47 deletions(-) diff --git a/apps/emqx_gateway/src/coap/emqx_coap_api.erl b/apps/emqx_gateway/src/coap/emqx_coap_api.erl index beb32b2cf..5e94934da 100644 --- a/apps/emqx_gateway/src/coap/emqx_coap_api.erl +++ b/apps/emqx_gateway/src/coap/emqx_coap_api.erl @@ -42,7 +42,7 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}). paths() -> - [?PREFIX ++ "/request"]. + emqx_gateway_utils:make_deprecated_paths([?PREFIX ++ "/request"]). schema(?PREFIX ++ "/request") -> #{ @@ -60,7 +60,9 @@ schema(?PREFIX ++ "/request") -> ) } } - }. + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). request(post, #{body := Body, bindings := Bindings}) -> ClientId = maps:get(clientid, Bindings, undefined), diff --git a/apps/emqx_gateway/src/emqx_gateway_api.erl b/apps/emqx_gateway/src/emqx_gateway_api.erl index 6a2f2313c..5ae8fe1e7 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api.erl @@ -61,10 +61,10 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). paths() -> - [ + emqx_gateway_utils:make_deprecated_paths([ "/gateways", "/gateways/:name" - ]. + ]). %%-------------------------------------------------------------------- %% http handlers @@ -210,7 +210,9 @@ schema("/gateways/:name") -> responses => ?STANDARD_RESP(#{200 => schema_gateways_conf()}) } - }. + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). %%-------------------------------------------------------------------- %% params defines diff --git a/apps/emqx_gateway/src/emqx_gateway_api_authn.erl b/apps/emqx_gateway/src/emqx_gateway_api_authn.erl index e1cd39c7c..6fd073a3b 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_authn.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_authn.erl @@ -60,11 +60,11 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). paths() -> - [ + emqx_gateway_utils:make_deprecated_paths([ "/gateways/:name/authentication", "/gateways/:name/authentication/users", "/gateways/:name/authentication/users/:uid" - ]. + ]). %%-------------------------------------------------------------------- %% http handlers @@ -298,8 +298,9 @@ schema("/gateways/:name/authentication/users/:uid") -> responses => ?STANDARD_RESP(#{204 => <<"User Deleted">>}) } - }. - + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). %%-------------------------------------------------------------------- %% params defines diff --git a/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl b/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl index 09482c593..38036f7c7 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_authn_user_import.erl @@ -53,10 +53,10 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE, #{check_schema => false}). paths() -> - [ + emqx_gateway_utils:make_deprecated_paths([ "/gateways/:name/authentication/import_users", "/gateways/:name/listeners/:id/authentication/import_users" - ]. + ]). %%-------------------------------------------------------------------- %% http handlers @@ -141,8 +141,9 @@ schema("/gateways/:name/listeners/:id/authentication/import_users") -> responses => ?STANDARD_RESP(#{204 => <<"Imported">>}) } - }. - + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). %%-------------------------------------------------------------------- %% params defines %%-------------------------------------------------------------------- diff --git a/apps/emqx_gateway/src/emqx_gateway_api_clients.erl b/apps/emqx_gateway/src/emqx_gateway_api_clients.erl index cb6618fd1..b7cf9fc64 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_clients.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_clients.erl @@ -67,12 +67,12 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}). paths() -> - [ + emqx_gateway_utils:make_deprecated_paths([ "/gateways/:name/clients", "/gateways/:name/clients/:clientid", "/gateways/:name/clients/:clientid/subscriptions", "/gateways/:name/clients/:clientid/subscriptions/:topic" - ]. + ]). -define(CLIENT_QSCHEMA, [ {<<"node">>, atom}, @@ -537,7 +537,9 @@ schema("/gateways/:name/clients/:clientid/subscriptions/:topic") -> responses => ?STANDARD_RESP(#{204 => <<"Unsubscribed">>}) } - }. + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). params_client_query() -> params_gateway_name_in_path() ++ diff --git a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl index bdf0c4c02..92903ec35 100644 --- a/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl +++ b/apps/emqx_gateway/src/emqx_gateway_api_listeners.erl @@ -68,13 +68,13 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). paths() -> - [ + emqx_gateway_utils:make_deprecated_paths([ "/gateways/:name/listeners", "/gateways/:name/listeners/:id", "/gateways/:name/listeners/:id/authentication", "/gateways/:name/listeners/:id/authentication/users", "/gateways/:name/listeners/:id/authentication/users/:uid" - ]. + ]). %%-------------------------------------------------------------------- %% http handlers @@ -567,8 +567,9 @@ schema("/gateways/:name/listeners/:id/authentication/users/:uid") -> responses => ?STANDARD_RESP(#{204 => <<"Deleted">>}) } - }. - + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). %%-------------------------------------------------------------------- %% params defines diff --git a/apps/emqx_gateway/src/emqx_gateway_utils.erl b/apps/emqx_gateway/src/emqx_gateway_utils.erl index 3a6de2031..8df7d84c0 100644 --- a/apps/emqx_gateway/src/emqx_gateway_utils.erl +++ b/apps/emqx_gateway/src/emqx_gateway_utils.erl @@ -44,7 +44,9 @@ parse_listener_id/1, is_running/2, global_chain/1, - listener_chain/3 + listener_chain/3, + make_deprecated_paths/1, + make_compatible_schema/2 ]). -export([stringfy/1]). @@ -538,3 +540,36 @@ default_subopts() -> qos => 0, is_new => true }. + +%% Since 5.0.8, the API path of the gateway has been changed from "gateway" to "gateways" +%% and we need to be compatible with the old path +get_compatible_path("/gateway") -> + "/gateways"; +get_compatible_path("/gateway/" ++ Rest) -> + "/gateways/" ++ Rest. + +get_deprecated_path("/gateways") -> + "/gateway"; +get_deprecated_path("/gateways/" ++ Rest) -> + "/gateway/" ++ Rest. + +make_deprecated_paths(Paths) -> + Paths ++ [get_deprecated_path(Path) || Path <- Paths]. + +make_compatible_schema(Path, SchemaFun) -> + OldPath = get_compatible_path(Path), + make_compatible_schema2(OldPath, SchemaFun). + +make_compatible_schema2(Path, SchemaFun) -> + Schema = SchemaFun(Path), + maps:map( + fun(Key, Value) -> + case lists:member(Key, [get, delete, put, post]) of + true -> + Value#{deprecated => true}; + _ -> + Value + end + end, + Schema + ). diff --git a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl index 3e0a9f9b7..1aa0bac93 100644 --- a/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl +++ b/apps/emqx_gateway/src/lwm2m/emqx_lwm2m_api.erl @@ -37,7 +37,9 @@ api_spec() -> emqx_dashboard_swagger:spec(?MODULE). paths() -> - [?PATH("/lookup"), ?PATH("/observe"), ?PATH("/read"), ?PATH("/write")]. + emqx_gateway_utils:make_deprecated_paths([ + ?PATH("/lookup"), ?PATH("/observe"), ?PATH("/read"), ?PATH("/write") + ]). schema(?PATH("/lookup")) -> #{ @@ -118,7 +120,9 @@ schema(?PATH("/write")) -> 404 => error_codes(['CLIENT_NOT_FOUND'], <<"Clientid not found">>) } } - }. + }; +schema(Path) -> + emqx_gateway_utils:make_compatible_schema(Path, fun schema/1). fields(resource) -> [ diff --git a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl index 577da1a26..0f0f3787f 100644 --- a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl @@ -71,28 +71,33 @@ end_per_suite(Config) -> t_send_request_api(_) -> ClientId = start_client(), timer:sleep(200), - Path = emqx_mgmt_api_test_util:api_path(["gateways/coap/clients/client1/request"]), - Token = <<"atoken">>, - Payload = <<"simple echo this">>, - Req = #{ - token => Token, - payload => Payload, - timeout => <<"10s">>, - content_type => <<"text/plain">>, - method => <<"get">> - }, - Auth = emqx_mgmt_api_test_util:auth_header_(), - {ok, Response} = emqx_mgmt_api_test_util:request_api( - post, - Path, - "method=get", - Auth, - Req - ), - #{<<"token">> := RToken, <<"payload">> := RPayload} = - emqx_json:decode(Response, [return_maps]), - ?assertEqual(Token, RToken), - ?assertEqual(Payload, RPayload), + Test = fun(API) -> + Path = emqx_mgmt_api_test_util:api_path([API]), + Token = <<"atoken">>, + Payload = <<"simple echo this">>, + Req = #{ + token => Token, + payload => Payload, + timeout => <<"10s">>, + content_type => <<"text/plain">>, + method => <<"get">> + }, + Auth = emqx_mgmt_api_test_util:auth_header_(), + {ok, Response} = emqx_mgmt_api_test_util:request_api( + post, + Path, + "method=get", + Auth, + Req + ), + #{<<"token">> := RToken, <<"payload">> := RPayload} = + emqx_json:decode(Response, [return_maps]), + ?assertEqual(Token, RToken), + ?assertEqual(Payload, RPayload) + end, + Test("gateways/coap/clients/client1/request"), + timer:sleep(100), + Test("gateway/coap/clients/client1/request"), erlang:exit(ClientId, kill), ok. diff --git a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl index d873ad1a4..8532a3a74 100644 --- a/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_gateway_api_SUITE.erl @@ -75,6 +75,22 @@ t_gateway(_) -> assert_gw_unloaded(StompGw2), ok. +t_deprecated_gateway(_) -> + {200, Gateways} = request(get, "/gateway"), + lists:foreach(fun assert_gw_unloaded/1, Gateways), + {400, BadReq} = request(get, "/gateway/uname_gateway"), + assert_bad_request(BadReq), + {201, _} = request(post, "/gateway", #{name => <<"stomp">>}), + {200, StompGw1} = request(get, "/gateway/stomp"), + assert_feilds_apperence( + [name, status, enable, created_at, started_at], + StompGw1 + ), + {204, _} = request(delete, "/gateway/stomp"), + {200, StompGw2} = request(get, "/gateway/stomp"), + assert_gw_unloaded(StompGw2), + ok. + t_gateway_stomp(_) -> {200, Gw} = request(get, "/gateways/stomp"), assert_gw_unloaded(Gw), diff --git a/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl b/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl index 8c011c54a..6128b9b62 100644 --- a/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_lwm2m_api_SUITE.erl @@ -326,7 +326,7 @@ t_observe(Config) -> test_recv_mqtt_response(RespTopic), %% step2, call observe API - call_send_api(Epn, "observe", "path=/3/0/1&enable=false"), + call_deprecated_send_api(Epn, "observe", "path=/3/0/1&enable=false"), timer:sleep(100), #coap_message{type = Type, method = Method, options = Opts} = test_recv_coap_request(UdpSock), ?assertEqual(con, Type), @@ -346,7 +346,13 @@ call_lookup_api(ClientId, Path, Action) -> Response. call_send_api(ClientId, Cmd, Query) -> - ApiPath = emqx_mgmt_api_test_util:api_path(["gateways/lwm2m/clients", ClientId, Cmd]), + call_send_api(ClientId, Cmd, Query, "gateways/lwm2m/clients"). + +call_deprecated_send_api(ClientId, Cmd, Query) -> + call_send_api(ClientId, Cmd, Query, "gateway/lwm2m/clients"). + +call_send_api(ClientId, Cmd, Query, API) -> + ApiPath = emqx_mgmt_api_test_util:api_path([API, ClientId, Cmd]), Auth = emqx_mgmt_api_test_util:auth_header_(), {ok, Response} = emqx_mgmt_api_test_util:request_api(post, ApiPath, Query, Auth), ?LOGT("rest api response:~ts~n", [Response]), From ce615a0a7bec0c9df330c78d9ea54f29d1d0694e Mon Sep 17 00:00:00 2001 From: firest Date: Wed, 31 Aug 2022 10:56:07 +0800 Subject: [PATCH 5/5] chore: update CHANGES-5.0.md --- CHANGES-5.0.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGES-5.0.md b/CHANGES-5.0.md index 1d1c15e1f..742883e27 100644 --- a/CHANGES-5.0.md +++ b/CHANGES-5.0.md @@ -1,3 +1,9 @@ +# 5.0.8 + +## Enhancements + +* change the `/gateway` API path to plural form. [#8823](https://github.com/emqx/emqx/pull/8823) + # 5.0.7 ## Bug fixes