use crate::{models::Ipam, Error, Result};
use containers_api::opts::{Filter, FilterItem};
use containers_api::{
impl_field, impl_filter_func, impl_map_field, impl_opts_builder, impl_str_field, impl_vec_field,
};
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_item(&self) -> FilterItem {
use NetworkFilter::*;
match &self {
Dangling(dangling) => FilterItem::new("dangling", dangling.to_string()),
Driver(driver) => FilterItem::new("driver", driver.to_owned()),
Id(id) => FilterItem::new("id", id.to_owned()),
LabelKey(key) => FilterItem::new("label", key.to_owned()),
LabelKeyVal(key, val) => FilterItem::new("label", format!("{key}={val}")),
Name(name) => FilterItem::new("name", name.to_owned()),
Scope(scope) => FilterItem::new("scope", scope.as_ref().to_owned()),
Type(type_) => FilterItem::new("type", type_.as_ref().to_owned()),
}
}
}
impl NetworkListOptsBuilder {
impl_filter_func!(
NetworkFilter
);
}
#[derive(Serialize, Debug, Clone)]
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)
}
pub fn serialize_vec(&self) -> Result<Vec<u8>> {
serde_json::to_vec(&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 => "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 => "Options"
);
impl_map_field!(json
labels => "Labels"
);
impl_field!(
ipam: Ipam => "IPAM"
);
pub fn build(&self) -> NetworkCreateOpts {
NetworkCreateOpts {
params: self.params.clone(),
}
}
}
#[derive(Serialize, Debug)]
pub struct ContainerDisconnectionOpts {
params: HashMap<&'static str, Value>,
}
impl ContainerDisconnectionOpts {
pub fn serialize(&self) -> Result<String> {
serde_json::to_string(&self.params).map_err(Error::from)
}
pub fn serialize_vec(&self) -> Result<Vec<u8>> {
serde_json::to_vec(&self.params).map_err(Error::from)
}
pub fn builder<I>(container_id: I) -> ContainerDisconnectionOptsBuilder
where
I: AsRef<str>,
{
ContainerDisconnectionOptsBuilder::new(container_id.as_ref())
}
}
#[derive(Default)]
pub struct ContainerDisconnectionOptsBuilder {
params: HashMap<&'static str, Value>,
}
impl ContainerDisconnectionOptsBuilder {
pub(crate) fn new(container_id: &str) -> Self {
ContainerDisconnectionOptsBuilder {
params: [("Container", json!(container_id.to_string()))].into(),
}
}
impl_field!(
force: bool => "Force"
);
pub fn build(self) -> ContainerDisconnectionOpts {
ContainerDisconnectionOpts {
params: self.params,
}
}
}
#[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 serialize_vec(&self) -> Result<Vec<u8>> {
serde_json::to_vec(&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,
}
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) -> Self {
self.params.insert("EndpointConfig", json!(config.params));
self
}
impl_vec_field!(aliases => "Aliases");
impl_vec_field!(links => "Links");
impl_str_field!(
network_id => "NetworkID"
);
impl_str_field!(
endpoint_id => "EndpointID"
);
impl_str_field!(
gateway => "Gateway"
);
impl_str_field!(
ipv4 => "IPAddress"
);
impl_field!(
prefix_len: isize => "IPPrefixLen"
);
impl_str_field!(
ipv6_gateway => "IPv6Gateway"
);
impl_str_field!(
ipv6 => "GlobalIPv6Address"
);
impl_field!(
ipv6_prefix_len: i64 => "GlobalIPv6PrefixLen"
);
impl_str_field!(
mac => "MacAddress"
);
impl_map_field!(json
driver_opts => "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 }
}
}
#[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_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_item(&self) -> FilterItem {
use NetworkPruneFilter::*;
match &self {
Until(until) => FilterItem::new("until", until.to_owned()),
#[cfg(feature = "chrono")]
UntilDate(until) => FilterItem::new("until", until.timestamp().to_string()),
LabelKey(label) => FilterItem::new("label", label.to_owned()),
Label(key, val) => FilterItem::new("label", format!("{key}={val}")),
}
}
}
impl NetworkPruneOptsBuilder {
impl_filter_func!(
NetworkPruneFilter
);
}