1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use clap::builder::NonEmptyStringValueParser;
use clap::Args;
use colorful::Colorful;

use ockam::Context;
use ockam_api::cloud::addon::Addons;
use ockam_api::nodes::InMemoryNode;

use crate::operation::util::check_for_operation_completion;
use crate::util::node_rpc;
use crate::{fmt_ok, CommandGlobalOpts};

/// Disable an addon for a project
#[derive(Clone, Debug, Args)]
pub struct AddonDisableSubcommand {
    /// Project name
    #[arg(
        long = "project",
        id = "project",
        value_name = "PROJECT_NAME",
        value_parser(NonEmptyStringValueParser::new())
    )]
    project_name: String,

    /// Addon id/name
    #[arg(
        long = "addon",
        id = "addon",
        value_name = "ADDON_ID",
        value_parser(NonEmptyStringValueParser::new())
    )]
    addon_id: String,
}

impl AddonDisableSubcommand {
    pub fn run(self, opts: CommandGlobalOpts) {
        node_rpc(run_impl, (opts, self));
    }
}

async fn run_impl(
    ctx: Context,
    (opts, cmd): (CommandGlobalOpts, AddonDisableSubcommand),
) -> miette::Result<()> {
    let AddonDisableSubcommand {
        project_name,
        addon_id,
    } = cmd;
    let project_id = &opts.state.get_project_by_name(&project_name).await?.id();
    let node = InMemoryNode::start(&ctx, &opts.state).await?;
    let controller = node.create_controller().await?;

    let response = controller
        .disable_addon(&ctx, project_id, &addon_id)
        .await?;
    let operation_id = response.operation_id;
    check_for_operation_completion(&opts, &ctx, &node, &operation_id, "the addon disabling")
        .await?;

    opts.terminal
        .write_line(&fmt_ok!("Addon disabled successfully"))?;
    Ok(())
}