use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
use super::StorageConfig;
use crate::Result;
pub const ADLS_CONNECTION_STRING: &str = "adls.connection-string";
pub const ADLS_ACCOUNT_NAME: &str = "adls.account-name";
pub const ADLS_ACCOUNT_KEY: &str = "adls.account-key";
pub const ADLS_SAS_TOKEN: &str = "adls.sas-token";
pub const ADLS_TENANT_ID: &str = "adls.tenant-id";
pub const ADLS_CLIENT_ID: &str = "adls.client-id";
pub const ADLS_CLIENT_SECRET: &str = "adls.client-secret";
pub const ADLS_AUTHORITY_HOST: &str = "adls.authority-host";
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, TypedBuilder)]
pub struct AzdlsConfig {
#[builder(default, setter(strip_option, into))]
pub connection_string: Option<String>,
#[builder(default, setter(strip_option, into))]
pub account_name: Option<String>,
#[builder(default, setter(strip_option, into))]
pub account_key: Option<String>,
#[builder(default, setter(strip_option, into))]
pub sas_token: Option<String>,
#[builder(default, setter(strip_option, into))]
pub tenant_id: Option<String>,
#[builder(default, setter(strip_option, into))]
pub client_id: Option<String>,
#[builder(default, setter(strip_option, into))]
pub client_secret: Option<String>,
#[builder(default, setter(strip_option, into))]
pub authority_host: Option<String>,
#[builder(default, setter(strip_option, into))]
pub endpoint: Option<String>,
#[builder(default, setter(into))]
pub filesystem: String,
}
impl TryFrom<&StorageConfig> for AzdlsConfig {
type Error = crate::Error;
fn try_from(config: &StorageConfig) -> Result<Self> {
let props = config.props();
let mut cfg = AzdlsConfig::default();
if let Some(connection_string) = props.get(ADLS_CONNECTION_STRING) {
cfg.connection_string = Some(connection_string.clone());
}
if let Some(account_name) = props.get(ADLS_ACCOUNT_NAME) {
cfg.account_name = Some(account_name.clone());
}
if let Some(account_key) = props.get(ADLS_ACCOUNT_KEY) {
cfg.account_key = Some(account_key.clone());
}
if let Some(sas_token) = props.get(ADLS_SAS_TOKEN) {
cfg.sas_token = Some(sas_token.clone());
}
if let Some(tenant_id) = props.get(ADLS_TENANT_ID) {
cfg.tenant_id = Some(tenant_id.clone());
}
if let Some(client_id) = props.get(ADLS_CLIENT_ID) {
cfg.client_id = Some(client_id.clone());
}
if let Some(client_secret) = props.get(ADLS_CLIENT_SECRET) {
cfg.client_secret = Some(client_secret.clone());
}
if let Some(authority_host) = props.get(ADLS_AUTHORITY_HOST) {
cfg.authority_host = Some(authority_host.clone());
}
Ok(cfg)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_azdls_config_builder() {
let config = AzdlsConfig::builder()
.account_name("myaccount")
.account_key("my-account-key")
.build();
assert_eq!(config.account_name.as_deref(), Some("myaccount"));
assert_eq!(config.account_key.as_deref(), Some("my-account-key"));
}
#[test]
fn test_azdls_config_from_storage_config() {
let storage_config = StorageConfig::new()
.with_prop(ADLS_ACCOUNT_NAME, "myaccount")
.with_prop(ADLS_ACCOUNT_KEY, "my-account-key");
let azdls_config = AzdlsConfig::try_from(&storage_config).unwrap();
assert_eq!(azdls_config.account_name.as_deref(), Some("myaccount"));
assert_eq!(azdls_config.account_key.as_deref(), Some("my-account-key"));
}
#[test]
fn test_azdls_config_with_sas_token() {
let storage_config = StorageConfig::new()
.with_prop(ADLS_ACCOUNT_NAME, "myaccount")
.with_prop(ADLS_SAS_TOKEN, "my-sas-token");
let azdls_config = AzdlsConfig::try_from(&storage_config).unwrap();
assert_eq!(azdls_config.account_name.as_deref(), Some("myaccount"));
assert_eq!(azdls_config.sas_token.as_deref(), Some("my-sas-token"));
}
#[test]
fn test_azdls_config_with_client_credentials() {
let storage_config = StorageConfig::new()
.with_prop(ADLS_ACCOUNT_NAME, "myaccount")
.with_prop(ADLS_TENANT_ID, "my-tenant")
.with_prop(ADLS_CLIENT_ID, "my-client")
.with_prop(ADLS_CLIENT_SECRET, "my-secret");
let azdls_config = AzdlsConfig::try_from(&storage_config).unwrap();
assert_eq!(azdls_config.account_name.as_deref(), Some("myaccount"));
assert_eq!(azdls_config.tenant_id.as_deref(), Some("my-tenant"));
assert_eq!(azdls_config.client_id.as_deref(), Some("my-client"));
assert_eq!(azdls_config.client_secret.as_deref(), Some("my-secret"));
}
}