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
use async_trait::async_trait;
use aws_sdk_ebs as ebs;
use clap::{Args, Subcommand};

use config::Config;
use error::RawsError;

mod block;
mod snapshot;

type EbsResult<T = Box<dyn show::Show>> = std::result::Result<T, ebs::Error>;

#[async_trait]
pub trait Execute {
    async fn execute(self: Box<Self>, config: &Config) -> EbsResult;
}

trait ClientExt {
    fn client(&self) -> ebs::Client;
}

impl ClientExt for Config {
    fn client(&self) -> ebs::Client {
        ebs::Client::new(self.config())
    }
}

/// Amazon Elastic Block Store (Amazon EBS) direct API
///
#[derive(Debug, Subcommand)]
pub enum Ebs {
    CompleteSnapshot(snapshot::CompleteSnapshot),
    StartSnapshot(snapshot::StartSnapshot),
    ListSnapshotBlocks(snapshot::ListSnapshotBlocks),
    ListChangedBlocks(snapshot::ListChangedBlocks),
    GetSnapshotBlock(block::GetSnapshotBlock),
    PutSnapshotBlock(block::PutSnapshotBlock),
}

impl Ebs {
    fn boxed(self) -> Box<dyn Execute> {
        match self {
            Self::CompleteSnapshot(complete_snapshot) => Box::new(complete_snapshot),
            Self::StartSnapshot(start_snapshot) => Box::new(start_snapshot),
            Self::ListSnapshotBlocks(list_snapshot_blocks) => Box::new(list_snapshot_blocks),
            Self::ListChangedBlocks(list_changed_blocks) => Box::new(list_changed_blocks),
            Self::GetSnapshotBlock(get_snapshot_block) => Box::new(get_snapshot_block),
            Self::PutSnapshotBlock(put_snapshot_block) => Box::new(put_snapshot_block),
        }
    }

    pub async fn dispatch(self, config: Config) -> Result<(), RawsError<ebs::Error>> {
        self.boxed()
            .execute(&config)
            .await
            .map(|output| config.show(output))?;
        Ok(())
    }
}