Skip to content

Commit f781d9b

Browse files
analogrelayjpalvarezl
authored andcommitted
Initial CosmosDB crate (Azure#1773)
* implement initial CosmosClient, DatabaseClient and "read database" API * updates after rebase * fix some warnings and test issues * rename "adhoc" sample to "connect" * add CODEOWNERS for CosmosDB * use 'cosmosdb' as example prefix * fix spacing * a few more tidy-ups * switch to a team in the cosmos CODEOWNERS * clippy and lint fixes * spelling updates * auth policy test updates * doc comment updates * switch to using Model derive macro * a couple build fixes * ok that one was my bad * fix conditional imports * omit CODEOWNERS from cspell check * use direct mentions in CODEOWNERS for now * pr feedback * fix derive macro broken during refactoring * use dictionary file and update Cosmos to "Cosmos DB"
1 parent 2a90322 commit f781d9b

File tree

17 files changed

+977
-7
lines changed

17 files changed

+977
-7
lines changed

.github/CODEOWNERS

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@
3131
# PRLabel: %Storage
3232
/sdk/storage/ @heaths @RickWinter @JeffreyRichter
3333

34+
# AzureSDKOwners: @heaths
35+
# ServiceOwner: @Pilchie
36+
# ServiceLabel: %Cosmos
37+
# PRLabel: %Cosmos
38+
/sdk/cosmos/ @analogrelay @Pilchie
39+
3440
###########
3541
# Eng Sys
3642
###########
37-
/eng/ @weshaggard @heaths @RickWinter
43+
/eng/ @weshaggard @heaths @RickWinter

.vscode/cspell.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
"**/test-resources.bicep",
77
"**/test-resources.json",
88
".vscode/cspell.json",
9+
".github/CODEOWNERS",
910
"NOTICE.txt",
1011
"eng/",
12+
"*.dict.txt",
1113
"rust-toolchain.toml"
1214
],
1315
"words": [
@@ -19,8 +21,10 @@
1921
"datalake",
2022
"datetime",
2123
"devicecode",
24+
"docsrs",
2225
"downcasted",
2326
"downcasting",
27+
"entra",
2428
"etag",
2529
"eventhub",
2630
"eventhubs",
@@ -50,6 +54,11 @@
5054
"name": "rust-custom",
5155
"path": "../eng/dict/rust-custom.txt",
5256
"noSuggest": true
57+
},
58+
{
59+
"name": "cosmos",
60+
"path": "../sdk/cosmos/.dict.txt",
61+
"noSuggest": true
5362
}
5463
],
5564
"overrides": [
@@ -92,6 +101,14 @@
92101
"azure",
93102
"azurite"
94103
]
104+
},
105+
{
106+
"filename": "sdk/cosmos/**",
107+
"dictionaries": [
108+
"crates",
109+
"rust-custom",
110+
"cosmos"
111+
]
95112
}
96113
]
97-
}
114+
}

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ members = [
66
"sdk/typespec/typespec_derive",
77
"sdk/core/azure_core",
88
"sdk/core/azure_core_amqp",
9+
"sdk/cosmos/azure_data_cosmos",
910
"sdk/identity/azure_identity",
1011
"sdk/eventhubs/azure_messaging_eventhubs",
1112
"eng/test/mock_transport",

sdk/cosmos/.dict.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
colls
2+
pkranges
3+
sprocs
4+
udfs
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[package]
2+
name = "azure_data_cosmos"
3+
version = "0.19.0"
4+
description = "Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB"
5+
readme = "README.md"
6+
authors.workspace = true
7+
edition.workspace = true
8+
license.workspace = true
9+
repository.workspace = true
10+
rust-version.workspace = true
11+
homepage = "https://github.com/azure/azure-sdk-for-rust"
12+
documentation = "https://docs.rs/azure_data_cosmos"
13+
keywords = ["sdk", "azure", "rest", "cloud", "cosmos", "database"]
14+
categories = ["api-bindings"]
15+
16+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
17+
18+
[dependencies]
19+
async-trait.workspace = true
20+
azure_core.workspace = true
21+
typespec_client_core = { workspace = true, features = ["derive"] }
22+
tracing.workspace = true
23+
url.workspace = true
24+
serde.workspace = true
25+
26+
[dev-dependencies]
27+
tokio.workspace = true
28+
serde_json.workspace = true
29+
azure_identity.workspace = true
30+
clap.workspace = true
31+
32+
[lints]
33+
workspace = true
34+
35+
[features]
36+
default = ["hmac_rust"]
37+
hmac_rust = ["azure_core/hmac_rust"]
38+
hmac_openssl = ["azure_core/hmac_openssl"]
39+
key_auth = [] # Enables support for key-based authentication (Primary Keys and Resource Tokens)
40+
41+
[package.metadata.docs.rs]
42+
features = ["key_auth"]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Azure Cosmos DB SDK for Rust.
2+
3+
## Introduction
4+
5+
This client library enables client applications to connect to [Azure Cosmos DB](https://azure.microsoft.com/en-us/products/cosmos-db) via the NoSQL API. Azure Cosmos DB is a globally distributed, multi-model database service.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use azure_data_cosmos::{clients::DatabaseClientMethods, CosmosClient, CosmosClientMethods};
2+
use clap::Parser;
3+
4+
/// A simple example to show connecting to a Cosmos DB Account and retrieving the properties of a database.
5+
#[derive(Parser)]
6+
pub struct Args {
7+
/// The Cosmos DB endpoint to connect to.
8+
endpoint: String,
9+
10+
/// The database to fetch information for.
11+
database: String,
12+
13+
/// An authentication key to use when connecting to the Cosmos DB account. If omitted, the connection will use Entra ID.
14+
#[clap(long)]
15+
#[cfg(feature = "key_auth")]
16+
key: Option<String>,
17+
}
18+
19+
#[tokio::main]
20+
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
21+
let args = Args::parse();
22+
23+
let client = create_client(&args);
24+
25+
let db_client = client.database_client(&args.database);
26+
let response = db_client.read(None).await?.deserialize_body().await?;
27+
println!("{:?}", response);
28+
Ok(())
29+
}
30+
31+
#[cfg(feature = "key_auth")]
32+
fn create_client(args: &Args) -> CosmosClient {
33+
if let Some(key) = args.key.as_ref() {
34+
CosmosClient::with_key(&args.endpoint, key.clone(), None).unwrap()
35+
} else {
36+
let cred = azure_identity::create_default_credential().unwrap();
37+
CosmosClient::new(&args.endpoint, cred, None).unwrap()
38+
}
39+
}
40+
41+
#[cfg(not(feature = "key_auth"))]
42+
fn create_client(args: &Args) -> CosmosClient {
43+
let cred = azure_identity::create_default_credential().unwrap();
44+
CosmosClient::new(&args.endpoint, cred, None).unwrap()
45+
}

0 commit comments

Comments
 (0)