Skip to main content

opendal_service_tos/
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 opendal_core::Configurator;
21use opendal_core::OperatorUri;
22use opendal_core::Result;
23use serde::Deserialize;
24use serde::Serialize;
25
26use crate::backend::TosBuilder;
27
28/// Config for Volcengine TOS service.
29#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
30#[serde(default)]
31#[non_exhaustive]
32pub struct TosConfig {
33    /// root of this backend.
34    ///
35    /// All operations will happen under this root.
36    ///
37    /// default to `/` if not set.
38    pub root: Option<String>,
39    /// bucket name of this backend.
40    ///
41    /// required.
42    pub bucket: String,
43    /// endpoint of this backend.
44    ///
45    /// Endpoint must be full uri, e.g.
46    /// - TOS: `https://tos-cn-beijing.volces.com`
47    /// - TOS with region: `https://tos-{region}.volces.com`
48    ///
49    /// If user inputs endpoint without scheme like "tos-cn-beijing.volces.com", we
50    /// will prepend "https://" before it.
51    pub endpoint: Option<String>,
52    /// Region represent the signing region of this endpoint.
53    ///
54    /// Required if endpoint is not provided.
55    ///
56    /// - If region is set, we will take user's input first.
57    /// - If not, we will try to load it from environment.
58    /// - If still not set, default to `cn-beijing`.
59    pub region: Option<String>,
60    /// access_key_id of this backend.
61    ///
62    /// - If access_key_id is set, we will take user's input first.
63    /// - If not, we will try to load it from environment.
64    #[serde(alias = "tos_access_key_id", alias = "volcengine_access_key_id")]
65    pub access_key_id: Option<String>,
66    /// secret_access_key of this backend.
67    ///
68    /// - If secret_access_key is set, we will take user's input first.
69    /// - If not, we will try to load it from environment.
70    #[serde(
71        alias = "tos_secret_access_key",
72        alias = "volcengine_secret_access_key"
73    )]
74    pub secret_access_key: Option<String>,
75    /// security_token of this backend.
76    ///
77    /// This token will expire after sometime, it's recommended to set security_token
78    /// by hand.
79    #[serde(alias = "tos_security_token", alias = "volcengine_session_token")]
80    pub security_token: Option<String>,
81    /// Disable config load so that opendal will not load config from
82    /// environment.
83    ///
84    /// For examples:
85    /// - envs like `TOS_ACCESS_KEY_ID`
86    pub disable_config_load: bool,
87    /// Skip signature will skip loading credentials and signing requests.
88    pub skip_signature: bool,
89}
90
91impl Debug for TosConfig {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        f.debug_struct("TosConfig")
94            .field("root", &self.root)
95            .field("bucket", &self.bucket)
96            .field("endpoint", &self.endpoint)
97            .field("region", &self.region)
98            .finish_non_exhaustive()
99    }
100}
101
102impl Configurator for TosConfig {
103    type Builder = TosBuilder;
104
105    fn from_uri(uri: &OperatorUri) -> Result<Self> {
106        let mut map = uri.options().clone();
107
108        if let Some(name) = uri.name() {
109            map.insert("bucket".to_string(), name.to_string());
110        }
111
112        if let Some(root) = uri.root() {
113            map.insert("root".to_string(), root.to_string());
114        }
115
116        Self::from_iter(map)
117    }
118
119    fn into_builder(self) -> Self::Builder {
120        TosBuilder { config: self }
121    }
122}
123
124#[cfg(test)]
125mod tests {
126    use std::iter;
127
128    use super::*;
129    use opendal_core::Configurator;
130    use opendal_core::OperatorUri;
131
132    #[test]
133    fn test_tos_config_original_field_names() {
134        let json = r#"{
135            "bucket": "test-bucket",
136            "access_key_id": "test-key",
137            "secret_access_key": "test-secret",
138            "region": "cn-beijing",
139            "endpoint": "https://tos-cn-beijing.volces.com"
140        }"#;
141
142        let config: TosConfig = serde_json::from_str(json).unwrap();
143        assert_eq!(config.bucket, "test-bucket");
144        assert_eq!(config.access_key_id, Some("test-key".to_string()));
145        assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
146        assert_eq!(config.region, Some("cn-beijing".to_string()));
147        assert_eq!(
148            config.endpoint,
149            Some("https://tos-cn-beijing.volces.com".to_string())
150        );
151    }
152
153    #[test]
154    fn test_tos_config_tos_prefixed_aliases() {
155        let json = r#"{
156            "tos_access_key_id": "test-key",
157            "tos_secret_access_key": "test-secret",
158            "tos_security_token": "test-token"
159        }"#;
160
161        let config: TosConfig = serde_json::from_str(json).unwrap();
162        assert_eq!(config.access_key_id, Some("test-key".to_string()));
163        assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
164        assert_eq!(config.security_token, Some("test-token".to_string()));
165    }
166
167    #[test]
168    fn test_tos_config_volcengine_prefixed_aliases() {
169        let json = r#"{
170            "volcengine_access_key_id": "test-key",
171            "volcengine_secret_access_key": "test-secret",
172            "volcengine_session_token": "test-token"
173        }"#;
174
175        let config: TosConfig = serde_json::from_str(json).unwrap();
176        assert_eq!(config.access_key_id, Some("test-key".to_string()));
177        assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
178        assert_eq!(config.security_token, Some("test-token".to_string()));
179    }
180
181    #[test]
182    fn from_uri_extracts_bucket_and_root() {
183        let uri = OperatorUri::new("tos://example-bucket/path/to/root", iter::empty()).unwrap();
184        let cfg = TosConfig::from_uri(&uri).unwrap();
185        assert_eq!(cfg.bucket, "example-bucket");
186        assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
187    }
188
189    #[test]
190    fn from_uri_extracts_endpoint() {
191        let uri = OperatorUri::new(
192            "tos://example-bucket/path/to/root?endpoint=https%3A%2F%2Fcustom-tos-endpoint.com",
193            iter::empty(),
194        )
195        .unwrap();
196        let cfg = TosConfig::from_uri(&uri).unwrap();
197        assert_eq!(cfg.bucket, "example-bucket");
198        assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
199        assert_eq!(
200            cfg.endpoint.as_deref(),
201            Some("https://custom-tos-endpoint.com")
202        );
203    }
204}