docker_pyo3/
volume.rs

1use docker_api::{
2    models::VolumeList200Response,
3    models::VolumePrune200Response,
4    opts::{VolumeCreateOpts, VolumeListOpts, VolumePruneOpts},
5    Volume, Volumes,
6};
7use pyo3::prelude::*;
8
9use crate::Pyo3Docker;
10use pyo3::exceptions;
11use pyo3::types::PyDict;
12use pythonize::pythonize;
13
14#[pymodule]
15pub fn volume(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
16    m.add_class::<Pyo3Volumes>()?;
17    m.add_class::<Pyo3Volume>()?;
18    Ok(())
19}
20
21#[derive(Debug)]
22#[pyclass(name = "Volumes")]
23pub struct Pyo3Volumes(pub Volumes);
24
25#[derive(Debug)]
26#[pyclass(name = "Volume")]
27pub struct Pyo3Volume(pub Volume);
28
29#[pymethods]
30impl Pyo3Volumes {
31    #[new]
32    pub fn new(docker: Pyo3Docker) -> Self {
33        Pyo3Volumes(Volumes::new(docker.0))
34    }
35
36    pub fn get(&self, name: &str) -> Pyo3Volume {
37        Pyo3Volume(self.0.get(name))
38    }
39
40    pub fn prune(&self) -> PyResult<Py<PyAny>> {
41        let rv = __volumes_prune(&self.0, &Default::default());
42
43        match rv {
44            Ok(rv) => Ok(pythonize_this!(rv)),
45            Err(rv) => Err(py_sys_exception!(rv)),
46        }
47    }
48
49    pub fn list(&self) -> PyResult<Py<PyAny>> {
50        let rv = __volumes_list(&self.0, &Default::default());
51
52        match rv {
53            Ok(rv) => Ok(pythonize_this!(rv)),
54            Err(rv) => Err(py_sys_exception!(rv)),
55        }
56    }
57
58    #[pyo3(signature = (name=None, driver=None, driver_opts=None, labels=None))]
59    pub fn create(
60        &self,
61        name: Option<&str>,
62        driver: Option<&str>,
63        driver_opts: Option<&Bound<'_, PyDict>>,
64        labels: Option<&Bound<'_, PyDict>>,
65    ) -> PyResult<Py<PyAny>> {
66        let mut opts = VolumeCreateOpts::builder();
67        bo_setter!(name, opts);
68        bo_setter!(driver, opts);
69        // bo_setter!(driver_opts, opts);
70        // bo_setter!(labels, opts);
71
72        let rv = __volumes_create(&self.0, &opts.build());
73
74        match rv {
75            Ok(rv) => Ok(pythonize_this!(rv)),
76            Err(rv) => Err(py_sys_exception!(rv)),
77        }
78    }
79}
80
81#[tokio::main]
82async fn __volumes_prune(
83    volumes: &Volumes,
84    opts: &VolumePruneOpts,
85) -> Result<VolumePrune200Response, docker_api::Error> {
86    volumes.prune(opts).await
87}
88
89#[tokio::main]
90async fn __volumes_list(
91    volumes: &Volumes,
92    opts: &VolumeListOpts,
93) -> Result<VolumeList200Response, docker_api::Error> {
94    volumes.list(opts).await
95}
96
97#[tokio::main]
98async fn __volumes_create(
99    volumes: &Volumes,
100    opts: &VolumeCreateOpts,
101) -> Result<docker_api::models::Volume, docker_api::Error> {
102    volumes.create(opts).await
103}
104
105#[pymethods]
106impl Pyo3Volume {
107    #[new]
108    pub fn new(docker: Pyo3Docker, name: &str) -> Self {
109        Pyo3Volume(Volume::new(docker.0, name))
110    }
111
112    pub fn name(&self) -> String {
113        self.0.name().to_string()
114    }
115
116    pub fn inspect(&self) -> PyResult<Py<PyAny>> {
117        let rv = __volume_inspect(&self.0);
118
119        match rv {
120            Ok(rv) => Ok(pythonize_this!(rv)),
121            Err(rv) => Err(py_sys_exception!(rv)),
122        }
123    }
124
125    pub fn delete(&self) -> PyResult<()> {
126        let rv = __volume_delete(&self.0);
127
128        match rv {
129            Ok(rv) => Ok(rv),
130            Err(rv) => Err(py_sys_exception!(rv)),
131        }
132    }
133}
134
135#[tokio::main]
136async fn __volume_inspect(
137    volume: &Volume,
138) -> Result<docker_api::models::Volume, docker_api::Error> {
139    volume.inspect().await
140}
141
142#[tokio::main]
143async fn __volume_delete(volume: &Volume) -> Result<(), docker_api::Error> {
144    volume.delete().await
145}