Skip to content

Commit 3e7c190

Browse files
Copilotseesharprun
andauthored
Simplify connect samples to only connect and read account metadata (#9)
## Summary Simplifies the connect samples in all languages to only connect to the account and perform a quick metadata read. CRUD operations (create/upsert/query) have been removed from both samples. ## Changes ### .NET (`001-connect-passwordless`) - Removed `Newtonsoft.Json` package dependency (no longer needed without item types) - Removed database creation, container creation, item upsert, and item read - Added `client.ReadAccountAsync()` to read and print the account name - Updated readme: role changed from `Cosmos DB Built-in Data Contributor` to `Cosmos DB Built-in Data Reader`; updated "What this sample does" description ### Python (`002-connect-connection-string`) - Removed `PartitionKey` import (no longer needed) - Removed database creation, container creation, item upsert, and item query - Added `client.get_database_account()` to read and print the account name - Updated readme: updated "What this sample does" description --------- Signed-off-by: Sidney Andrews <sidandrews@microsoft.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: seesharprun <5067401+seesharprun@users.noreply.github.com> Co-authored-by: Sidney Andrews <sidandrews@microsoft.com>
1 parent ec8b784 commit 3e7c190

4 files changed

Lines changed: 9 additions & 61 deletions

File tree

dotnet/001-connect-passwordless/connect.cs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,5 @@
1717
var credential = new DefaultAzureCredential();
1818
var client = new CosmosClient(endpoint, credential);
1919

20-
Database database = await client.CreateDatabaseIfNotExistsAsync("cosmicworks");
21-
Console.WriteLine($"Created/retrieved database: {database.Id}");
22-
23-
Container container = await database.CreateContainerIfNotExistsAsync("products", "/category");
24-
Console.WriteLine($"Created/retrieved container: {container.Id}");
25-
26-
Product newItem = new(
27-
id: "70b63682-b93a-4c77-aad2-65501347265f",
28-
category: "gear-surf-surfboards",
29-
name: "Yamba Surfboard",
30-
quantity: 12,
31-
sale: false
32-
);
33-
34-
ItemResponse<Product> createResponse = await container.UpsertItemAsync(newItem, new PartitionKey(newItem.category));
35-
Console.WriteLine($"Upserted item: {createResponse.Resource.id} (status: {createResponse.StatusCode})");
36-
37-
ItemResponse<Product> readResponse = await container.ReadItemAsync<Product>(
38-
newItem.id,
39-
new PartitionKey(newItem.category)
40-
);
41-
Console.WriteLine($"Read item: {readResponse.Resource.name} | Quantity: {readResponse.Resource.quantity}");
42-
43-
record Product(string id, string category, string name, int quantity, bool sale);
20+
AccountProperties account = await client.ReadAccountAsync();
21+
Console.WriteLine($"Account: {account.Id}");

dotnet/001-connect-passwordless/readme.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ This sample demonstrates how to authenticate to Azure Cosmos DB for NoSQL using
1313

1414
### 1. Assign the required role
1515

16-
Grant your Azure AD identity the **Cosmos DB Built-in Data Contributor** role on your Cosmos DB account:
16+
Grant your Azure AD identity the **Cosmos DB Built-in Data Reader** role on your Cosmos DB account:
1717

1818
```bash
1919
az cosmosdb sql role assignment create \
2020
--account-name <cosmos-account-name> \
2121
--resource-group <resource-group> \
22-
--role-definition-name "Cosmos DB Built-in Data Contributor" \
22+
--role-definition-name "Cosmos DB Built-in Data Reader" \
2323
--principal-id $(az ad signed-in-user show --query id -o tsv) \
2424
--scope "/"
2525
```
@@ -50,7 +50,4 @@ dotnet run connect.cs
5050

5151
1. Reads `COSMOS_ENDPOINT` from user secrets (via `Microsoft.Extensions.Configuration.UserSecrets`)
5252
2. Authenticates using `DefaultAzureCredential` (Azure CLI / Managed Identity / etc.)
53-
3. Creates (or retrieves) a database named `cosmicworks`
54-
4. Creates (or retrieves) a container named `products` with partition key `/category`
55-
5. Creates a product item
56-
6. Reads the item back and prints its details
53+
3. Reads account metadata and prints the account name
Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22

3-
from azure.cosmos import CosmosClient, PartitionKey
3+
from azure.cosmos import CosmosClient
44

55
endpoint = os.environ.get("COSMOS_ENDPOINT")
66
if not endpoint:
@@ -12,29 +12,5 @@
1212

1313
client = CosmosClient(url=endpoint, credential=key)
1414

15-
database = client.create_database_if_not_exists("cosmicworks")
16-
print(f"Created/retrieved database: {database.id}")
17-
18-
container = database.create_container_if_not_exists(
19-
id="products",
20-
partition_key=PartitionKey(path="/category"),
21-
)
22-
print(f"Created/retrieved container: {container.id}")
23-
24-
new_item = {
25-
"id": "70b63682-b93a-4c77-aad2-65501347265f",
26-
"category": "gear-surf-surfboards",
27-
"name": "Yamba Surfboard",
28-
"quantity": 12,
29-
"sale": False,
30-
}
31-
32-
created = container.upsert_item(body=new_item)
33-
print(f"Upserted item: {created['id']}")
34-
35-
query = "SELECT * FROM products p WHERE p.category = @category"
36-
parameters = [{"name": "@category", "value": "gear-surf-surfboards"}]
37-
38-
items = list(container.query_items(query=query, parameters=parameters, enable_cross_partition_query=False))
39-
for item in items:
40-
print(f"Read item: {item['name']} | Quantity: {item['quantity']} | Sale: {item['sale']}")
15+
account = client.get_database_account()
16+
print(f"Account: {account['id']}")

python/002-connect-connection-string/readme.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,4 @@ You can find your endpoint and primary key in the Azure portal under your Cosmos
3535

3636
1. Reads `COSMOS_ENDPOINT` and `COSMOS_KEY` from the environment
3737
2. Creates a `CosmosClient` using the endpoint and key
38-
3. Creates (or retrieves) a database named `cosmicworks`
39-
4. Creates (or retrieves) a container named `products` with partition key `/category`
40-
5. Creates a product item
41-
6. Queries items filtered by category and prints the results
38+
3. Reads account metadata and prints the account name

0 commit comments

Comments
 (0)