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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
use crate::core::Port;
use crate::{Container, Docker, Image, WaitForMessage};
use std::{collections::HashMap, env::var, thread::sleep, time::Duration};

const ADDITIONAL_SLEEP_PERIOD: &str = "DYNAMODB_ADDITIONAL_SLEEP_PERIOD";
const CONTAINER_IDENTIFIER: &str = "amazon/dynamodb-local";
const DEFAULT_WAIT: u64 = 2000;
const DEFAULT_TAG: &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,
    ports: Option<Vec<Port>>,
}

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

impl Image for DynamoDb {
    type Args = DynamoDbArgs;
    type EnvVars = HashMap<String, String>;
    type Volumes = HashMap<String, String>;
    type EntryPoint = std::convert::Infallible;

    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);

        log::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 volumes(&self) -> Self::Volumes {
        HashMap::new()
    }

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

    fn ports(&self) -> Option<Vec<Port>> {
        self.ports.clone()
    }

    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
        }
    }

    pub fn with_mapped_port<P: Into<Port>>(mut self, port: P) -> Self {
        let mut ports = self.ports.unwrap_or_default();
        ports.push(port.into());
        self.ports = Some(ports);
        self
    }
}