From b8ef357fef8c1ee912941879800b81a721720b00 Mon Sep 17 00:00:00 2001 From: Kjell Winblad Date: Tue, 19 Mar 2024 11:28:42 +0100 Subject: [PATCH] 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`. --- .../src/emqx_bridge_http.app.src | 5 +- .../src/emqx_bridge_http_connector_info.erl | 56 +++++++++++++++++++ .../src/emqx_connector_info.erl | 2 +- .../src/emqx_connector_resource.erl | 9 ++- .../src/schema/emqx_connector_ee_schema.erl | 7 +-- .../src/schema/emqx_connector_schema.erl | 16 +----- 6 files changed, 70 insertions(+), 25 deletions(-) create mode 100644 apps/emqx_bridge_http/src/emqx_bridge_http_connector_info.erl diff --git a/apps/emqx_bridge_http/src/emqx_bridge_http.app.src b/apps/emqx_bridge_http/src/emqx_bridge_http.app.src index 681095a08..52899d2a0 100644 --- a/apps/emqx_bridge_http/src/emqx_bridge_http.app.src +++ b/apps/emqx_bridge_http/src/emqx_bridge_http.app.src @@ -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, []} ]}. diff --git a/apps/emqx_bridge_http/src/emqx_bridge_http_connector_info.erl b/apps/emqx_bridge_http/src/emqx_bridge_http_connector_info.erl new file mode 100644 index 000000000..f424e2fe4 --- /dev/null +++ b/apps/emqx_bridge_http/src/emqx_bridge_http_connector_info.erl @@ -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" + ). diff --git a/apps/emqx_connector/src/emqx_connector_info.erl b/apps/emqx_connector/src/emqx_connector_info.erl index 30287a0a1..9ab09e821 100644 --- a/apps/emqx_connector/src/emqx_connector_info.erl +++ b/apps/emqx_connector/src/emqx_connector_info.erl @@ -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(). diff --git a/apps/emqx_connector/src/emqx_connector_resource.erl b/apps/emqx_connector/src/emqx_connector_resource.erl index 1b434496a..dec4ab2d7 100644 --- a/apps/emqx_connector/src/emqx_connector_resource.erl +++ b/apps/emqx_connector/src/emqx_connector_resource.erl @@ -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>>. diff --git a/apps/emqx_connector/src/schema/emqx_connector_ee_schema.erl b/apps/emqx_connector/src/schema/emqx_connector_ee_schema.erl index 0e6634d79..d2ccdff61 100644 --- a/apps/emqx_connector/src/schema/emqx_connector_ee_schema.erl +++ b/apps/emqx_connector/src/schema/emqx_connector_ee_schema.erl @@ -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) -> diff --git a/apps/emqx_connector/src/schema/emqx_connector_schema.erl b/apps/emqx_connector/src/schema/emqx_connector_schema.erl index ee52c7361..edac6976d 100644 --- a/apps/emqx_connector/src/schema/emqx_connector_schema.erl +++ b/apps/emqx_connector/src/schema/emqx_connector_schema.erl @@ -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")),