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

View File

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

View File

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