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-2021 VMware, Inc. or its affiliates.  All rights reserved.
6%%
7
8-module('Elixir.RabbitMQ.CLI.Ctl.Commands.DeleteShovelCommand').
9
10-include("rabbit_shovel.hrl").
11
12-behaviour('Elixir.RabbitMQ.CLI.CommandBehaviour').
13
14-ignore_xref([
15    {'Elixir.RabbitMQ.CLI.DefaultOutput', output, 1},
16    {'Elixir.RabbitMQ.CLI.Core.Helpers', cli_acting_user, 0}
17]).
18
19-export([
20         usage/0,
21         usage_additional/0,
22         usage_doc_guides/0,
23         validate/2,
24         merge_defaults/2,
25         banner/2,
26         run/2,
27         switches/0,
28         aliases/0,
29         output/2,
30         help_section/0,
31         description/0
32        ]).
33
34
35%%----------------------------------------------------------------------------
36%% Callbacks
37%%----------------------------------------------------------------------------
38usage() ->
39    <<"delete_shovel [--vhost <vhost>] <name>">>.
40
41usage_additional() ->
42    [
43      {<<"<name>">>, <<"Shovel to delete">>}
44    ].
45
46usage_doc_guides() ->
47    [?SHOVEL_GUIDE_URL].
48
49description() ->
50    <<"Deletes a Shovel">>.
51
52help_section() ->
53    {plugin, shovel}.
54
55validate([], _Opts) ->
56    {validation_failure, not_enough_args};
57validate([_, _ | _], _Opts) ->
58    {validation_failure, too_many_args};
59validate([_], _Opts) ->
60    ok.
61
62merge_defaults(A, Opts) ->
63    {A, maps:merge(#{vhost => <<"/">>}, Opts)}.
64
65banner([Name], #{vhost := VHost}) ->
66    erlang:list_to_binary(io_lib:format("Deleting shovel ~s in vhost ~s",
67                                        [Name, VHost])).
68
69run([Name], #{node := Node, vhost := VHost}) ->
70    ActingUser = 'Elixir.RabbitMQ.CLI.Core.Helpers':cli_acting_user(),
71    case rabbit_misc:rpc_call(Node, rabbit_shovel_util, delete_shovel, [VHost, Name, ActingUser]) of
72        {badrpc, _} = Error ->
73            Error;
74        {error, not_found} ->
75            ErrMsg = rabbit_misc:format("Shovel with the given name was not found "
76                                        "on the target node '~s' and / or virtual host '~s'",
77                                        [Node, VHost]),
78            {error, rabbit_data_coercion:to_binary(ErrMsg)};
79        ok -> ok
80    end.
81
82switches() ->
83    [].
84
85aliases() ->
86    [].
87
88output(E, _Opts) ->
89    'Elixir.RabbitMQ.CLI.DefaultOutput':output(E).
90