docker_api/api/
swarm.rs

1//! Control and manage clusters of engines also known as Swarm
2
3use crate::{
4    conn::{Headers, Payload},
5    models,
6    opts::{SwarmInitOpts, SwarmJoinOpts},
7    Docker, Result,
8};
9
10api_doc! { Swarm
11|
12pub struct Swarm {
13    docker: Docker,
14}
15}
16
17impl Swarm {
18    pub fn new(docker: Docker) -> Self {
19        Self { docker }
20    }
21
22    impl_api_ep! {_swarm: Swarm, resp
23        Inspect -> "/swarm", models::Swarm
24    }
25
26    api_doc! { Swarm => Unlockkey
27    |
28    /// Get the unlock key.
29    pub async fn get_unlock_key(&self) -> Result<models::SwarmUnlockkey200Response> {
30        self.docker.get_json("/swarm/unlockkey").await
31    }}
32
33    api_doc! { Swarm => Unlock
34    |
35    /// Unlock a locked manager.
36    pub async fn unlock_manager(&self, key: &models::SwarmUnlockBodyParam) -> Result<()> {
37        self.docker
38            .post("/swarm/unlock", Payload::Json(serde_json::to_string(key)?), Headers::none())
39            .await
40            .map(|_| ())
41    }}
42
43    api_doc! { Swarm => Init
44    |
45    /// Initialize a new swarm.
46    pub async fn initialize(&self, opts: &SwarmInitOpts) -> Result<()> {
47        self.docker
48            .post("/swarm/init", Payload::Json(opts.serialize_vec()?), Headers::none())
49            .await
50            .map(|_| ())
51    }}
52
53    api_doc! { Swarm => Join
54    |
55    /// Join an existing swarm.
56    pub async fn join(&self, opts: &SwarmJoinOpts) -> Result<()> {
57        self.docker
58            .post("/swarm/join", Payload::Json(opts.serialize_vec()?), Headers::none())
59            .await
60            .map(|_| ())
61    }}
62
63    api_doc! { Swarm => Leave
64    |
65    /// Leave the current swarm.
66    pub async fn leave(&self) -> Result<()> {
67        self.docker
68            .post("/swarm/leave?force=false", Payload::empty(), Headers::none())
69            .await
70            .map(|_| ())
71    }}
72
73    api_doc! { Swarm => Leave
74    |
75    /// Leave the current swarm forcefully, even if this is the last manager or that it will break the cluster.
76    pub async fn force_leave(&self) -> Result<()> {
77        self.docker
78            .post("/swarm/leave?force=true", Payload::empty(), Headers::none())
79            .await
80            .map(|_| ())
81    }}
82}