Skip to content

Commit 0c9b41b

Browse files
Merge pull request #5410 from rabbitmq/rabbitmq-server-5305-follow-up
ctl add_vhost: check if relevant feature flags are enabled
2 parents 5ecb546 + 83676fa commit 0c9b41b

File tree

4 files changed

+57
-5
lines changed

4 files changed

+57
-5
lines changed

deps/rabbit/src/rabbit_queue_type.erl

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
init/0,
1414
close/1,
1515
discover/1,
16+
feature_flag_name/1,
1617
default/0,
1718
is_enabled/1,
1819
is_compatible/4,
@@ -223,14 +224,23 @@
223224
-callback notify_decorators(amqqueue:amqqueue()) ->
224225
ok.
225226

226-
%% TODO: this should be controlled by a registry that is populated on boot
227+
%% TODO: should this use a registry that's populated on boot?
227228
discover(<<"quorum">>) ->
228229
rabbit_quorum_queue;
229230
discover(<<"classic">>) ->
230231
rabbit_classic_queue;
231232
discover(<<"stream">>) ->
232233
rabbit_stream_queue.
233234

235+
feature_flag_name(<<"quorum">>) ->
236+
quorum_queue;
237+
feature_flag_name(<<"classic">>) ->
238+
undefined;
239+
feature_flag_name(<<"stream">>) ->
240+
stream_queue;
241+
feature_flag_name(_) ->
242+
undefined.
243+
234244
default() ->
235245
rabbit_classic_queue.
236246

deps/rabbit/src/rabbit_vhost.erl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,17 @@ do_add(Name, Metadata, ActingUser) ->
166166
%% validate default_queue_type
167167
case Metadata of
168168
#{default_queue_type := DQT} ->
169+
%% check that the queue type is known
169170
try rabbit_queue_type:discover(DQT) of
170171
_ ->
171-
ok
172+
case rabbit_queue_type:feature_flag_name(DQT) of
173+
undefined -> ok;
174+
Flag when is_atom(Flag) ->
175+
case rabbit_feature_flags:is_enabled(Flag) of
176+
true -> ok;
177+
false -> throw({error, queue_type_feature_flag_is_not_enabled})
178+
end
179+
end
172180
catch _:_ ->
173181
throw({error, invalid_queue_type})
174182
end;

deps/rabbitmq_cli/lib/rabbitmq/cli/core/feature_flags.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,31 @@
55
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule RabbitMQ.CLI.Core.FeatureFlags do
8+
alias RabbitMQ.CLI.Core.ExitCodes
89

910
#
1011
# API
1112
#
1213

14+
def is_enabled_remotely(node_name, feature_flag) do
15+
case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :is_enabled, [feature_flag]) do
16+
true -> true
17+
false -> false
18+
{:error, _} = error -> error
19+
end
20+
end
21+
22+
def assert_feature_flag_enabled(node_name, feature_flag, success_fun) do
23+
case is_enabled_remotely(node_name, feature_flag) do
24+
true ->
25+
success_fun.()
26+
false ->
27+
{:error, ExitCodes.exit_dataerr(), "The #{feature_flag} feature flag is not enabled on the target node"}
28+
{:error, _} = error ->
29+
error
30+
end
31+
end
32+
1333
def feature_flag_lines(feature_flags) do
1434
feature_flags
1535
|> Enum.map(fn %{name: name, state: state} ->

deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/add_vhost_command.ex

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved.
66

77
defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do
8-
alias RabbitMQ.CLI.Core.{DocGuide, Helpers}
8+
alias RabbitMQ.CLI.Core.{DocGuide, ExitCodes, FeatureFlags, Helpers}
99

1010
@behaviour RabbitMQ.CLI.CommandBehaviour
1111

@@ -24,15 +24,29 @@ defmodule RabbitMQ.CLI.Ctl.Commands.AddVhostCommand do
2424
meta = %{description: desc,
2525
tags: parse_tags(tags),
2626
default_queue_type: default_qt}
27-
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, meta, Helpers.cli_acting_user()])
27+
# check if the respective feature flag is enabled
28+
case default_qt do
29+
"quorum" ->
30+
FeatureFlags.assert_feature_flag_enabled(node_name, :quorum_queue, fn () ->
31+
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, meta, Helpers.cli_acting_user()])
32+
end)
33+
"stream" ->
34+
FeatureFlags.assert_feature_flag_enabled(node_name, :stream_queue, fn () ->
35+
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, meta, Helpers.cli_acting_user()])
36+
end)
37+
_ ->
38+
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, meta, Helpers.cli_acting_user()])
39+
end
2840
end
2941
def run([vhost], %{node: node_name, description: desc, tags: tags}) do
3042
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, description, tags, Helpers.cli_acting_user()])
3143
end
3244
def run([vhost], %{node: node_name}) do
3345
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :add, [vhost, Helpers.cli_acting_user()])
3446
end
35-
47+
def output({:error, :invalid_queue_type}, _opts) do
48+
{:error, ExitCodes.exit_usage, "Unsupported default queue type"}
49+
end
3650
use RabbitMQ.CLI.DefaultOutput
3751

3852
def usage, do: "add_vhost <vhost> [--description <description> --tags \"<tag1>,<tag2>,<...>\" --default-queue-type <quorum|classic|stream>]"

0 commit comments

Comments
 (0)