config_vault_source/
builder.rs

1use crate::{KvVersion, VaultAddr, VaultConfig, VaultSource};
2
3#[cfg(feature = "tls")]
4use crate::TlsConfig;
5
6#[derive(Debug)]
7pub struct VaultSourceBuilder {
8    address: Option<String>,
9    token: Option<String>,
10    mount: Option<String>,
11    path: Option<String>,
12    kv_version: KvVersion,
13
14    #[cfg(feature = "tls")]
15    tls: Option<TlsConfig>,
16}
17
18impl VaultSourceBuilder {
19    pub fn new() -> Self {
20        Self {
21            ..Default::default()
22        }
23    }
24
25    pub fn address(mut self, address: impl Into<String>) -> Self {
26        self.address = Some(address.into());
27        self
28    }
29
30    pub fn token(mut self, token: impl Into<String>) -> Self {
31        self.token = Some(token.into());
32        self
33    }
34
35    pub fn mount(mut self, mount: impl Into<String>) -> Self {
36        self.mount = Some(mount.into());
37        self
38    }
39
40    pub fn path(mut self, path: impl Into<String>) -> Self {
41        self.path = Some(path.into());
42        self
43    }
44
45    pub fn kv_version(mut self, version: KvVersion) -> Self {
46        self.kv_version = version;
47        self
48    }
49
50    #[cfg(feature = "tls")]
51    pub fn tls(mut self, tls: TlsConfig) -> Self {
52        self.tls = Some(tls);
53        self
54    }
55
56    pub fn build(self) -> Result<VaultSource, config::ConfigError> {
57        let address = self.address.ok_or(config::ConfigError::Message(
58            "Vault address must present".to_string(),
59        ))?;
60
61        let parsed_addr = VaultAddr::try_from(address)?;
62
63        let mount = self.mount.ok_or(config::ConfigError::Message(
64            "Vault mount point must present".to_string(),
65        ))?;
66
67        let path = self.path.ok_or(config::ConfigError::Message(
68            "Vault config path must present".to_string(),
69        ))?;
70
71        let token = self.token.ok_or(config::ConfigError::Message(
72            "Vault config path must present".to_string(),
73        ))?;
74
75        let config = VaultConfig {
76            address: parsed_addr,
77            token: token,
78            mount,
79            path,
80
81            #[cfg(feature = "tls")]
82            tls: self.tls,
83        };
84
85        Ok(VaultSource {
86            config,
87            kv_version: self.kv_version,
88        })
89    }
90}
91
92impl Default for VaultSourceBuilder {
93    fn default() -> Self {
94        Self {
95            address: Some("http://127.0.0.1:8200".to_string()),
96            token: Some("root".to_string()),
97            mount: Some("secret".to_string()),
98            path: Some("dev".to_string()),
99            kv_version: Default::default(),
100
101            #[cfg(feature = "tls")]
102            tls: None,
103        }
104    }
105}