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) 2018-2021 VMware, Inc. or its affiliates.  All rights reserved.
6
7
8defmodule RabbitMQ.CLI.Ctl.Commands.EnableFeatureFlagCommand do
9  @behaviour RabbitMQ.CLI.CommandBehaviour
10
11  def merge_defaults(args, opts), do: {args, opts}
12
13  def validate([], _), do: {:validation_failure, :not_enough_args}
14  def validate([_|_] = args, _) when length(args) > 1, do: {:validation_failure, :too_many_args}
15  def validate([""], _), do: {:validation_failure, {:bad_argument, "feature_flag cannot be an empty string."}}
16  def validate([_], _), do: :ok
17
18  use RabbitMQ.CLI.Core.RequiresRabbitAppRunning
19
20  def run(["all"], %{node: node_name}) do
21    case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :enable_all, []) do
22      # Server does not support feature flags, consider none are available.
23      # See rabbitmq/rabbitmq-cli#344 for context. MK.
24      {:badrpc, {:EXIT, {:undef, _}}} -> {:error, :unsupported}
25      {:badrpc, _} = err    -> err
26      other                 -> other
27    end
28  end
29
30  def run([feature_flag], %{node: node_name}) do
31    case :rabbit_misc.rpc_call(node_name, :rabbit_feature_flags, :enable, [String.to_atom(feature_flag)]) do
32      # Server does not support feature flags, consider none are available.
33      # See rabbitmq/rabbitmq-cli#344 for context. MK.
34      {:badrpc, {:EXIT, {:undef, _}}} -> {:error, :unsupported}
35      {:badrpc, _} = err    -> err
36      other                 -> other
37    end
38  end
39
40  def output({:error, :unsupported}, %{node: node_name}) do
41    {:error, RabbitMQ.CLI.Core.ExitCodes.exit_usage, "This feature flag is not supported by node #{node_name}"}
42  end
43  use RabbitMQ.CLI.DefaultOutput
44
45  def usage, do: "enable_feature_flag <all | feature_flag>"
46
47  def usage_additional() do
48  [
49    ["<feature_flag>", "name of the feature flag to enable, or \"all\" to enable all supported flags"]
50  ]
51end
52
53  def help_section(), do: :feature_flags
54
55  def description(), do: "Enables a feature flag or all supported feature flags on the target node"
56
57  def banner(["all"], _), do: "Enabling all feature flags ..."
58
59  def banner([feature_flag], _), do: "Enabling feature flag \"#{feature_flag}\" ..."
60end
61