This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Program.cs
256 lines (205 loc) · 10.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.Batch.Fluent.Models;
using Microsoft.Azure.Management.Batch.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
using System.Linq;
using Microsoft.Azure.Management.ResourceManager.Fluent;
namespace ManageBatchAccount
{
public class Program
{
private const string AppName = "application";
private const string AppDisplayName = "My application display name";
private const string AppPackageName = "app_package";
private static readonly Region Region = Region.AustraliaSouthEast;
private static readonly Region Region2 = Region.USCentral;
/**
* Azure Batch sample for managing batch accounts -
* - Get subscription batch account quota for a particular location.
* - List all the batch accounts, look if quota allows you to create a new batch account at specified location by counting batch accounts in that particular location.
* - Create a batch account with new application and application package, along with new storage account.
* - Get the keys for batch account.
* - Regenerate keys for batch account
* - Regenerate the keys of storage accounts, sync with batch account.
* - Update application's display name.
* - Create another batch account using existing storage account.
* - List the batch account.
* - Delete the batch account.
* - Delete the application packages.
* - Delete applications.
*/
public static void RunSample(IAzure azure)
{
string batchAccountName = Utilities.CreateRandomName("ba");
string storageAccountName = Utilities.CreateRandomName("sa");
string batchAccountName2 = Utilities.CreateRandomName("ba2");
string rgName = Utilities.CreateRandomName("rgBAMB");
try
{
// ===========================================================
// Get how many batch accounts can be created in specified region.
int allowedNumberOfBatchAccounts = azure.BatchAccounts.GetBatchAccountQuotaByLocation(Region);
// ===========================================================
// List all the batch accounts in subscription.
var batchAccounts = azure.BatchAccounts.List();
int batchAccountsAtSpecificRegion = batchAccounts.Count(x => x.Region == Region);
if (batchAccountsAtSpecificRegion >= allowedNumberOfBatchAccounts)
{
Utilities.Log("No more batch accounts can be created at "
+ Region + " region, this region already have "
+ batchAccountsAtSpecificRegion
+ " batch accounts, current quota to create batch account in "
+ Region + " region is " + allowedNumberOfBatchAccounts + ".");
return;
}
// ============================================================
// Create a batch account
Utilities.Log("Creating a batch Account");
var batchAccount = azure.BatchAccounts.Define(batchAccountName)
.WithRegion(Region)
.WithNewResourceGroup(rgName)
.DefineNewApplication(AppName)
.DefineNewApplicationPackage(AppPackageName)
.WithAllowUpdates(true)
.WithDisplayName(AppDisplayName)
.Attach()
.WithNewStorageAccount(storageAccountName)
.Create();
Utilities.Log("Created a batch Account:");
Utilities.PrintBatchAccount(batchAccount);
// ============================================================
// Get | regenerate batch account access keys
Utilities.Log("Getting batch account access keys");
var batchAccountKeys = batchAccount.GetKeys();
Utilities.PrintBatchAccountKey(batchAccountKeys);
Utilities.Log("Regenerating primary batch account primary access key");
batchAccountKeys = batchAccount.RegenerateKeys(AccountKeyType.Primary);
Utilities.PrintBatchAccountKey(batchAccountKeys);
// ============================================================
// Regenerate the keys for storage account
var storageAccount = azure.StorageAccounts.GetByResourceGroup(rgName, storageAccountName);
var storageAccountKeys = storageAccount.GetKeys();
Utilities.PrintStorageAccountKeys(storageAccountKeys);
Utilities.Log("Regenerating first storage account access key");
storageAccountKeys = storageAccount.RegenerateKey(storageAccountKeys[0].KeyName);
Utilities.PrintStorageAccountKeys(storageAccountKeys);
// ============================================================
// Synchronize storage account keys with batch account
batchAccount.SynchronizeAutoStorageKeys();
// ============================================================
// Update name of application.
batchAccount
.Update()
.UpdateApplication(AppName)
.WithDisplayName("New application display name")
.Parent()
.Apply();
batchAccount.Refresh();
Utilities.PrintBatchAccount(batchAccount);
// ============================================================
// Create another batch account
Utilities.Log("Creating another Batch Account");
allowedNumberOfBatchAccounts = azure.BatchAccounts.GetBatchAccountQuotaByLocation(Region2);
// ===========================================================
// List all the batch accounts in subscription.
batchAccounts = azure.BatchAccounts.List();
batchAccountsAtSpecificRegion = batchAccounts.Count(x => x.Region == Region2);
IBatchAccount batchAccount2 = null;
if (batchAccountsAtSpecificRegion < allowedNumberOfBatchAccounts)
{
batchAccount2 = azure.BatchAccounts.Define(batchAccountName2)
.WithRegion(Region2)
.WithExistingResourceGroup(rgName)
.WithExistingStorageAccount(storageAccount)
.Create();
Utilities.Log("Created second Batch Account:");
Utilities.PrintBatchAccount(batchAccount2);
}
// ============================================================
// List batch accounts
Utilities.Log("Listing Batch accounts");
var accounts = azure.BatchAccounts.ListByResourceGroup(rgName);
foreach (var account in accounts)
{
Utilities.Log("Batch Account - " + account.Name);
}
// ============================================================
// Refresh a batch account.
batchAccount.Refresh();
Utilities.PrintBatchAccount(batchAccount);
// ============================================================
// Delete a batch account
Utilities.Log("Deleting a batch account - " + batchAccount.Name);
foreach (var applicationEntry in batchAccount.Applications)
{
foreach (var applicationPackageEntry in applicationEntry.Value.ApplicationPackages)
{
Utilities.Log("Deleting a application package - " + applicationPackageEntry.Key);
applicationPackageEntry.Value.Delete();
}
Utilities.Log("Deleting a application - " + applicationEntry.Key);
batchAccount.Update().WithoutApplication(applicationEntry.Key).Apply();
}
try
{
azure.BatchAccounts.DeleteById(batchAccount.Id);
}
catch
{
}
Utilities.Log("Deleted batch account");
if (batchAccount2 != null)
{
Utilities.Log("Deleting second batch account - " + batchAccount2.Name);
try
{
azure.BatchAccounts.DeleteById(batchAccount2.Id);
}
catch
{
}
Utilities.Log("Deleted second batch account");
}
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch (Exception)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
AzureCredentials credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}