Skip to content

Commit c2bfeb8

Browse files
authored
Update batch-management-dotnet.md
updating code
1 parent 0691dab commit c2bfeb8

File tree

1 file changed

+71
-48
lines changed

1 file changed

+71
-48
lines changed

articles/batch/batch-management-dotnet.md

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Use the Batch Management .NET library to manage account resources
33
description: Create, delete, and modify Azure Batch account resources with the Batch Management .NET library.
44
ms.topic: how-to
5-
ms.date: 06/13/2024
5+
ms.date: 08/12/2024
66
ms.devlang: csharp
77
ms.custom: has-adal-ref, devx-track-csharp, devx-track-dotnet
88
---
@@ -20,53 +20,67 @@ You can lower maintenance overhead in your Azure Batch applications by using the
2020
2121
## Create and delete Batch accounts
2222

23-
One of the primary features of the Batch Management API is to create and delete [Batch accounts](accounts.md) in an Azure region. To do so, use [BatchManagementClient.Account.CreateAsync](/dotnet/api/microsoft.azure.management.batch.batchaccountoperationsextensions.createasync) and [DeleteAsync](/dotnet/api/microsoft.azure.management.batch.batchaccountoperationsextensions.deleteasync), or their synchronous counterparts.
23+
One of the primary features of the Batch Management API is to create and delete [Batch accounts](accounts.md) in an Azure region. To do so, use [BatchAccountCollection.CreateOrUpdate](/dotnet/api/azure.resourcemanager.batch.batchaccountcollection.createorupdatea) and [Delete](/dotnet/api/azure.resourcemanager.batch.batchaccountresource.deletea), or their asynchronous counterparts.
2424

25-
The following code snippet creates an account, obtains the newly created account from the Batch service, and then deletes it. In this snippet and the others in this article, `batchManagementClient` is a fully initialized instance of [BatchManagementClient](/dotnet/api/microsoft.azure.management.batch.batchmanagementclient).
25+
The following code snippet creates an account, obtains the newly created account from the Batch service, and then deletes it.
2626

2727
```csharp
28-
// Create a new Batch account
29-
await batchManagementClient.Account.CreateAsync("MyResourceGroup",
30-
"mynewaccount",
31-
new BatchAccountCreateParameters() { Location = "West US" });
32-
33-
// Get the new account from the Batch service
34-
AccountResource account = await batchManagementClient.Account.GetAsync(
35-
"MyResourceGroup",
36-
"mynewaccount");
37-
38-
// Delete the account
39-
await batchManagementClient.Account.DeleteAsync("MyResourceGroup", account.Name);
28+
string subscriptionId = "Your SubscriptionID";
29+
string resourceGroupName = "Your ResourceGroup name";
30+
31+
var credential = new DefaultAzureCredential();
32+
ArmClient _armClient = new ArmClient(credential);
33+
34+
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);
35+
ResourceGroupResource resourceGroupResource = _armClient.GetResourceGroupResource(resourceGroupResourceId);
36+
37+
var data = new BatchAccountCreateOrUpdateContent(AzureLocation.EastUS);
38+
39+
// Create a new batch account
40+
resourceGroupResource.GetBatchAccounts().CreateOrUpdate(WaitUntil.Completed, "Your BatchAccount name", data);
41+
42+
// Get an existing batch account
43+
BatchAccountResource batchAccount = resourceGroupResource.GetBatchAccount("Your BatchAccount name");
44+
45+
// Delete the batch account
46+
batchAccount.Delete(WaitUntil.Completed);
4047
```
4148

4249
> [!NOTE]
43-
> Applications that use the Batch Management .NET library and its BatchManagementClient class require service administrator or coadministrator access to the subscription that owns the Batch account to be managed. For more information, see the Microsoft Entra ID section and the [AccountManagement](https://github.com/Azure-Samples/azure-batch-samples/tree/master/CSharp/AccountManagement) code sample.
50+
> Applications that use the Batch Management .NET library require service administrator or coadministrator access to the subscription that owns the Batch account to be managed. For more information, see the Microsoft Entra ID section and the [AccountManagement](https://github.com/Azure-Samples/azure-batch-samples/tree/master/CSharp/AccountManagement) code sample.
4451
4552
## Retrieve and regenerate account keys
4653

47-
Obtain primary and secondary account keys from any Batch account within your subscription by using [GetKeysAsync](/dotnet/api/microsoft.azure.management.batch.batchaccountoperationsextensions.getkeysasync). You can regenerate those keys by using [RegenerateKeyAsync](/dotnet/api/microsoft.azure.management.batch.batchaccountoperationsextensions.regeneratekeyasync).
54+
Obtain primary and secondary account keys from any Batch account within your subscription by using [GetKeys](/dotnet/api/azure.resourcemanager.batch.batchaccountresource.getkeys). You can regenerate those keys by using [RegenerateKey](/dotnet/api/microsoft.azure.management.batch.batchaccountoperationsextensions.regeneratekey).
4855

4956
```csharp
57+
string subscriptionId = "Your SubscriptionID";
58+
string resourceGroupName = "Your ResourceGroup name";
59+
60+
var credential = new DefaultAzureCredential();
61+
ArmClient _armClient = new ArmClient(credential);
62+
63+
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);
64+
ResourceGroupResource resourceGroupResource = _armClient.GetResourceGroupResource(resourceGroupResourceId);
65+
66+
var data = new BatchAccountCreateOrUpdateContent(AzureLocation.EastUS);
67+
68+
// Get an existing batch account
69+
BatchAccountResource batchAccount = resourceGroupResource.GetBatchAccount("Your BatchAccount name");
70+
5071
// Get and print the primary and secondary keys
51-
BatchAccountGetKeyResult accountKeys =
52-
await batchManagementClient.Account.GetKeysAsync(
53-
"MyResourceGroup",
54-
"mybatchaccount");
72+
BatchAccountKeys accountKeys = batchAccount.GetKeys();
73+
5574
Console.WriteLine("Primary key: {0}", accountKeys.Primary);
5675
Console.WriteLine("Secondary key: {0}", accountKeys.Secondary);
5776

5877
// Regenerate the primary key
59-
BatchAccountRegenerateKeyResponse newKeys =
60-
await batchManagementClient.Account.RegenerateKeyAsync(
61-
"MyResourceGroup",
62-
"mybatchaccount",
63-
new BatchAccountRegenerateKeyParameters() {
64-
KeyName = AccountKeyType.Primary
65-
});
78+
BatchAccountRegenerateKeyContent regenerateKeyContent = new BatchAccountRegenerateKeyContent(BatchAccountKeyType.Primary);
79+
batchAccount.RegenerateKey(regenerateKeyContent);
6680
```
6781

6882
> [!TIP]
69-
> You can create a streamlined connection workflow for your management applications. First, obtain an account key for the Batch account you wish to manage with [GetKeysAsync](/dotnet/api/microsoft.azure.management.batch.batchaccountoperationsextensions.getkeysasync). Then, use this key when initializing the Batch .NET library's [BatchSharedKeyCredentials](/dotnet/api/microsoft.azure.batch.auth.batchsharedkeycredentials) class, which is used when initializing [BatchClient](/dotnet/api/microsoft.azure.batch.batchclient).
83+
> You can create a streamlined connection workflow for your management applications. First, obtain an account key for the Batch account you wish to manage with [GetKeys](/dotnet/api/azure.resourcemanager.batch.batchaccountresource.getkeys). Then, use this key when initializing the Batch .NET library's [BatchSharedKeyCredentials](/dotnet/api/microsoft.azure.batch.auth.batchsharedkeycredentials) class, which is used when initializing [BatchClient](/dotnet/api/microsoft.azure.batch.batchclient).
7084
7185
## Check Azure subscription and Batch account quotas
7286

@@ -76,28 +90,30 @@ Azure subscriptions and the individual Azure services like Batch all have defaul
7690

7791
Before creating a Batch account in a region, you can check your Azure subscription to see whether you are able to add an account in that region.
7892

79-
In the code snippet below, we first use **ListAsync** to get a collection of all Batch accounts that are within a subscription. Once we've obtained this collection, we determine how many accounts are in the target region. Then we use **GetQuotasAsync** to obtain the Batch account quota and determine how many accounts (if any) can be created in that region.
93+
In the code snippet below, we first use **GetBatchAccounts** to get a collection of all Batch accounts that are within a subscription. Once we've obtained this collection, we determine how many accounts are in the target region. Then we use **GetBatchQuotas** to obtain the Batch account quota and determine how many accounts (if any) can be created in that region.
8094

8195
```csharp
96+
string subscriptionId = "Your SubscriptionID";
97+
ArmClient _armClient = new ArmClient(new DefaultAzureCredential());
98+
99+
ResourceIdentifier subscriptionResourceId = SubscriptionResource.CreateResourceIdentifier(subscriptionId);
100+
SubscriptionResource subscriptionResource = _armClient.GetSubscriptionResource(subscriptionResourceId);
101+
82102
// Get a collection of all Batch accounts within the subscription
83-
BatchAccountListResponse listResponse =
84-
await batchManagementClient.BatchAccount.ListAsync(new AccountListParameters());
85-
IList<AccountResource> accounts = listResponse.Accounts;
86-
Console.WriteLine("Total number of Batch accounts under subscription id {0}: {1}",
87-
creds.SubscriptionId,
88-
accounts.Count);
103+
var batchAccounts = subscriptionResource.GetBatchAccounts();
104+
Console.WriteLine("Total number of Batch accounts under subscription id {0}: {1}", subscriptionId, batchAccounts.Count());
89105

90106
// Get a count of all accounts within the target region
91-
string region = "westus";
92-
int accountsInRegion = accounts.Count(o => o.Location == region);
107+
string region = "eastus";
108+
int accountsInRegion = batchAccounts.Count(o => o.Data.Location == region);
93109

94110
// Get the account quota for the specified region
95-
SubscriptionQuotasGetResponse quotaResponse = await batchManagementClient.Location.GetQuotasAsync(region);
96-
Console.WriteLine("Account quota for {0} region: {1}", region, quotaResponse.AccountQuota);
111+
BatchLocationQuota batchLocationQuota = subscriptionResource.GetBatchQuotas(AzureLocation.EastUS);
112+
Console.WriteLine("Account quota for {0} region: {1}", region, batchLocationQuota.AccountQuota);
97113

98114
// Determine how many accounts can be created in the target region
99115
Console.WriteLine("Accounts in {0}: {1}", region, accountsInRegion);
100-
Console.WriteLine("You can create {0} accounts in the {1} region.", quotaResponse.AccountQuota - accountsInRegion, region);
116+
Console.WriteLine("You can create {0} accounts in the {1} region.", batchLocationQuota.AccountQuota - accountsInRegion, region);
101117
```
102118

103119
In the snippet above, `creds` is an instance of **TokenCredentials**. To see an example of creating this object, see the [AccountManagement](https://github.com/Azure-Samples/azure-batch-samples/tree/master/CSharp/AccountManagement) code sample on GitHub.
@@ -107,15 +123,22 @@ In the snippet above, `creds` is an instance of **TokenCredentials**. To see an
107123
Before increasing compute resources in your Batch solution, you can check to ensure the resources you want to allocate won't exceed the account's quotas. In the code snippet below, we print the quota information for the Batch account named `mybatchaccount`. In your own application, you could use such information to determine whether the account can handle the additional resources to be created.
108124

109125
```csharp
110-
// First obtain the Batch account
111-
BatchAccountGetResponse getResponse =
112-
await batchManagementClient.Account.GetAsync("MyResourceGroup", "mybatchaccount");
113-
AccountResource account = getResponse.Resource;
126+
string subscriptionId = "Your SubscriptionID";
127+
string resourceGroupName = "Your ResourceGroup name";
128+
129+
var credential = new DefaultAzureCredential();
130+
ArmClient _armClient = new ArmClient(credential);
131+
132+
ResourceIdentifier resourceGroupResourceId = ResourceGroupResource.CreateResourceIdentifier(subscriptionId, resourceGroupName);
133+
ResourceGroupResource resourceGroupResource = _armClient.GetResourceGroupResource(resourceGroupResourceId);
134+
135+
// Get an existing batch account
136+
BatchAccountResource batchAccount = resourceGroupResource.GetBatchAccount("Your BatchAccount name");
114137

115138
// Now print the compute resource quotas for the account
116-
Console.WriteLine("Core quota: {0}", account.Properties.CoreQuota);
117-
Console.WriteLine("Pool quota: {0}", account.Properties.PoolQuota);
118-
Console.WriteLine("Active job and job schedule quota: {0}", account.Properties.ActiveJobAndJobScheduleQuota);
139+
Console.WriteLine("Core quota: {0}", batchAccount.Data.DedicatedCoreQuota);
140+
Console.WriteLine("Pool quota: {0}", batchAccount.Data.PoolQuota);
141+
Console.WriteLine("Active job and job schedule quota: {0}", batchAccount.Data.ActiveJobAndJobScheduleQuota);
119142
```
120143

121144
> [!IMPORTANT]

0 commit comments

Comments
 (0)