diff --git a/apps/emqx/include/http_api.hrl b/apps/emqx/include/http_api.hrl index 4e7053565..ac09df989 100644 --- a/apps/emqx/include/http_api.hrl +++ b/apps/emqx/include/http_api.hrl @@ -27,6 +27,7 @@ -define(INVALID_PARAMETER, 'INVALID_PARAMETER'). -define(CONFLICT, 'CONFLICT'). -define(NO_DEFAULT_VALUE, 'NO_DEFAULT_VALUE'). +-define(DEPENDENCY_EXISTS, 'DEPENDENCY_EXISTS'). -define(MESSAGE_ID_SCHEMA_ERROR, 'MESSAGE_ID_SCHEMA_ERROR'). %% Resource Not Found @@ -58,6 +59,7 @@ , {'INVALID_PARAMETER', <<"Request parameters is not legal and exceeds the boundary value">>} , {'CONFLICT', <<"Conflicting request resources">>} , {'NO_DEFAULT_VALUE', <<"Request parameters do not use default values">>} + , {'DEPENDENCY_EXISTS', <<"Resource is dependent by another resource">>} , {'MESSAGE_ID_SCHEMA_ERROR', <<"Message ID parsing error">>} , {'MESSAGE_ID_NOT_FOUND', <<"Message ID does not exist">>} , {'NOT_FOUND', <<"Resource was not found or does not exist">>} diff --git a/apps/emqx_connector/src/emqx_connector_api.erl b/apps/emqx_connector/src/emqx_connector_api.erl index c13a1b2cd..031492d86 100644 --- a/apps/emqx_connector/src/emqx_connector_api.erl +++ b/apps/emqx_connector/src/emqx_connector_api.erl @@ -50,10 +50,10 @@ api_spec() -> paths() -> ["/connectors_test", "/connectors", "/connectors/:id"]. -error_schema(Code, Message) -> - [ {code, mk(string(), #{example => Code})} - , {message, mk(string(), #{example => Message})} - ]. +error_schema(Codes, Message) when is_list(Message) -> + error_schema(Codes, list_to_binary(Message)); +error_schema(Codes, Message) when is_binary(Message) -> + emqx_dashboard_swagger:error_codes(Codes, Message). put_request_body_schema() -> emqx_dashboard_swagger:schema_with_examples( @@ -134,8 +134,8 @@ schema("/connectors_test") -> summary => <<"Test creating connector">>, requestBody => post_request_body_schema(), responses => #{ - 200 => <<"Test connector OK">>, - 400 => error_schema('TEST_FAILED', "connector test failed") + 204 => <<"Test connector OK">>, + 400 => error_schema(['TEST_FAILED'], "connector test failed") } } }; @@ -160,7 +160,7 @@ schema("/connectors") -> requestBody => post_request_body_schema(), responses => #{ 201 => get_response_body_schema(), - 400 => error_schema('ALREADY_EXISTS', "connector already exists") + 400 => error_schema(['ALREADY_EXISTS'], "connector already exists") } } }; @@ -175,7 +175,7 @@ schema("/connectors/:id") -> parameters => param_path_id(), responses => #{ 200 => get_response_body_schema(), - 404 => error_schema('NOT_FOUND', "Connector not found") + 404 => error_schema(['NOT_FOUND'], "Connector not found") } }, put => #{ @@ -186,8 +186,7 @@ schema("/connectors/:id") -> requestBody => put_request_body_schema(), responses => #{ 200 => get_response_body_schema(), - 400 => error_schema('UPDATE_FAIL', "Update failed"), - 404 => error_schema('NOT_FOUND', "Connector not found") + 404 => error_schema(['NOT_FOUND'], "Connector not found") }}, delete => #{ tags => [<<"connectors">>], @@ -196,15 +195,17 @@ schema("/connectors/:id") -> parameters => param_path_id(), responses => #{ 204 => <<"Delete connector successfully">>, - 400 => error_schema('DELETE_FAIL', "Delete failed") + 403 => error_schema(['DEPENDENCY_EXISTS'], "Cannot remove dependent connector"), + 404 => error_schema(['NOT_FOUND'], "Delete failed, not found") }} }. '/connectors_test'(post, #{body := #{<<"type">> := ConnType} = Params}) -> case emqx_connector:create_dry_run(ConnType, maps:remove(<<"type">>, Params)) of - ok -> {200}; + ok -> + {204}; {error, Error} -> - {400, error_msg('BAD_ARG', Error)} + {400, error_msg(['TEST_FAILED'], Error)} end. '/connectors'(get, _Request) -> @@ -221,14 +222,16 @@ schema("/connectors/:id") -> {ok, #{raw_config := RawConf}} -> Id = emqx_connector:connector_id(ConnType, ConnName), {201, format_resp(Id, RawConf)}; - {error, Error} -> {400, error_msg('BAD_ARG', Error)} + {error, Error} -> + {400, error_msg('ALREADY_EXISTS', Error)} end end. '/connectors/:id'(get, #{bindings := #{id := Id}}) -> ?TRY_PARSE_ID(Id, case emqx_connector:lookup(ConnType, ConnName) of - {ok, Conf} -> {200, format_resp(Id, Conf)}; + {ok, Conf} -> + {200, format_resp(Id, Conf)}; {error, not_found} -> {404, error_msg('NOT_FOUND', <<"connector not found">>)} end); @@ -241,7 +244,8 @@ schema("/connectors/:id") -> case emqx_connector:update(ConnType, ConnName, Params) of {ok, #{raw_config := RawConf}} -> {200, format_resp(Id, RawConf)}; - {error, Error} -> {400, error_msg('BAD_ARG', Error)} + {error, Error} -> + {500, error_msg('INTERNAL_ERROR', Error)} end; {error, not_found} -> {404, error_msg('NOT_FOUND', <<"connector not found">>)} @@ -252,12 +256,14 @@ schema("/connectors/:id") -> case emqx_connector:lookup(ConnType, ConnName) of {ok, _} -> case emqx_connector:delete(ConnType, ConnName) of - {ok, _} -> {204}; + {ok, _} -> + {204}; {error, {post_config_update, _, {dependency_bridges_exist, BridgeID}}} -> {403, error_msg('DEPENDENCY_EXISTS', <<"Cannot remove the connector as it's in use by a bridge: ", BridgeID/binary>>)}; - {error, Error} -> {400, error_msg('BAD_ARG', Error)} + {error, Error} -> + {500, error_msg('INTERNAL_ERROR', Error)} end; {error, not_found} -> {404, error_msg('NOT_FOUND', <<"connector not found">>)} diff --git a/apps/emqx_connector/test/emqx_connector_api_SUITE.erl b/apps/emqx_connector/test/emqx_connector_api_SUITE.erl index 9e5e44273..5684611be 100644 --- a/apps/emqx_connector/test/emqx_connector_api_SUITE.erl +++ b/apps/emqx_connector/test/emqx_connector_api_SUITE.erl @@ -377,7 +377,7 @@ t_mqtt_conn_update(_) -> %% then we try to update 'server' of the connector, to an unavailable IP address %% the update should fail because of 'unreachable' or 'connrefused' - {ok, 400, _ErrorMsg} = request(put, uri(["connectors", ConnctorID]), + {ok, 500, _ErrorMsg} = request(put, uri(["connectors", ConnctorID]), ?MQTT_CONNECTOR2(<<"127.0.0.1:2603">>)), %% we fix the 'server' parameter to a normal one, it should work {ok, 200, _} = request(put, uri(["connectors", ConnctorID]), @@ -468,7 +468,7 @@ t_mqtt_conn_update3(_) -> t_mqtt_conn_testing(_) -> %% APIs for testing the connectivity %% then we add a mqtt connector, using POST - {ok, 200, <<>>} = request(post, uri(["connectors_test"]), + {ok, 204, <<>>} = request(post, uri(["connectors_test"]), ?MQTT_CONNECTOR2(<<"127.0.0.1:1883">>)#{ <<"type">> => ?CONNECTR_TYPE, <<"name">> => ?BRIDGE_NAME_EGRESS