|
| 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 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +use opendal::Configurator; |
| 21 | +use opendal::services::AzdlsConfig; |
| 22 | +use url::Url; |
| 23 | + |
| 24 | +use crate::{Error, ErrorKind, Result}; |
| 25 | + |
| 26 | +/// A connection string. |
| 27 | +/// |
| 28 | +/// This could be used to use FileIO with any adls-compatible object storage |
| 29 | +/// service that has a different endpoint (like Azurite). |
| 30 | +/// |
| 31 | +/// Note, this string is parsed first, and any other passed adls.* properties |
| 32 | +/// will override values from the connection string. |
| 33 | +const ADLS_CONNECTION_STRING: &str = "adls.connection-string"; |
| 34 | + |
| 35 | +/// The account that you want to connect to. |
| 36 | +pub const ADLS_ACCOUNT_NAME: &str = "adls.account-name"; |
| 37 | + |
| 38 | +/// The key to authentication against the account. |
| 39 | +pub const ADLS_ACCOUNT_KEY: &str = "adls.account-key"; |
| 40 | + |
| 41 | +/// The shared access signature. |
| 42 | +pub const ADLS_SAS_TOKEN: &str = "adls.sas-token"; |
| 43 | + |
| 44 | +/// The tenant-id. |
| 45 | +pub const ADLS_TENANT_ID: &str = "adls.tenant-id"; |
| 46 | + |
| 47 | +/// The client-id. |
| 48 | +pub const ADLS_CLIENT_ID: &str = "adls.client-id"; |
| 49 | + |
| 50 | +/// The client-secret. |
| 51 | +pub const ADLS_CLIENT_SECRET: &str = "adls.client-secret"; |
| 52 | + |
| 53 | +/// Parses adls.* prefixed configuration properties. |
| 54 | +pub(crate) fn azdls_config_parse(mut m: HashMap<String, String>) -> Result<AzdlsConfig> { |
| 55 | + let mut cfg = AzdlsConfig::default(); |
| 56 | + |
| 57 | + if let Some(_conn_str) = m.remove(ADLS_CONNECTION_STRING) { |
| 58 | + return Err(Error::new( |
| 59 | + ErrorKind::FeatureUnsupported, |
| 60 | + "Azdls: connection string currently not supported", |
| 61 | + )); |
| 62 | + } |
| 63 | + |
| 64 | + if let Some(account_name) = m.remove(ADLS_ACCOUNT_NAME) { |
| 65 | + cfg.account_name = Some(account_name); |
| 66 | + } |
| 67 | + |
| 68 | + if let Some(account_key) = m.remove(ADLS_ACCOUNT_KEY) { |
| 69 | + cfg.account_key = Some(account_key); |
| 70 | + } |
| 71 | + |
| 72 | + if let Some(sas_token) = m.remove(ADLS_SAS_TOKEN) { |
| 73 | + cfg.sas_token = Some(sas_token); |
| 74 | + } |
| 75 | + |
| 76 | + if let Some(tenant_id) = m.remove(ADLS_TENANT_ID) { |
| 77 | + cfg.tenant_id = Some(tenant_id); |
| 78 | + } |
| 79 | + |
| 80 | + if let Some(client_id) = m.remove(ADLS_CLIENT_ID) { |
| 81 | + cfg.client_id = Some(client_id); |
| 82 | + } |
| 83 | + |
| 84 | + if let Some(client_secret) = m.remove(ADLS_CLIENT_SECRET) { |
| 85 | + cfg.client_secret = Some(client_secret); |
| 86 | + } |
| 87 | + |
| 88 | + Ok(cfg) |
| 89 | +} |
| 90 | + |
| 91 | +pub(crate) fn azdls_config_build(cfg: &AzdlsConfig, path: &str) -> Result<opendal::Operator> { |
| 92 | + let url = Url::parse(path)?; |
| 93 | + |
| 94 | + // This is ok to be empty, OpenDAL will use the default filesystem. |
| 95 | + let filesystem = url.username(); |
| 96 | + |
| 97 | + let builder = cfg.clone().into_builder().filesystem(filesystem); |
| 98 | + |
| 99 | + Ok(opendal::Operator::new(builder)?.finish()) |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use opendal::services::AzdlsConfig; |
| 105 | + |
| 106 | + use crate::io::{azdls_config_build, azdls_config_parse}; |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_azdls_config_parse() { |
| 110 | + let mut m = std::collections::HashMap::new(); |
| 111 | + m.insert(super::ADLS_ACCOUNT_NAME.to_string(), "test".to_string()); |
| 112 | + m.insert(super::ADLS_ACCOUNT_KEY.to_string(), "secret".to_string()); |
| 113 | + |
| 114 | + let config = azdls_config_parse(m).unwrap(); |
| 115 | + |
| 116 | + assert_eq!(config.account_name, Some("test".to_string())); |
| 117 | + assert_eq!(config.account_key, Some("secret".to_string())); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_azdls_config_build() { |
| 122 | + let mut config = AzdlsConfig::default(); |
| 123 | + config.endpoint = Some("https://myaccount.dfs.core.windows.net".to_string()); |
| 124 | + |
| 125 | + let path = "abfss://myfs@myaccount.dfs.core.windows.net/mydir/myfile.parquet"; |
| 126 | + |
| 127 | + let op = azdls_config_build(&config, path).unwrap(); |
| 128 | + assert_eq!(op.info().name(), "myfs"); |
| 129 | + } |
| 130 | +} |
0 commit comments