Skip to main content

bollard/
config.rs

1//! Config API: manage and inspect docker configs within a swarm
2
3pub use crate::models::*;
4
5use super::Docker;
6use crate::{docker::BodyType, errors::Error};
7use bytes::Bytes;
8use http::request::Builder;
9use http_body_util::Full;
10use hyper::Method;
11
12impl Docker {
13    /// ---
14    ///
15    /// # List Configs
16    ///
17    /// Returns a list of configs.
18    ///
19    /// # Arguments
20    ///
21    ///  - Optional [ListConfigsOptions](crate::query_parameters::ListConfigsOptions) struct.
22    ///
23    /// # Returns
24    ///
25    ///  - Vector of [Config](Config), wrapped in a Future.
26    ///
27    /// # Examples
28    ///
29    /// ```rust
30    /// # use bollard::Docker;
31    /// # let docker = Docker::connect_with_http_defaults().unwrap();
32    /// use bollard::query_parameters::ListConfigsOptionsBuilder;
33    ///
34    /// use std::collections::HashMap;
35    /// use std::default::Default;
36    ///
37    /// let mut filters = HashMap::new();
38    /// filters.insert("label", vec!["config-label=label-value"]);
39    ///
40    /// let filters: HashMap<String, Vec<String>> = filters.into_iter().map(|(k, v)| (k.to_string(), v.into_iter().map(String::from).collect())).collect();
41    /// let options = ListConfigsOptionsBuilder::default()
42    ///     .filters(&filters)
43    ///     .build();
44    ///
45    /// docker.list_configs(Some(options));
46    /// ```
47    pub async fn list_configs(
48        &self,
49        options: Option<crate::query_parameters::ListConfigsOptions>,
50    ) -> Result<Vec<Config>, Error> {
51        let url = "/configs";
52
53        let req = self.build_request(
54            url,
55            Builder::new().method(Method::GET),
56            options,
57            Ok(BodyType::Left(Full::new(Bytes::new()))),
58        );
59
60        self.process_into_value(req).await
61    }
62
63    /// ---
64    ///
65    /// # Create Config
66    ///
67    /// Create a new config on the docker swarm.
68    ///
69    /// # Arguments
70    ///
71    ///  - [ConfigSpec](ConfigSpec) struct.
72    ///
73    /// # Returns
74    ///
75    ///  - A [IdResponse](IdResponse) wrapped in a Future.
76    ///
77    /// # Examples
78    ///
79    /// ```rust
80    /// # use bollard::Docker;
81    /// # use std::default::Default;
82    /// # use base64::Engine;
83    /// # let docker = Docker::connect_with_http_defaults().unwrap();
84    /// use bollard::config::ConfigSpec;
85    ///
86    /// use base64;
87    ///
88    /// let config_spec = ConfigSpec {
89    ///     name: Some(String::from("config-name")),
90    ///     data: Some(base64::engine::general_purpose::STANDARD.encode("config-data")),
91    ///     ..Default::default()
92    /// };
93    ///
94    /// docker.create_config(config_spec);
95    /// ```
96    pub async fn create_config(&self, config_spec: ConfigSpec) -> Result<IdResponse, Error> {
97        let url = "/configs/create";
98
99        let req = self.build_request(
100            url,
101            Builder::new().method(Method::POST),
102            None::<String>,
103            Docker::serialize_payload(Some(config_spec)),
104        );
105
106        self.process_into_value(req).await
107    }
108
109    /// ---
110    ///
111    /// # Inspect Config
112    ///
113    /// Inspect a config.
114    ///
115    /// # Arguments
116    ///
117    ///  - Config id or name as a string slice.
118    ///
119    /// # Returns
120    ///
121    ///  - [Config](Config), wrapped in a Future.
122    ///
123    /// # Examples
124    ///
125    /// ```rust
126    /// # use bollard::Docker;
127    /// # let docker = Docker::connect_with_http_defaults().unwrap();
128    ///
129    /// docker.inspect_config("config-id");
130    /// docker.inspect_config("config-name");
131    /// ```
132    pub async fn inspect_config(&self, config_id: &str) -> Result<Config, Error> {
133        let url = format!("/configs/{config_id}");
134
135        let req = self.build_request(
136            &url,
137            Builder::new().method(Method::GET),
138            None::<String>,
139            Ok(BodyType::Left(Full::new(Bytes::new()))),
140        );
141
142        self.process_into_value(req).await
143    }
144
145    /// ---
146    ///
147    /// # Delete Config
148    ///
149    /// Delete a config.
150    ///
151    /// # Arguments
152    ///
153    ///  - Config id or name as a string slice.
154    ///
155    /// # Returns
156    ///
157    ///  - unit type `()`, wrapped in a Future.
158    ///
159    /// # Examples
160    ///
161    /// ```rust
162    /// # use bollard::Docker;
163    /// # let docker = Docker::connect_with_http_defaults().unwrap();
164    ///
165    /// docker.delete_config("config-id");
166    /// docker.delete_config("config-name");
167    /// ```
168    pub async fn delete_config(&self, config_id: &str) -> Result<(), Error> {
169        let url = format!("/configs/{config_id}");
170
171        let req = self.build_request(
172            &url,
173            Builder::new().method(Method::DELETE),
174            None::<String>,
175            Ok(BodyType::Left(Full::new(Bytes::new()))),
176        );
177
178        self.process_into_unit(req).await
179    }
180
181    /// ---
182    ///
183    /// # Update Config
184    ///
185    /// Update an existing config.
186    ///
187    /// # Arguments
188    ///
189    ///  - Config id or name as a string slice.
190    ///  - [ConfigSpec](ConfigSpec) struct.
191    ///  - [UpdateConfigOptions](crate::query_parameters::UpdateConfigOptions) struct.
192    ///
193    /// # Returns
194    ///
195    ///  - unit type `()`, wrapped in a Future.
196    ///
197    /// # Examples
198    /// ```rust
199    /// # use bollard::Docker;
200    /// # let docker = Docker::connect_with_http_defaults().unwrap();
201    ///
202    /// use std::collections::HashMap;
203    /// use bollard::query_parameters::UpdateConfigOptionsBuilder;
204    ///
205    /// let result = async move {
206    ///     let existing = docker.inspect_config("my-config").await?;
207    ///     let version = existing.version.unwrap().index.unwrap();
208    ///     let mut spec = existing.spec.unwrap().clone();
209    ///
210    ///     let mut labels = HashMap::new();
211    ///     labels.insert(String::from("config-label"), String::from("label-value"));
212    ///     spec.labels = Some(labels.clone());
213    ///
214    ///     let options = UpdateConfigOptionsBuilder::default()
215    ///         .version(version as i64)
216    ///         .build();
217    ///
218    ///     docker.update_config("my-config", spec, options).await
219    /// };
220    /// ```
221    pub async fn update_config(
222        &self,
223        config_id: &str,
224        config_spec: ConfigSpec,
225        options: crate::query_parameters::UpdateConfigOptions,
226    ) -> Result<(), Error> {
227        let url = format!("/configs/{config_id}/update");
228
229        let req = self.build_request(
230            &url,
231            Builder::new().method(Method::POST),
232            Some(options),
233            Docker::serialize_payload(Some(config_spec)),
234        );
235
236        self.process_into_unit(req).await
237    }
238}