|
17 | 17 | -module(rabbit_ct_broker_helpers).
|
18 | 18 |
|
19 | 19 | -include_lib("common_test/include/ct.hrl").
|
| 20 | +-include_lib("kernel/include/inet.hrl"). |
20 | 21 | -include("include/rabbit.hrl").
|
21 | 22 |
|
22 | 23 | -export([
|
|
29 | 30 | get_node_configs/1, get_node_configs/2,
|
30 | 31 | get_node_config/2, get_node_config/3, set_node_config/3,
|
31 | 32 | nodename_to_index/2,
|
| 33 | + node_uri/2, node_uri/3, |
32 | 34 |
|
33 | 35 | control_action/2, control_action/3, control_action/4,
|
34 | 36 | rabbitmqctl/3, rabbitmqctl_list/3,
|
@@ -576,6 +578,48 @@ nodename_to_index1([NodeConfig | Rest], Node, I) ->
|
576 | 578 | nodename_to_index1([], Node, _) ->
|
577 | 579 | exit({unknown_node, Node}).
|
578 | 580 |
|
| 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 | + |
579 | 623 | %% Functions to execute code on a remote node/broker.
|
580 | 624 |
|
581 | 625 | add_code_path_to_node(Node, Module) ->
|
|
0 commit comments