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

use clap parser in cosmos examples #884

Merged
merged 2 commits into from
Jul 4, 2022
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
3 changes: 2 additions & 1 deletion sdk/data_cosmos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ sha2 = "0.10"

[dev-dependencies]
env_logger = "0.9"
tokio = { version = "1", features = ["macros"] }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
hyper = "0.14"
hyper-rustls = "0.23"
reqwest = "0.11.0"
stop-token = { version = "0.7.0", features = ["tokio"] }
clap = { version = "3.2.7", features = ["derive", "env"] }

[features]
test_e2e = []
Expand Down
40 changes: 21 additions & 19 deletions sdk/data_cosmos/examples/attachments_00.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use azure_data_cosmos::prelude::*;
use clap::Parser;
use futures::StreamExt;
use serde::{Deserialize, Serialize};

Expand All @@ -19,32 +20,33 @@ impl azure_data_cosmos::CosmosEntity for MySampleStruct {
}
}

#[derive(Debug, Parser)]
struct Args {
/// Name of the database.
database_name: String,
/// Name of the collection in the database
collection_name: String,
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
}

// This example expects you to have created a collection
// with partitionKey on "id".
#[tokio::main]
async fn main() -> azure_core::Result<()> {
let database_name = std::env::args()
.nth(1)
.expect("please specify database name as first command line parameter");
let collection_name = std::env::args()
.nth(2)
.expect("please specify collection name as second command line parameter");

let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");

let authorization_token = AuthorizationToken::primary_from_base64(&primary_key)?;

let client = CosmosClient::new(account, authorization_token, CosmosOptions::default());
let client = client
.database_client(database_name)
.collection_client(collection_name);
let args = Args::parse();
let authorization_token = AuthorizationToken::primary_from_base64(&args.primary_key)?;

let id = format!("unique_id{}", 100);
let client = CosmosClient::new(args.account, authorization_token, CosmosOptions::default())
.database_client(args.database_name)
.collection_client(args.collection_name);

let doc = MySampleStruct {
id,
id: format!("unique_id{}", 100),
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it was this way before, but "unique_id100".to_owned() might be better.

a_string: "Something here".into(),
a_number: 100,
a_timestamp: chrono::Utc::now().timestamp(),
Expand Down
19 changes: 14 additions & 5 deletions sdk/data_cosmos/examples/cancellation.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
use azure_data_cosmos::prelude::*;
use clap::Parser;
use stop_token::prelude::*;
use stop_token::StopSource;
use tokio::time::{Duration, Instant};

#[derive(Debug, Parser)]
struct Args {
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
}

#[tokio::main]
async fn main() -> azure_core::Result<()> {
env_logger::init();
// First we retrieve the account name and access key from environment variables, and
// create an authorization token.
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let authorization_token = AuthorizationToken::primary_from_base64(&primary_key)?;
let args = Args::parse();
let authorization_token = AuthorizationToken::primary_from_base64(&args.primary_key)?;

// Create a new Cosmos client.
let options = CosmosOptions::default();
let client = CosmosClient::new(account.clone(), authorization_token.clone(), options);
let client = CosmosClient::new(args.account.clone(), authorization_token.clone(), options);

// Create a new database, and time out if it takes more than 1 second.
let future = client.create_database("my_database").into_future();
Expand Down
21 changes: 15 additions & 6 deletions sdk/data_cosmos/examples/collection.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
use azure_data_cosmos::prelude::*;
use clap::Parser;
use futures::stream::StreamExt;

#[derive(Debug, Parser)]
struct Args {
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
}

#[tokio::main]
async fn main() -> azure_core::Result<()> {
// First we retrieve the account name and access key from environment variables.
// We expect access keys (ie, not resource constrained)
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");
let args = Args::parse();

// This is how you construct an authorization token.
// Remember to pick the correct token type.
Expand All @@ -17,14 +26,14 @@ async fn main() -> azure_core::Result<()> {
// errors, plus Azure specific ones. For example if a REST call returns the
// unexpected result (ie NotFound instead of Ok) we return an Err telling
// you that.
let authorization_token = AuthorizationToken::primary_from_base64(&primary_key)?;
let authorization_token = AuthorizationToken::primary_from_base64(&args.primary_key)?;

// Once we have an authorization token you can create a client instance. You can change the
// authorization token at later time if you need, for example, to escalate the privileges for a
// single operation.
// Here we are using reqwest but other clients are supported (check the documentation).
let client = CosmosClient::new(
account.clone(),
args.account.clone(),
authorization_token,
CosmosOptions::default(),
);
Expand All @@ -39,7 +48,7 @@ async fn main() -> azure_core::Result<()> {

println!(
"Account {} has {} database(s)",
account,
args.account,
databases.databases.len()
);

Expand Down
35 changes: 23 additions & 12 deletions sdk/data_cosmos/examples/create_delete_database.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
use azure_data_cosmos::prelude::*;
use clap::Parser;
use futures::stream::StreamExt;

#[derive(Debug, Parser)]
struct Args {
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
/// The name of the database
database_name: String,
}

#[tokio::main]
async fn main() -> azure_core::Result<()> {
// First we retrieve the account name and access key from environment variables.
// We expect access keys (ie, not resource constrained)
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");

let database_name = std::env::args()
.nth(1)
.expect("please specify database name as first command line parameter");
let args = Args::parse();

// This is how you construct an authorization token.
// Remember to pick the correct token type.
Expand All @@ -21,12 +28,13 @@ async fn main() -> azure_core::Result<()> {
// errors, plus Azure specific ones. For example if a REST call returns the
// unexpected result (ie NotFound instead of Ok) we return an Err telling
// you that.
let authorization_token = permission::AuthorizationToken::primary_from_base64(&primary_key)?;
let authorization_token =
permission::AuthorizationToken::primary_from_base64(&args.primary_key)?;

// Once we have an authorization token you can create a client instance. You can change the
// authorization token at later time if you need, for example, to escalate the privileges for a
// single operation.
let client = CosmosClient::new(account, authorization_token, CosmosOptions::default());
let client = CosmosClient::new(args.account, authorization_token, CosmosOptions::default());

// The Cosmos' client exposes a lot of methods. This one lists the databases in the specified
// account. Database do not implement Display but deref to &str so you can pass it to methods
Expand All @@ -38,12 +46,15 @@ async fn main() -> azure_core::Result<()> {
}
drop(list_databases_stream);

let db = client.create_database(&database_name).into_future().await?;
let db = client
.create_database(&args.database_name)
.into_future()
.await?;
println!("created database = {:#?}", db);

// create collection!
{
let database = client.database_client(database_name.clone());
let database = client.database_client(args.database_name.clone());
let create_collection_response = database
.create_collection("panzadoro", "/id")
.into_future()
Expand All @@ -70,7 +81,7 @@ async fn main() -> azure_core::Result<()> {
}

let resp = client
.database_client(database_name)
.database_client(args.database_name)
.delete_database()
.into_future()
.await?;
Expand Down
21 changes: 16 additions & 5 deletions sdk/data_cosmos/examples/database_00.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
use azure_data_cosmos::prelude::*;
use clap::Parser;
use futures::stream::StreamExt;
use serde_json::Value;

#[derive(Debug, Parser)]
struct Args {
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
}

#[tokio::main]
async fn main() -> azure_core::Result<()> {
// First we retrieve the account name and access key from environment variables.
// We expect access keys (ie, not resource constrained)
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");

let authorization_token = permission::AuthorizationToken::primary_from_base64(&primary_key)?;
let args = Args::parse();

let authorization_token =
permission::AuthorizationToken::primary_from_base64(&args.primary_key)?;

let client = CosmosClient::new(account, authorization_token, CosmosOptions::default());
let client = CosmosClient::new(args.account, authorization_token, CosmosOptions::default());

let dbs = client
.list_databases()
Expand Down
20 changes: 14 additions & 6 deletions sdk/data_cosmos/examples/database_01.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
use azure_data_cosmos::prelude::*;
use clap::Parser;
use futures::stream::StreamExt;

#[derive(Debug, Parser)]
struct Args {
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
}

#[tokio::main]
async fn main() -> azure_core::Result<()> {
// First we retrieve the account name and access key from environment variables.
// We expect access keys (ie, not resource constrained)
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");

let authorization_token = AuthorizationToken::primary_from_base64(&primary_key)?;
let args = Args::parse();
let authorization_token = AuthorizationToken::primary_from_base64(&args.primary_key)?;

let client = CosmosClient::new(account, authorization_token, CosmosOptions::default());
let client = CosmosClient::new(args.account, authorization_token, CosmosOptions::default());

let database = client.database_client("pollo");
println!("database_name == {}", database.database_name());
Expand Down
19 changes: 14 additions & 5 deletions sdk/data_cosmos/examples/document_00.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use clap::Parser;
use futures::stream::StreamExt;
use serde::{Deserialize, Serialize};
// Using the prelude module of the Cosmos crate makes easier to use the Rust Azure SDK for Cosmos DB.
use azure_core::prelude::*;

use azure_data_cosmos::prelude::*;

#[derive(Debug, Parser)]
struct Args {
/// Cosmos primary key name
#[clap(env = "COSMOS_PRIMARY_KEY")]
primary_key: String,
/// The cosmos account your're using
#[clap(env = "COSMOS_ACCOUNT")]
account: String,
}

#[derive(Clone, Serialize, Deserialize, Debug)]
struct MySampleStruct {
id: String,
Expand Down Expand Up @@ -34,19 +45,17 @@ const COLLECTION: &str = "azuresdktc";
async fn main() -> azure_core::Result<()> {
// Let's get Cosmos account and access key from env variables.
// This helps automated testing.
let primary_key =
std::env::var("COSMOS_PRIMARY_KEY").expect("Set env variable COSMOS_PRIMARY_KEY first!");
let account = std::env::var("COSMOS_ACCOUNT").expect("Set env variable COSMOS_ACCOUNT first!");
let args = Args::parse();

// First, we create an authorization token. There are two types of tokens, master and resource
// constrained. Please check the Azure documentation for details. You can change tokens
// at will and it's a good practice to raise your privileges only when needed.
let authorization_token = AuthorizationToken::primary_from_base64(&primary_key)?;
let authorization_token = AuthorizationToken::primary_from_base64(&args.primary_key)?;

// Next we will create a Cosmos client. You need an authorization_token but you can later
// change it if needed.
let client = CosmosClient::new(
account.clone(),
args.account.clone(),
authorization_token.clone(),
CosmosOptions::default(),
);
Expand Down
Loading