diff --git a/sdk/cosmos/azure_data_cosmos/Cargo.toml b/sdk/cosmos/azure_data_cosmos/Cargo.toml index 0f98e1e9cc..e5688578e8 100644 --- a/sdk/cosmos/azure_data_cosmos/Cargo.toml +++ b/sdk/cosmos/azure_data_cosmos/Cargo.toml @@ -13,8 +13,6 @@ 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 diff --git a/sdk/cosmos/azure_data_cosmos/examples/cosmos/create.rs b/sdk/cosmos/azure_data_cosmos/examples/cosmos/create.rs index 9a5a3d3ae5..9b5eb0044d 100644 --- a/sdk/cosmos/azure_data_cosmos/examples/cosmos/create.rs +++ b/sdk/cosmos/azure_data_cosmos/examples/cosmos/create.rs @@ -4,42 +4,80 @@ use azure_data_cosmos::{ clients::{ContainerClientMethods, DatabaseClientMethods}, CosmosClient, CosmosClientMethods, PartitionKey, }; -use clap::Args; +use clap::{Args, Subcommand}; /// Creates a new item. #[derive(Clone, Args)] pub struct CreateCommand { - /// The database in which to create the item. - database: String, + #[command(subcommand)] + subcommand: Subcommands, +} + +#[derive(Clone, Subcommand)] +pub enum Subcommands { + /// Create an item in a container. + Item { + /// The database in which to create the item. + database: String, + + /// The container in which to create the item. + container: String, - /// The container in which to create the item. - container: String, + /// The partition key of the new item. + #[clap(long, short)] + partition_key: String, - /// The partition key of the new item. - #[clap(long, short)] - partition_key: String, + /// The JSON of the new item. + #[clap(long, short)] + json: String, + }, - /// The JSON of the new item. - #[clap(long, short)] - json: String, + /// Create a database (does not support Entra ID). + #[cfg(feature = "control_plane")] + Database { + /// The ID of the new database to create. + id: String, + }, } impl CreateCommand { pub async fn run(self, client: CosmosClient) -> Result<(), Box> { - let db_client = client.database_client(&self.database); - let container_client = db_client.container_client(&self.container); - - let pk = PartitionKey::from(&self.partition_key); - let item: serde_json::Value = serde_json::from_str(&self.json)?; - - let created = container_client - .create_item(pk, item, None) - .await? - .deserialize_body() - .await? - .unwrap(); - println!("Created item:"); - println!("{:#?}", created); - Ok(()) + match self.subcommand { + Subcommands::Item { + database, + container, + partition_key, + json, + } => { + let db_client = client.database_client(database); + let container_client = db_client.container_client(container); + + let pk = PartitionKey::from(&partition_key); + let item: serde_json::Value = serde_json::from_str(&json)?; + + let created = container_client + .create_item(pk, item, None) + .await? + .deserialize_body() + .await? + .unwrap(); + println!("Created item:"); + println!("{:#?}", created); + Ok(()) + } + + #[cfg(feature = "control_plane")] + Subcommands::Database { id } => { + let db = client + .create_database(id, None) + .await? + .deserialize_body() + .await? + .unwrap(); + println!("Created database:"); + println!("{:#?}", db); + Ok(()) + } + } } } diff --git a/sdk/cosmos/azure_data_cosmos/src/clients/cosmos_client.rs b/sdk/cosmos/azure_data_cosmos/src/clients/cosmos_client.rs index bbee10a0c1..bef54aae85 100644 --- a/sdk/cosmos/azure_data_cosmos/src/clients/cosmos_client.rs +++ b/sdk/cosmos/azure_data_cosmos/src/clients/cosmos_client.rs @@ -3,15 +3,26 @@ use crate::{ clients::DatabaseClient, - models::{DatabaseProperties, DatabaseQueryResults}, + models::DatabaseQueryResults, pipeline::{AuthorizationPolicy, CosmosPipeline, ResourceType}, utils::AppendPathSegments, - CosmosClientOptions, CreateDatabaseOptions, Query, QueryDatabasesOptions, + CosmosClientOptions, Query, QueryDatabasesOptions, }; -use azure_core::{credentials::TokenCredential, Context, Method, Request, Response, Url}; -use serde::Serialize; +use azure_core::{credentials::TokenCredential, Request, Url}; use std::sync::Arc; +#[cfg(feature = "control_plane")] +use crate::{ + models::{DatabaseProperties, Item}, + CreateDatabaseOptions, +}; + +#[cfg(feature = "control_plane")] +use azure_core::{Context, Method, Response}; + +#[cfg(feature = "control_plane")] +use serde::Serialize; + #[cfg(feature = "key_auth")] use azure_core::credentials::Secret; @@ -75,12 +86,13 @@ pub trait CosmosClientMethods { /// # Arguments /// * `id` - The ID of the new database. /// * `options` - Optional parameters for the request. + #[allow(async_fn_in_trait)] // REASON: See https://github.com/Azure/azure-sdk-for-rust/issues/1796 for detailed justification #[cfg(feature = "control_plane")] async fn create_database( &self, id: String, options: Option, - ) -> azure_core::Result>; + ) -> azure_core::Result>>; } impl CosmosClient { @@ -180,6 +192,7 @@ impl CosmosClientMethods for CosmosClient { .send_query_request(query.into(), base_request, ResourceType::Databases) } + #[cfg(feature = "control_plane")] async fn create_database( &self, id: String, @@ -187,7 +200,7 @@ impl CosmosClientMethods for CosmosClient { #[allow(unused_variables)] // REASON: This is a documented public API so prefixing with '_' is undesirable. options: Option, - ) -> azure_core::Result> { + ) -> azure_core::Result>> { #[derive(Serialize)] struct RequestBody { id: String,