Skip to content

Commit a4628b7

Browse files
committed
minor code updates
1 parent bfa1628 commit a4628b7

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

articles/active-directory/managed-identities-azure-resources/tutorial-vm-managed-identities-cosmos.md

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: active-directory
77
ms.subservice: msi
88
ms.workload: integration
99
ms.topic: tutorial
10-
ms.date: 06/24/2022
10+
ms.date: 03/31/2023
1111
ms.author: barclayn
1212
ms.custom: ep-miar, ignite-2022, devx-track-azurepowershell, devx-track-azurecli
1313
ms.tool: azure-cli, azure-powershell
@@ -28,7 +28,7 @@ In this article, we set up a virtual machine to use managed identities to connec
2828

2929
## Create a resource group
3030

31-
Create a resource group called **mi-test**. We'll use this resource group for all resources used in this tutorial.
31+
Create a resource group called **mi-test**. We use this resource group for all resources used in this tutorial.
3232

3333
- [Create a resource group using the Azure portal](../../azure-resource-manager/management/manage-resource-groups-portal.md#create-resource-groups)
3434
- [Create a resource group using the CLI](../../azure-resource-manager/management/manage-resource-groups-cli.md#create-resource-groups)
@@ -177,7 +177,7 @@ az vm create --resource-group <MyResourceGroup> --name <myVM> --image UbuntuLTS
177177

178178
# [Resource Manager Template](#tab/azure-resource-manager)
179179

180-
Depending on your API version, you have to take [different steps](qs-configure-template-windows-vm.md#user-assigned-managed-identity). If your apiVersion is 2018-06-01, your user-assigned managed identities are stored in the userAssignedIdentities dictionary format and the ```<identityName>``` value is the name of a variable that you define in the variables section of your template. In the variable, you point to the user assigned managed identity that you want to assign.
180+
Depending on your API version, you have to take [different steps](qs-configure-template-windows-vm.md#user-assigned-managed-identity). If your apiVersion is 2018-06-01, your user-assigned managed identities are stored in the userAssignedIdentities dictionary format. The ```<identityName>``` value is the name of a variable that you define in the variables section of your template. In the variable, you point to the user assigned managed identity that you want to assign.
181181

182182
```json
183183
"variables": {
@@ -310,7 +310,7 @@ To use the sample below, you need to have the following NuGet packages:
310310
- Microsoft.Azure.Cosmos
311311
- Microsoft.Azure.Management.CosmosDB
312312

313-
In addition to the NuGet packages above, you also need to enable **Include prerelease** and then add **Azure.ResourceManager.CosmosDB**.
313+
In addition to the NuGet packages above, you also need to enable **Include prerelease** and then add **Azure.ResourceManager.CosmosDB**.
314314

315315
```csharp
316316
using Azure.Identity;
@@ -326,44 +326,53 @@ namespace MITest
326326
{
327327
static async Task Main(string[] args)
328328
{
329+
// Replace the placeholders with your own values
329330
var subscriptionId = "Your subscription ID";
330331
var resourceGroupName = "You resource group";
331332
var accountName = "Cosmos DB Account name";
332333
var databaseName = "mi-test";
333334
var containerName = "container01";
334335

336+
// Authenticate to Azure using Managed Identity (system-assigned or user-assigned)
335337
var tokenCredential = new DefaultAzureCredential();
336338

337-
// create the management clientSS
338-
var managementClient = new CosmosDBManagementClient(subscriptionId, tokenCredential);
339+
// Create the Cosmos DB management client using the subscription ID and token credential
340+
var managementClient = new CosmosDBManagementClient(tokenCredential)
341+
{
342+
SubscriptionId = subscriptionId
343+
};
339344

340-
// create the data client
341-
var dataClient = new CosmosClient("https://[Account].documents.azure.com:443/", tokenCredential);
345+
// Create the Cosmos DB data client using the account URL and token credential
346+
var dataClient = new CosmosClient($"https://{accountName}.documents.azure.com:443/", tokenCredential);
342347

343-
// create a new database
344-
var createDatabaseOperation = await managementClient.SqlResources.StartCreateUpdateSqlDatabaseAsync(resourceGroupName, accountName, databaseName,
348+
// Create a new database using the management client
349+
var createDatabaseOperation = await managementClient.SqlResources.StartCreateUpdateSqlDatabaseAsync(
350+
resourceGroupName,
351+
accountName,
352+
databaseName,
345353
new SqlDatabaseCreateUpdateParameters(new SqlDatabaseResource(databaseName), new CreateUpdateOptions()));
346354
await createDatabaseOperation.WaitForCompletionAsync();
347355

348-
// create a new container
349-
var createContainerOperation = await managementClient.SqlResources.StartCreateUpdateSqlContainerAsync(resourceGroupName, accountName, databaseName, containerName,
356+
// Create a new container using the management client
357+
var createContainerOperation = await managementClient.SqlResources.StartCreateUpdateSqlContainerAsync(
358+
resourceGroupName,
359+
accountName,
360+
databaseName,
361+
containerName,
350362
new SqlContainerCreateUpdateParameters(new SqlContainerResource(containerName), new CreateUpdateOptions()));
351363
await createContainerOperation.WaitForCompletionAsync();
352364

353-
354-
// create a new item
365+
// Create a new item in the container using the data client
355366
var partitionKey = "pkey";
356367
var id = Guid.NewGuid().ToString();
357368
await dataClient.GetContainer(databaseName, containerName)
358369
.CreateItemAsync(new { id = id, _partitionKey = partitionKey }, new PartitionKey(partitionKey));
359370

360-
361-
// read back the item
371+
// Read back the item from the container using the data client
362372
var pointReadResult = await dataClient.GetContainer(databaseName, containerName)
363373
.ReadItemAsync<dynamic>(id, new PartitionKey(partitionKey));
364374

365-
366-
// run a query
375+
// Run a query to get all items from the container using the data client
367376
await dataClient.GetContainer(databaseName, containerName)
368377
.GetItemQueryIterator<dynamic>("SELECT * FROM c")
369378
.ReadNextAsync();

0 commit comments

Comments
 (0)