Skip to main content

opendal_service_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    /// Use HTTPS endpoints for TLS. Set `ca_path` to trust a custom certificate authority.
32    /// Client authentication additionally requires both `cert_path` and `key_path`.
33    /// 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"
34    ///
35    /// default is "http://127.0.0.1:2379"
36    pub endpoints: Option<String>,
37    /// the username to connect etcd service.
38    ///
39    /// default is None
40    pub username: Option<String>,
41    /// the password for authentication
42    ///
43    /// default is None
44    pub password: Option<String>,
45    /// the working directory of the etcd service. Can be "/path/to/dir"
46    ///
47    /// default is "/"
48    pub root: Option<String>,
49    /// PEM certificate authority file used to verify the server.
50    /// Enables TLS independently of `cert_path` and `key_path`.
51    ///
52    /// default is None
53    pub ca_path: Option<String>,
54    /// PEM client certificate file used for mutual TLS.
55    /// Must be configured together with `key_path`.
56    ///
57    /// default is None
58    pub cert_path: Option<String>,
59    /// PEM client private key file used for mutual TLS.
60    /// Must be configured together with `cert_path`.
61    ///
62    /// default is None
63    pub key_path: Option<String>,
64    /// the maximum size in bytes of a gRPC response that the etcd client decodes.
65    ///
66    /// Set this to read values larger than the etcd client's own limit. The etcd
67    /// server must also allow responses of that size.
68    ///
69    /// Refer to [`etcd_client::KvClient::max_decoding_message_size`](https://docs.rs/etcd-client/latest/etcd_client/struct.KvClient.html#method.max_decoding_message_size)
70    ///
71    /// default is None, which keeps the etcd client default of 4 MiB
72    pub max_decoding_message_size: Option<usize>,
73}
74
75impl Debug for EtcdConfig {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        f.debug_struct("EtcdConfig")
78            .field("root", &self.root)
79            .field("endpoints", &self.endpoints)
80            .field("username", &self.username)
81            .field("ca_path", &self.ca_path)
82            .field("cert_path", &self.cert_path)
83            .field("key_path", &self.key_path)
84            .field("max_decoding_message_size", &self.max_decoding_message_size)
85            .finish_non_exhaustive()
86    }
87}
88
89impl opendal_core::Configurator for EtcdConfig {
90    type Builder = EtcdBuilder;
91
92    fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
93        let mut map = uri.options().clone();
94
95        if let Some(authority) = uri.authority() {
96            map.entry("endpoints".to_string())
97                .or_insert_with(|| format!("http://{authority}"));
98        }
99
100        if let Some(root) = uri.root()
101            && !root.is_empty()
102        {
103            map.insert("root".to_string(), root.to_string());
104        }
105
106        Self::from_iter(map)
107    }
108
109    fn into_builder(self) -> Self::Builder {
110        EtcdBuilder { config: self }
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117    use opendal_core::Configurator;
118    use opendal_core::OperatorUri;
119
120    #[test]
121    fn from_uri_sets_endpoints_and_root() {
122        let uri = OperatorUri::new(
123            "etcd://127.0.0.1:2379/app/config",
124            Vec::<(String, String)>::new(),
125        )
126        .unwrap();
127
128        let cfg = EtcdConfig::from_uri(&uri).unwrap();
129        assert_eq!(cfg.endpoints.as_deref(), Some("http://127.0.0.1:2379"));
130        assert_eq!(cfg.root.as_deref(), Some("app/config"));
131    }
132}