-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
278 additions
and
7 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/disable_vhost_deletion_protection_command.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
defmodule RabbitMQ.CLI.Ctl.Commands.DisableVhostDeletionProtectionCommand do | ||
alias RabbitMQ.CLI.Core.{DocGuide, Helpers} | ||
|
||
@behaviour RabbitMQ.CLI.CommandBehaviour | ||
|
||
@metadata_key :protected_from_deletion | ||
|
||
def switches(), do: [] | ||
def aliases(), do: [] | ||
|
||
def merge_defaults(args, opts) do | ||
{args, opts} | ||
end | ||
|
||
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning | ||
use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument | ||
|
||
def run([vhost], %{node: node_name}) do | ||
metadata_patch = %{ | ||
@metadata_key => false | ||
} | ||
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :update_metadata, [ | ||
vhost, | ||
metadata_patch, | ||
Helpers.cli_acting_user() | ||
]) | ||
end | ||
|
||
use RabbitMQ.CLI.DefaultOutput | ||
|
||
def usage, | ||
do: | ||
"disable_vhost_deletion_protection <vhost>" | ||
|
||
def usage_additional() do | ||
[ | ||
["<vhost>", "Virtual host name"] | ||
] | ||
end | ||
|
||
def usage_doc_guides() do | ||
[ | ||
DocGuide.virtual_hosts() | ||
] | ||
end | ||
|
||
def help_section(), do: :virtual_hosts | ||
|
||
def description(), do: "Removes deletion protection from a virtual host (so that it can be deleted)" | ||
|
||
def banner([vhost], _), do: "Removing deletion protection from virtual host \"#{vhost}\" by updating its metadata..." | ||
end |
58 changes: 58 additions & 0 deletions
58
deps/rabbitmq_cli/lib/rabbitmq/cli/ctl/commands/enable_vhost_deletion_protection_command.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
defmodule RabbitMQ.CLI.Ctl.Commands.EnableVhostDeletionProtectionCommand do | ||
alias RabbitMQ.CLI.Core.{DocGuide, Helpers} | ||
|
||
@behaviour RabbitMQ.CLI.CommandBehaviour | ||
|
||
@metadata_key :protected_from_deletion | ||
|
||
def switches(), do: [] | ||
def aliases(), do: [] | ||
|
||
def merge_defaults(args, opts) do | ||
{args, opts} | ||
end | ||
|
||
use RabbitMQ.CLI.Core.RequiresRabbitAppRunning | ||
use RabbitMQ.CLI.Core.AcceptsOnePositionalArgument | ||
|
||
def run([vhost], %{node: node_name}) do | ||
metadata_patch = %{ | ||
@metadata_key => true | ||
} | ||
:rabbit_misc.rpc_call(node_name, :rabbit_vhost, :update_metadata, [ | ||
vhost, | ||
metadata_patch, | ||
Helpers.cli_acting_user() | ||
]) | ||
end | ||
|
||
use RabbitMQ.CLI.DefaultOutput | ||
|
||
def usage, | ||
do: | ||
"enable_vhost_deletion_protection <vhost>" | ||
|
||
def usage_additional() do | ||
[ | ||
["<vhost>", "Virtual host name"] | ||
] | ||
end | ||
|
||
def usage_doc_guides() do | ||
[ | ||
DocGuide.virtual_hosts() | ||
] | ||
end | ||
|
||
def help_section(), do: :virtual_hosts | ||
|
||
def description(), do: "Protects a virtual host from deletion (until the protection is removed)" | ||
|
||
def banner([vhost], _), do: "Protecting virtual host \"#{vhost}\" from removal by updating its metadata..." | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
deps/rabbitmq_cli/test/ctl/disable_vhost_deletion_protection_command_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
## 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-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
defmodule DisableVhostDeletionProtectionCommandTest do | ||
use ExUnit.Case, async: false | ||
import TestHelper | ||
|
||
@command RabbitMQ.CLI.Ctl.Commands.DisableVhostDeletionProtectionCommand | ||
@inverse_command RabbitMQ.CLI.Ctl.Commands.EnableVhostDeletionProtectionCommand | ||
@vhost "disable-vhost-deletion-protection" | ||
|
||
setup_all do | ||
RabbitMQ.CLI.Core.Distribution.start() | ||
{:ok, opts: %{node: get_rabbit_hostname()}} | ||
end | ||
|
||
setup context do | ||
on_exit(context, fn -> delete_vhost(context[:vhost]) end) | ||
:ok | ||
end | ||
|
||
test "validate: no arguments fails validation" do | ||
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} | ||
end | ||
|
||
test "validate: too many arguments fails validation" do | ||
assert @command.validate(["test", "extra"], %{}) == {:validation_failure, :too_many_args} | ||
end | ||
|
||
test "validate: virtual host name without options fails validation" do | ||
assert @command.validate(["a-vhost"], %{}) == :ok | ||
end | ||
|
||
test "run: enabling deletion protection succeeds", context do | ||
_ = @command.run([@vhost], context[:opts]) | ||
delete_vhost(@vhost) | ||
add_vhost(@vhost) | ||
|
||
assert @inverse_command.run([@vhost], context[:opts]) == :ok | ||
vh = find_vhost(@vhost) | ||
assert vh[:protected_from_deletion] | ||
|
||
assert @command.run([@vhost], context[:opts]) == :ok | ||
vh = find_vhost(@vhost) | ||
assert !vh[:protected_from_deletion] | ||
|
||
delete_vhost(@vhost) | ||
end | ||
|
||
test "run: attempt to use a non-existent virtual host fails", context do | ||
vh = "a-non-existent-3882-vhost" | ||
|
||
assert match?( | ||
{:error, {:no_such_vhost, _}}, | ||
@command.run([vh], Map.merge(context[:opts], %{})) | ||
) | ||
end | ||
|
||
test "run: attempt to use an unreachable node returns a nodedown" do | ||
opts = %{node: :jake@thedog, timeout: 200, description: "does not matter"} | ||
assert match?({:badrpc, _}, @command.run(["na"], opts)) | ||
end | ||
|
||
test "banner", context do | ||
assert @command.banner([@vhost], context[:opts]) =~ | ||
~r/Removing deletion protection/ | ||
end | ||
end |
69 changes: 69 additions & 0 deletions
69
deps/rabbitmq_cli/test/ctl/enable_vhost_deletion_protection_command_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
## 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-2025 Broadcom. All Rights Reserved. The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. All rights reserved. | ||
|
||
defmodule EnableVhostDeletionProtectionCommandTest do | ||
use ExUnit.Case, async: false | ||
import TestHelper | ||
|
||
@command RabbitMQ.CLI.Ctl.Commands.EnableVhostDeletionProtectionCommand | ||
@inverse_command RabbitMQ.CLI.Ctl.Commands.DisableVhostDeletionProtectionCommand | ||
@vhost "enable-vhost-deletion-protection" | ||
|
||
setup_all do | ||
RabbitMQ.CLI.Core.Distribution.start() | ||
{:ok, opts: %{node: get_rabbit_hostname()}} | ||
end | ||
|
||
setup context do | ||
on_exit(context, fn -> delete_vhost(context[:vhost]) end) | ||
:ok | ||
end | ||
|
||
test "validate: no arguments fails validation" do | ||
assert @command.validate([], %{}) == {:validation_failure, :not_enough_args} | ||
end | ||
|
||
test "validate: too many arguments fails validation" do | ||
assert @command.validate(["test", "extra"], %{}) == {:validation_failure, :too_many_args} | ||
end | ||
|
||
test "validate: virtual host name without options fails validation" do | ||
assert @command.validate(["a-vhost"], %{}) == :ok | ||
end | ||
|
||
test "run: enabling deletion protection succeeds", context do | ||
add_vhost(@vhost) | ||
|
||
assert @command.run([@vhost], context[:opts]) == :ok | ||
vh = find_vhost(@vhost) | ||
assert vh[:protected_from_deletion] | ||
|
||
assert @inverse_command.run([@vhost], context[:opts]) == :ok | ||
vh = find_vhost(@vhost) | ||
assert !vh[:protected_from_deletion] | ||
|
||
delete_vhost(@vhost) | ||
end | ||
|
||
test "run: attempt to use a non-existent virtual host fails", context do | ||
vh = "a-non-existent-3882-vhost" | ||
|
||
assert match?( | ||
{:error, {:no_such_vhost, _}}, | ||
@command.run([vh], Map.merge(context[:opts], %{})) | ||
) | ||
end | ||
|
||
test "run: attempt to use an unreachable node returns a nodedown" do | ||
opts = %{node: :jake@thedog, timeout: 200, description: "does not matter"} | ||
assert match?({:badrpc, _}, @command.run(["na"], opts)) | ||
end | ||
|
||
test "banner", context do | ||
assert @command.banner([@vhost], context[:opts]) =~ | ||
~r/Protecting virtual host/ | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters