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