Skip to content
This repository was archived by the owner on Nov 17, 2020. It is now read-only.

Commit 25db052

Browse files
committed
rabbit_ct_broker_helpers: Add node_uri() to format the URI for a given node
1 parent bab1d6b commit 25db052

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/rabbit_ct_broker_helpers.erl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
-module(rabbit_ct_broker_helpers).
1818

1919
-include_lib("common_test/include/ct.hrl").
20+
-include_lib("kernel/include/inet.hrl").
2021
-include("include/rabbit.hrl").
2122

2223
-export([
@@ -29,6 +30,7 @@
2930
get_node_configs/1, get_node_configs/2,
3031
get_node_config/2, get_node_config/3, set_node_config/3,
3132
nodename_to_index/2,
33+
node_uri/2, node_uri/3,
3234

3335
control_action/2, control_action/3, control_action/4,
3436
rabbitmqctl/3, rabbitmqctl_list/3,
@@ -576,6 +578,48 @@ nodename_to_index1([NodeConfig | Rest], Node, I) ->
576578
nodename_to_index1([], Node, _) ->
577579
exit({unknown_node, Node}).
578580

581+
node_uri(Config, Node) ->
582+
node_uri(Config, Node, []).
583+
584+
node_uri(Config, Node, Options) ->
585+
Scheme = proplists:get_value(scheme, Options, "amqp"),
586+
Hostname = case proplists:get_value(use_ipaddr, Options, false) of
587+
true ->
588+
{ok, Hostent} = inet:gethostbyname(?config(rmq_hostname, Config)),
589+
format_ipaddr_for_uri(Hostent);
590+
Family when Family =:= inet orelse Family =:= inet6 ->
591+
{ok, Hostent} = inet:gethostbyname(?config(rmq_hostname, Config),
592+
Family),
593+
format_ipaddr_for_uri(Hostent);
594+
false ->
595+
?config(rmq_hostname, Config)
596+
end,
597+
TcpPort = get_node_config(Config, Node, tcp_port_amqp),
598+
UserPass = case proplists:get_value(with_user, Options, false) of
599+
true ->
600+
User = proplists:get_value(user, Options, "guest"),
601+
Password = proplists:get_value(password, Options, "guest"),
602+
io_lib:forma("~s:~s@", [User, Password]);
603+
false ->
604+
""
605+
end,
606+
list_to_binary(
607+
rabbit_misc:format("~s://~s~s:~b",
608+
[Scheme, UserPass, Hostname, TcpPort])).
609+
610+
format_ipaddr_for_uri(
611+
#hostent{h_addrtype = inet, h_addr_list = [IPAddr | _]}) ->
612+
{A, B, C, D} = IPAddr,
613+
io_lib:format("~b.~b.~b.~b", [A, B, C, D]);
614+
format_ipaddr_for_uri(
615+
#hostent{h_addrtype = inet6, h_addr_list = [IPAddr | _]}) ->
616+
{A, B, C, D, E, F, G, H} = IPAddr,
617+
Res0 = io_lib:format(
618+
"~.16b:~.16b:~.16b:~.16b:~.16b:~.16b:~.16b:~.16b",
619+
[A, B, C, D, E, F, G, H]),
620+
Res1 = re:replace(Res0, "(^0(:0)+$|^(0:)+|(:0)+$)|:(0:)+", "::"),
621+
"[" ++ Res1 ++ "]".
622+
579623
%% Functions to execute code on a remote node/broker.
580624

581625
add_code_path_to_node(Node, Module) ->

0 commit comments

Comments
 (0)