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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
//! Volume API: Create and manage persistent storage that can be attached to containers. use arrayvec::ArrayVec; use chrono::{DateTime, Utc}; use http::request::Builder; use hyper::{Body, Method}; use serde::Serialize; use serde_json; use std::cmp::Eq; use std::collections::HashMap; use std::hash::Hash; use super::Docker; use crate::docker::{FALSE_STR, TRUE_STR}; use crate::errors::Error; use crate::errors::ErrorKind::JsonSerializeError; /// Subresult type for the [List Volumes API](../struct.Docker.html#method.list_volumes) #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] #[allow(missing_docs)] pub struct VolumesListVolumesResults { pub created_at: DateTime<Utc>, pub name: String, pub driver: String, pub mountpoint: String, pub labels: Option<HashMap<String, String>>, pub scope: String, pub options: Option<HashMap<String, String>>, } /// Result type for the [List Volumes API](../struct.Docker.html#method.list_volumes) #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] #[allow(missing_docs)] pub struct ListVolumesResults { pub volumes: Vec<VolumesListVolumesResults>, pub warnings: Option<Vec<String>>, } /// Parameters used in the [List Volume API](../struct.Docker.html#method.list_volumes) #[derive(Debug, Clone, Default, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct ListVolumesOptions<T> where T: AsRef<str> + Eq + Hash, { /// JSON encoded value of the filters (a `map[string][]string`) to process on the volumes list. Available filters: /// - `dangling=<boolean>` When set to `true` (or `1`), returns all volumes that are not in use by a container. When set to `false` (or `0`), only volumes that are in use by one or more containers are returned. /// - `driver=<volume-driver-name>` Matches volumes based on their driver. /// - `label=<key>` or `label=<key>:<value>` Matches volumes based on the presence of a `label` alone or a `label` and a value. /// - `name=<volume-name>` Matches all or part of a volume name. pub filters: HashMap<T, Vec<T>>, } #[allow(missing_docs)] /// Trait providing implementations for [List Volumes Options](struct.ListVolumesOptions.html) /// struct. pub trait ListVolumesQueryParams<K, V> where K: AsRef<str>, V: AsRef<str>, { fn into_array(self) -> Result<ArrayVec<[(K, String); 1]>, Error>; } impl<'a, T: AsRef<str> + Eq + Hash + Serialize> ListVolumesQueryParams<&'a str, String> for ListVolumesOptions<T> { fn into_array(self) -> Result<ArrayVec<[(&'a str, String); 1]>, Error> { Ok(ArrayVec::from([( "filters", serde_json::to_string(&self.filters) .map_err::<Error, _>(|e| JsonSerializeError { err: e }.into())?, )])) } } /// Result type for the [Inspect Volume API](../struct.Docker.html#method.inspect_volume) and the /// [Create Volume API](../struct.Docker.html#method.create_volume) #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] #[allow(missing_docs)] pub struct VolumeAPI { pub name: String, pub driver: String, pub mountpoint: String, pub labels: HashMap<String, String>, pub scope: String, pub created_at: DateTime<Utc>, } /// Volume configuration used in the [Create Volume /// API](../struct.Docker.html#method.create_volume) #[derive(Debug, Clone, Default, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct CreateVolumeOptions<T> where T: AsRef<str> + Eq + Hash, { /// The new volume's name. If not specified, Docker generates a name. pub name: T, /// Name of the volume driver to use. pub driver: T, /// A mapping of driver options and values. These options are passed directly to the driver and /// are driver specific. pub driver_opts: HashMap<T, T>, /// User-defined key/value metadata. pub labels: HashMap<T, T>, } /// Parameters used in the [Remove Volume API](../struct.Docker.html#method.remove_volume) #[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] pub struct RemoveVolumeOptions { /// Force the removal of the volume. pub force: bool, } #[allow(missing_docs)] /// Trait providing implementations for [Remove Volume Options](struct.RemoveVolumeOptions.html) /// struct. pub trait RemoveVolumeQueryParams<K, V> where K: AsRef<str>, V: AsRef<str>, { fn into_array(self) -> Result<ArrayVec<[(K, V); 1]>, Error>; } impl<'a> RemoveVolumeQueryParams<&'a str, &'a str> for RemoveVolumeOptions { fn into_array(self) -> Result<ArrayVec<[(&'a str, &'a str); 1]>, Error> { Ok(ArrayVec::from([( "force", if self.force { TRUE_STR } else { FALSE_STR }, )])) } } /// Parameters used in the [Prune Volumes API](../struct.Docker.html#method.prune_volumes) /// /// ## Examples /// /// ```rust /// use bollard::volume::PruneVolumesOptions; /// /// use std::collections::HashMap; /// /// let mut filters = HashMap::new(); /// filters.insert("label!", vec!("maintainer=some_maintainer")); /// /// PruneVolumesOptions{ /// filters: filters /// }; /// ``` /// /// ```rust /// # use bollard::volume::PruneVolumesOptions; /// # use std::default::Default; /// /// PruneVolumesOptions::<&str>{ /// ..Default::default() /// }; /// ``` #[derive(Debug, Clone, Default)] pub struct PruneVolumesOptions<T> where T: AsRef<str> + Eq + Hash, { /// Filters to process on the prune list, encoded as JSON. /// - `label` (`label=<key>`, `label=<key>=<value>`, `label!=<key>`, or /// `label!=<key>=<value>`) Prune volumes with (or without, in case `label!=...` is used) the /// specified labels. pub filters: HashMap<T, Vec<T>>, } /// Trait providing implementations for [Prune Volumes Options](struct.PruneVolumesOptions.html) /// struct. #[allow(missing_docs)] pub trait PruneVolumesQueryParams<K, V> where K: AsRef<str>, V: AsRef<str>, { fn into_array(self) -> Result<ArrayVec<[(K, String); 1]>, Error>; } impl<'a> PruneVolumesQueryParams<&'a str, String> for PruneVolumesOptions<&'a str> { fn into_array(self) -> Result<ArrayVec<[(&'a str, String); 1]>, Error> { Ok(ArrayVec::from([( "filters", serde_json::to_string(&self.filters) .map_err::<Error, _>(|e| JsonSerializeError { err: e }.into())?, )])) } } /// Result type for the [Prune Volumes API](../struct.Docker.html#method.prune_volumes) #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "PascalCase")] #[allow(missing_docs)] pub struct PruneVolumesResults { pub volumes_deleted: Option<Vec<String>>, pub space_reclaimed: u64, } impl Docker { /// --- /// /// # List volumes /// /// # Arguments /// /// - [List Volumes Options](volume/struct.ListVolumesOptions.html) struct. /// /// # Returns /// /// - A [List Volumes Results](volume/struct.ListVolumesResults.html) struct, wrapped in a /// Future. /// /// # Examples /// /// ```rust /// # use bollard::Docker; /// # let docker = Docker::connect_with_http_defaults().unwrap(); /// /// use bollard::volume::ListVolumesOptions; /// /// use std::collections::HashMap; /// /// let mut filters = HashMap::new(); /// filters.insert("dangling", vec!("1")); /// /// let options = ListVolumesOptions { /// filters: filters, /// }; /// /// docker.list_volumes(Some(options)); /// ``` pub async fn list_volumes<T>( &self, options: Option<ListVolumesOptions<T>>, ) -> Result<ListVolumesResults, Error> where T: AsRef<str> + Eq + Hash + Serialize, { let url = "/volumes"; let req = self.build_request( &url, Builder::new().method(Method::GET), Docker::transpose_option(options.map(|o| o.into_array())), Ok(Body::empty()), ); self.process_into_value(req).await } /// --- /// /// # Create Volume /// /// Create a new volume. /// /// # Arguments /// /// - [Create Volume Options](volume/struct.CreateVolumeOptions.html) struct. /// /// # Returns /// /// - A [Volume Api](volume/struct.VolumeAPI.html) struct, wrapped in a /// Future. /// /// # Examples /// /// ```rust /// # use bollard::Docker; /// # let docker = Docker::connect_with_http_defaults().unwrap(); /// /// use bollard::volume::CreateVolumeOptions; /// /// use std::default::Default; /// /// let config = CreateVolumeOptions { /// name: "certs", /// ..Default::default() /// }; /// /// docker.create_volume(config); /// ``` pub async fn create_volume<T>(&self, config: CreateVolumeOptions<T>) -> Result<VolumeAPI, Error> where T: AsRef<str> + Eq + Hash + Serialize, { let url = "/volumes/create"; let req = self.build_request::<_, String, String>( &url, Builder::new().method(Method::POST), Ok(None::<ArrayVec<[(_, _); 0]>>), Docker::serialize_payload(Some(config)), ); self.process_into_value(req).await } /// --- /// /// # Inspect a Volume /// /// # Arguments /// /// - Volume name as a string slice. /// /// # Returns /// /// - A [Volume API](volume/struct.VolumeAPI.html) struct, wrapped in a /// Future. /// /// # Examples /// /// ```rust /// # use bollard::Docker; /// # let docker = Docker::connect_with_http_defaults().unwrap(); /// /// docker.inspect_volume("my_volume_name"); /// ``` pub async fn inspect_volume(&self, volume_name: &str) -> Result<VolumeAPI, Error> { let url = format!("/volumes/{}", volume_name); let req = self.build_request::<_, String, String>( &url, Builder::new().method(Method::GET), Ok(None::<ArrayVec<[(_, _); 0]>>), Ok(Body::empty()), ); self.process_into_value(req).await } /// --- /// /// # Remove a Volume /// /// # Arguments /// /// - Volume name as a string slice. /// /// # Arguments /// /// - [Remove Volume Options](volume/struct.RemoveVolumeOptions.html) struct. /// /// # Returns /// /// - unit type `()`, wrapped in a Future. /// /// # Examples /// /// ```rust /// # use bollard::Docker; /// # let docker = Docker::connect_with_http_defaults().unwrap(); /// /// use bollard::volume::RemoveVolumeOptions; /// /// let options = RemoveVolumeOptions { /// force: true, /// }; /// /// docker.remove_volume("my_volume_name", Some(options)); /// ``` pub async fn remove_volume( &self, volume_name: &str, options: Option<RemoveVolumeOptions>, ) -> Result<(), Error> { let url = format!("/volumes/{}", volume_name); let req = self.build_request( &url, Builder::new().method(Method::DELETE), Docker::transpose_option(options.map(|o| o.into_array())), Ok(Body::empty()), ); self.process_into_unit(req).await } /// --- /// /// # Prune Volumes /// /// Delete unused volumes. /// /// # Arguments /// /// - A [Prune Volumes Options](volume/struct.PruneVolumesOptions.html) struct. /// /// # Returns /// /// - A [Prune Volumes Results](volume/struct.PruneVolumesResults.html) struct. /// /// # Examples /// /// ```rust /// # use bollard::Docker; /// # let docker = Docker::connect_with_http_defaults().unwrap(); /// /// use bollard::volume::PruneVolumesOptions; /// /// use std::collections::HashMap; /// /// let mut filters = HashMap::new(); /// filters.insert("label", vec!("maintainer=some_maintainer")); /// /// let options = PruneVolumesOptions { /// filters: filters, /// }; /// /// docker.prune_volumes(Some(options)); /// ``` pub async fn prune_volumes<T, K, V>( &self, options: Option<T>, ) -> Result<PruneVolumesResults, Error> where T: PruneVolumesQueryParams<K, V>, K: AsRef<str>, V: AsRef<str>, { let url = "/volumes/prune"; let req = self.build_request( &url, Builder::new().method(Method::POST), Docker::transpose_option(options.map(|o| o.into_array())), Ok(Body::empty()), ); self.process_into_value(req).await } }