refactor(http connector): to use emqx_connector_info

This commit refactors the `emqx_bridge_http` to use the
`emqx_connector_info` behavior. The `emqx_bridge_http` related
information can thus be removed from `emqx_connector_chema` and
`emqx_connector_resource`.
This commit is contained in:
Kjell Winblad 2024-03-19 11:28:42 +01:00
parent dc08fab0e8
commit b8ef357fef
6 changed files with 70 additions and 25 deletions

View File

@ -3,7 +3,10 @@
{vsn, "0.2.4"},
{registered, []},
{applications, [kernel, stdlib, emqx_resource, ehttpc]},
{env, [{emqx_action_info_modules, [emqx_bridge_http_action_info]}]},
{env, [
{emqx_action_info_modules, [emqx_bridge_http_action_info]},
{emqx_connector_info_modules, [emqx_bridge_http_connector_info]}
]},
{modules, []},
{links, []}
]}.

View File

@ -0,0 +1,56 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2023-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%--------------------------------------------------------------------
-module(emqx_bridge_http_connector_info).
-behaviour(emqx_connector_info).
-export([
type_name/0,
bridge_types/0,
resource_callback_module/0,
config_schema/0,
schema_module/0,
api_schema/1
]).
type_name() ->
http.
bridge_types() ->
[webhook, http].
resource_callback_module() ->
emqx_bridge_http_connector.
config_schema() ->
{http,
hoconsc:mk(
hoconsc:map(name, hoconsc:ref(emqx_bridge_http_schema, "config_connector")),
#{
alias => [webhook],
desc => <<"HTTP Connector Config">>,
required => false
}
)}.
schema_module() ->
emqx_bridge_http_schema.
api_schema(Method) ->
emqx_connector_schema:api_ref(
emqx_bridge_http_schema, <<"http">>, Method ++ "_connector"
).

View File

@ -69,7 +69,7 @@ hard_coded_connector_info_modules_ee() ->
-endif.
hard_coded_connector_info_modules_common() ->
[].
[emqx_bridge_http_connector_info].
hard_coded_connector_info_modules() ->
hard_coded_connector_info_modules_common() ++ hard_coded_connector_info_modules_ee().

View File

@ -88,14 +88,17 @@ connector_impl_module(ConnectorType) ->
-endif.
connector_to_resource_type_ce(http) ->
emqx_bridge_http_connector;
connector_to_resource_type_ce(mqtt) ->
emqx_bridge_mqtt_connector;
% connector_to_resource_type_ce(mqtt_subscriber) ->
% emqx_bridge_mqtt_subscriber_connector;
connector_to_resource_type_ce(ConnectorType) ->
error({no_bridge_v2, ConnectorType}).
try
emqx_connector_info:resource_callback_module(ConnectorType)
catch
_:_ ->
error({unknown_connector_type, ConnectorType})
end.
resource_id(ConnectorId) when is_binary(ConnectorId) ->
<<"connector:", ConnectorId/binary>>.

View File

@ -72,12 +72,7 @@ resource_type(rabbitmq) ->
resource_type(s3) ->
emqx_bridge_s3_connector;
resource_type(Type) ->
try
emqx_connector_info:resource_callback_module(Type)
catch
_:_ ->
error({unknown_connector_type, Type})
end.
error({unknown_connector_type, Type}).
%% For connectors that need to override connector configurations.
connector_impl_module(ConnectorType) when is_binary(ConnectorType) ->

View File

@ -91,7 +91,6 @@ api_schemas(Method) ->
[
%% We need to map the `type' field of a request (binary) to a
%% connector schema module.
api_ref(emqx_bridge_http_schema, <<"http">>, Method ++ "_connector"),
api_ref(emqx_bridge_mqtt_connector_schema, <<"mqtt">>, Method ++ "_connector")
].
@ -112,11 +111,11 @@ examples(Method) ->
-if(?EMQX_RELEASE_EDITION == ee).
schema_modules() ->
[emqx_bridge_http_schema, emqx_bridge_mqtt_connector_schema] ++
[emqx_bridge_mqtt_connector_schema] ++
emqx_connector_ee_schema:schema_modules() ++ connector_info_schema_modules().
-else.
schema_modules() ->
[emqx_bridge_http_schema, emqx_bridge_mqtt_connector_schema] ++ connector_info_schema_modules().
[emqx_bridge_mqtt_connector_schema] ++ connector_info_schema_modules().
-endif.
connector_info_schema_modules() ->
@ -128,8 +127,6 @@ connector_info_schema_modules() ->
%% @doc Return old bridge(v1) and/or connector(v2) type
%% from the latest connector type name.
connector_type_to_bridge_types(http) ->
[webhook, http];
connector_type_to_bridge_types(kafka_consumer) ->
[kafka_consumer];
connector_type_to_bridge_types(kafka_producer) ->
@ -533,15 +530,6 @@ roots() ->
fields(connectors) ->
[
{http,
mk(
hoconsc:map(name, ref(emqx_bridge_http_schema, "config_connector")),
#{
alias => [webhook],
desc => <<"HTTP Connector Config">>,
required => false
}
)},
{mqtt,
mk(
hoconsc:map(name, ref(emqx_bridge_mqtt_connector_schema, "config_connector")),