emqx_postgresql_connector_schema.erl 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2023 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_postgresql_connector_schema).
  17. -include_lib("hocon/include/hoconsc.hrl").
  18. -include_lib("emqx_postgresql/include/emqx_postgresql.hrl").
  19. -define(PGSQL_HOST_OPTIONS, #{
  20. default_port => ?PGSQL_DEFAULT_PORT
  21. }).
  22. -export([
  23. namespace/0,
  24. roots/0,
  25. fields/1,
  26. desc/1
  27. ]).
  28. %% Examples
  29. -export([
  30. connector_examples/1,
  31. values/1
  32. ]).
  33. -define(CONNECTOR_TYPE, pgsql).
  34. namespace() ->
  35. "connector_postgres".
  36. roots() ->
  37. [].
  38. fields("connection_fields") ->
  39. [{server, server()}] ++
  40. adjust_fields(emqx_connector_schema_lib:relational_db_fields()) ++
  41. emqx_connector_schema_lib:ssl_fields();
  42. fields("config_connector") ->
  43. fields("connection_fields") ++
  44. emqx_connector_schema:common_fields() ++
  45. emqx_connector_schema:resource_opts_ref(?MODULE, resource_opts);
  46. fields(resource_opts) ->
  47. emqx_connector_schema:resource_opts_fields();
  48. fields(config) ->
  49. fields("config_connector") ++
  50. fields(action);
  51. fields(action) ->
  52. {pgsql,
  53. hoconsc:mk(
  54. hoconsc:map(name, hoconsc:ref(emqx_bridge_pgsql, pgsql_action)),
  55. #{
  56. desc => <<"PostgreSQL Action Config">>,
  57. required => false
  58. }
  59. )};
  60. fields(pgsql_action) ->
  61. emqx_bridge_v2_schema:make_producer_action_schema(hoconsc:ref(?MODULE, action_parameters));
  62. fields("put_bridge_v2") ->
  63. fields(pgsql_action);
  64. fields("get_bridge_v2") ->
  65. fields(pgsql_action);
  66. fields("post_bridge_v2") ->
  67. fields(pgsql_action);
  68. fields(Field) when
  69. Field == "get_connector";
  70. Field == "put_connector";
  71. Field == "post_connector"
  72. ->
  73. fields({Field, ?CONNECTOR_TYPE});
  74. fields({Field, Type}) when
  75. Field == "get_connector";
  76. Field == "put_connector";
  77. Field == "post_connector"
  78. ->
  79. Fields =
  80. fields("connection_fields") ++
  81. emqx_connector_schema:resource_opts_ref(?MODULE, resource_opts),
  82. emqx_connector_schema:api_fields(Field, Type, Fields).
  83. server() ->
  84. Meta = #{desc => ?DESC("server")},
  85. emqx_schema:servers_sc(Meta, ?PGSQL_HOST_OPTIONS).
  86. adjust_fields(Fields) ->
  87. lists:map(
  88. fun
  89. ({username, Sc}) ->
  90. %% to please dialyzer...
  91. Override = #{type => hocon_schema:field_schema(Sc, type), required => true},
  92. {username, hocon_schema:override(Sc, Override)};
  93. (Field) ->
  94. Field
  95. end,
  96. Fields
  97. ).
  98. %% Examples
  99. connector_examples(Method) ->
  100. [
  101. #{
  102. <<"pgsql">> => #{
  103. summary => <<"PostgreSQL Connector">>,
  104. value => values({Method, <<"pgsql">>})
  105. }
  106. }
  107. ].
  108. %% TODO: All of these needs to be adjusted from Kafka to PostgreSQL
  109. values({get, PostgreSQLType}) ->
  110. maps:merge(
  111. #{
  112. status => <<"connected">>,
  113. node_status => [
  114. #{
  115. node => <<"emqx@localhost">>,
  116. status => <<"connected">>
  117. }
  118. ],
  119. actions => [<<"my_action">>]
  120. },
  121. values({post, PostgreSQLType})
  122. );
  123. values({post, PostgreSQLType}) ->
  124. maps:merge(
  125. #{
  126. name => <<"my_", PostgreSQLType/binary, "_connector">>,
  127. type => PostgreSQLType
  128. },
  129. values(common)
  130. );
  131. values({put, _PostgreSQLType}) ->
  132. values(common);
  133. values(common) ->
  134. #{
  135. <<"database">> => <<"emqx_data">>,
  136. <<"enable">> => true,
  137. <<"password">> => <<"public">>,
  138. <<"pool_size">> => 8,
  139. <<"server">> => <<"127.0.0.1:5432">>,
  140. <<"ssl">> => #{
  141. <<"ciphers">> => [],
  142. <<"depth">> => 10,
  143. <<"enable">> => false,
  144. <<"hibernate_after">> => <<"5s">>,
  145. <<"log_level">> => <<"notice">>,
  146. <<"reuse_sessions">> => true,
  147. <<"secure_renegotiate">> => true,
  148. <<"verify">> => <<"verify_peer">>,
  149. <<"versions">> => [<<"tlsv1.3">>, <<"tlsv1.2">>]
  150. },
  151. <<"username">> => <<"postgres">>
  152. }.
  153. desc("config_connector") ->
  154. ?DESC("config_connector");
  155. desc(resource_opts) ->
  156. ?DESC(emqx_resource_schema, "resource_opts");
  157. desc(_) ->
  158. undefined.