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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
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 =>
    /// Options for filtering networks list results"
    NetworkList
);

/// Used for [`NetworkFilter::Scope`](NetworkFilter::Scope).
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",
        }
    }
}

/// A single filter item used to filter the output of listing the networks.
pub enum NetworkFilter {
    /// When set to true (or 1), returns all networks that are not in use by a container.
    /// When set to false (or 0), only networks that are in use by one or more containers are returned.
    Dangling(bool),
    /// Matches a network's driver.
    Driver(String),
    /// Matches all or part of a network ID.
    Id(String),
    /// Label in the form of `label=key`
    LabelKey(String),
    /// Label in the form of `label=key=val`
    LabelKeyVal(String, String),
    /// Matches all or part of a network name.
    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!(
        /// Filter the list of networks by one of the variants of the filter.
        NetworkFilter
    );
}

/// Interface for creating new docker network
#[derive(Serialize, Debug, Clone)]
pub struct NetworkCreateOpts {
    params: HashMap<&'static str, Value>,
}

impl NetworkCreateOpts {
    /// Return a new instance of a opts-builder for creating a network.
    pub fn builder<N>(name: N) -> NetworkCreateOptsBuilder
    where
        N: AsRef<str>,
    {
        NetworkCreateOptsBuilder::new(name.as_ref())
    }

    /// Serializes the options as a JSON string.
    pub fn serialize(&self) -> Result<String> {
        serde_json::to_string(&self.params).map_err(Error::from)
    }

    /// Serializes the options as a JSON bytes.
    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 for networks with duplicate names. Since Network is primarily keyed based on a
        /// random ID and not on the name, and network name is strictly a user-friendly alias to
        /// the network which is uniquely identified using ID, there is no guaranteed way to check
        /// for duplicates. CheckDuplicate is there to provide a best effort checking of any
        /// networks which has the same name but it is not guaranteed to catch all name collisions.
        check_duplicate: bool => "CheckDuplicate"
    );

    impl_str_field!(
        /// Name of the network driver plugin to use.
        driver => "Driver"
    );

    impl_field!(
        /// Restrict external access to the network.
        internal: bool => "Internal"
    );

    impl_field!(
        /// Globally scoped network is manually attachable by regular containers from workers
        /// in swarm mode.
        attachable: bool => "Attachable"
    );

    impl_field!(
        /// Ingress network is the network which provides the routing-mesh in swarm mode.
        ingress: bool => "Ingress"
    );

    impl_field!(
        /// Enable IPv6 on the network.
        enable_ipv6: bool => "EnableIPv6"
    );

    impl_map_field!(json
        /// Network specific options to be used by the drivers.
        options => "Options"
    );

    impl_map_field!(json
        /// User-defined key/value metadata.
        labels => "Labels"
    );

    impl_field!(
        /// IP Address Management configuration
        ipam: Ipam => "IPAM"
    );

    pub fn build(&self) -> NetworkCreateOpts {
        NetworkCreateOpts {
            params: self.params.clone(),
        }
    }
}
#[derive(Serialize, Debug)]
/// Interface for disconnecting a container from a network.
pub struct ContainerDisconnectionOpts {
    params: HashMap<&'static str, Value>,
}

impl ContainerDisconnectionOpts {
    /// Serializes the options as a JSON string.
    pub fn serialize(&self) -> Result<String> {
        serde_json::to_string(&self.params).map_err(Error::from)
    }

    /// Serializes the options as a JSON bytes.
    pub fn serialize_vec(&self) -> Result<Vec<u8>> {
        serde_json::to_vec(&self.params).map_err(Error::from)
    }

    /// Return a new instance of a builder for disconnecting a container from a network.
    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 the container to disconnect from the network.
        force: bool => "Force"
    );

    pub fn build(self) -> ContainerDisconnectionOpts {
        ContainerDisconnectionOpts {
            params: self.params,
        }
    }
}

#[derive(Serialize, Debug)]
/// Interface for connecting a container to a network.
pub struct ContainerConnectionOpts {
    params: HashMap<&'static str, Value>,
}

impl ContainerConnectionOpts {
    /// Serializes the options as a JSON string.
    pub fn serialize(&self) -> Result<String> {
        serde_json::to_string(&self.params).map_err(Error::from)
    }

    /// Serializes the options as a JSON bytes.
    pub fn serialize_vec(&self) -> Result<Vec<u8>> {
        serde_json::to_vec(&self.params).map_err(Error::from)
    }

    /// Return a new instance of a builder for connecting a container to a network.
    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(),
        }
    }

    /// Endpoint's IPAM configuration.
    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!(
        /// Unique ID of the network.
        network_id => "NetworkID"
    );

    impl_str_field!(
        /// Unique ID for the service endpoint in a Sandbox.
        endpoint_id => "EndpointID"
    );

    impl_str_field!(
        /// Gateway address for this network.
        gateway => "Gateway"
    );

    impl_str_field!(
        /// IPv4 address.
        ipv4 => "IPAddress"
    );

    impl_field!(
        /// Mask length of the IPv4 address.
        prefix_len: isize => "IPPrefixLen"
    );

    impl_str_field!(
        /// IPv6 gateway address.
        ipv6_gateway => "IPv6Gateway"
    );

    impl_str_field!(
        /// Global IPv6 address.
        ipv6 => "GlobalIPv6Address"
    );

    impl_field!(
        /// Mask length of the global IPv6 address.
        ipv6_prefix_len: i64 => "GlobalIPv6PrefixLen"
    );

    impl_str_field!(
        /// MAC address for the endpoint on this network.
        mac => "MacAddress"
    );

    impl_map_field!(json
        /// DriverOpts is a mapping of driver options and values. These options are passed directly
        /// to the driver and are driver specific.
        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)]
/// Used to configure endpoint IPAM configuration when connection a container to a network.
/// See [`ipam_config`](ContainerConnectOptsBuilder::ipam_config).
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 {
    /// Prune networks created before this timestamp. The <timestamp> can be Unix timestamps,
    /// date formatted timestamps, or Go duration strings (e.g. 10m, 1h30m) computed relative
    /// to the daemon machine’s time.
    Until(String),
    #[cfg(feature = "chrono")]
    #[cfg_attr(docsrs, doc(cfg(feature = "chrono")))]
    /// Prune networks created before this timestamp. Same as `Until` but takes a datetime object.
    UntilDate(chrono::DateTime<chrono::Utc>),
    /// Label in the form of `label=key`.
    LabelKey(String),
    /// Label in the form of `label=key=val`.
    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!(
        /// Filter the networks to prune by one of the variants of the enum.
        NetworkPruneFilter
    );
}