podman_api/api/
volumes.rs

1use crate::{
2    api::ApiResource,
3    conn::{Headers, Payload},
4    models, opts, Result,
5};
6
7use containers_api::url;
8
9impl_api_ty!(
10    Volume => name
11);
12
13impl Volume {
14    api_doc! {
15    Volume => ExistsLibpod
16    |
17    /// Quick way to determine if this volume exists.
18    ///
19    /// Examples:
20    ///
21    /// ```no_run
22    /// async {
23    ///     use podman_api::Podman;
24    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
25    ///
26    ///     match podman.volumes().get("some_vol").exists().await {
27    ///         Ok(exists) => if exists {
28    ///             println!("volume exists!");
29    ///         } else {
30    ///             println!("volume doesn't exists!");
31    ///         },
32    ///         Err(e) => eprintln!("check failed: {}", e),
33    ///     }
34    /// };
35    /// ```
36    pub async fn exists(&self) -> Result<bool> {
37        self.podman
38            .resource_exists(ApiResource::Volumes, &self.name)
39            .await
40    }}
41
42    api_doc! {
43    Volume => InspectLibpod
44    |
45    /// Obtain low-level information about this volume.
46    ///
47    /// Examples:
48    ///
49    /// ```no_run
50    /// async {
51    ///     use podman_api::Podman;
52    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
53    ///
54    ///     match podman.volumes().get("my-vol").inspect().await {
55    ///         Ok(info) => println!("{:?}", info),
56    ///         Err(e) => eprintln!("{}", e),
57    ///     }
58    /// };
59    /// ```
60    pub async fn inspect(&self) -> Result<models::VolumeInspect> {
61        self.podman
62            .get_json(&format!("/libpod/volumes/{}/json", &self.name))
63            .await
64    }}
65
66    api_doc! {
67    Volume => DeleteLibpod
68    |
69    /// Delete this volume. To forcefully remove an volume use
70    /// [`Volume::remove`](Volume::remove).
71    ///
72    /// Examples:
73    ///
74    /// ```no_run
75    /// async {
76    ///     use podman_api::Podman;
77    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
78    ///
79    ///     if let Err(e) = podman.volumes().get("my-vol").delete().await {
80    ///         eprintln!("{}", e);
81    ///     }
82    /// };
83    /// ```
84    pub async fn delete(&self) -> Result<()> {
85        self.podman
86            .delete(&format!("/libpod/volumes/{}", &self.name))
87            .await
88            .map(|_| ())
89    }}
90
91    api_doc! {
92    Volume => DeleteLibpod
93    |
94    /// Remove this volume forcefully. To remove the volume normally use
95    /// [`Volume::delete`](Volume::delete).
96    ///
97    /// Examples:
98    ///
99    /// ```no_run
100    /// async {
101    ///     use podman_api::Podman;
102    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
103    ///
104    ///     if let Err(e) = podman.volumes().get("my-vol").remove().await {
105    ///         eprintln!("{}", e);
106    ///     }
107    /// };
108    /// ```
109    pub async fn remove(&self) -> Result<()> {
110        let ep = url::construct_ep(
111            format!("/libpod/volumes/{}", &self.name),
112            Some(url::encoded_pair("force", true)),
113        );
114        self.podman.delete(&ep).await.map(|_| ())
115    }}
116}
117
118impl Volumes {
119    api_doc! {
120    Volume => CreateLibpod
121    |
122    /// Create a volume with specified options.
123    ///
124    /// Examples:
125    ///
126    /// ```no_run
127    /// async {
128    ///     use podman_api::Podman;
129    ///     use podman_api::opts::VolumeCreateOpts;
130    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
131    ///
132    ///     match podman
133    ///         .volumes()
134    ///         .create(
135    ///             &VolumeCreateOpts::builder()
136    ///                 .driver("my-driver")
137    ///                 .name("my-vol")
138    ///                 .build(),
139    ///         )
140    ///         .await
141    ///     {
142    ///         Ok(info) => println!("{:?}", info),
143    ///         Err(e) => eprintln!("{}", e),
144    ///     }
145    /// };
146    /// ```
147    pub async fn create(
148        &self,
149        opts: &opts::VolumeCreateOpts,
150    ) -> Result<models::VolumeCreateResponse> {
151        self.podman
152            .post_json(
153                "/libpod/volumes/create",
154                Payload::Json(opts.serialize_vec()?),
155                Headers::none(),
156            )
157            .await
158    }}
159
160    api_doc! {
161    Volume => ListLibpod
162    |
163    /// Returns a list of volumes.
164    ///
165    /// Examples:
166    ///
167    /// ```no_run
168    /// async {
169    ///     use podman_api::Podman;
170    ///     use podman_api::opts::{VolumeListOpts, VolumeListFilter};
171    ///
172    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
173    ///
174    ///     for volume in podman
175    ///         .volumes()
176    ///         .list(
177    ///             &VolumeListOpts::builder()
178    ///                 .filter([VolumeListFilter::Driver("my-sd".into())])
179    ///                 .build(),
180    ///         )
181    ///         .await
182    ///         .unwrap()
183    ///     {
184    ///         println!("{:?}", volume);
185    ///     }
186    /// };
187    /// ```
188    pub async fn list(&self, opts: &opts::VolumeListOpts) -> Result<Vec<models::Volume>> {
189        let ep = url::construct_ep("/libpod/volumes/json", opts.serialize());
190        self.podman.get_json(&ep).await
191    }}
192
193    api_doc! {
194    Volume => PruneLibpod
195    |
196    /// Delete unused volumes.
197    ///
198    /// Examples:
199    ///
200    /// ```no_run
201    /// async {
202    ///     use podman_api::Podman;
203    ///     let podman = Podman::unix("/run/user/1000/podman/podman.sock");
204    ///
205    ///     match podman.volumes().prune(&Default::default()).await {
206    ///         Ok(info) => println!("{:?}", info),
207    ///         Err(e) => eprintln!("{}", e),
208    ///     }
209    /// };
210    /// ```
211    pub async fn prune(&self, opts: &opts::VolumePruneOpts) -> Result<Vec<models::PruneReport>> {
212        let ep = url::construct_ep("/libpod/volumes/prune", opts.serialize());
213        self.podman
214            .post_json(&ep, Payload::empty(), Headers::none())
215            .await
216    }}
217}