use std::fmt::Debug;
use opendal_core::Configurator;
use opendal_core::OperatorUri;
use opendal_core::Result;
use serde::Deserialize;
use serde::Serialize;
use crate::backend::TosBuilder;
#[derive(Default, Serialize, Deserialize, Clone, PartialEq, Eq)]
#[serde(default)]
#[non_exhaustive]
pub struct TosConfig {
pub root: Option<String>,
pub bucket: String,
pub endpoint: Option<String>,
pub region: Option<String>,
#[serde(alias = "tos_access_key_id", alias = "volcengine_access_key_id")]
pub access_key_id: Option<String>,
#[serde(
alias = "tos_secret_access_key",
alias = "volcengine_secret_access_key"
)]
pub secret_access_key: Option<String>,
#[serde(alias = "tos_security_token", alias = "volcengine_session_token")]
pub security_token: Option<String>,
pub disable_config_load: bool,
pub allow_anonymous: bool,
pub enable_versioning: bool,
}
impl Debug for TosConfig {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TosConfig")
.field("root", &self.root)
.field("bucket", &self.bucket)
.field("endpoint", &self.endpoint)
.field("region", &self.region)
.finish_non_exhaustive()
}
}
impl Configurator for TosConfig {
type Builder = TosBuilder;
fn from_uri(uri: &OperatorUri) -> Result<Self> {
let mut map = uri.options().clone();
if let Some(name) = uri.name() {
map.insert("bucket".to_string(), name.to_string());
}
if let Some(root) = uri.root() {
map.insert("root".to_string(), root.to_string());
}
Self::from_iter(map)
}
fn into_builder(self) -> Self::Builder {
TosBuilder { config: self }
}
}
#[cfg(test)]
mod tests {
use std::iter;
use super::*;
use opendal_core::Configurator;
use opendal_core::OperatorUri;
#[test]
fn test_tos_config_original_field_names() {
let json = r#"{
"bucket": "test-bucket",
"access_key_id": "test-key",
"secret_access_key": "test-secret",
"region": "cn-beijing",
"endpoint": "https://tos-cn-beijing.volces.com"
}"#;
let config: TosConfig = serde_json::from_str(json).unwrap();
assert_eq!(config.bucket, "test-bucket");
assert_eq!(config.access_key_id, Some("test-key".to_string()));
assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
assert_eq!(config.region, Some("cn-beijing".to_string()));
assert_eq!(
config.endpoint,
Some("https://tos-cn-beijing.volces.com".to_string())
);
}
#[test]
fn test_tos_config_tos_prefixed_aliases() {
let json = r#"{
"tos_access_key_id": "test-key",
"tos_secret_access_key": "test-secret",
"tos_security_token": "test-token"
}"#;
let config: TosConfig = serde_json::from_str(json).unwrap();
assert_eq!(config.access_key_id, Some("test-key".to_string()));
assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
assert_eq!(config.security_token, Some("test-token".to_string()));
}
#[test]
fn test_tos_config_volcengine_prefixed_aliases() {
let json = r#"{
"volcengine_access_key_id": "test-key",
"volcengine_secret_access_key": "test-secret",
"volcengine_session_token": "test-token"
}"#;
let config: TosConfig = serde_json::from_str(json).unwrap();
assert_eq!(config.access_key_id, Some("test-key".to_string()));
assert_eq!(config.secret_access_key, Some("test-secret".to_string()));
assert_eq!(config.security_token, Some("test-token".to_string()));
}
#[test]
fn from_uri_extracts_bucket_and_root() {
let uri = OperatorUri::new("tos://example-bucket/path/to/root", iter::empty()).unwrap();
let cfg = TosConfig::from_uri(&uri).unwrap();
assert_eq!(cfg.bucket, "example-bucket");
assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
}
#[test]
fn from_uri_extracts_endpoint() {
let uri = OperatorUri::new(
"tos://example-bucket/path/to/root?endpoint=https%3A%2F%2Fcustom-tos-endpoint.com",
iter::empty(),
)
.unwrap();
let cfg = TosConfig::from_uri(&uri).unwrap();
assert_eq!(cfg.bucket, "example-bucket");
assert_eq!(cfg.root.as_deref(), Some("path/to/root"));
assert_eq!(
cfg.endpoint.as_deref(),
Some("https://custom-tos-endpoint.com")
);
}
}