use crate::{api::Filter, Error, Result};
use std::{collections::HashMap, convert::AsRef};
use serde::Serialize;
use serde_json::{json, Value};
impl_opts_builder!(url =>
NetworkList
);
pub enum Scope {
Swarm,
Global,
Local,
}
impl AsRef<str> for Scope {
fn as_ref(&self) -> &str {
match &self {
Scope::Swarm => "swarm",
Scope::Global => "global",
Scope::Local => "local",
}
}
}
pub enum NetworkType {
Custom,
Builtin,
}
impl AsRef<str> for NetworkType {
fn as_ref(&self) -> &str {
match &self {
NetworkType::Custom => "custom",
NetworkType::Builtin => "builtin",
}
}
}
pub enum NetworkFilter {
Dangling(bool),
Driver(String),
Id(String),
LabelKey(String),
LabelKeyVal(String, String),
Name(String),
Scope(Scope),
Type(NetworkType),
}
impl Filter for NetworkFilter {
fn query_key_val(&self) -> (&'static str, String) {
use NetworkFilter::*;
match &self {
Dangling(dangling) => ("dangling", dangling.to_string()),
Driver(driver) => ("driver", driver.to_owned()),
Id(id) => ("id", id.to_owned()),
LabelKey(key) => ("label", key.to_owned()),
LabelKeyVal(key, val) => ("label", format!("{}={}", key, val)),
Name(name) => ("name", name.to_owned()),
Scope(scope) => ("scope", scope.as_ref().to_owned()),
Type(type_) => ("type", type_.as_ref().to_owned()),
}
}
}
impl NetworkListOptsBuilder {
impl_filter_func!(
NetworkFilter
);
}
#[derive(Serialize, Debug)]
pub struct NetworkCreateOpts {
params: HashMap<&'static str, Value>,
}
impl NetworkCreateOpts {
pub fn builder<N>(name: N) -> NetworkCreateOptsBuilder
where
N: AsRef<str>,
{
NetworkCreateOptsBuilder::new(name.as_ref())
}
pub fn serialize(&self) -> Result<String> {
serde_json::to_string(&self.params).map_err(Error::from)
}
}
#[derive(Default)]
pub struct NetworkCreateOptsBuilder {
params: HashMap<&'static str, Value>,
}
impl NetworkCreateOptsBuilder {
pub(crate) fn new(name: &str) -> Self {
let mut params = HashMap::new();
params.insert("Name", json!(name));
NetworkCreateOptsBuilder { params }
}
impl_field!(
check_duplicate: bool => "CheckDuplicate"
);
impl_str_field!(
driver: D => "Driver"
);
impl_field!(
internal: bool => "Internal"
);
impl_field!(
attachable: bool => "Attachable"
);
impl_field!(
ingress: bool => "Ingress"
);
impl_field!(
enable_ipv6: bool => "EnableIPv6"
);
impl_map_field!(json
options: O => "Options"
);
impl_map_field!(json
labels: L => "Labels"
);
pub fn build(&self) -> NetworkCreateOpts {
NetworkCreateOpts {
params: self.params.clone(),
}
}
}
#[derive(Serialize, Debug)]
pub struct ContainerConnectionOpts {
params: HashMap<&'static str, Value>,
}
impl ContainerConnectionOpts {
pub fn serialize(&self) -> Result<String> {
serde_json::to_string(&self.params).map_err(Error::from)
}
pub fn builder<I>(container_id: I) -> ContainerConnectionOptsBuilder
where
I: AsRef<str>,
{
ContainerConnectionOptsBuilder::new(container_id.as_ref())
}
}
#[derive(Default)]
pub struct ContainerConnectionOptsBuilder {
params: HashMap<&'static str, Value>,
container: String,
}
#[derive(Default)]
pub struct EndpointIpamConfig {
params: HashMap<&'static str, serde_json::Value>,
}
impl EndpointIpamConfig {
pub fn new() -> Self {
Self::default()
}
pub fn ipv4<A>(mut self, address: A) -> Self
where
A: Into<String>,
{
self.params.insert("IPv4Address", json!(address.into()));
self
}
pub fn ipv6<A>(mut self, address: A) -> Self
where
A: Into<String>,
{
self.params.insert("IPv6Address", json!(address.into()));
self
}
pub fn link_local_ips<I>(mut self, ips: I) -> Self
where
I: IntoIterator,
I::Item: Into<String>,
{
self.params.insert(
"LinkLocalIPs",
json!(ips.into_iter().map(I::Item::into).collect::<Vec<_>>()),
);
self
}
}
impl ContainerConnectionOptsBuilder {
pub(crate) fn new(container_id: &str) -> Self {
ContainerConnectionOptsBuilder {
params: HashMap::new(),
container: container_id.to_string(),
}
}
pub fn ipam_config(&mut self, config: EndpointIpamConfig) -> &mut Self {
self.params.insert("EndpointConfig", json!(config.params));
self
}
impl_vec_field!(aliases: A => "Aliases");
impl_vec_field!(links: L => "Links");
impl_str_field!(
network_id: I => "NetworkID"
);
impl_str_field!(
endpoint_id: I => "EndpointID"
);
impl_str_field!(
gateway: G => "Gateway"
);
impl_str_field!(
ipv4: A => "IPAddress"
);
impl_field!(
prefix_len: isize => "IPPrefixLen"
);
impl_str_field!(
ipv6_gateway: G => "IPv6Gateway"
);
impl_str_field!(
ipv6: A => "GlobalIPv6Address"
);
impl_field!(
ipv6_prefix_len: i64 => "GlobalIPv6PrefixLen"
);
impl_str_field!(
mac: M => "MacAddress"
);
impl_map_field!(json
driver_opts: O => "DriverOpts"
);
pub fn build(&self) -> ContainerConnectionOpts {
let mut params = HashMap::new();
params.insert("EndpointConfig", json!(self.params));
params.insert("Container", json!(self.container));
ContainerConnectionOpts {
params: self.params.clone(),
}
}
}
impl_opts_builder!(url => NetworkPrune);
pub enum NetworkPruneFilter {
Until(String),
#[cfg(feature = "chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
UntilDate(chrono::DateTime<chrono::Utc>),
LabelKey(String),
Label(String, String),
}
impl Filter for NetworkPruneFilter {
fn query_key_val(&self) -> (&'static str, String) {
use NetworkPruneFilter::*;
match &self {
Until(until) => ("until", until.to_owned()),
#[cfg(feature = "chrono")]
UntilDate(until) => ("until", until.timestamp().to_string()),
LabelKey(label) => ("label", label.to_owned()),
Label(key, val) => ("label", format!("{}={}", key, val)),
}
}
}
impl NetworkPruneOptsBuilder {
impl_filter_func!(
NetworkPruneFilter
);
}