use crate::{
Container, ExposedPort, ImageName, Port, PortError, RunnableContainer,
RunnableContainerBuilder, ToRunnableContainer, WaitStrategy,
};
const MONGO_IMAGE: &ImageName = &ImageName::new("docker.io/mongo");
const PORT: Port = Port(27017);
#[derive(Debug)]
pub struct Mongo {
image: ImageName,
port: ExposedPort,
}
impl Mongo {
#[must_use]
pub fn with_tag(self, tag: impl Into<String>) -> Self {
let Self { mut image, .. } = self;
image.set_tag(tag);
Self { image, ..self }
}
#[must_use]
pub fn with_digest(self, digest: impl Into<String>) -> Self {
let Self { mut image, .. } = self;
image.set_digest(digest);
Self { image, ..self }
}
#[must_use]
pub fn with_port(mut self, port: ExposedPort) -> Self {
self.port = port;
self
}
}
impl Container<Mongo> {
pub async fn endpoint(&self) -> Result<String, PortError> {
let port = self.port.host_port().await?;
let host_ip = self.runner.container_host_ip().await?;
let url = format!("mongodb://{host_ip}:{port}");
Ok(url)
}
}
impl Default for Mongo {
fn default() -> Self {
Self {
image: MONGO_IMAGE.clone(),
port: ExposedPort::new(PORT),
}
}
}
impl ToRunnableContainer for Mongo {
fn to_runnable(&self, builder: RunnableContainerBuilder) -> RunnableContainer {
builder
.with_image(self.image.clone())
.with_wait_strategy(WaitStrategy::stdout_contains("Waiting for connections"))
.with_port_mappings([self.port.clone()])
.build()
}
}