diff --git a/sdk/cosmos/src/clients/cosmos_client.rs b/sdk/cosmos/src/clients/cosmos_client.rs index aa170255ee..9b995a7b52 100644 --- a/sdk/cosmos/src/clients/cosmos_client.rs +++ b/sdk/cosmos/src/clients/cosmos_client.rs @@ -19,6 +19,11 @@ use std::borrow::Cow; use std::fmt::Debug; use std::sync::Arc; +/// The well-known account key used by Azure Cosmos DB Emulator. +/// https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator?tabs=ssl-netstd21#connect-with-emulator-apis +pub const EMULATOR_ACCOUNT_KEY: &str = + "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; + const AZURE_VERSION: &str = "2018-12-31"; const VERSION: &str = "1.0"; const TIME_FORMAT: &str = "%a, %d %h %Y %T GMT"; @@ -104,11 +109,7 @@ impl CosmosClient { /// Create a new `CosmosClient` which connects to the account's instance in Azure emulator pub fn new_emulator(address: &str, port: u16, options: CosmosOptions) -> Self { - //Account name: localhost: - //Account key: C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw== - let auth_token = AuthorizationToken::primary_from_base64( - "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==", - ).unwrap(); + let auth_token = AuthorizationToken::primary_from_base64(EMULATOR_ACCOUNT_KEY).unwrap(); let uri = format!("https://{}:{}", address, port); let cloud_location = CloudLocation::Custom { account: String::from("Custom"), diff --git a/sdk/storage/examples/emulator_00.rs b/sdk/storage/examples/emulator_00.rs index da2401b868..1d4875a8bb 100644 --- a/sdk/storage/examples/emulator_00.rs +++ b/sdk/storage/examples/emulator_00.rs @@ -1,28 +1,13 @@ -use azure_core::prelude::*; use azure_storage::blob::prelude::*; use azure_storage::core::prelude::*; use std::error::Error; -use url::Url; #[tokio::main] async fn main() -> Result<(), Box> { env_logger::init(); // this is how you use the emulator. - let blob_storage_url = Url::parse("http://127.0.0.1:10000")?; - let queue_storage_url = Url::parse("http://127.0.0.1:10001")?; - let table_storage_url = Url::parse("http://127.0.0.1:10002")?; - let filesystem_url = Url::parse("http://127.0.0.1:10004")?; - - let http_client = new_http_client(); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); let container = storage_account.as_container_client("emulcont"); // create container diff --git a/sdk/storage/src/blob/clients/blob_client.rs b/sdk/storage/src/blob/clients/blob_client.rs index 86077e2810..14330e3b54 100644 --- a/sdk/storage/src/blob/clients/blob_client.rs +++ b/sdk/storage/src/blob/clients/blob_client.rs @@ -179,28 +179,9 @@ impl BlobClient { mod integration_tests { use super::*; use crate::blob::clients::AsBlobClient; - use url::Url; fn get_emulator_client(container_name: &str) -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); - + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); storage_account.as_container_client(container_name) } diff --git a/sdk/storage/src/blob/clients/container_client.rs b/sdk/storage/src/blob/clients/container_client.rs index 75b21d5dc1..47fa2ded4a 100644 --- a/sdk/storage/src/blob/clients/container_client.rs +++ b/sdk/storage/src/blob/clients/container_client.rs @@ -113,27 +113,9 @@ impl ContainerClient { mod integration_tests { use super::*; use crate::{blob::clients::AsBlobClient, core::prelude::*}; - use url::Url; fn get_emulator_client(container_name: &str) -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); storage_account.as_container_client(container_name) } diff --git a/sdk/storage/src/core/clients/storage_account_client.rs b/sdk/storage/src/core/clients/storage_account_client.rs index a9041eae2a..a6a753cf0d 100644 --- a/sdk/storage/src/core/clients/storage_account_client.rs +++ b/sdk/storage/src/core/clients/storage_account_client.rs @@ -15,6 +15,15 @@ use ring::hmac; use std::sync::Arc; use url::Url; +/// The well-known account used by Azurite and the legacy Azure Storage Emulator. +/// https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite#well-known-storage-account-and-key +pub const EMULATOR_ACCOUNT: &str = "devstoreaccount1"; + +/// The well-known account key used by Azurite and the legacy Azure Storage Emulator. +/// https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azurite#well-known-storage-account-and-key +pub const EMULATOR_ACCOUNT_KEY: &str = + "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="; + pub(crate) const HEADER_VERSION: &str = "x-ms-version"; pub(crate) const AZURE_VERSION: &str = "2019-12-12"; @@ -94,6 +103,7 @@ impl StorageAccountClient { }) } + /// Create a new client for customized emulator endpoints. pub fn new_emulator( http_client: Arc, blob_storage_url: &Url, @@ -107,8 +117,24 @@ impl StorageAccountClient { table_storage_url, queue_storage_url, filesystem_url, - "devstoreaccount1", - "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==", + EMULATOR_ACCOUNT, + EMULATOR_ACCOUNT_KEY, + ) + } + + /// Create a new client using the default HttpClient and the default emulator endpoints. + pub fn new_emulator_default() -> Arc { + let http_client = new_http_client(); + let blob_storage_url = Url::parse("http://127.0.0.1:10000").unwrap(); + let queue_storage_url = Url::parse("http://127.0.0.1:10001").unwrap(); + let table_storage_url = Url::parse("http://127.0.0.1:10002").unwrap(); + let filesystem_url = Url::parse("http://127.0.0.1:10004").unwrap(); + Self::new_emulator( + http_client, + &blob_storage_url, + &table_storage_url, + &queue_storage_url, + &filesystem_url, ) } diff --git a/sdk/storage/src/queue/clients/queue_client.rs b/sdk/storage/src/queue/clients/queue_client.rs index 77372e1cc6..6dd7577125 100644 --- a/sdk/storage/src/queue/clients/queue_client.rs +++ b/sdk/storage/src/queue/clients/queue_client.rs @@ -110,29 +110,9 @@ mod integration_tests { use super::*; use crate::core::prelude::*; use crate::queue::clients::AsQueueClient; - use azure_core::HttpClient; - use url::Url; fn get_emulator_client(queue_name: &str) -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); - + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); storage_account.as_queue_client(queue_name) } diff --git a/sdk/storage/src/table/clients/entity_client.rs b/sdk/storage/src/table/clients/entity_client.rs index 73f1a25acb..53c5b20bee 100644 --- a/sdk/storage/src/table/clients/entity_client.rs +++ b/sdk/storage/src/table/clients/entity_client.rs @@ -111,8 +111,6 @@ mod integration_tests { core::prelude::*, table::clients::{AsTableClient, AsTableServiceClient}, }; - use azure_core::prelude::*; - use url::Url; #[derive(Debug, Clone, Serialize, Deserialize)] struct TestEntity { @@ -133,25 +131,7 @@ mod integration_tests { } fn get_emulator_client() -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); - + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); storage_account .as_table_service_client() .expect("a table service client") diff --git a/sdk/storage/src/table/clients/partition_key_client.rs b/sdk/storage/src/table/clients/partition_key_client.rs index 63245217d5..1146cd00e2 100644 --- a/sdk/storage/src/table/clients/partition_key_client.rs +++ b/sdk/storage/src/table/clients/partition_key_client.rs @@ -73,9 +73,7 @@ mod integration_tests { core::prelude::*, table::clients::{AsTableClient, AsTableServiceClient}, }; - use azure_core::prelude::*; use http::StatusCode; - use url::Url; #[derive(Debug, Clone, Serialize, Deserialize)] struct TestEntity { @@ -87,25 +85,7 @@ mod integration_tests { } fn get_emulator_client() -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); - + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); storage_account .as_table_service_client() .expect("a table service client") diff --git a/sdk/storage/src/table/clients/table_client.rs b/sdk/storage/src/table/clients/table_client.rs index 91ed8335e8..2b34c9add9 100644 --- a/sdk/storage/src/table/clients/table_client.rs +++ b/sdk/storage/src/table/clients/table_client.rs @@ -85,9 +85,7 @@ mod integration_tests { core::prelude::*, table::clients::{AsTableClient, AsTableServiceClient}, }; - use azure_core::prelude::*; use futures::StreamExt; - use url::Url; #[derive(Debug, Clone, Serialize, Deserialize)] struct TestEntity { @@ -99,25 +97,7 @@ mod integration_tests { } fn get_emulator_client() -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); - + let storage_account = StorageAccountClient::new_emulator_default().as_storage_client(); storage_account .as_table_service_client() .expect("a table service client") diff --git a/sdk/storage/src/table/clients/table_service_client.rs b/sdk/storage/src/table/clients/table_service_client.rs index 52210e71fe..df5fd5c57f 100644 --- a/sdk/storage/src/table/clients/table_service_client.rs +++ b/sdk/storage/src/table/clients/table_service_client.rs @@ -78,31 +78,10 @@ impl TableServiceClient { mod integration_tests { use super::*; use crate::{core::prelude::*, table::clients::AsTableClient}; - use azure_core::prelude::*; use futures::StreamExt; - use url::Url; fn get_emulator_client() -> Arc { - let blob_storage_url = - Url::parse("http://127.0.0.1:10000").expect("the default local storage emulator URL"); - let queue_storage_url = - Url::parse("http://127.0.0.1:10001").expect("the default local storage emulator URL"); - let table_storage_url = - Url::parse("http://127.0.0.1:10002").expect("the default local storage emulator URL"); - let filesystem_url = - Url::parse("http://127.0.0.1:10004").expect("the default local storage emulator URL"); - - let http_client: Arc = Arc::new(reqwest::Client::new()); - let storage_account = StorageAccountClient::new_emulator( - http_client, - &blob_storage_url, - &table_storage_url, - &queue_storage_url, - &filesystem_url, - ) - .as_storage_client(); - - storage_account + StorageAccountClient::new_emulator_default().as_storage_client() } #[tokio::test]