Skip to main content

iceberg/io/storage/config/
oss.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
18//! Alibaba Cloud OSS storage configuration.
19//!
20//! This module provides configuration constants and types for Alibaba Cloud OSS storage.
21
22use serde::{Deserialize, Serialize};
23use typed_builder::TypedBuilder;
24
25use super::StorageConfig;
26use crate::Result;
27
28/// Aliyun OSS endpoint.
29pub const OSS_ENDPOINT: &str = "oss.endpoint";
30/// Aliyun OSS access key ID.
31pub const OSS_ACCESS_KEY_ID: &str = "oss.access-key-id";
32/// Aliyun OSS access key secret.
33pub const OSS_ACCESS_KEY_SECRET: &str = "oss.access-key-secret";
34
35/// Alibaba Cloud OSS storage configuration.
36///
37/// This struct contains all the configuration options for connecting to Alibaba Cloud OSS.
38/// Use the builder pattern via `OssConfig::builder()` to construct instances.
39#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, TypedBuilder)]
40pub struct OssConfig {
41    /// OSS endpoint URL.
42    #[builder(default, setter(strip_option, into))]
43    pub endpoint: Option<String>,
44    /// OSS access key ID.
45    #[builder(default, setter(strip_option, into))]
46    pub access_key_id: Option<String>,
47    /// OSS access key secret.
48    #[builder(default, setter(strip_option, into))]
49    pub access_key_secret: Option<String>,
50}
51
52impl TryFrom<&StorageConfig> for OssConfig {
53    type Error = crate::Error;
54
55    fn try_from(config: &StorageConfig) -> Result<Self> {
56        let props = config.props();
57
58        let mut cfg = OssConfig::default();
59        if let Some(endpoint) = props.get(OSS_ENDPOINT) {
60            cfg.endpoint = Some(endpoint.clone());
61        }
62        if let Some(access_key_id) = props.get(OSS_ACCESS_KEY_ID) {
63            cfg.access_key_id = Some(access_key_id.clone());
64        }
65        if let Some(access_key_secret) = props.get(OSS_ACCESS_KEY_SECRET) {
66            cfg.access_key_secret = Some(access_key_secret.clone());
67        }
68
69        Ok(cfg)
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn test_oss_config_builder() {
79        let config = OssConfig::builder()
80            .endpoint("https://oss-cn-hangzhou.aliyuncs.com")
81            .access_key_id("my-access-key")
82            .access_key_secret("my-secret-key")
83            .build();
84
85        assert_eq!(
86            config.endpoint.as_deref(),
87            Some("https://oss-cn-hangzhou.aliyuncs.com")
88        );
89        assert_eq!(config.access_key_id.as_deref(), Some("my-access-key"));
90        assert_eq!(config.access_key_secret.as_deref(), Some("my-secret-key"));
91    }
92
93    #[test]
94    fn test_oss_config_from_storage_config() {
95        let storage_config = StorageConfig::new()
96            .with_prop(OSS_ENDPOINT, "https://oss-cn-hangzhou.aliyuncs.com")
97            .with_prop(OSS_ACCESS_KEY_ID, "my-access-key")
98            .with_prop(OSS_ACCESS_KEY_SECRET, "my-secret-key");
99
100        let oss_config = OssConfig::try_from(&storage_config).unwrap();
101
102        assert_eq!(
103            oss_config.endpoint.as_deref(),
104            Some("https://oss-cn-hangzhou.aliyuncs.com")
105        );
106        assert_eq!(oss_config.access_key_id.as_deref(), Some("my-access-key"));
107        assert_eq!(
108            oss_config.access_key_secret.as_deref(),
109            Some("my-secret-key")
110        );
111    }
112
113    #[test]
114    fn test_oss_config_empty() {
115        let storage_config = StorageConfig::new();
116
117        let oss_config = OssConfig::try_from(&storage_config).unwrap();
118
119        assert_eq!(oss_config.endpoint, None);
120        assert_eq!(oss_config.access_key_id, None);
121        assert_eq!(oss_config.access_key_secret, None);
122    }
123}