iceberg/io/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/// ```
40#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, TypedBuilder)]
41pub struct OssConfig {
42    /// OSS endpoint URL.
43    #[builder(default, setter(strip_option, into))]
44    pub endpoint: Option<String>,
45    /// OSS access key ID.
46    #[builder(default, setter(strip_option, into))]
47    pub access_key_id: Option<String>,
48    /// OSS access key secret.
49    #[builder(default, setter(strip_option, into))]
50    pub access_key_secret: Option<String>,
51}
52
53impl TryFrom<&StorageConfig> for OssConfig {
54    type Error = crate::Error;
55
56    fn try_from(config: &StorageConfig) -> Result<Self> {
57        let props = config.props();
58
59        let mut cfg = OssConfig::default();
60        if let Some(endpoint) = props.get(OSS_ENDPOINT) {
61            cfg.endpoint = Some(endpoint.clone());
62        }
63        if let Some(access_key_id) = props.get(OSS_ACCESS_KEY_ID) {
64            cfg.access_key_id = Some(access_key_id.clone());
65        }
66        if let Some(access_key_secret) = props.get(OSS_ACCESS_KEY_SECRET) {
67            cfg.access_key_secret = Some(access_key_secret.clone());
68        }
69
70        Ok(cfg)
71    }
72}
73
74#[cfg(test)]
75mod tests {
76    use super::*;
77
78    #[test]
79    fn test_oss_config_builder() {
80        let config = OssConfig::builder()
81            .endpoint("https://oss-cn-hangzhou.aliyuncs.com")
82            .access_key_id("my-access-key")
83            .access_key_secret("my-secret-key")
84            .build();
85
86        assert_eq!(
87            config.endpoint.as_deref(),
88            Some("https://oss-cn-hangzhou.aliyuncs.com")
89        );
90        assert_eq!(config.access_key_id.as_deref(), Some("my-access-key"));
91        assert_eq!(config.access_key_secret.as_deref(), Some("my-secret-key"));
92    }
93
94    #[test]
95    fn test_oss_config_from_storage_config() {
96        let storage_config = StorageConfig::new()
97            .with_prop(OSS_ENDPOINT, "https://oss-cn-hangzhou.aliyuncs.com")
98            .with_prop(OSS_ACCESS_KEY_ID, "my-access-key")
99            .with_prop(OSS_ACCESS_KEY_SECRET, "my-secret-key");
100
101        let oss_config = OssConfig::try_from(&storage_config).unwrap();
102
103        assert_eq!(
104            oss_config.endpoint.as_deref(),
105            Some("https://oss-cn-hangzhou.aliyuncs.com")
106        );
107        assert_eq!(oss_config.access_key_id.as_deref(), Some("my-access-key"));
108        assert_eq!(
109            oss_config.access_key_secret.as_deref(),
110            Some("my-secret-key")
111        );
112    }
113
114    #[test]
115    fn test_oss_config_empty() {
116        let storage_config = StorageConfig::new();
117
118        let oss_config = OssConfig::try_from(&storage_config).unwrap();
119
120        assert_eq!(oss_config.endpoint, None);
121        assert_eq!(oss_config.access_key_id, None);
122        assert_eq!(oss_config.access_key_secret, None);
123    }
124}