Skip to content

Commit f13f993

Browse files
committed
Management part for internal shovels. Experimenting with owner linking
1 parent 7a34bf8 commit f13f993

File tree

4 files changed

+108
-46
lines changed

4 files changed

+108
-46
lines changed

deps/rabbitmq_management/priv/www/js/formatters.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,3 +1094,15 @@ function fmt_deprecation_phase(phase, deprecation_phases){
10941094
}
10951095
}
10961096
}
1097+
1098+
function fmt_resource(res) {
1099+
return `${res.kind} '${res.name}' in vhost '${res.virtual_host}'`;
1100+
}
1101+
1102+
function fmt_resource_link(res) {
1103+
if (res.kind == "queue") {
1104+
return `${res.kind} '${link_queue(res.virtual_host, res.name, {})}' in vhost '${link_vhost(res.virtual_host)}'`;
1105+
} else if (res.kind == "exchange") {
1106+
return `${res.kind} '${link_exchange(res.virtual_host, res.name, {})}' in vhost '${link_vhost(res.virtual_host)}'`;
1107+
}
1108+
}

deps/rabbitmq_shovel_management/priv/www/js/shovel.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,27 @@ function fmt_shovel_endpoint(prefix, shovel) {
206206
return txt;
207207
}
208208

209+
function is_internal_shovel(shovel) {
210+
if (!shovel.hasOwnProperty('internal')) {
211+
return false;
212+
} else {
213+
return shovel['internal'];
214+
}
215+
}
216+
217+
function shovel_has_internal_owner(shovel) {
218+
if (!shovel.hasOwnProperty('internal_owner')) {
219+
return false;
220+
} else {
221+
return true;
222+
}
223+
}
224+
225+
function shovel_internal_owner(shovel) {
226+
return shovel.internal_owner;
227+
}
228+
229+
209230
function fallback_value(shovel, key1, key2) {
210231
var v = shovel.value[key1];
211232
return (v !== undefined ? v : shovel.value[key2]);

deps/rabbitmq_shovel_management/priv/www/js/tmpl/dynamic-shovel.ejs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,23 @@
4444
</div>
4545
</div>
4646

47-
<div class="section-hidden">
47+
48+
<div class="section-hidden">
4849
<h2>Delete this shovel</h2>
4950
<div class="hider">
50-
<form action="#/shovel-parameters" method="delete" class="confirm">
51-
<input type="hidden" name="component" value="shovel"/>
52-
<input type="hidden" name="vhost" value="<%= fmt_string(shovel.vhost) %>"/>
53-
<input type="hidden" name="name" value="<%= fmt_string(shovel.name) %>"/>
54-
<input type="submit" value="Delete this shovel"/>
55-
</form>
51+
<% if (!is_internal_shovel(shovel.value)) { %>
52+
<form action="#/shovel-parameters" method="delete" class="confirm">
53+
<input type="hidden" name="component" value="shovel"/>
54+
<input type="hidden" name="vhost" value="<%= fmt_string(shovel.vhost) %>"/>
55+
<input type="hidden" name="name" value="<%= fmt_string(shovel.name) %>"/>
56+
<input type="submit" value="Delete this shovel"/>
57+
</form>
58+
<% } else { %>
59+
<% if (shovel_has_internal_owner(shovel.value)) { %>
60+
<span>This shovel is internal and owned by <%= fmt_resource_link(shovel_internal_owner(shovel.value)) %>. Could be deleted only via CLI command with --force.</span>
61+
<% } else { %>
62+
<span>This shovel is internal. Could be deleted only via CLI command with '--force'.</span>
63+
<% } %>
64+
<% } %>
65+
</div>
5666
</div>
57-
</div>

deps/rabbitmq_shovel_management/src/rabbit_shovel_mgmt_shovel.erl

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -79,45 +79,60 @@ is_authorized(ReqData, Context) ->
7979

8080
delete_resource(ReqData, #context{user = #user{username = Username}}=Context) ->
8181
VHost = rabbit_mgmt_util:id(vhost, ReqData),
82-
Reply = case rabbit_mgmt_util:id(name, ReqData) of
83-
none ->
84-
false;
85-
Name ->
86-
case get_shovel_node(VHost, Name, ReqData, Context) of
87-
undefined -> rabbit_log:error("Could not find shovel data for shovel '~ts' in vhost: '~ts'", [Name, VHost]),
88-
case is_restart(ReqData) of
89-
true ->
90-
false;
91-
%% this is a deletion attempt
92-
false ->
93-
%% if we do not know the node, use the local one
94-
try_delete(node(), VHost, Name, Username),
95-
true
82+
case rabbit_mgmt_util:id(name, ReqData) of
83+
none ->
84+
{false, ReqData, Context};
85+
Name ->
86+
case get_shovel_node(VHost, Name, ReqData, Context) of
87+
undefined -> rabbit_log:error("Could not find shovel data for shovel '~ts' in vhost: '~ts'", [Name, VHost]),
88+
case is_restart(ReqData) of
89+
true ->
90+
{false, ReqData, Context};
91+
%% this is a deletion attempt
92+
false ->
93+
%% if we do not know the node, use the local one
94+
case try_delete(node(), VHost, Name, Username) of
95+
true -> {true, ReqData, Context};
96+
%% NOTE: that how it was before, try_delete return was ignored and true returned ¯\_(ツ)_/¯
97+
false -> {true, ReqData, Context};
98+
locked -> Reply = cowboy_req:reply(405, #{<<"content-type">> => <<"text/plain">>},
99+
"Protected", ReqData),
100+
{halt, Reply, Context};
101+
%% NOTE: that how it was before, try_delete return was ignored and true returned ¯\_(ツ)_/¯
102+
error -> {true, ReqData, Context}
103+
end
104+
end;
105+
Node ->
106+
%% We must distinguish between a delete and a restart
107+
case is_restart(ReqData) of
108+
true ->
109+
rabbit_log:info("Asked to restart shovel '~ts' in vhost '~ts' on node '~s'", [Name, VHost, Node]),
110+
try erpc:call(Node, rabbit_shovel_util, restart_shovel, [VHost, Name], ?SHOVEL_CALLS_TIMEOUT_MS) of
111+
ok -> {true, ReqData, Context};
112+
{error, not_found} ->
113+
rabbit_log:error("Could not find shovel data for shovel '~s' in vhost: '~s'", [Name, VHost]),
114+
{false, ReqData, Context}
115+
catch _:Reason ->
116+
rabbit_log:error("Failed to restart shovel '~s' on vhost '~s', reason: ~p",
117+
[Name, VHost, Reason]),
118+
{false, ReqData, Context}
96119
end;
97-
Node ->
98-
%% We must distinguish between a delete and a restart
99-
case is_restart(ReqData) of
100-
true ->
101-
rabbit_log:info("Asked to restart shovel '~ts' in vhost '~ts' on node '~s'", [Name, VHost, Node]),
102-
try erpc:call(Node, rabbit_shovel_util, restart_shovel, [VHost, Name], ?SHOVEL_CALLS_TIMEOUT_MS) of
103-
ok -> true;
104-
{error, not_found} ->
105-
rabbit_log:error("Could not find shovel data for shovel '~s' in vhost: '~s'", [Name, VHost]),
106-
false
107-
catch _:Reason ->
108-
rabbit_log:error("Failed to restart shovel '~s' on vhost '~s', reason: ~p",
109-
[Name, VHost, Reason]),
110-
false
111-
end;
112-
113-
_ ->
114-
try_delete(Node, VHost, Name, Username),
115-
true
116120

121+
_ ->
122+
case try_delete(Node, VHost, Name, Username) of
123+
true -> {true, ReqData, Context};
124+
%% NOTE: that how it was before, try_delete return was ignored and true returned ¯\_(ツ)_/¯
125+
false -> {true, ReqData, Context};
126+
locked -> Reply = cowboy_req:reply(405, #{<<"content-type">> => <<"text/plain">>},
127+
"Protected", ReqData),
128+
{halt, Reply, Context};
129+
%% NOTE: that how it was before, try_delete return was ignored and true returned ¯\_(ツ)_/¯
130+
error -> {true, ReqData, Context}
117131
end
132+
118133
end
119-
end,
120-
{Reply, ReqData, Context}.
134+
end
135+
end.
121136

122137
%%--------------------------------------------------------------------
123138

@@ -168,7 +183,7 @@ find_matching_shovel(VHost, Name, Shovels) ->
168183
undefined
169184
end.
170185

171-
-spec try_delete(node(), vhost:name(), any(), rabbit_types:username()) -> boolean().
186+
-spec try_delete(node(), vhost:name(), any(), rabbit_types:username()) -> true | false | locked | error.
172187
try_delete(Node, VHost, Name, Username) ->
173188
rabbit_log:info("Asked to delete shovel '~ts' in vhost '~ts' on node '~s'", [Name, VHost, Node]),
174189
%% this will clear the runtime parameter, the ultimate way of deleting a dynamic Shovel eventually. MK.
@@ -177,8 +192,13 @@ try_delete(Node, VHost, Name, Username) ->
177192
{error, not_found} ->
178193
rabbit_log:error("Could not find shovel data for shovel '~s' in vhost: '~s'", [Name, VHost]),
179194
false
180-
catch _:Reason ->
195+
catch
196+
_:{exception, {amqp_error, resource_locked, Reason, _}} ->
181197
rabbit_log:error("Failed to delete shovel '~s' on vhost '~s', reason: ~p",
182198
[Name, VHost, Reason]),
183-
false
199+
locked;
200+
_:Reason ->
201+
rabbit_log:error("Failed to delete shovel '~s' on vhost '~s', reason: ~p",
202+
[Name, VHost, Reason]),
203+
error
184204
end.

0 commit comments

Comments
 (0)