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