Просмотр исходного кода

Revert "chore: mv emqx_connector to emqx_data_bridge"

This reverts commit d640e2ccfa68a1c0529ef302b3eadc627d07f5e5.
turtleDeng 4 лет назад
Родитель
Сommit
54dedc8343

+ 19 - 0
apps/emqx_connector/.gitignore

@@ -0,0 +1,19 @@
+.rebar3
+_*
+.eunit
+*.o
+*.beam
+*.plt
+*.swp
+*.swo
+.erlang.cookie
+ebin
+log
+erl_crash.dump
+.rebar
+logs
+_build
+.idea
+*.iml
+rebar3.crashdump
+*~

+ 27 - 0
apps/emqx_connector/README.md

@@ -0,0 +1,27 @@
+# emqx_connector
+
+This application is a collection of `connectors`.
+
+A `connector` is a callback module of `emqx_resource` that maintains the data related to
+external resources. Put all resource related callback modules in a single application is good as
+we can put some util functions/modules here for reusing purpose.
+
+For example, a mysql connector is an emqx resource that maintains all the mysql connection
+related parameters (configs) and the TCP connections to the mysql server.
+
+An mysql connector can be used as following:
+
+```
+(emqx@127.0.0.1)5> emqx_resource:list_instances_verbose().
+[#{config =>
+       #{auto_reconnect => true,cacertfile => [],certfile => [],
+         database => "mqtt",keyfile => [],password => "public",
+         pool_size => 1,
+         server => {{127,0,0,1},3306},
+         ssl => false,user => "root",verify => false},
+   id => <<"mysql-abc">>,mod => emqx_connector_mysql,
+   state => #{poolname => 'mysql-abc'},
+   status => started}]
+(emqx@127.0.0.1)6> emqx_resource:query(<<"mysql-abc">>, {sql, <<"SELECT count(1)">>}).
+{ok,[<<"count(1)">>],[[1]]}
+```

+ 4 - 0
apps/emqx_connector/etc/emqx_connector.conf

@@ -0,0 +1,4 @@
+##--------------------------------------------------------------------
+## EMQ X CONNECTOR Plugin
+##--------------------------------------------------------------------
+

apps/emqx_data_bridge/include/emqx_data_bridge.hrl → apps/emqx_connector/include/emqx_connector.hrl


+ 2 - 0
apps/emqx_connector/priv/emqx_connector.schema

@@ -0,0 +1,2 @@
+%%-*- mode: erlang -*-
+%% emqx_connector config mapping

+ 13 - 0
apps/emqx_connector/rebar.config

@@ -0,0 +1,13 @@
+{erl_opts, [
+  nowarn_unused_import,
+  debug_info
+]}.
+
+{deps, [
+  {mysql, {git, "https://github.com/emqx/mysql-otp", {tag, "1.7.1"}}}
+]}.
+
+{shell, [
+  % {config, "config/sys.config"},
+    {apps, [emqx_connector]}
+]}.

+ 17 - 0
apps/emqx_connector/src/emqx_connector.app.src

@@ -0,0 +1,17 @@
+{application, emqx_connector,
+ [{description, "An OTP application"},
+  {vsn, "0.1.0"},
+  {registered, []},
+  {mod, {emqx_connector_app, []}},
+  {applications,
+   [kernel,
+    stdlib,
+    emqx_resource,
+    ecpool
+   ]},
+  {env,[]},
+  {modules, []},
+
+  {licenses, ["Apache 2.0"]},
+  {links, []}
+ ]}.

+ 16 - 0
apps/emqx_connector/src/emqx_connector.erl

@@ -0,0 +1,16 @@
+%%--------------------------------------------------------------------
+%% Copyright (c) 2020-2021 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_connector).

+ 31 - 0
apps/emqx_connector/src/emqx_connector_app.erl

@@ -0,0 +1,31 @@
+%%--------------------------------------------------------------------
+%% Copyright (c) 2020-2021 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_connector_app).
+
+-behaviour(application).
+
+-emqx_plugin(?MODULE).
+
+-export([start/2, stop/1]).
+
+start(_StartType, _StartArgs) ->
+    emqx_connector_sup:start_link().
+
+stop(_State) ->
+    ok.
+
+%% internal functions

+ 1 - 1
apps/emqx_data_bridge/src/connector/emqx_connector_ldap.erl

@@ -15,7 +15,7 @@
 %%--------------------------------------------------------------------
 -module(emqx_connector_ldap).
 
--include("emqx_data_bridge.hrl").
+-include("emqx_connector.hrl").
 -include_lib("typerefl/include/types.hrl").
 -include_lib("emqx_resource/include/emqx_resource_behaviour.hrl").
 

+ 1 - 1
apps/emqx_data_bridge/src/connector/emqx_connector_mongo.erl

@@ -15,7 +15,7 @@
 %%--------------------------------------------------------------------
 -module(emqx_connector_mongo).
 
--include("emqx_data_bridge.hrl").
+-include("emqx_connector.hrl").
 -include_lib("typerefl/include/types.hrl").
 -include_lib("emqx_resource/include/emqx_resource_behaviour.hrl").
 

apps/emqx_data_bridge/src/connector/emqx_connector_mysql.erl → apps/emqx_connector/src/emqx_connector_mysql.erl


apps/emqx_data_bridge/src/connector/emqx_connector_pgsql.erl → apps/emqx_connector/src/emqx_connector_pgsql.erl


+ 1 - 1
apps/emqx_data_bridge/src/connector/emqx_connector_redis.erl

@@ -15,7 +15,7 @@
 %%--------------------------------------------------------------------
 -module(emqx_connector_redis).
 
--include("emqx_data_bridge.hrl").
+-include("emqx_connector.hrl").
 -include_lib("typerefl/include/types.hrl").
 -include_lib("emqx_resource/include/emqx_resource_behaviour.hrl").
 

+ 1 - 1
apps/emqx_data_bridge/src/connector/emqx_connector_schema_lib.erl

@@ -15,7 +15,7 @@
 %%--------------------------------------------------------------------
 -module(emqx_connector_schema_lib).
 
--include("emqx_data_bridge.hrl").
+-include("emqx_connector.hrl").
 -include_lib("typerefl/include/types.hrl").
 
 -export([ relational_db_fields/0

+ 36 - 0
apps/emqx_connector/src/emqx_connector_sup.erl

@@ -0,0 +1,36 @@
+%%--------------------------------------------------------------------
+%% Copyright (c) 2020-2021 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_connector_sup).
+
+-behaviour(supervisor).
+
+-export([start_link/0]).
+
+-export([init/1]).
+
+-define(SERVER, ?MODULE).
+
+start_link() ->
+    supervisor:start_link({local, ?SERVER}, ?MODULE, []).
+
+init([]) ->
+    SupFlags = #{strategy => one_for_all,
+                 intensity => 0,
+                 period => 1},
+    ChildSpecs = [],
+    {ok, {SupFlags, ChildSpecs}}.
+
+%% internal functions

+ 16 - 0
apps/emqx_data_bridge/priv/emqx_data_bridge.schema

@@ -0,0 +1,16 @@
+%%-*- mode: erlang -*-
+%% emqx_data_bridge config mapping
+
+{mapping, "emqx_data_bridge.bridges", "emqx_data_bridge.bridges", [
+  {default, []},
+  {datatype, string}
+]}.
+
+% fields("emqx_data_bridge") ->
+%     [
+%         {bridges,
+%           [fun(mapping) -> "emqx_data_bridge.bridges";
+%               (type) -> list();
+%               (_) -> undefined
+%            end]}
+%     ]

+ 0 - 1
apps/emqx_data_bridge/rebar.config

@@ -1,6 +1,5 @@
 {erl_opts, [debug_info]}.
 {deps, []}.
-% {extra_src_dirs, [{"src", [{recursive, true}]}]}.
 
 {shell, [
   % {config, "config/sys.config"},

+ 1 - 2
apps/emqx_data_bridge/src/emqx_data_bridge.app.src

@@ -5,8 +5,7 @@
   {mod, {emqx_data_bridge_app, []}},
   {applications,
    [kernel,
-    stdlib,
-    ecpool
+    stdlib
    ]},
   {env,[]},
   {modules, []},

+ 2 - 3
apps/emqx_data_bridge/src/emqx_data_bridge.erl

@@ -25,9 +25,8 @@
         ]).
 
 load_bridges() ->
-    ConfFile = filename:join([emqx:get_env(plugins_etc_dir), ?MODULE]) ++ ".conf",
-    {ok, #{<<"emqx_data_bridge">> := RawConfig}} = hocon:load(ConfFile),
-    Bridges = maps:get(<<"bridges">>, RawConfig, []),
+    Bridges = proplists:get_value(bridges,
+        application:get_all_env(emqx_data_bridge), []),
     emqx_data_bridge_monitor:ensure_all_started(Bridges).
 
 resource_type(<<"mysql">>) -> emqx_connector_mysql;

+ 2 - 0
apps/emqx_resource/priv/emqx_resource.schema

@@ -0,0 +1,2 @@
+%%-*- mode: erlang -*-
+%% emqx-resource config mapping

+ 3 - 0
data/loaded_plugins.tmpl

@@ -5,4 +5,7 @@
 {emqx_retainer, {{enable_plugin_emqx_retainer}}}.
 {emqx_telemetry, {{enable_plugin_emqx_telemetry}}}.
 {emqx_rule_engine, {{enable_plugin_emqx_rule_engine}}}.
+{emqx_resource, {{enable_plugin_emqx_resource}}}.
+{emqx_connector, {{enable_plugin_emqx_connector}}}.
+{emqx_data_bridge, {{enable_plugin_emqx_data_bridge}}}.
 {emqx_bridge_mqtt, {{enable_plugin_emqx_bridge_mqtt}}}.

+ 6 - 3
rebar.config.erl

@@ -186,6 +186,9 @@ overlay_vars_rel(RelType) ->
              end,
     [ {enable_plugin_emqx_rule_engine, RelType =:= cloud}
     , {enable_plugin_emqx_bridge_mqtt, RelType =:= edge}
+    , {enable_plugin_emqx_resource, true}
+    , {enable_plugin_emqx_connector, true}
+    , {enable_plugin_emqx_data_bridge, true}
     , {enable_plugin_emqx_modules, false} %% modules is not a plugin in ce
     , {enable_plugin_emqx_recon, true}
     , {enable_plugin_emqx_retainer, true}
@@ -244,8 +247,6 @@ relx_apps(ReleaseType) ->
     , {emqx_plugin_libs, load}
     , observer_cli
     , emqx_http_lib
-    , emqx_resource
-    , emqx_data_bridge
     ]
     ++ [emqx_modules || not is_enterprise()]
     ++ [emqx_license || is_enterprise()]
@@ -283,8 +284,10 @@ relx_plugin_apps(ReleaseType) ->
     , emqx_auth_mnesia
     , emqx_web_hook
     , emqx_recon
-    , emqx_rule_engine
+    , emqx_resource
+    , emqx_connector
     , emqx_data_bridge
+    , emqx_rule_engine
     , emqx_sasl
     ]
     ++ [emqx_telemetry || not is_enterprise()]