stackify_docker_api/opts/
secret.rs

1use crate::models::{Driver, Labels};
2use crate::{Error, Result};
3use containers_api::opts::{Filter, FilterItem};
4use containers_api::{impl_filter_func, impl_opts_builder};
5use serde::{Deserialize, Serialize};
6
7impl_opts_builder!(url => SecretList);
8
9pub enum SecretFilter {
10    /// The ID of the secret.
11    Id(String),
12    /// Label in the form of `label=key`
13    LabelKey(String),
14    /// Label in the form of `label=key=val`
15    Label(String, String),
16    /// The name of the secret.
17    Name(String),
18    Names(String),
19}
20
21impl Filter for SecretFilter {
22    fn query_item(&self) -> FilterItem {
23        use SecretFilter::*;
24        match &self {
25            Id(id) => FilterItem::new("id", id.to_owned()),
26            LabelKey(label) => FilterItem::new("label", label.to_owned()),
27            Label(key, val) => FilterItem::new("label", format!("{key}={val}")),
28            Name(name) => FilterItem::new("name", name.to_owned()),
29            Names(names) => FilterItem::new("names", names.to_owned()),
30        }
31    }
32}
33
34impl SecretListOptsBuilder {
35    impl_filter_func!(
36        /// Filter the list of filters by one of the variants of the enum.
37        SecretFilter
38    );
39}
40
41#[derive(Clone, Debug, Serialize, Deserialize)]
42#[serde(rename_all = "PascalCase")]
43/// Structure used to create a new secret with [`Secrets::create`](crate::Secrets::create).
44pub struct SecretCreateOpts {
45    name: String,
46    labels: Labels,
47    data: String,
48    driver: Driver,
49    templating: Driver,
50}
51
52impl SecretCreateOpts {
53    /// Create a new secret with name and data. This function will take care of
54    /// encoding the secret's data as base64.
55    pub fn new<N, D>(name: N, data: D) -> Self
56    where
57        N: Into<String>,
58        D: AsRef<str>,
59    {
60        Self {
61            name: name.into(),
62            labels: Labels::new(),
63            data: base64::encode(data.as_ref()),
64            driver: Driver {
65                name: "".into(),
66                options: None,
67            },
68            templating: Driver {
69                name: "".into(),
70                options: None,
71            },
72        }
73    }
74
75    /// Set the driver of this secret.
76    pub fn set_driver(mut self, driver: Driver) -> Self {
77        self.driver = driver;
78        self
79    }
80
81    /// Set the templating driver of this secret.
82    pub fn set_templating(mut self, driver: Driver) -> Self {
83        self.templating = driver;
84        self
85    }
86
87    /// Add a label to this secret
88    pub fn add_label<K, V>(mut self, key: K, val: V) -> Self
89    where
90        K: Into<String>,
91        V: Into<String>,
92    {
93        self.labels.insert(key.into(), val.into());
94        self
95    }
96
97    pub fn serialize(&self) -> Result<String> {
98        serde_json::to_string(&self).map_err(Error::from)
99    }
100
101    pub fn serialize_vec(&self) -> Result<Vec<u8>> {
102        serde_json::to_vec(&self).map_err(Error::from)
103    }
104}