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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use std::collections::HashMap;
use std::{env::var, thread::sleep, time::Duration};
use tc_core::{Container, Docker, Image, WaitForMessage};

const ADDITIONAL_SLEEP_PERIOD: &'static str = "DYNAMODB_ADDITIONAL_SLEEP_PERIOD";
const CONTAINER_IDENTIFIER: &'static str = "amazon/dynamodb-local";
const DEFAULT_WAIT: u64 = 2000;
const DEFAULT_TAG: &'static str = "latest";

#[derive(Debug, Default, Clone)]
pub struct DynamoDbArgs;

impl IntoIterator for DynamoDbArgs {
    type Item = String;
    type IntoIter = ::std::vec::IntoIter<String>;

    fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
        vec![].into_iter()
    }
}

#[derive(Debug)]
pub struct DynamoDb {
    tag: String,
    arguments: DynamoDbArgs,
}

impl Default for DynamoDb {
    fn default() -> Self {
        DynamoDb {
            tag: DEFAULT_TAG.to_string(),
            arguments: DynamoDbArgs {},
        }
    }
}

impl Image for DynamoDb {
    type Args = DynamoDbArgs;
    type EnvVars = HashMap<String, String>;

    fn descriptor(&self) -> String {
        format!("{}:{}", CONTAINER_IDENTIFIER, &self.tag)
    }

    fn wait_until_ready<D: Docker>(&self, container: &Container<D, Self>) {
        container
            .logs()
            .stdout
            .wait_for_message("Initializing DynamoDB Local with the following configuration")
            .unwrap();

        let additional_sleep_period = var(ADDITIONAL_SLEEP_PERIOD)
            .map(|value| value.parse().unwrap_or(DEFAULT_WAIT))
            .unwrap_or(DEFAULT_WAIT);

        let sleep_period = Duration::from_millis(additional_sleep_period);

        trace!(
            "Waiting for an additional {:?} for container {}.",
            sleep_period,
            container.id()
        );

        sleep(sleep_period)
    }

    fn args(&self) -> <Self as Image>::Args {
        self.arguments.clone()
    }

    fn env_vars(&self) -> Self::EnvVars {
        HashMap::new()
    }

    fn with_args(self, arguments: <Self as Image>::Args) -> Self {
        DynamoDb { arguments, ..self }
    }
}

impl DynamoDb {
    pub fn with_tag(self, tag_str: &str) -> Self {
        DynamoDb {
            tag: tag_str.to_string(),
            ..self
        }
    }
}