opendal/services/tikv/
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::TikvBuilder;
24
25/// Config for Tikv services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct TikvConfig {
30    /// network address of the TiKV service.
31    pub endpoints: Option<Vec<String>>,
32    /// whether using insecure connection to TiKV
33    pub insecure: bool,
34    /// certificate authority file path
35    pub ca_path: Option<String>,
36    /// cert path
37    pub cert_path: Option<String>,
38    /// key path
39    pub key_path: Option<String>,
40}
41
42impl Debug for TikvConfig {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("TikvConfig")
45            .field("endpoints", &self.endpoints)
46            .field("insecure", &self.insecure)
47            .field("ca_path", &self.ca_path)
48            .field("cert_path", &self.cert_path)
49            .field("key_path", &self.key_path)
50            .finish_non_exhaustive()
51    }
52}
53
54impl crate::Configurator for TikvConfig {
55    type Builder = TikvBuilder;
56
57    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
58        let map = uri.options().clone();
59
60        let mut endpoints = Vec::new();
61        if let Some(authority) = uri.authority() {
62            if !authority.is_empty() {
63                endpoints.push(authority.to_string());
64            }
65        }
66
67        if let Some(path) = uri.root() {
68            for segment in path.split('/') {
69                for endpoint in segment.split(',') {
70                    let trimmed = endpoint.trim();
71                    if !trimmed.is_empty() {
72                        endpoints.push(trimmed.to_string());
73                    }
74                }
75            }
76        }
77
78        let mut cfg = Self::from_iter(map)?;
79
80        if !endpoints.is_empty() {
81            if let Some(existing) = cfg.endpoints.as_mut() {
82                existing.extend(endpoints);
83            } else {
84                cfg.endpoints = Some(endpoints);
85            }
86        }
87
88        Ok(cfg)
89    }
90
91    fn into_builder(self) -> Self::Builder {
92        TikvBuilder { config: self }
93    }
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99    use crate::Configurator;
100    use crate::types::OperatorUri;
101
102    #[test]
103    fn from_uri_collects_endpoints() {
104        let uri = OperatorUri::new(
105            "tikv://pd1:2379/pd2:2379,pd3:2379",
106            Vec::<(String, String)>::new(),
107        )
108        .unwrap();
109
110        let cfg = TikvConfig::from_uri(&uri).unwrap();
111        assert_eq!(
112            cfg.endpoints,
113            Some(vec![
114                "pd1:2379".to_string(),
115                "pd2:2379".to_string(),
116                "pd3:2379".to_string()
117            ])
118        );
119    }
120}