|
| 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-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(rabbit_mgmt_wm_user_queues). |
| 9 | + |
| 10 | +-export([init/2, to_json/2, content_types_provided/2, is_authorized/2, |
| 11 | + resource_exists/2, basic/1]). |
| 12 | +-export([variances/2]). |
| 13 | + |
| 14 | +-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl"). |
| 15 | + |
| 16 | +-define(BASIC_COLUMNS, |
| 17 | + ["vhost", |
| 18 | + "name", |
| 19 | + "node", |
| 20 | + "durable", |
| 21 | + "auto_delete", |
| 22 | + "exclusive", |
| 23 | + "owner_pid", |
| 24 | + "arguments", |
| 25 | + "type", |
| 26 | + "pid", |
| 27 | + "state"]). |
| 28 | + |
| 29 | +-define(DEFAULT_SORT, ["vhost", "name"]). |
| 30 | + |
| 31 | +%%-------------------------------------------------------------------- |
| 32 | + |
| 33 | +init(Req, _InitialState) -> |
| 34 | + {cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}. |
| 35 | + |
| 36 | +variances(Req, Context) -> |
| 37 | + {[<<"accept-encoding">>, <<"origin">>], Req, Context}. |
| 38 | + |
| 39 | +content_types_provided(ReqData, Context) -> |
| 40 | + {rabbit_mgmt_util:responder_map(to_json), ReqData, Context}. |
| 41 | + |
| 42 | +resource_exists(ReqData, Context) -> |
| 43 | + %% just checking that the vhost requested exists |
| 44 | + {case rabbit_mgmt_util:all_or_one_vhost(ReqData, fun (_) -> [] end) of |
| 45 | + vhost_not_found -> false; |
| 46 | + _ -> true |
| 47 | + end, ReqData, Context}. |
| 48 | + |
| 49 | +to_json(ReqData, Context) -> |
| 50 | + try |
| 51 | + Basic = basic_owner_and_vhost_filtered(ReqData, Context), |
| 52 | + Data = rabbit_mgmt_util:augment_resources(Basic, ?DEFAULT_SORT, |
| 53 | + ?BASIC_COLUMNS, ReqData, |
| 54 | + Context, augment()), |
| 55 | + rabbit_mgmt_util:reply(Data, ReqData, Context) |
| 56 | + catch |
| 57 | + {error, invalid_range_parameters, Reason} -> |
| 58 | + rabbit_mgmt_util:bad_request(iolist_to_binary(Reason), ReqData, |
| 59 | + Context) |
| 60 | + end. |
| 61 | + |
| 62 | +is_authorized(ReqData, Context) -> |
| 63 | + rabbit_mgmt_util:is_authorized_vhost(ReqData, Context). |
| 64 | + |
| 65 | +%%-------------------------------------------------------------------- |
| 66 | +%% Exported functions |
| 67 | + |
| 68 | +basic(ReqData) -> |
| 69 | + %% rabbit_nodes:list_running/1 is a potentially slow function that performs |
| 70 | + %% a cluster wide query with a reasonably long (10s) timeout. |
| 71 | + %% TODO: replace with faster approximate function |
| 72 | + Running = rabbit_nodes:list_running(), |
| 73 | + Ctx = #{running_nodes => Running}, |
| 74 | + FmtQ = fun (Q) -> rabbit_mgmt_format:queue(Q, Ctx) end, |
| 75 | + User = rabbit_mgmt_util:id(user, ReqData), |
| 76 | + list_queues(ReqData, Running, FmtQ, FmtQ, User). |
| 77 | + |
| 78 | +list_queues(ReqData, Running, FormatRunningFun, FormatDownFun, User) -> |
| 79 | + [begin |
| 80 | + Pid = amqqueue:get_pid(Q), |
| 81 | + %% only queues whose leader pid is a on a non running node |
| 82 | + %% are considered "down", all other states should be passed |
| 83 | + %% as they are and the queue type impl will decide how to |
| 84 | + %% emit them. |
| 85 | + case not rabbit_amqqueue:is_local_to_node_set(Pid, Running) of |
| 86 | + false -> |
| 87 | + FormatRunningFun(Q); |
| 88 | + true -> |
| 89 | + FormatDownFun(amqqueue:set_state(Q, down)) |
| 90 | + end |
| 91 | + end || Q <- all_queues(ReqData, User)]. |
| 92 | + |
| 93 | + |
| 94 | +%%-------------------------------------------------------------------- |
| 95 | +%% Private helpers |
| 96 | + |
| 97 | +augment() -> |
| 98 | + fun(Basic, ReqData) -> |
| 99 | + case rabbit_mgmt_util:disable_stats(ReqData) of |
| 100 | + false -> |
| 101 | + Stats = case rabbit_mgmt_util:columns(ReqData) of |
| 102 | + all -> basic; |
| 103 | + _ -> detailed |
| 104 | + end, |
| 105 | + rabbit_mgmt_db:augment_queues(Basic, |
| 106 | + rabbit_mgmt_util:range_ceil(ReqData), |
| 107 | + Stats); |
| 108 | + true -> |
| 109 | + Basic |
| 110 | + end |
| 111 | + end. |
| 112 | + |
| 113 | +basic_owner_and_vhost_filtered(ReqData, Context) -> |
| 114 | + rabbit_mgmt_util:filter_vhost(basic(ReqData), ReqData, Context). |
| 115 | + |
| 116 | +all_queues(ReqData, User) -> |
| 117 | + rabbit_mgmt_util:all_or_one_vhost(ReqData, fun (VHost) -> list_all_for_user(VHost, User) end). |
| 118 | + |
| 119 | +list_all_for_user(VHost, User) -> |
| 120 | + All = rabbit_amqqueue:list_all(VHost), |
| 121 | + [Q || Q <- All, |
| 122 | + maps:get(user, amqqueue:get_options(Q)) =:= User]. |
0 commit comments