emqx_postgresql_connector_schema.erl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. emqx_connector_schema:api_fields(Field, Type, fields("connection_fields")).
  80. server() ->
  81. Meta = #{desc => ?DESC("server")},
  82. emqx_schema:servers_sc(Meta, ?PGSQL_HOST_OPTIONS).
  83. adjust_fields(Fields) ->
  84. lists:map(
  85. fun
  86. ({username, Sc}) ->
  87. %% to please dialyzer...
  88. Override = #{type => hocon_schema:field_schema(Sc, type), required => true},
  89. {username, hocon_schema:override(Sc, Override)};
  90. (Field) ->
  91. Field
  92. end,
  93. Fields
  94. ).
  95. %% Examples
  96. connector_examples(Method) ->
  97. [
  98. #{
  99. <<"pgsql">> => #{
  100. summary => <<"PostgreSQL Connector">>,
  101. value => values({Method, <<"pgsql">>})
  102. }
  103. }
  104. ].
  105. %% TODO: All of these needs to be adjusted from Kafka to PostgreSQL
  106. values({get, PostgreSQLType}) ->
  107. maps:merge(
  108. #{
  109. status => <<"connected">>,
  110. node_status => [
  111. #{
  112. node => <<"emqx@localhost">>,
  113. status => <<"connected">>
  114. }
  115. ],
  116. actions => [<<"my_action">>]
  117. },
  118. values({post, PostgreSQLType})
  119. );
  120. values({post, PostgreSQLType}) ->
  121. maps:merge(
  122. #{
  123. name => <<"my_", PostgreSQLType/binary, "_connector">>,
  124. type => PostgreSQLType
  125. },
  126. values(common)
  127. );
  128. values({put, _PostgreSQLType}) ->
  129. values(common);
  130. values(common) ->
  131. #{
  132. <<"database">> => <<"emqx_data">>,
  133. <<"enable">> => true,
  134. <<"password">> => <<"public">>,
  135. <<"pool_size">> => 8,
  136. <<"server">> => <<"127.0.0.1:5432">>,
  137. <<"ssl">> => #{
  138. <<"ciphers">> => [],
  139. <<"depth">> => 10,
  140. <<"enable">> => false,
  141. <<"hibernate_after">> => <<"5s">>,
  142. <<"log_level">> => <<"notice">>,
  143. <<"reuse_sessions">> => true,
  144. <<"secure_renegotiate">> => true,
  145. <<"verify">> => <<"verify_peer">>,
  146. <<"versions">> => [<<"tlsv1.3">>, <<"tlsv1.2">>]
  147. },
  148. <<"username">> => <<"postgres">>
  149. }.
  150. desc("config_connector") ->
  151. ?DESC("config_connector");
  152. desc(resource_opts) ->
  153. ?DESC(emqx_resource_schema, "resource_opts");
  154. desc(_) ->
  155. undefined.