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
//! Secret API: manage and inspect docker secrets within a swarm

pub use crate::models::*;

use super::Docker;
use crate::errors::Error;
use http::request::Builder;
use hyper::{Body, Method};
use serde::ser::Serialize;
use std::{collections::HashMap, hash::Hash};

/// Parameters used in the [List Secret API](super::Docker::list_secrets())
///
/// ## Examples
///
/// ```rust
/// # use std::collections::HashMap;
/// # use std::default::Default;
/// use bollard::secret::ListSecretsOptions;
///
/// let mut filters = HashMap::new();
/// filters.insert("name", vec!["my-secret-name"]);
///
/// ListSecretsOptions{
///     filters,
/// };
/// ```
///
/// ```rust
/// # use bollard::secret::ListSecretsOptions;
/// # use std::default::Default;
///
/// let options: ListSecretsOptions<&str> = Default::default();
/// ```
#[derive(Debug, Clone, Default, PartialEq, Serialize)]
pub struct ListSecretsOptions<T>
where
    T: Into<String> + Eq + Hash + Serialize,
{
    /// Filters to process on the secret list, encoded as JSON. Available filters:
    ///  - `id`=`<ID>` a secret's ID
    ///  - `label`=`key` or `label`=`"key=value"` of a secret label
    ///  - `name`=`<name>` a secret's name
    ///  - `names`=`<name>` a multiple secret's name comma separated
    #[serde(serialize_with = "crate::docker::serialize_as_json")]
    pub filters: HashMap<T, Vec<T>>,
}

/// Parameters used in the [Update Secret API](Docker::update_secret())
///
/// ## Examples
///
/// ```rust
/// use bollard::secret::UpdateSecretOptions;
///
/// UpdateSecretOptions{
///     version: 1234,
///     ..Default::default()
/// };
/// ```
#[derive(Debug, Copy, Clone, Default, PartialEq, Serialize)]
pub struct UpdateSecretOptions {
    /// The version number of the secret object being updated. This is required to avoid conflicting writes. This version number should be the value as currently set on the secret before the update.
    pub version: u64,
}

impl Docker {
    /// ---
    ///
    /// # List Secrets
    ///
    /// Returns a list of secrets.
    ///
    /// # Arguments
    ///
    ///  - Optional [ListSecretsOptions](ListSecretsOptions) struct.
    ///
    /// # Returns
    ///
    ///  - Vector of [Secret](Secret), wrapped in a Future.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use bollard::Docker;
    /// # let docker = Docker::connect_with_http_defaults().unwrap();
    /// use bollard::secret::ListSecretsOptions;
    ///
    /// use std::collections::HashMap;
    /// use std::default::Default;
    ///
    /// let mut filters = HashMap::new();
    /// filters.insert("label", vec!["secret-label=label-value"]);
    ///
    /// let options = Some(ListSecretsOptions{
    ///     filters,
    ///     ..Default::default()
    /// });
    ///
    /// docker.list_secrets(options);
    /// ```
    pub async fn list_secrets<T>(
        &self,
        options: Option<ListSecretsOptions<T>>,
    ) -> Result<Vec<Secret>, Error>
    where
        T: Into<String> + Eq + Hash + Serialize,
    {
        let url = "/secrets";

        let req = self.build_request(
            url,
            Builder::new().method(Method::GET),
            options,
            Ok(Body::empty()),
        );

        self.process_into_value(req).await
    }

    /// ---
    ///
    /// # Create Secret
    ///
    /// Create new secret on the docker swarm.
    ///
    /// # Arguments
    ///
    ///  - [SecretSpec](SecretSpec) struct.
    ///
    /// # Returns
    ///
    ///  - A [IdResponse](IdResponse) wrapped in a Future.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use bollard::Docker;
    /// # use std::default::Default;
    /// # use base64::Engine;
    /// # let docker = Docker::connect_with_http_defaults().unwrap();
    /// use bollard::secret::SecretSpec;
    ///
    /// use base64;
    ///
    /// let secret_spec = SecretSpec {
    ///     name: Some(String::from("secret-name")),
    ///     data: Some(base64::engine::general_purpose::STANDARD.encode("secret-data")),
    ///     ..Default::default()
    /// };
    ///
    /// docker.create_secret(secret_spec);
    /// ```
    pub async fn create_secret(&self, secret_spec: SecretSpec) -> Result<IdResponse, Error> {
        let url = "/secrets/create";

        let req = self.build_request(
            url,
            Builder::new().method(Method::POST),
            None::<String>,
            Docker::serialize_payload(Some(secret_spec)),
        );

        self.process_into_value(req).await
    }

    /// ---
    ///
    /// # Inspect Secret
    ///
    /// Inspect a secret.
    ///
    /// # Arguments
    ///
    ///  - Secret id or name as a string slice.
    ///
    /// # Returns
    ///
    ///  - [Secret](Secret), wrapped in a Future.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use bollard::Docker;
    /// # let docker = Docker::connect_with_http_defaults().unwrap();
    ///
    /// docker.inspect_secret("secret-id");
    /// docker.inspect_secret("secret-name");
    /// ```
    pub async fn inspect_secret(&self, secret_id: &str) -> Result<Secret, Error> {
        let url = format!("/secrets/{}", secret_id);

        let req = self.build_request(
            &url,
            Builder::new().method(Method::GET),
            None::<String>,
            Ok(Body::empty()),
        );

        self.process_into_value(req).await
    }

    /// ---
    ///
    /// # Delete Secret
    ///
    /// Delete a secret, fails when more than one service use that secret..
    ///
    /// # Arguments
    ///
    ///  - Secret id or name as a string slice.
    ///
    /// # Returns
    ///
    ///  - unit type `()`, wrapped in a Future.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # use bollard::Docker;
    /// # let docker = Docker::connect_with_http_defaults().unwrap();
    ///
    /// docker.delete_secret("secret-id");
    /// docker.delete_secret("secret-name");
    /// ```
    pub async fn delete_secret(&self, secret_id: &str) -> Result<(), Error> {
        let url = format!("/secrets/{}", secret_id);

        let req = self.build_request(
            &url,
            Builder::new().method(Method::DELETE),
            None::<String>,
            Ok(Body::empty()),
        );

        self.process_into_unit(req).await
    }

    /// ---
    ///
    /// # Update Secret
    ///
    /// Update an existing secret,
    /// fails when more than one service use that secret or trying update data.
    ///
    /// # Arguments
    ///
    ///  - Secret id or name as a string slice.
    ///  - [SecretSpec](SecretSpec) struct.
    ///  - [UpdateSecretOptions](UpdateSecretOptions) struct.
    ///
    /// # Returns
    ///
    ///  - unit type `()`, wrapped in a Future.
    ///
    /// # Examples
    /// ```rust
    /// # use bollard::Docker;
    /// # let docker = Docker::connect_with_http_defaults().unwrap();
    ///
    /// use std::collections::HashMap;
    /// use bollard::secret::UpdateSecretOptions;
    ///
    /// let result = async move {
    ///     let existing = docker.inspect_secret("my-secret").await?;
    ///     let version = existing.version.unwrap().index.unwrap();
    ///     let mut spec = existing.spec.unwrap().clone();
    ///
    ///     let mut labels = HashMap::new();
    ///     labels.insert(String::from("secret-label"), String::from("label-value"));
    ///     spec.labels = Some(labels.clone());
    ///
    ///     let options = UpdateSecretOptions { version };
    ///
    ///     docker.update_secret("my-secret", spec, options).await
    /// };
    /// ```
    pub async fn update_secret(
        &self,
        secret_id: &str,
        secret_spec: SecretSpec,
        options: UpdateSecretOptions,
    ) -> Result<(), Error> {
        let url = format!("/secrets/{}/update", secret_id);

        let req = self.build_request(
            &url,
            Builder::new().method(Method::POST),
            Some(options),
            Docker::serialize_payload(Some(secret_spec)),
        );

        self.process_into_unit(req).await
    }
}