fix: use check_schema to simplify API implementation

This commit is contained in:
Stefan Strigler 2023-10-18 16:18:24 +02:00 committed by Zaiming (Stone) Shi
parent 2a1e135304
commit 676572c65a
3 changed files with 18 additions and 22 deletions

View File

@ -70,7 +70,7 @@
namespace() -> "bridge_v2". namespace() -> "bridge_v2".
api_spec() -> api_spec() ->
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => false}). emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
paths() -> paths() ->
[ [
@ -348,11 +348,7 @@ schema("/bridges_v2_probe") ->
?OK(zip_bridges(AllBridges)); ?OK(zip_bridges(AllBridges));
{error, Reason} -> {error, Reason} ->
?INTERNAL_ERROR(Reason) ?INTERNAL_ERROR(Reason)
end; end.
'/bridges_v2'(post, _Params) ->
?BAD_REQUEST("Bad Request");
'/bridges_v2'(_, _) ->
?METHOD_NOT_ALLOWED.
'/bridges_v2/:id'(get, #{bindings := #{id := Id}}) -> '/bridges_v2/:id'(get, #{bindings := #{id := Id}}) ->
?TRY_PARSE_ID(Id, lookup_from_all_nodes(BridgeType, BridgeName, 200)); ?TRY_PARSE_ID(Id, lookup_from_all_nodes(BridgeType, BridgeName, 200));
@ -390,9 +386,7 @@ schema("/bridges_v2_probe") ->
{error, not_found} -> {error, not_found} ->
?BRIDGE_NOT_FOUND(BridgeType, BridgeName) ?BRIDGE_NOT_FOUND(BridgeType, BridgeName)
end end
); ).
'/bridges_v2/:id'(_, _) ->
?METHOD_NOT_ALLOWED.
'/bridges_v2/:id/enable/:enable'(put, #{bindings := #{id := Id, enable := Enable}}) -> '/bridges_v2/:id/enable/:enable'(put, #{bindings := #{id := Id, enable := Enable}}) ->
?TRY_PARSE_ID( ?TRY_PARSE_ID(
@ -414,9 +408,7 @@ schema("/bridges_v2_probe") ->
?INTERNAL_ERROR(Reason) ?INTERNAL_ERROR(Reason)
end end
end end
); ).
'/bridges_v2/:id/enable/:enable'(_, _) ->
?METHOD_NOT_ALLOWED.
'/bridges_v2/:id/:operation'(post, #{ '/bridges_v2/:id/:operation'(post, #{
bindings := bindings :=

View File

@ -92,16 +92,14 @@ bridge_api_union(Refs) ->
undefined -> undefined ->
throw(#{ throw(#{
field_name => type, field_name => type,
value => T,
reason => <<"unknown bridge type">> reason => <<"unknown bridge type">>
}); });
Ref -> Ref ->
[Ref] [Ref]
end; end;
_ -> _ ->
throw(#{ maps:values(Index)
field_name => type,
reason => <<"unknown bridge type">>
})
end end
end. end.

View File

@ -367,9 +367,9 @@ t_bridges_lifecycle(Config) ->
), ),
?assertMatch( ?assertMatch(
{ok, 404, #{ {ok, 400, #{
<<"code">> := <<"NOT_FOUND">>, <<"code">> := <<"BAD_REQUEST">>,
<<"message">> := <<"Invalid operation", _/binary>> <<"message">> := _
}}, }},
request_json(post, uri([?ROOT, BridgeID, "brababbel"]), Config) request_json(post, uri([?ROOT, BridgeID, "brababbel"]), Config)
), ),
@ -388,7 +388,10 @@ t_bridges_lifecycle(Config) ->
request_json( request_json(
put, put,
uri([?ROOT, BridgeID]), uri([?ROOT, BridgeID]),
?KAFKA_BRIDGE(?BRIDGE_NAME, <<"foobla">>), maps:without(
[<<"type">>, <<"name">>],
?KAFKA_BRIDGE(?BRIDGE_NAME, <<"foobla">>)
),
Config Config
) )
), ),
@ -406,7 +409,10 @@ t_bridges_lifecycle(Config) ->
request_json( request_json(
put, put,
uri([?ROOT, BridgeID]), uri([?ROOT, BridgeID]),
?KAFKA_BRIDGE(?BRIDGE_NAME), maps:without(
[<<"type">>, <<"name">>],
?KAFKA_BRIDGE(?BRIDGE_NAME)
),
Config Config
) )
), ),
@ -489,7 +495,7 @@ do_start_bridge(TestType, Config) ->
request_json(get, uri([?ROOT, BridgeID]), Config) request_json(get, uri([?ROOT, BridgeID]), Config)
), ),
{ok, 404, _} = request(post, {operation, TestType, invalidop, BridgeID}, Config), {ok, 400, _} = request(post, {operation, TestType, invalidop, BridgeID}, Config),
%% delete the bridge %% delete the bridge
{ok, 204, <<>>} = request(delete, uri([?ROOT, BridgeID]), Config), {ok, 204, <<>>} = request(delete, uri([?ROOT, BridgeID]), Config),