summaryrefslogtreecommitdiff
path: root/deps/rabbitmq_management/src/rabbit_mgmt_wm_rebalance_queues.erl
diff options
context:
space:
mode:
Diffstat (limited to 'deps/rabbitmq_management/src/rabbit_mgmt_wm_rebalance_queues.erl')
-rw-r--r--deps/rabbitmq_management/src/rabbit_mgmt_wm_rebalance_queues.erl58
1 files changed, 58 insertions, 0 deletions
diff --git a/deps/rabbitmq_management/src/rabbit_mgmt_wm_rebalance_queues.erl b/deps/rabbitmq_management/src/rabbit_mgmt_wm_rebalance_queues.erl
new file mode 100644
index 0000000000..982655493c
--- /dev/null
+++ b/deps/rabbitmq_management/src/rabbit_mgmt_wm_rebalance_queues.erl
@@ -0,0 +1,58 @@
+%% This Source Code Form is subject to the terms of the Mozilla Public
+%% License, v. 2.0. If a copy of the MPL was not distributed with this
+%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
+%%
+%% Copyright (c) 2007-2020 VMware, Inc. or its affiliates. All rights reserved.
+%%
+
+-module(rabbit_mgmt_wm_rebalance_queues).
+
+-export([init/2, service_available/2, resource_exists/2,
+ content_types_provided/2, content_types_accepted/2,
+ is_authorized/2, allowed_methods/2, accept_content/2]).
+-export([variances/2]).
+
+-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl").
+-include_lib("rabbit_common/include/rabbit.hrl").
+
+%%--------------------------------------------------------------------
+
+init(Req, [Mode]) ->
+ Headers = rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE),
+ {cowboy_rest, Headers, {Mode, #context{}}}.
+
+service_available(Req, {{queues, all}, _Context}=State) ->
+ {true, Req, State};
+service_available(Req, State) ->
+ {false, Req, State}.
+
+variances(Req, State) ->
+ {[<<"accept-encoding">>, <<"origin">>], Req, State}.
+
+content_types_provided(Req, State) ->
+ {[{{<<"text">>, <<"plain">>, '*'}, undefined}], Req, State}.
+
+content_types_accepted(Req, State) ->
+ {[{'*', accept_content}], Req, State}.
+
+allowed_methods(Req, State) ->
+ {[<<"POST">>, <<"OPTIONS">>], Req, State}.
+
+resource_exists(Req, State) ->
+ {true, Req, State}.
+
+accept_content(Req, {_Mode, #context{user = #user{username = Username}}}=State) ->
+ try
+ rabbit_log:info("User '~s' has initiated a queue rebalance", [Username]),
+ spawn(fun() ->
+ rabbit_amqqueue:rebalance(all, <<".*">>, <<".*">>)
+ end),
+ {true, Req, State}
+ catch
+ {error, Reason} ->
+ rabbit_mgmt_util:bad_request(iolist_to_binary(Reason), Req, State)
+ end.
+
+is_authorized(Req0, {Mode, Context0}) ->
+ {Res, Req1, Context1} = rabbit_mgmt_util:is_authorized_admin(Req0, Context0),
+ {Res, Req1, {Mode, Context1}}.