use crate::command::{CommandExecutor, CommandOutput, DockerCommand};
use crate::error::Result;
use async_trait::async_trait;
use std::collections::HashMap;
#[derive(Debug, Clone)]
#[allow(clippy::struct_excessive_bools)]
pub struct NetworkCreateCommand {
name: String,
driver: Option<String>,
driver_opts: HashMap<String, String>,
subnet: Option<String>,
ip_range: Option<String>,
gateway: Option<String>,
ipv6: bool,
attachable: bool,
internal: bool,
labels: HashMap<String, String>,
scope: Option<String>,
config_from: Option<String>,
config_only: bool,
ingress: bool,
aux_addresses: HashMap<String, String>,
pub executor: CommandExecutor,
}
impl NetworkCreateCommand {
#[must_use]
pub fn new(name: impl Into<String>) -> Self {
Self {
name: name.into(),
driver: None,
driver_opts: HashMap::new(),
subnet: None,
ip_range: None,
gateway: None,
ipv6: false,
attachable: false,
internal: false,
labels: HashMap::new(),
scope: None,
config_from: None,
config_only: false,
ingress: false,
aux_addresses: HashMap::new(),
executor: CommandExecutor::new(),
}
}
#[must_use]
pub fn driver(mut self, driver: impl Into<String>) -> Self {
self.driver = Some(driver.into());
self
}
#[must_use]
pub fn driver_opt(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.driver_opts.insert(key.into(), value.into());
self
}
#[must_use]
pub fn subnet(mut self, subnet: impl Into<String>) -> Self {
self.subnet = Some(subnet.into());
self
}
#[must_use]
pub fn ip_range(mut self, range: impl Into<String>) -> Self {
self.ip_range = Some(range.into());
self
}
#[must_use]
pub fn gateway(mut self, gateway: impl Into<String>) -> Self {
self.gateway = Some(gateway.into());
self
}
#[must_use]
pub fn ipv6(mut self) -> Self {
self.ipv6 = true;
self
}
#[must_use]
pub fn attachable(mut self) -> Self {
self.attachable = true;
self
}
#[must_use]
pub fn internal(mut self) -> Self {
self.internal = true;
self
}
#[must_use]
pub fn label(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.labels.insert(key.into(), value.into());
self
}
#[must_use]
pub fn scope(mut self, scope: impl Into<String>) -> Self {
self.scope = Some(scope.into());
self
}
#[must_use]
pub fn config_from(mut self, network: impl Into<String>) -> Self {
self.config_from = Some(network.into());
self
}
#[must_use]
pub fn config_only(mut self) -> Self {
self.config_only = true;
self
}
#[must_use]
pub fn ingress(mut self) -> Self {
self.ingress = true;
self
}
#[must_use]
pub fn aux_address(mut self, name: impl Into<String>, ip: impl Into<String>) -> Self {
self.aux_addresses.insert(name.into(), ip.into());
self
}
pub async fn run(&self) -> Result<NetworkCreateResult> {
self.execute().await.map(NetworkCreateResult::from)
}
}
#[async_trait]
impl DockerCommand for NetworkCreateCommand {
type Output = CommandOutput;
fn build_command_args(&self) -> Vec<String> {
let mut args = vec!["network".to_string(), "create".to_string()];
if let Some(ref driver) = self.driver {
args.push("--driver".to_string());
args.push(driver.clone());
}
for (key, value) in &self.driver_opts {
args.push("--opt".to_string());
args.push(format!("{key}={value}"));
}
if let Some(ref subnet) = self.subnet {
args.push("--subnet".to_string());
args.push(subnet.clone());
}
if let Some(ref ip_range) = self.ip_range {
args.push("--ip-range".to_string());
args.push(ip_range.clone());
}
if let Some(ref gateway) = self.gateway {
args.push("--gateway".to_string());
args.push(gateway.clone());
}
if self.ipv6 {
args.push("--ipv6".to_string());
}
if self.attachable {
args.push("--attachable".to_string());
}
if self.internal {
args.push("--internal".to_string());
}
for (key, value) in &self.labels {
args.push("--label".to_string());
args.push(format!("{key}={value}"));
}
if let Some(ref scope) = self.scope {
args.push("--scope".to_string());
args.push(scope.clone());
}
if let Some(ref config_from) = self.config_from {
args.push("--config-from".to_string());
args.push(config_from.clone());
}
if self.config_only {
args.push("--config-only".to_string());
}
if self.ingress {
args.push("--ingress".to_string());
}
for (name, ip) in &self.aux_addresses {
args.push("--aux-address".to_string());
args.push(format!("{name}={ip}"));
}
args.push(self.name.clone());
args.extend(self.executor.raw_args.clone());
args
}
fn get_executor(&self) -> &CommandExecutor {
&self.executor
}
fn get_executor_mut(&mut self) -> &mut CommandExecutor {
&mut self.executor
}
async fn execute(&self) -> Result<Self::Output> {
let args = self.build_command_args();
let command_name = args[0].clone();
let command_args = args[1..].to_vec();
self.executor
.execute_command(&command_name, command_args)
.await
}
}
#[derive(Debug, Clone)]
pub struct NetworkCreateResult {
pub network_id: String,
pub raw_output: CommandOutput,
}
impl From<CommandOutput> for NetworkCreateResult {
fn from(output: CommandOutput) -> Self {
let network_id = output.stdout.trim().to_string();
Self {
network_id,
raw_output: output,
}
}
}
impl NetworkCreateResult {
#[must_use]
pub fn is_success(&self) -> bool {
self.raw_output.success
}
#[must_use]
pub fn id(&self) -> &str {
&self.network_id
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_network_create_basic() {
let cmd = NetworkCreateCommand::new("my-network");
let args = cmd.build_command_args();
assert_eq!(args, vec!["network", "create", "my-network"]);
}
#[test]
fn test_network_create_with_driver() {
let cmd = NetworkCreateCommand::new("my-network").driver("overlay");
let args = cmd.build_command_args();
assert_eq!(
args,
vec!["network", "create", "--driver", "overlay", "my-network"]
);
}
#[test]
fn test_network_create_with_subnet() {
let cmd = NetworkCreateCommand::new("my-network")
.subnet("172.20.0.0/16")
.gateway("172.20.0.1");
let args = cmd.build_command_args();
assert_eq!(
args,
vec![
"network",
"create",
"--subnet",
"172.20.0.0/16",
"--gateway",
"172.20.0.1",
"my-network"
]
);
}
#[test]
fn test_network_create_all_options() {
let cmd = NetworkCreateCommand::new("my-network")
.driver("bridge")
.driver_opt("com.docker.network.bridge.name", "br0")
.subnet("172.20.0.0/16")
.ip_range("172.20.240.0/20")
.gateway("172.20.0.1")
.ipv6()
.attachable()
.internal()
.label("env", "test")
.aux_address("host1", "172.20.0.5");
let args = cmd.build_command_args();
assert!(args.contains(&"--driver".to_string()));
assert!(args.contains(&"--ipv6".to_string()));
assert!(args.contains(&"--attachable".to_string()));
assert!(args.contains(&"--internal".to_string()));
}
}