docker_api/api/
config.rs

1//! Configs are application configurations that can be used by services.
2//! Swarm mode must be enabled for these endpoints to work.
3
4use crate::{
5    conn::{Headers, Payload},
6    models, Result,
7};
8
9impl_api_ty!(Config => name);
10
11impl Config {
12    impl_api_ep! { cfg: Config, resp
13        Inspect -> &format!("/configs/{}", cfg.name), models::Config
14        Delete -> &format!("/configs/{}", cfg.name), ()
15    }
16
17    // TODO: add Config::update
18}
19
20impl Configs {
21    impl_api_ep! { __: Config, resp
22        List -> "/configs", models::Config
23    }
24
25    api_doc! {
26    Config => Create
27    |
28    /// Create a new config.
29    pub async fn create(&self, opts: &ConfigCreateOpts) -> Result<Config> {
30        use serde::Deserialize;
31        #[derive(Deserialize)]
32        struct ConfigCreateResponse {
33            #[serde(rename = "Id")]
34            pub id: String,
35        }
36        self.docker
37            .post_json("/configs/create", Payload::Json(opts.serialize_vec()?), Headers::none())
38            .await
39            .map(|resp: ConfigCreateResponse| {
40                Config::new(self.docker.clone(), resp.id)
41            })
42    }}
43}
44
45pub mod opts {
46    use crate::models::{Driver, Labels};
47    use crate::{Error, Result};
48    use containers_api::opts::{Filter, FilterItem};
49    use containers_api::{impl_filter_func, impl_opts_builder};
50    use serde::{Deserialize, Serialize};
51
52    impl_opts_builder!(url => ConfigList);
53
54    pub enum ConfigFilter {
55        /// The ID of the config.
56        Id(String),
57        /// Label in the form of `label=key`
58        LabelKey(String),
59        /// Label in the form of `label=key=val`
60        Label(String, String),
61        /// The name of the config.
62        Name(String),
63        Names(String),
64    }
65
66    impl Filter for ConfigFilter {
67        fn query_item(&self) -> FilterItem {
68            use ConfigFilter::*;
69            match &self {
70                Id(id) => FilterItem::new("id", id.to_owned()),
71                LabelKey(label) => FilterItem::new("label", label.to_owned()),
72                Label(key, val) => FilterItem::new("label", format!("{key}={val}")),
73                Name(name) => FilterItem::new("name", name.to_owned()),
74                Names(names) => FilterItem::new("names", names.to_owned()),
75            }
76        }
77    }
78
79    impl ConfigListOptsBuilder {
80        impl_filter_func!(
81            /// Filter listed configs by variants of the enum.
82            ConfigFilter
83        );
84    }
85
86    #[derive(Clone, Debug, Serialize, Deserialize)]
87    #[serde(rename_all = "PascalCase")]
88    /// Structure used to create a new config with [`Configs::create`](crate::Configs::create).
89    pub struct ConfigCreateOpts {
90        name: String,
91        labels: Labels,
92        data: String,
93        templating: Driver,
94    }
95
96    impl ConfigCreateOpts {
97        /// Create a new config with name and data. This function will take care of
98        /// encoding the config's data as base64.
99        pub fn new<N, D>(name: N, data: D) -> Self
100        where
101            N: Into<String>,
102            D: AsRef<str>,
103        {
104            Self {
105                name: name.into(),
106                labels: Labels::new(),
107                data: base64::encode(data.as_ref()),
108                templating: Driver {
109                    name: "".into(),
110                    options: None,
111                },
112            }
113        }
114
115        /// Set the templating driver of this config.
116        pub fn set_templating(mut self, driver: Driver) -> Self {
117            self.templating = driver;
118            self
119        }
120
121        /// Add a label to this config
122        pub fn add_label<K, V>(mut self, key: K, val: V) -> Self
123        where
124            K: Into<String>,
125            V: Into<String>,
126        {
127            self.labels.insert(key.into(), val.into());
128            self
129        }
130
131        pub fn serialize(&self) -> Result<String> {
132            serde_json::to_string(&self).map_err(Error::from)
133        }
134
135        pub fn serialize_vec(&self) -> Result<Vec<u8>> {
136            serde_json::to_vec(&self).map_err(Error::from)
137        }
138    }
139}
140
141pub use opts::*;