docker_pyo3/
config.rs

1use std::collections::HashMap;
2
3use crate::Pyo3Docker;
4use docker_api::config::{ConfigCreateOpts, ConfigListOpts};
5use docker_api::{Config, Configs};
6use pyo3::exceptions;
7use pyo3::prelude::*;
8use pyo3::types::PyDict;
9use pythonize::pythonize;
10
11#[pymodule]
12pub fn config(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
13    m.add_class::<Pyo3Configs>()?;
14    m.add_class::<Pyo3Config>()?;
15    Ok(())
16}
17
18/// Interface for managing Docker Swarm configs collection.
19///
20/// Configs are non-sensitive configuration data that can be mounted into containers.
21/// Swarm mode must be enabled for these operations to work.
22#[derive(Debug)]
23#[pyclass(name = "Configs")]
24pub struct Pyo3Configs {
25    configs: Configs,
26}
27
28/// Represents an individual Docker Swarm config.
29///
30/// Swarm mode must be enabled for these operations to work.
31#[derive(Debug)]
32#[pyclass(name = "Config")]
33pub struct Pyo3Config {
34    config: Config,
35}
36
37#[pymethods]
38impl Pyo3Configs {
39    #[new]
40    pub fn new(docker: Pyo3Docker) -> Self {
41        Pyo3Configs {
42            configs: Configs::new(docker.0),
43        }
44    }
45
46    /// Get a specific config by ID or name.
47    ///
48    /// Args:
49    ///     id: Config ID or name
50    ///
51    /// Returns:
52    ///     Config: Config instance
53    pub fn get(&self, id: &str) -> Pyo3Config {
54        Pyo3Config {
55            config: self.configs.get(id),
56        }
57    }
58
59    /// List all configs in the swarm.
60    ///
61    /// Returns:
62    ///     list[dict]: List of config information dictionaries
63    ///
64    /// Raises:
65    ///     SystemError: If the operation fails (e.g., swarm not initialized)
66    pub fn list(&self) -> PyResult<Py<PyAny>> {
67        let rv = __configs_list(&self.configs, &Default::default());
68
69        match rv {
70            Ok(rv) => Ok(pythonize_this!(rv)),
71            Err(rv) => Err(py_sys_exception!(rv)),
72        }
73    }
74
75    /// Create a new config in the swarm.
76    ///
77    /// Args:
78    ///     name: Name of the config
79    ///     data: Config data (will be base64 encoded automatically)
80    ///     labels: Labels as dict (e.g., {"app": "web"})
81    ///
82    /// Returns:
83    ///     Config: Created config instance
84    ///
85    /// Raises:
86    ///     SystemError: If the config cannot be created
87    #[pyo3(signature = (name, data, labels=None))]
88    pub fn create(
89        &self,
90        name: &str,
91        data: &str,
92        labels: Option<&Bound<'_, PyDict>>,
93    ) -> PyResult<Pyo3Config> {
94        let mut opts = ConfigCreateOpts::new(name, data);
95
96        if let Some(labels_dict) = labels {
97            let labels_map: HashMap<String, String> = labels_dict.extract().unwrap();
98            for (k, v) in labels_map {
99                opts = opts.add_label(k, v);
100            }
101        }
102
103        let rv = __configs_create(&self.configs, &opts);
104
105        match rv {
106            Ok(config) => Ok(Pyo3Config { config }),
107            Err(rv) => Err(py_sys_exception!(rv)),
108        }
109    }
110}
111
112#[tokio::main]
113async fn __configs_list(
114    configs: &Configs,
115    opts: &ConfigListOpts,
116) -> Result<Vec<docker_api::models::Config>, docker_api::Error> {
117    configs.list(opts).await
118}
119
120#[tokio::main]
121async fn __configs_create(
122    configs: &Configs,
123    opts: &ConfigCreateOpts,
124) -> Result<Config, docker_api::Error> {
125    configs.create(opts).await
126}
127
128#[pymethods]
129impl Pyo3Config {
130    #[new]
131    pub fn new(docker: Pyo3Docker, id: &str) -> Self {
132        Pyo3Config {
133            config: Config::new(docker.0, id),
134        }
135    }
136
137    /// Get the config ID.
138    ///
139    /// Returns:
140    ///     str: Config ID
141    pub fn id(&self) -> String {
142        self.config.name().to_string()
143    }
144
145    /// Inspect the config to get detailed information.
146    ///
147    /// Returns:
148    ///     dict: Config metadata including ID, version, created/updated times, and labels
149    ///
150    /// Raises:
151    ///     SystemError: If the operation fails
152    pub fn inspect(&self) -> PyResult<Py<PyAny>> {
153        let rv = __config_inspect(&self.config);
154
155        match rv {
156            Ok(rv) => Ok(pythonize_this!(rv)),
157            Err(rv) => Err(py_sys_exception!(rv)),
158        }
159    }
160
161    /// Delete the config from the swarm.
162    ///
163    /// Returns:
164    ///     None
165    ///
166    /// Raises:
167    ///     SystemError: If the config cannot be deleted
168    pub fn delete(&self) -> PyResult<()> {
169        let rv = __config_delete(&self.config);
170        match rv {
171            Ok(rv) => Ok(rv),
172            Err(rv) => Err(py_sys_exception!(rv)),
173        }
174    }
175}
176
177#[tokio::main]
178async fn __config_inspect(
179    config: &Config,
180) -> Result<docker_api::models::Config, docker_api::Error> {
181    config.inspect().await
182}
183
184#[tokio::main]
185async fn __config_delete(config: &Config) -> Result<(), docker_api::Error> {
186    config.delete().await
187}