Skip to content

Commit 624cbde

Browse files
Merge pull request #12045 from rabbitmq/mergify/bp/v4.0.x/pr-12041
2 parents 1bb771f + 436df7d commit 624cbde

File tree

6 files changed

+147
-35
lines changed

6 files changed

+147
-35
lines changed

deps/rabbitmq_management/src/rabbit_mgmt_wm_parameters.erl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ is_authorized(ReqData, Context) ->
4141
%%--------------------------------------------------------------------
4242

4343
%% Hackish fix to make sure we return a JSON object instead of an empty list
44-
%% when the publish-properties value is empty. Should be removed in 3.7.0
45-
%% when we switch to a new JSON library.
44+
%% when the publish-properties value is empty.
4645
fix_shovel_publish_properties(P) ->
4746
case lists:keyfind(component, 1, P) of
4847
{_, <<"shovel">>} ->

deps/rabbitmq_shovel_management/app.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ def all_beam_files(name = "all_beam_files"):
99
erlang_bytecode(
1010
name = "other_beam",
1111
srcs = [
12-
"src/rabbit_shovel_mgmt.erl",
12+
"src/rabbit_shovel_mgmt_shovel.erl",
13+
"src/rabbit_shovel_mgmt_shovels.erl",
1314
"src/rabbit_shovel_mgmt_util.erl",
1415
],
1516
hdrs = [":public_and_private_hdrs"],
@@ -33,7 +34,8 @@ def all_test_beam_files(name = "all_test_beam_files"):
3334
name = "test_other_beam",
3435
testonly = True,
3536
srcs = [
36-
"src/rabbit_shovel_mgmt.erl",
37+
"src/rabbit_shovel_mgmt_shovel.erl",
38+
"src/rabbit_shovel_mgmt_shovels.erl",
3739
"src/rabbit_shovel_mgmt_util.erl",
3840
],
3941
hdrs = [":public_and_private_hdrs"],
@@ -72,7 +74,8 @@ def all_srcs(name = "all_srcs"):
7274
filegroup(
7375
name = "srcs",
7476
srcs = [
75-
"src/rabbit_shovel_mgmt.erl",
77+
"src/rabbit_shovel_mgmt_shovel.erl",
78+
"src/rabbit_shovel_mgmt_shovels.erl",
7679
"src/rabbit_shovel_mgmt_util.erl",
7780
],
7881
)

deps/rabbitmq_shovel_management/src/rabbit_shovel_mgmt.erl renamed to deps/rabbitmq_shovel_management/src/rabbit_shovel_mgmt_shovel.erl

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
66
%%
77

8-
-module(rabbit_shovel_mgmt).
8+
-module(rabbit_shovel_mgmt_shovel).
99

1010
-behaviour(rabbit_mgmt_extension).
1111

@@ -19,9 +19,9 @@
1919
-include_lib("amqp_client/include/amqp_client.hrl").
2020
-include("rabbit_shovel_mgmt.hrl").
2121

22-
dispatcher() -> [{"/shovels", ?MODULE, []},
23-
{"/shovels/:vhost", ?MODULE, []},
24-
{"/shovels/vhost/:vhost/:name", ?MODULE, []},
22+
-define(COMPONENT, <<"shovel">>).
23+
24+
dispatcher() -> [{"/shovels/vhost/:vhost/:name", ?MODULE, []},
2525
{"/shovels/vhost/:vhost/:name/restart", ?MODULE, []}].
2626

2727
web_ui() -> [{javascript, <<"shovel.js">>}].
@@ -42,7 +42,7 @@ resource_exists(ReqData, Context) ->
4242
not_found ->
4343
false;
4444
VHost ->
45-
case rabbit_mgmt_util:id(name, ReqData) of
45+
case name(ReqData) of
4646
none -> true;
4747
Name ->
4848
%% Deleting or restarting a shovel
@@ -65,8 +65,10 @@ resource_exists(ReqData, Context) ->
6565
{Reply, ReqData, Context}.
6666

6767
to_json(ReqData, Context) ->
68-
rabbit_mgmt_util:reply_list(
69-
filter_vhost_req(rabbit_shovel_mgmt_util:status(ReqData, Context), ReqData), ReqData, Context).
68+
Shovel = parameter(ReqData),
69+
rabbit_mgmt_util:reply(rabbit_mgmt_format:parameter(
70+
rabbit_mgmt_wm_parameters:fix_shovel_publish_properties(Shovel)),
71+
ReqData, Context).
7072

7173
is_authorized(ReqData, Context) ->
7274
rabbit_mgmt_util:is_authorized_monitor(ReqData, Context).
@@ -115,20 +117,26 @@ delete_resource(ReqData, #context{user = #user{username = Username}}=Context) ->
115117

116118
%%--------------------------------------------------------------------
117119

120+
name(ReqData) -> rabbit_mgmt_util:id(name, ReqData).
121+
122+
parameter(ReqData) ->
123+
VHostName = rabbit_mgmt_util:vhost(ReqData),
124+
Name = name(ReqData),
125+
if
126+
VHostName =/= not_found andalso
127+
Name =/= none ->
128+
rabbit_runtime_parameters:lookup(VHostName, ?COMPONENT, Name);
129+
true ->
130+
not_found
131+
end.
132+
118133
is_restart(ReqData) ->
119134
Path = cowboy_req:path(ReqData),
120135
case string:find(Path, "/restart", trailing) of
121136
nomatch -> false;
122137
_ -> true
123138
end.
124139

125-
filter_vhost_req(List, ReqData) ->
126-
case rabbit_mgmt_util:vhost(ReqData) of
127-
none -> List;
128-
VHost -> [I || I <- List,
129-
pget(vhost, I) =:= VHost]
130-
end.
131-
132140
get_shovel_node(VHost, Name, ReqData, Context) ->
133141
Shovels = rabbit_shovel_mgmt_util:status(ReqData, Context),
134142
Match = find_matching_shovel(VHost, Name, Shovels),
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
%% This Source Code Form is subject to the terms of the Mozilla Public
2+
%% License, v. 2.0. If a copy of the MPL was not distributed with this
3+
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
%%
5+
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
6+
%%
7+
8+
-module(rabbit_shovel_mgmt_shovels).
9+
10+
-behaviour(rabbit_mgmt_extension).
11+
12+
-export([dispatcher/0, web_ui/0]).
13+
-export([init/2, to_json/2, resource_exists/2, content_types_provided/2,
14+
is_authorized/2, allowed_methods/2]).
15+
16+
-import(rabbit_misc, [pget/2]).
17+
18+
-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
19+
-include_lib("amqp_client/include/amqp_client.hrl").
20+
-include("rabbit_shovel_mgmt.hrl").
21+
22+
dispatcher() -> [{"/shovels", ?MODULE, []},
23+
{"/shovels/:vhost", ?MODULE, []}].
24+
25+
web_ui() -> [{javascript, <<"shovel.js">>}].
26+
27+
%%--------------------------------------------------------------------
28+
29+
init(Req, _Opts) ->
30+
{cowboy_rest, rabbit_mgmt_cors:set_headers(Req, ?MODULE), #context{}}.
31+
32+
content_types_provided(ReqData, Context) ->
33+
{[{<<"application/json">>, to_json}], ReqData, Context}.
34+
35+
allowed_methods(ReqData, Context) ->
36+
{[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>], ReqData, Context}.
37+
38+
resource_exists(ReqData, Context) ->
39+
Reply = case rabbit_mgmt_util:vhost(ReqData) of
40+
not_found -> false;
41+
_Found -> true
42+
end,
43+
{Reply, ReqData, Context}.
44+
45+
to_json(ReqData, Context) ->
46+
rabbit_mgmt_util:reply_list(
47+
filter_vhost_req(rabbit_shovel_mgmt_util:status(ReqData, Context), ReqData), ReqData, Context).
48+
49+
is_authorized(ReqData, Context) ->
50+
rabbit_mgmt_util:is_authorized_monitor(ReqData, Context).
51+
52+
filter_vhost_req(List, ReqData) ->
53+
case rabbit_mgmt_util:vhost(ReqData) of
54+
none -> List;
55+
VHost -> [I || I <- List,
56+
pget(vhost, I) =:= VHost]
57+
end.

deps/rabbitmq_shovel_management/test/http_SUITE.erl

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
-module(http_SUITE).
99

1010
-include_lib("common_test/include/ct.hrl").
11+
-include_lib("eunit/include/eunit.hrl").
1112
-include_lib("rabbit_common/include/rabbit_framing.hrl").
1213
-include_lib("rabbitmq_ct_helpers/include/rabbit_mgmt_test.hrl").
1314

@@ -27,6 +28,7 @@ groups() ->
2728
[
2829
{dynamic_shovels, [], [
2930
start_and_list_a_dynamic_amqp10_shovel,
31+
start_and_get_a_dynamic_amqp10_shovel,
3032
create_and_delete_a_dynamic_shovel_that_successfully_connects,
3133
create_and_delete_a_dynamic_shovel_that_fails_to_connect
3234
]},
@@ -124,25 +126,33 @@ start_inets(Config) ->
124126
%% -------------------------------------------------------------------
125127

126128
start_and_list_a_dynamic_amqp10_shovel(Config) ->
127-
Port = integer_to_binary(
128-
rabbit_ct_broker_helpers:get_node_config(Config, 0, tcp_port_amqp)),
129-
130129
remove_all_dynamic_shovels(Config, <<"/">>),
131-
ID = {<<"/">>, <<"dynamic-amqp10-1">>},
130+
Name = <<"dynamic-amqp10-await-startup-1">>,
131+
ID = {<<"/">>, Name},
132132
await_shovel_removed(Config, ID),
133133

134-
http_put(Config, "/parameters/shovel/%2f/dynamic-amqp10-1",
135-
#{value => #{'src-protocol' => <<"amqp10">>,
136-
'src-uri' => <<"amqp://localhost:", Port/binary>>,
137-
'src-address' => <<"test">>,
138-
'dest-protocol' => <<"amqp10">>,
139-
'dest-uri' => <<"amqp://localhost:", Port/binary>>,
140-
'dest-address' => <<"test2">>,
141-
'dest-properties' => #{},
142-
'dest-application-properties' => #{},
143-
'dest-message-annotations' => #{}}}, ?CREATED),
134+
declare_shovel(Config, Name),
135+
await_shovel_startup(Config, ID),
136+
Shovels = list_shovels(Config),
137+
?assert(lists:any(
138+
fun(M) ->
139+
maps:get(name, M) =:= Name
140+
end, Shovels)),
141+
delete_shovel(Config, <<"dynamic-amqp10-await-startup-1">>),
142+
143+
ok.
144+
145+
start_and_get_a_dynamic_amqp10_shovel(Config) ->
146+
remove_all_dynamic_shovels(Config, <<"/">>),
147+
Name = <<"dynamic-amqp10-get-shovel-1">>,
148+
ID = {<<"/">>, Name},
149+
await_shovel_removed(Config, ID),
144150

151+
declare_shovel(Config, Name),
145152
await_shovel_startup(Config, ID),
153+
Sh = get_shovel(Config, Name),
154+
?assertEqual(Name, maps:get(name, Sh)),
155+
delete_shovel(Config, <<"dynamic-amqp10-await-startup-1">>),
146156

147157
ok.
148158

@@ -317,14 +327,48 @@ assert_item(ExpI, ActI) ->
317327
ExpI = maps:with(maps:keys(ExpI), ActI),
318328
ok.
319329

330+
list_shovels(Config) ->
331+
list_shovels(Config, "%2F").
332+
333+
list_shovels(Config, VirtualHost) ->
334+
Path = io_lib:format("/shovels/~s", [VirtualHost]),
335+
http_get(Config, Path, ?OK).
336+
337+
get_shovel(Config, Name) ->
338+
get_shovel(Config, "%2F", Name).
339+
340+
get_shovel(Config, VirtualHost, Name) ->
341+
Path = io_lib:format("/shovels/vhost/~s/~s", [VirtualHost, Name]),
342+
http_get(Config, Path, ?OK).
343+
320344
delete_shovel(Config, Name) ->
321-
Path = io_lib:format("/shovels/vhost/%2F/~s", [Name]),
345+
delete_shovel(Config, "%2F", Name).
346+
347+
delete_shovel(Config, VirtualHost, Name) ->
348+
Path = io_lib:format("/shovels/vhost/~s/~s", [VirtualHost, Name]),
322349
http_delete(Config, Path, ?NO_CONTENT).
323350

324351
remove_all_dynamic_shovels(Config, VHost) ->
325352
rabbit_ct_broker_helpers:rpc(Config, 0,
326353
rabbit_runtime_parameters, clear_vhost, [VHost, <<"CT tests">>]).
327354

355+
declare_shovel(Config, Name) ->
356+
Port = integer_to_binary(
357+
rabbit_ct_broker_helpers:get_node_config(Config, 0, tcp_port_amqp)),
358+
http_put(Config, io_lib:format("/parameters/shovel/%2f/~ts", [Name]),
359+
#{
360+
value => #{
361+
'src-protocol' => <<"amqp10">>,
362+
'src-uri' => <<"amqp://localhost:", Port/binary>>,
363+
'src-address' => <<"test">>,
364+
'dest-protocol' => <<"amqp10">>,
365+
'dest-uri' => <<"amqp://localhost:", Port/binary>>,
366+
'dest-address' => <<"test2">>,
367+
'dest-properties' => #{},
368+
'dest-application-properties' => #{},
369+
'dest-message-annotations' => #{}}
370+
}, ?CREATED).
371+
328372
await_shovel_startup(Config, Name) ->
329373
await_shovel_startup(Config, Name, 10_000).
330374

moduleindex.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,8 @@ rabbitmq_shovel:
11311131
- rabbit_shovel_worker
11321132
- rabbit_shovel_worker_sup
11331133
rabbitmq_shovel_management:
1134-
- rabbit_shovel_mgmt
1134+
- rabbit_shovel_mgmt_shovel
1135+
- rabbit_shovel_mgmt_shovels
11351136
- rabbit_shovel_mgmt_util
11361137
rabbitmq_shovel_prometheus:
11371138
- rabbit_shovel_prometheus_app

0 commit comments

Comments
 (0)