refactor(connector): parse servers for `rs` and `sharded` mongo_type
This commit is contained in:
parent
ab7c2b72e3
commit
28735dc6d7
|
@ -298,6 +298,8 @@ server(_) -> undefined.
|
||||||
servers(type) -> binary();
|
servers(type) -> binary();
|
||||||
servers(nullable) -> false;
|
servers(nullable) -> false;
|
||||||
servers(validator) -> [?NOT_EMPTY("the value of the field 'servers' cannot be empty")];
|
servers(validator) -> [?NOT_EMPTY("the value of the field 'servers' cannot be empty")];
|
||||||
|
servers(converter) -> fun to_servers_raw/1;
|
||||||
|
servers(desc) -> ?SERVERS_DESC ++ server(desc);
|
||||||
servers(_) -> undefined.
|
servers(_) -> undefined.
|
||||||
|
|
||||||
w_mode(type) -> hoconsc:enum([unsafe, safe]);
|
w_mode(type) -> hoconsc:enum([unsafe, safe]);
|
||||||
|
@ -323,19 +325,9 @@ srv_record(_) -> undefined.
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
|
|
||||||
parse_servers(Type, Servers) when is_binary(Servers) ->
|
|
||||||
parse_servers(Type, binary_to_list(Servers));
|
|
||||||
parse_servers(Type, Servers) when is_list(Servers) ->
|
|
||||||
case string:split(Servers, ",", all) of
|
|
||||||
[Host | _] when Type =:= single ->
|
|
||||||
[Host];
|
|
||||||
Hosts ->
|
|
||||||
Hosts
|
|
||||||
end.
|
|
||||||
|
|
||||||
may_parse_srv_and_txt_records(#{server := Server} = Config) ->
|
may_parse_srv_and_txt_records(#{server := Server} = Config) ->
|
||||||
NConfig = maps:remove(server, Config),
|
NConfig = maps:remove(server, Config),
|
||||||
may_parse_srv_and_txt_records_(NConfig#{servers => Server});
|
may_parse_srv_and_txt_records_(NConfig#{servers => [Server]});
|
||||||
may_parse_srv_and_txt_records(Config) ->
|
may_parse_srv_and_txt_records(Config) ->
|
||||||
may_parse_srv_and_txt_records_(Config).
|
may_parse_srv_and_txt_records_(Config).
|
||||||
|
|
||||||
|
@ -346,31 +338,34 @@ may_parse_srv_and_txt_records_(#{mongo_type := Type,
|
||||||
true ->
|
true ->
|
||||||
error({missing_parameter, replica_set_name});
|
error({missing_parameter, replica_set_name});
|
||||||
false ->
|
false ->
|
||||||
Config#{hosts => parse_servers(Type, Servers)}
|
Config#{hosts => servers_to_bin(Servers)}
|
||||||
end;
|
end;
|
||||||
may_parse_srv_and_txt_records_(#{mongo_type := Type,
|
may_parse_srv_and_txt_records_(#{mongo_type := Type,
|
||||||
srv_record := true,
|
srv_record := true,
|
||||||
servers := Servers} = Config) ->
|
servers := Servers} = Config) ->
|
||||||
NServers = binary_to_list(Servers),
|
Hosts = parse_srv_records(Type, Servers),
|
||||||
Hosts = parse_srv_records(Type, NServers),
|
ExtraOpts = parse_txt_records(Type, Servers),
|
||||||
ExtraOpts = parse_txt_records(Type, NServers),
|
|
||||||
maps:merge(Config#{hosts => Hosts}, ExtraOpts).
|
maps:merge(Config#{hosts => Hosts}, ExtraOpts).
|
||||||
|
|
||||||
parse_srv_records(Type, Server) ->
|
parse_srv_records(Type, Servers) ->
|
||||||
case inet_res:lookup("_mongodb._tcp." ++ Server, in, srv) of
|
Fun = fun(AccIn, {IpOrHost, _Port}) ->
|
||||||
|
case inet_res:lookup("_mongodb._tcp." ++ ip_or_host_to_string(IpOrHost), in, srv) of
|
||||||
[] ->
|
[] ->
|
||||||
error(service_not_found);
|
error(service_not_found);
|
||||||
Services ->
|
Services ->
|
||||||
case [Host ++ ":" ++ integer_to_list(Port) || {_, _, Port, Host} <- Services] of
|
[ [server_to_bin({Host, Port}) || {_, _, Port, Host} <- Services]
|
||||||
[H | _] when Type =:= single ->
|
| AccIn]
|
||||||
[H];
|
|
||||||
Hosts ->
|
|
||||||
Hosts
|
|
||||||
end
|
end
|
||||||
|
end,
|
||||||
|
Res = lists:foldl(Fun, [], Servers),
|
||||||
|
case Type of
|
||||||
|
single -> lists:nth(1, Res);
|
||||||
|
_ -> Res
|
||||||
end.
|
end.
|
||||||
|
|
||||||
parse_txt_records(Type, Server) ->
|
parse_txt_records(Type, Servers) ->
|
||||||
case inet_res:lookup(Server, in, txt) of
|
Fun = fun(AccIn, {IpOrHost, _Port}) ->
|
||||||
|
case inet_res:lookup(IpOrHost, in, txt) of
|
||||||
[] ->
|
[] ->
|
||||||
#{};
|
#{};
|
||||||
[[QueryString]] ->
|
[[QueryString]] ->
|
||||||
|
@ -382,11 +377,13 @@ parse_txt_records(Type, Server) ->
|
||||||
rs -> ["authSource", "replicaSet"];
|
rs -> ["authSource", "replicaSet"];
|
||||||
_ -> ["authSource"]
|
_ -> ["authSource"]
|
||||||
end,
|
end,
|
||||||
take_and_convert(Fields, Options)
|
maps:merge(AccIn, take_and_convert(Fields, Options))
|
||||||
end;
|
end;
|
||||||
_ ->
|
_ ->
|
||||||
error({invalid_txt_record, multiple_records})
|
error({invalid_txt_record, multiple_records})
|
||||||
end.
|
end
|
||||||
|
end,
|
||||||
|
lists:foldl(Fun, #{}, Servers).
|
||||||
|
|
||||||
take_and_convert(Fields, Options) ->
|
take_and_convert(Fields, Options) ->
|
||||||
take_and_convert(Fields, Options, #{}).
|
take_and_convert(Fields, Options, #{}).
|
||||||
|
@ -407,6 +404,21 @@ take_and_convert([Field | More], Options, Acc) ->
|
||||||
take_and_convert(More, Options, Acc)
|
take_and_convert(More, Options, Acc)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec ip_or_host_to_string(binary() | string() | tuple())
|
||||||
|
-> string().
|
||||||
|
ip_or_host_to_string(Ip) when is_tuple(Ip) ->
|
||||||
|
inet:ntoa(Ip);
|
||||||
|
ip_or_host_to_string(Host) ->
|
||||||
|
str(Host).
|
||||||
|
|
||||||
|
servers_to_bin([Server | Rest]) ->
|
||||||
|
[server_to_bin(Server) | servers_to_bin(Rest)];
|
||||||
|
servers_to_bin([]) ->
|
||||||
|
[].
|
||||||
|
|
||||||
|
server_to_bin({IpOrHost, Port}) ->
|
||||||
|
iolist_to_binary(ip_or_host_to_string(IpOrHost) ++ ":" ++ integer_to_list(Port)).
|
||||||
|
|
||||||
%% ===================================================================
|
%% ===================================================================
|
||||||
%% typereflt funcs
|
%% typereflt funcs
|
||||||
|
|
||||||
|
@ -414,3 +426,18 @@ take_and_convert([Field | More], Options, Acc) ->
|
||||||
-> {string(), pos_integer()}.
|
-> {string(), pos_integer()}.
|
||||||
to_server_raw(Server) ->
|
to_server_raw(Server) ->
|
||||||
emqx_connector_schema_lib:parse_server(Server, ?MONGO_HOST_OPTIONS).
|
emqx_connector_schema_lib:parse_server(Server, ?MONGO_HOST_OPTIONS).
|
||||||
|
|
||||||
|
-spec to_servers_raw(string())
|
||||||
|
-> [{string(), pos_integer()}].
|
||||||
|
to_servers_raw(Servers) ->
|
||||||
|
lists:map( fun(Server) ->
|
||||||
|
emqx_connector_schema_lib:parse_server(Server, ?MONGO_HOST_OPTIONS)
|
||||||
|
end
|
||||||
|
, string:tokens(str(Servers), ", ")).
|
||||||
|
|
||||||
|
str(A) when is_atom(A) ->
|
||||||
|
atom_to_list(A);
|
||||||
|
str(B) when is_binary(B) ->
|
||||||
|
binary_to_list(B);
|
||||||
|
str(S) when is_list(S) ->
|
||||||
|
S.
|
||||||
|
|
Loading…
Reference in New Issue