opendal/services/etcd/
config.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use std::fmt::Debug;
19
20use serde::Deserialize;
21use serde::Serialize;
22
23use super::backend::EtcdBuilder;
24
25/// Config for Etcd services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct EtcdConfig {
30    /// network address of the Etcd services.
31    /// If use https, must set TLS options: `ca_path`, `cert_path`, `key_path`.
32    /// e.g. "127.0.0.1:23790,127.0.0.1:23791,127.0.0.1:23792" or "http://127.0.0.1:23790,http://127.0.0.1:23791,http://127.0.0.1:23792" or "https://127.0.0.1:23790,https://127.0.0.1:23791,https://127.0.0.1:23792"
33    ///
34    /// default is "http://127.0.0.1:2379"
35    pub endpoints: Option<String>,
36    /// the username to connect etcd service.
37    ///
38    /// default is None
39    pub username: Option<String>,
40    /// the password for authentication
41    ///
42    /// default is None
43    pub password: Option<String>,
44    /// the working directory of the etcd service. Can be "/path/to/dir"
45    ///
46    /// default is "/"
47    pub root: Option<String>,
48    /// certificate authority file path
49    ///
50    /// default is None
51    pub ca_path: Option<String>,
52    /// cert path
53    ///
54    /// default is None
55    pub cert_path: Option<String>,
56    /// key path
57    ///
58    /// default is None
59    pub key_path: Option<String>,
60}
61
62impl Debug for EtcdConfig {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        f.debug_struct("EtcdConfig")
65            .field("root", &self.root)
66            .field("endpoints", &self.endpoints)
67            .field("username", &self.username)
68            .field("ca_path", &self.ca_path)
69            .field("cert_path", &self.cert_path)
70            .field("key_path", &self.key_path)
71            .finish_non_exhaustive()
72    }
73}
74
75impl crate::Configurator for EtcdConfig {
76    type Builder = EtcdBuilder;
77
78    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
79        let mut map = uri.options().clone();
80
81        if let Some(authority) = uri.authority() {
82            map.entry("endpoints".to_string())
83                .or_insert_with(|| format!("http://{authority}"));
84        }
85
86        if let Some(root) = uri.root() {
87            if !root.is_empty() {
88                map.insert("root".to_string(), root.to_string());
89            }
90        }
91
92        Self::from_iter(map)
93    }
94
95    fn into_builder(self) -> Self::Builder {
96        EtcdBuilder { config: self }
97    }
98}
99
100#[cfg(test)]
101mod tests {
102    use super::*;
103    use crate::Configurator;
104    use crate::types::OperatorUri;
105
106    #[test]
107    fn from_uri_sets_endpoints_and_root() {
108        let uri = OperatorUri::new(
109            "etcd://127.0.0.1:2379/app/config",
110            Vec::<(String, String)>::new(),
111        )
112        .unwrap();
113
114        let cfg = EtcdConfig::from_uri(&uri).unwrap();
115        assert_eq!(cfg.endpoints.as_deref(), Some("http://127.0.0.1:2379"));
116        assert_eq!(cfg.root.as_deref(), Some("app/config"));
117    }
118}