Skip to main content

opendal_service_sftp/
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::SftpBuilder;
24
25/// Config for Sftp Service support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct SftpConfig {
30    /// endpoint of this backend
31    pub endpoint: Option<String>,
32    /// root of this backend
33    pub root: Option<String>,
34    /// user of this backend
35    pub user: Option<String>,
36    /// key of this backend
37    pub key: Option<String>,
38    /// known_hosts_strategy of this backend
39    pub known_hosts_strategy: Option<String>,
40    /// Deprecated: SFTP copy capability is enabled by default.
41    #[deprecated(
42        since = "0.57.0",
43        note = "SFTP copy capability is enabled by default and this option is no longer needed."
44    )]
45    pub enable_copy: bool,
46}
47
48impl Debug for SftpConfig {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        f.debug_struct("SftpConfig")
51            .field("endpoint", &self.endpoint)
52            .field("root", &self.root)
53            .finish_non_exhaustive()
54    }
55}
56
57impl opendal_core::Configurator for SftpConfig {
58    type Builder = SftpBuilder;
59
60    fn from_uri(uri: &opendal_core::OperatorUri) -> opendal_core::Result<Self> {
61        let mut map = uri.options().clone();
62        if let Some(authority) = uri.authority() {
63            map.insert("endpoint".to_string(), authority.to_string());
64        }
65
66        if let Some(root) = uri.root() {
67            map.insert("root".to_string(), root.to_string());
68        }
69
70        Self::from_iter(map)
71    }
72
73    fn into_builder(self) -> Self::Builder {
74        SftpBuilder { config: self }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use opendal_core::Configurator;
82    use opendal_core::OperatorUri;
83
84    #[test]
85    fn from_uri_sets_endpoint_and_root() {
86        let uri = OperatorUri::new(
87            "sftp://sftp.example.com/home/alice",
88            Vec::<(String, String)>::new(),
89        )
90        .unwrap();
91
92        let cfg = SftpConfig::from_uri(&uri).unwrap();
93        assert_eq!(cfg.endpoint.as_deref(), Some("sftp.example.com"));
94        assert_eq!(cfg.root.as_deref(), Some("home/alice"));
95    }
96
97    #[test]
98    fn from_uri_applies_connection_overrides() {
99        let uri = OperatorUri::new(
100            "sftp://host",
101            vec![
102                ("user".to_string(), "alice".to_string()),
103                ("key".to_string(), "/home/alice/.ssh/id_rsa".to_string()),
104                ("known_hosts_strategy".to_string(), "accept".to_string()),
105            ],
106        )
107        .unwrap();
108
109        let cfg = SftpConfig::from_uri(&uri).unwrap();
110        assert_eq!(cfg.endpoint.as_deref(), Some("host"));
111        assert_eq!(cfg.user.as_deref(), Some("alice"));
112        assert_eq!(cfg.key.as_deref(), Some("/home/alice/.ssh/id_rsa"));
113        assert_eq!(cfg.known_hosts_strategy.as_deref(), Some("accept"));
114    }
115}