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
//! Create and manage user-defined networks that containers can be attached to.

use crate::{
    conn::Payload,
    models,
    opts::{
        ContainerConnectionOpts, ContainerDisconnectionOpts, NetworkCreateOpts, NetworkListOpts,
        NetworkPruneOpts,
    },
    Result,
};

impl_api_ty!(Network => id);

impl Network {
    impl_api_ep! { net: Network, resp
        Inspect -> &format!("/networks/{}", net.id), models::Network
        Delete -> &format!("/networks/{}", net.id), ()
    }

    api_doc! { Network => Connect
    /// Connect a container to a network.
    |
    pub async fn connect(&self, opts: &ContainerConnectionOpts) -> Result<()> {
        self.docker
            .post(
                &format!("/networks/{}/connect", self.id),
                Payload::Json(opts.serialize()?),
            )
            .await.map(|_| ())
    }}

    api_doc! { Network => Disconnect
    /// Disconnect a container from a network.
    |
    pub async fn disconnect(&self, opts: &ContainerDisconnectionOpts) -> Result<()> {
        self.docker
            .post(
                &format!("/networks/{}/disconnect", &self.id),
                Payload::Json(opts.serialize()?),
            )
            .await
            .map(|_| ())
    }}
}

impl Networks {
    impl_api_ep! { __: Network, resp
        List -> "/networks", models::Network
        Prune -> "/networks/prune", models::NetworkPrune200Response
    }

    api_doc! { Network => Create
    /// Create a new network.
    |
    pub async fn create(&self, opts: &NetworkCreateOpts) -> Result<Network> {
        // #TODO: handle missing id and return warnings (?)
        self.docker
            .post_json("/networks/create", Payload::Json(opts.serialize()?))
            .await
            .map(|resp: models::NetworkCreate201Response| {
                Network::new(self.docker.clone(), resp.id.unwrap_or_default())
            })
    }}
}