Skip to content

Commit 15536ad

Browse files
committed
Use Integer.parse/1 to mediate and parse target-cluster-size argument in QQ grow-to-N command and update banner
1 parent d2c4599 commit 15536ad

File tree

2 files changed

+41
-21
lines changed

2 files changed

+41
-21
lines changed

deps/rabbitmq_cli/lib/rabbitmq/cli/queues/commands/grow_command.ex

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,40 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do
3333
{:validation_failure, :too_many_args}
3434
end
3535

36-
def validate([_, s], _)
36+
def validate(args = [n, s], opts) do
37+
case Integer.parse(n) do
38+
{cluster_size, _} when is_integer(cluster_size) ->
39+
do_validate([cluster_size, s], opts)
40+
41+
:error ->
42+
do_validate(args, opts)
43+
end
44+
end
45+
46+
def do_validate([_, s], _)
3747
when not (s == "all" or
3848
s == "even") do
3949
{:validation_failure, "strategy '#{s}' is not recognised."}
4050
end
4151

42-
def validate([n, _], _)
52+
def do_validate([n, _], _)
4353
when (is_integer(n) and n <= 0) do
4454
{:validation_failure, "target quorum cluster size '#{n}' must be greater than 0."}
4555
end
4656

47-
def validate([n, _], %{membership: m})
57+
def do_validate([n, _], %{membership: m})
4858
when (is_integer(n) and not (m == "voter" or m == "promotable")) do
4959
{:validation_failure, "voter status '#{m}' must be 'voter' or 'promotable' to grow to target quorum cluster size '#{n}'."}
5060
end
5161

52-
def validate(_, %{membership: m})
62+
def do_validate(_, %{membership: m})
5363
when not (m == "promotable" or
5464
m == "non_voter" or
5565
m == "voter") do
5666
{:validation_failure, "voter status '#{m}' is not recognised."}
5767
end
5868

59-
def validate(_, _) do
69+
def do_validate(_, _) do
6070
:ok
6171
end
6272

@@ -65,10 +75,12 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do
6575
[
6676
&Validators.rabbit_is_running/2,
6777
fn args = [n, _], opts ->
68-
if is_integer(n) do
69-
:ok
70-
else
71-
Validators.existing_cluster_member(args, opts)
78+
case Integer.parse(n) do
79+
{cluster_size, _} when is_integer(cluster_size) ->
80+
:ok
81+
82+
:error ->
83+
Validators.existing_cluster_member(args, opts)
7284
end
7385
end
7486
],
@@ -85,10 +97,12 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do
8597
}) do
8698

8799
node_or_quorum_cluster_size =
88-
if is_integer(node_or_quorum_cluster_size) do
89-
node_or_quorum_cluster_size
90-
else
91-
to_atom(node_or_quorum_cluster_size)
100+
case Integer.parse(node_or_quorum_cluster_size) do
101+
{cluster_size, _} when is_integer(cluster_size) ->
102+
cluster_size
103+
104+
:error ->
105+
to_atom(node_or_quorum_cluster_size)
92106
end
93107

94108
args = [node_or_quorum_cluster_size, vhost_pat, queue_pat, to_atom(strategy)]
@@ -160,8 +174,14 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommand do
160174
do:
161175
"Grows quorum queue clusters by adding a member (replica) on the specified node for all matching queues"
162176

163-
def banner([node, strategy], _) do
164-
"Growing #{strategy} quorum queues on #{node}..."
177+
def banner([node_or_quorum_cluster_size, strategy], %{queue_pattern: queue_pattern}) do
178+
case Integer.parse(node_or_quorum_cluster_size) do
179+
{cluster_size, _} when is_integer(cluster_size) ->
180+
"Growing #{strategy} quorum queues matching '#{queue_pattern}' to a target cluster size of '#{cluster_size}'..."
181+
182+
:error ->
183+
"Growing #{strategy} quorum queues matching '#{queue_pattern}' to #{node_or_quorum_cluster_size}..."
184+
end
165185
end
166186

167187
#

deps/rabbitmq_cli/test/queues/grow_command_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,25 +83,25 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommandTest do
8383
end
8484

8585
test "validate: when target quorum cluster size greater than zero and membership is voter, returns a success" do
86-
assert @command.validate([7, "all"], %{membership: "voter", queue_pattern: "qq.*"}) == :ok
86+
assert @command.validate(["7", "all"], %{membership: "voter", queue_pattern: "qq.*"}) == :ok
8787
end
8888

8989
test "validate: when target quorum cluster size greater than zero and membership is promotable, returns a success" do
90-
assert @command.validate([5, "all"], %{membership: "promotable", queue_pattern: "qq.*"}) == :ok
90+
assert @command.validate(["5", "all"], %{membership: "promotable", queue_pattern: "qq.*"}) == :ok
9191
end
9292

9393
test "validate: when target quorum cluster size is zero, returns failure" do
94-
assert @command.validate([0, "all"], %{membership: "voter", queue_pattern: "qq.*"}) ==
94+
assert @command.validate(["0", "all"], %{membership: "voter", queue_pattern: "qq.*"}) ==
9595
{:validation_failure, "target quorum cluster size '0' must be greater than 0."}
9696
end
9797

9898
test "validate: when target quorum cluster size is less than zero, returns failure" do
99-
assert @command.validate([-1, "all"], %{membership: "voter", queue_pattern: "qq.*"}) ==
99+
assert @command.validate(["-1", "all"], %{membership: "voter", queue_pattern: "qq.*"}) ==
100100
{:validation_failure, "target quorum cluster size '-1' must be greater than 0."}
101101
end
102102

103103
test "validate: when target quorum cluster size is provided and membership is not voter, returns failure" do
104-
assert @command.validate([5, "all"], %{membership: "non_voter", queue_pattern: "qq.*"}) ==
104+
assert @command.validate(["5", "all"], %{membership: "non_voter", queue_pattern: "qq.*"}) ==
105105
{:validation_failure, "voter status 'non_voter' must be 'voter' or 'promotable' to grow to target quorum cluster size '5'."}
106106
end
107107

@@ -121,7 +121,7 @@ defmodule RabbitMQ.CLI.Queues.Commands.GrowCommandTest do
121121
assert match?(
122122
{:badrpc, _},
123123
@command.run(
124-
[5, "all"],
124+
["5", "all"],
125125
Map.merge(context[:opts], %{node: :jake@thedog, queue_pattern: "qq.*"})
126126
)
127127
)

0 commit comments

Comments
 (0)