Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new_emulator_default #312

Merged
merged 4 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions sdk/cosmos/src/clients/cosmos_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though just a source comment, Azure SDKs asks that URLs not be pre-localized so that a browser's preferred locales are used.

Suggested change
/// https://docs.microsoft.com/en-us/azure/cosmos-db/local-emulator?tabs=ssl-netstd21#connect-with-emulator-apis
/// https://docs.microsoft.com/azure/cosmos-db/local-emulator?tabs=ssl-netstd21#connect-with-emulator-apis

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. There are lots of links that need to be updated, so I'll make another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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";
Expand Down Expand Up @@ -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:<port>
//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"),
Expand Down
17 changes: 1 addition & 16 deletions sdk/storage/examples/emulator_00.rs
Original file line number Diff line number Diff line change
@@ -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<dyn Error + Send + Sync>> {
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
Expand Down
21 changes: 1 addition & 20 deletions sdk/storage/src/blob/clients/blob_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContainerClient> {
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<dyn HttpClient> = 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)
}

Expand Down
20 changes: 1 addition & 19 deletions sdk/storage/src/blob/clients/container_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContainerClient> {
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<dyn HttpClient> = 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)
}
Expand Down
30 changes: 28 additions & 2 deletions sdk/storage/src/core/clients/storage_account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
ctaggart marked this conversation as resolved.
Show resolved Hide resolved
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";
Expand Down Expand Up @@ -94,6 +103,7 @@ impl StorageAccountClient {
})
}

/// Create a new client for customized emulator endpoints.
pub fn new_emulator(
http_client: Arc<dyn HttpClient>,
blob_storage_url: &Url,
Expand All @@ -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<Self> {
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,
)
}

Expand Down
22 changes: 1 addition & 21 deletions sdk/storage/src/queue/clients/queue_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueueClient> {
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<dyn HttpClient> = 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)
}

Expand Down
22 changes: 1 addition & 21 deletions sdk/storage/src/table/clients/entity_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -133,25 +131,7 @@ mod integration_tests {
}

fn get_emulator_client() -> Arc<TableServiceClient> {
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<dyn HttpClient> = 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")
Expand Down
22 changes: 1 addition & 21 deletions sdk/storage/src/table/clients/partition_key_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -87,25 +85,7 @@ mod integration_tests {
}

fn get_emulator_client() -> Arc<TableServiceClient> {
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<dyn HttpClient> = 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")
Expand Down
22 changes: 1 addition & 21 deletions sdk/storage/src/table/clients/table_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -99,25 +97,7 @@ mod integration_tests {
}

fn get_emulator_client() -> Arc<TableServiceClient> {
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<dyn HttpClient> = 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")
Expand Down
23 changes: 1 addition & 22 deletions sdk/storage/src/table/clients/table_service_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<StorageClient> {
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<dyn HttpClient> = 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]
Expand Down