Skip to content

Commit

Permalink
Initial CosmosDB crate (#1773)
Browse files Browse the repository at this point in the history
* 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"
  • Loading branch information
analogrelay authored Sep 12, 2024
1 parent dfb354d commit 1fbc5dd
Show file tree
Hide file tree
Showing 17 changed files with 977 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@
# PRLabel: %Storage
/sdk/storage/ @heaths @RickWinter @JeffreyRichter

# AzureSDKOwners: @heaths
# ServiceOwner: @Pilchie
# ServiceLabel: %Cosmos
# PRLabel: %Cosmos
/sdk/cosmos/ @analogrelay @Pilchie

###########
# Eng Sys
###########
/eng/ @weshaggard @heaths @RickWinter
/eng/ @weshaggard @heaths @RickWinter
19 changes: 18 additions & 1 deletion .vscode/cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"**/test-resources.bicep",
"**/test-resources.json",
".vscode/cspell.json",
".github/CODEOWNERS",
"NOTICE.txt",
"eng/",
"*.dict.txt",
"rust-toolchain.toml"
],
"words": [
Expand All @@ -19,8 +21,10 @@
"datalake",
"datetime",
"devicecode",
"docsrs",
"downcasted",
"downcasting",
"entra",
"etag",
"eventhub",
"eventhubs",
Expand Down Expand Up @@ -50,6 +54,11 @@
"name": "rust-custom",
"path": "../eng/dict/rust-custom.txt",
"noSuggest": true
},
{
"name": "cosmos",
"path": "../sdk/cosmos/.dict.txt",
"noSuggest": true
}
],
"overrides": [
Expand Down Expand Up @@ -92,6 +101,14 @@
"azure",
"azurite"
]
},
{
"filename": "sdk/cosmos/**",
"dictionaries": [
"crates",
"rust-custom",
"cosmos"
]
}
]
}
}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"sdk/typespec/typespec_derive",
"sdk/core/azure_core",
"sdk/core/azure_core_amqp",
"sdk/cosmos/azure_data_cosmos",
"sdk/identity/azure_identity",
"sdk/eventhubs/azure_messaging_eventhubs",
"eng/test/mock_transport",
Expand Down
4 changes: 4 additions & 0 deletions sdk/cosmos/.dict.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
colls
pkranges
sprocs
udfs
42 changes: 42 additions & 0 deletions sdk/cosmos/azure_data_cosmos/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[package]
name = "azure_data_cosmos"
version = "0.19.0"
description = "Rust wrappers around Microsoft Azure REST APIs - Azure Cosmos DB"
readme = "README.md"
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
rust-version.workspace = true
homepage = "https://github.com/azure/azure-sdk-for-rust"
documentation = "https://docs.rs/azure_data_cosmos"
keywords = ["sdk", "azure", "rest", "cloud", "cosmos", "database"]
categories = ["api-bindings"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
async-trait.workspace = true
azure_core.workspace = true
typespec_client_core = { workspace = true, features = ["derive"] }
tracing.workspace = true
url.workspace = true
serde.workspace = true

[dev-dependencies]
tokio.workspace = true
serde_json.workspace = true
azure_identity.workspace = true
clap.workspace = true

[lints]
workspace = true

[features]
default = ["hmac_rust"]
hmac_rust = ["azure_core/hmac_rust"]
hmac_openssl = ["azure_core/hmac_openssl"]
key_auth = [] # Enables support for key-based authentication (Primary Keys and Resource Tokens)

[package.metadata.docs.rs]
features = ["key_auth"]
5 changes: 5 additions & 0 deletions sdk/cosmos/azure_data_cosmos/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Azure Cosmos DB SDK for Rust.

## Introduction

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.
45 changes: 45 additions & 0 deletions sdk/cosmos/azure_data_cosmos/examples/cosmos_connect.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use azure_data_cosmos::{clients::DatabaseClientMethods, CosmosClient, CosmosClientMethods};
use clap::Parser;

/// A simple example to show connecting to a Cosmos DB Account and retrieving the properties of a database.
#[derive(Parser)]
pub struct Args {
/// The Cosmos DB endpoint to connect to.
endpoint: String,

/// The database to fetch information for.
database: String,

/// An authentication key to use when connecting to the Cosmos DB account. If omitted, the connection will use Entra ID.
#[clap(long)]
#[cfg(feature = "key_auth")]
key: Option<String>,
}

#[tokio::main]
pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let args = Args::parse();

let client = create_client(&args);

let db_client = client.database_client(&args.database);
let response = db_client.read(None).await?.deserialize_body().await?;
println!("{:?}", response);
Ok(())
}

#[cfg(feature = "key_auth")]
fn create_client(args: &Args) -> CosmosClient {
if let Some(key) = args.key.as_ref() {
CosmosClient::with_key(&args.endpoint, key.clone(), None).unwrap()
} else {
let cred = azure_identity::create_default_credential().unwrap();
CosmosClient::new(&args.endpoint, cred, None).unwrap()
}
}

#[cfg(not(feature = "key_auth"))]
fn create_client(args: &Args) -> CosmosClient {
let cred = azure_identity::create_default_credential().unwrap();
CosmosClient::new(&args.endpoint, cred, None).unwrap()
}
Loading

0 comments on commit 1fbc5dd

Please sign in to comment.