fix(gateway): compatible with the old "/gateway" form API

This commit is contained in:
firest 2022-08-31 10:53:13 +08:00
parent bc12899ef8
commit 1233d3c8df
11 changed files with 122 additions and 47 deletions

View File

@ -42,7 +42,7 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}).
paths() -> paths() ->
[?PREFIX ++ "/request"]. emqx_gateway_utils:make_deprecated_paths([?PREFIX ++ "/request"]).
schema(?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}) -> request(post, #{body := Body, bindings := Bindings}) ->
ClientId = maps:get(clientid, Bindings, undefined), ClientId = maps:get(clientid, Bindings, undefined),

View File

@ -61,10 +61,10 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
paths() -> paths() ->
[ emqx_gateway_utils:make_deprecated_paths([
"/gateways", "/gateways",
"/gateways/:name" "/gateways/:name"
]. ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% http handlers %% http handlers
@ -210,7 +210,9 @@ schema("/gateways/:name") ->
responses => responses =>
?STANDARD_RESP(#{200 => schema_gateways_conf()}) ?STANDARD_RESP(#{200 => schema_gateways_conf()})
} }
}. };
schema(Path) ->
emqx_gateway_utils:make_compatible_schema(Path, fun schema/1).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% params defines %% params defines

View File

@ -60,11 +60,11 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
paths() -> paths() ->
[ emqx_gateway_utils:make_deprecated_paths([
"/gateways/:name/authentication", "/gateways/:name/authentication",
"/gateways/:name/authentication/users", "/gateways/:name/authentication/users",
"/gateways/:name/authentication/users/:uid" "/gateways/:name/authentication/users/:uid"
]. ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% http handlers %% http handlers
@ -298,8 +298,9 @@ schema("/gateways/:name/authentication/users/:uid") ->
responses => responses =>
?STANDARD_RESP(#{204 => <<"User Deleted">>}) ?STANDARD_RESP(#{204 => <<"User Deleted">>})
} }
}. };
schema(Path) ->
emqx_gateway_utils:make_compatible_schema(Path, fun schema/1).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% params defines %% params defines

View File

@ -53,10 +53,10 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => false}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => false}).
paths() -> paths() ->
[ emqx_gateway_utils:make_deprecated_paths([
"/gateways/:name/authentication/import_users", "/gateways/:name/authentication/import_users",
"/gateways/:name/listeners/:id/authentication/import_users" "/gateways/:name/listeners/:id/authentication/import_users"
]. ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% http handlers %% http handlers
@ -141,8 +141,9 @@ schema("/gateways/:name/listeners/:id/authentication/import_users") ->
responses => responses =>
?STANDARD_RESP(#{204 => <<"Imported">>}) ?STANDARD_RESP(#{204 => <<"Imported">>})
} }
}. };
schema(Path) ->
emqx_gateway_utils:make_compatible_schema(Path, fun schema/1).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% params defines %% params defines
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------

View File

@ -67,12 +67,12 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}).
paths() -> paths() ->
[ emqx_gateway_utils:make_deprecated_paths([
"/gateways/:name/clients", "/gateways/:name/clients",
"/gateways/:name/clients/:clientid", "/gateways/:name/clients/:clientid",
"/gateways/:name/clients/:clientid/subscriptions", "/gateways/:name/clients/:clientid/subscriptions",
"/gateways/:name/clients/:clientid/subscriptions/:topic" "/gateways/:name/clients/:clientid/subscriptions/:topic"
]. ]).
-define(CLIENT_QSCHEMA, [ -define(CLIENT_QSCHEMA, [
{<<"node">>, atom}, {<<"node">>, atom},
@ -537,7 +537,9 @@ schema("/gateways/:name/clients/:clientid/subscriptions/:topic") ->
responses => responses =>
?STANDARD_RESP(#{204 => <<"Unsubscribed">>}) ?STANDARD_RESP(#{204 => <<"Unsubscribed">>})
} }
}. };
schema(Path) ->
emqx_gateway_utils:make_compatible_schema(Path, fun schema/1).
params_client_query() -> params_client_query() ->
params_gateway_name_in_path() ++ params_gateway_name_in_path() ++

View File

@ -68,13 +68,13 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
paths() -> paths() ->
[ emqx_gateway_utils:make_deprecated_paths([
"/gateways/:name/listeners", "/gateways/:name/listeners",
"/gateways/:name/listeners/:id", "/gateways/:name/listeners/:id",
"/gateways/:name/listeners/:id/authentication", "/gateways/:name/listeners/:id/authentication",
"/gateways/:name/listeners/:id/authentication/users", "/gateways/:name/listeners/:id/authentication/users",
"/gateways/:name/listeners/:id/authentication/users/:uid" "/gateways/:name/listeners/:id/authentication/users/:uid"
]. ]).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% http handlers %% http handlers
@ -567,8 +567,9 @@ schema("/gateways/:name/listeners/:id/authentication/users/:uid") ->
responses => responses =>
?STANDARD_RESP(#{204 => <<"Deleted">>}) ?STANDARD_RESP(#{204 => <<"Deleted">>})
} }
}. };
schema(Path) ->
emqx_gateway_utils:make_compatible_schema(Path, fun schema/1).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% params defines %% params defines

View File

@ -44,7 +44,9 @@
parse_listener_id/1, parse_listener_id/1,
is_running/2, is_running/2,
global_chain/1, global_chain/1,
listener_chain/3 listener_chain/3,
make_deprecated_paths/1,
make_compatible_schema/2
]). ]).
-export([stringfy/1]). -export([stringfy/1]).
@ -538,3 +540,36 @@ default_subopts() ->
qos => 0, qos => 0,
is_new => true 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
).

View File

@ -37,7 +37,9 @@ api_spec() ->
emqx_dashboard_swagger:spec(?MODULE). emqx_dashboard_swagger:spec(?MODULE).
paths() -> 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")) -> schema(?PATH("/lookup")) ->
#{ #{
@ -118,7 +120,9 @@ schema(?PATH("/write")) ->
404 => error_codes(['CLIENT_NOT_FOUND'], <<"Clientid not found">>) 404 => error_codes(['CLIENT_NOT_FOUND'], <<"Clientid not found">>)
} }
} }
}. };
schema(Path) ->
emqx_gateway_utils:make_compatible_schema(Path, fun schema/1).
fields(resource) -> fields(resource) ->
[ [

View File

@ -71,28 +71,33 @@ end_per_suite(Config) ->
t_send_request_api(_) -> t_send_request_api(_) ->
ClientId = start_client(), ClientId = start_client(),
timer:sleep(200), timer:sleep(200),
Path = emqx_mgmt_api_test_util:api_path(["gateways/coap/clients/client1/request"]), Test = fun(API) ->
Token = <<"atoken">>, Path = emqx_mgmt_api_test_util:api_path([API]),
Payload = <<"simple echo this">>, Token = <<"atoken">>,
Req = #{ Payload = <<"simple echo this">>,
token => Token, Req = #{
payload => Payload, token => Token,
timeout => <<"10s">>, payload => Payload,
content_type => <<"text/plain">>, timeout => <<"10s">>,
method => <<"get">> content_type => <<"text/plain">>,
}, method => <<"get">>
Auth = emqx_mgmt_api_test_util:auth_header_(), },
{ok, Response} = emqx_mgmt_api_test_util:request_api( Auth = emqx_mgmt_api_test_util:auth_header_(),
post, {ok, Response} = emqx_mgmt_api_test_util:request_api(
Path, post,
"method=get", Path,
Auth, "method=get",
Req Auth,
), Req
#{<<"token">> := RToken, <<"payload">> := RPayload} = ),
emqx_json:decode(Response, [return_maps]), #{<<"token">> := RToken, <<"payload">> := RPayload} =
?assertEqual(Token, RToken), emqx_json:decode(Response, [return_maps]),
?assertEqual(Payload, RPayload), ?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), erlang:exit(ClientId, kill),
ok. ok.

View File

@ -75,6 +75,22 @@ t_gateway(_) ->
assert_gw_unloaded(StompGw2), assert_gw_unloaded(StompGw2),
ok. 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(_) -> t_gateway_stomp(_) ->
{200, Gw} = request(get, "/gateways/stomp"), {200, Gw} = request(get, "/gateways/stomp"),
assert_gw_unloaded(Gw), assert_gw_unloaded(Gw),

View File

@ -326,7 +326,7 @@ t_observe(Config) ->
test_recv_mqtt_response(RespTopic), test_recv_mqtt_response(RespTopic),
%% step2, call observe API %% 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), timer:sleep(100),
#coap_message{type = Type, method = Method, options = Opts} = test_recv_coap_request(UdpSock), #coap_message{type = Type, method = Method, options = Opts} = test_recv_coap_request(UdpSock),
?assertEqual(con, Type), ?assertEqual(con, Type),
@ -346,7 +346,13 @@ call_lookup_api(ClientId, Path, Action) ->
Response. Response.
call_send_api(ClientId, Cmd, Query) -> 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_(), Auth = emqx_mgmt_api_test_util:auth_header_(),
{ok, Response} = emqx_mgmt_api_test_util:request_api(post, ApiPath, Query, Auth), {ok, Response} = emqx_mgmt_api_test_util:request_api(post, ApiPath, Query, Auth),
?LOGT("rest api response:~ts~n", [Response]), ?LOGT("rest api response:~ts~n", [Response]),