opendal_core/services/webhdfs/
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::WebhdfsBuilder;
24
25/// Config for WebHDFS support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct WebhdfsConfig {
30    /// Root for webhdfs.
31    pub root: Option<String>,
32    /// Endpoint for webhdfs.
33    pub endpoint: Option<String>,
34    /// Name of the user for webhdfs.
35    pub user_name: Option<String>,
36    /// Delegation token for webhdfs.
37    pub delegation: Option<String>,
38    /// Disable batch listing
39    pub disable_list_batch: bool,
40    /// atomic_write_dir of this backend
41    pub atomic_write_dir: Option<String>,
42}
43
44impl Debug for WebhdfsConfig {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        f.debug_struct("WebhdfsConfig")
47            .field("root", &self.root)
48            .field("endpoint", &self.endpoint)
49            .field("user_name", &self.user_name)
50            .field("disable_list_batch", &self.disable_list_batch)
51            .field("atomic_write_dir", &self.atomic_write_dir)
52            .finish_non_exhaustive()
53    }
54}
55
56impl crate::Configurator for WebhdfsConfig {
57    type Builder = WebhdfsBuilder;
58
59    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
60        let mut map = uri.options().clone();
61        if let Some(authority) = uri.authority() {
62            map.insert("endpoint".to_string(), format!("http://{authority}"));
63        }
64
65        if let Some(root) = uri.root() {
66            if !root.is_empty() {
67                map.insert("root".to_string(), root.to_string());
68            }
69        }
70
71        Self::from_iter(map)
72    }
73
74    fn into_builder(self) -> Self::Builder {
75        WebhdfsBuilder { config: self }
76    }
77}
78
79#[cfg(test)]
80mod tests {
81    use super::*;
82    use crate::Configurator;
83    use crate::types::OperatorUri;
84
85    #[test]
86    fn from_uri_sets_endpoint_and_root() {
87        let uri = OperatorUri::new(
88            "webhdfs://namenode.example.com:50070/user/hadoop/data",
89            vec![("user_name".to_string(), "hadoop".to_string())],
90        )
91        .unwrap();
92
93        let cfg = WebhdfsConfig::from_uri(&uri).unwrap();
94        assert_eq!(
95            cfg.endpoint.as_deref(),
96            Some("http://namenode.example.com:50070")
97        );
98        assert_eq!(cfg.root.as_deref(), Some("user/hadoop/data"));
99        assert_eq!(cfg.user_name.as_deref(), Some("hadoop"));
100    }
101
102    #[test]
103    fn from_uri_allows_missing_authority() {
104        let uri = OperatorUri::new("webhdfs", Vec::<(String, String)>::new()).unwrap();
105
106        let cfg = WebhdfsConfig::from_uri(&uri).unwrap();
107        assert!(cfg.endpoint.is_none());
108    }
109}