opendal_core/services/alluxio/
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::AlluxioBuilder;
24
25/// Config for alluxio services support.
26#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
27#[serde(default)]
28#[non_exhaustive]
29pub struct AlluxioConfig {
30    /// root of this backend.
31    ///
32    /// All operations will happen under this root.
33    ///
34    /// default to `/` if not set.
35    pub root: Option<String>,
36    /// endpoint of this backend.
37    ///
38    /// Endpoint must be full uri, mostly like `http://127.0.0.1:39999`.
39    pub endpoint: Option<String>,
40}
41
42impl Debug for AlluxioConfig {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        f.debug_struct("AlluxioConfig")
45            .field("root", &self.root)
46            .field("endpoint", &self.endpoint)
47            .finish_non_exhaustive()
48    }
49}
50
51impl crate::Configurator for AlluxioConfig {
52    type Builder = AlluxioBuilder;
53
54    fn from_uri(uri: &crate::types::OperatorUri) -> crate::Result<Self> {
55        let mut map = uri.options().clone();
56        if let Some(authority) = uri.authority() {
57            map.insert("endpoint".to_string(), format!("http://{authority}"));
58        }
59
60        if let Some(root) = uri.root() {
61            if !root.is_empty() {
62                map.insert("root".to_string(), root.to_string());
63            }
64        }
65
66        Self::from_iter(map)
67    }
68
69    #[allow(deprecated)]
70    fn into_builder(self) -> Self::Builder {
71        AlluxioBuilder {
72            config: self,
73            http_client: None,
74        }
75    }
76}
77
78#[cfg(test)]
79mod tests {
80    use super::*;
81    use crate::Configurator;
82    use crate::types::OperatorUri;
83
84    #[test]
85    fn from_uri_sets_endpoint_and_root() {
86        let uri = OperatorUri::new(
87            "alluxio://127.0.0.1:39999/data/raw",
88            Vec::<(String, String)>::new(),
89        )
90        .unwrap();
91
92        let cfg = AlluxioConfig::from_uri(&uri).unwrap();
93        assert_eq!(cfg.endpoint.as_deref(), Some("http://127.0.0.1:39999"));
94        assert_eq!(cfg.root.as_deref(), Some("data/raw"));
95    }
96}