/// <summary> /// Performs various Batch account operations using the Batch Management library. /// </summary> /// <param name="context">The <see cref="Microsoft.Azure.Common.Authentication.Models.AzureContext"/> containing information /// about the user's Azure account and subscription.</param> /// <param name="accountName">The name of the Batch account to create.</param> /// <param name="location">The location where the Batch account will be created.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> private static async Task PerformBatchAccountOperationsAsync(AzureContext context, string accountName, string location) { using (IBatchManagementClient batchManagementClient = AzureSession.ClientFactory.CreateClient <BatchManagementClient>(context, AzureEnvironment.Endpoint.ResourceManager)) { // Get the account quota for the subscription SubscriptionQuotasGetResponse quotaResponse = await batchManagementClient.Subscriptions.GetSubscriptionQuotasAsync(location); Console.WriteLine("Your subscription can create {0} account(s) in the {1} region.", quotaResponse.AccountQuota, location); Console.WriteLine(); // Create account Console.WriteLine("Creating account {0} ...", accountName); await batchManagementClient.Accounts.CreateAsync(ResourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location }); Console.WriteLine("Account {0} created", accountName); Console.WriteLine(); // Get account Console.WriteLine("Getting account {0} ...", accountName); BatchAccountGetResponse getResponse = await batchManagementClient.Accounts.GetAsync(ResourceGroupName, accountName); AccountResource account = getResponse.Resource; Console.WriteLine("Got account {0}:", accountName); Console.WriteLine(" Account location: {0}", account.Location); Console.WriteLine(" Account resource type: {0}", account.Type); Console.WriteLine(" Account id: {0}", account.Id); Console.WriteLine(" Core quota: {0}", account.Properties.CoreQuota); Console.WriteLine(" Pool quota: {0}", account.Properties.PoolQuota); Console.WriteLine(" Active job and job schedule quota: {0}", account.Properties.ActiveJobAndJobScheduleQuota); Console.WriteLine(); // Get account keys Console.WriteLine("Getting account keys of account {0} ....", accountName); BatchAccountListKeyResponse accountKeys = await batchManagementClient.Accounts.ListKeysAsync(ResourceGroupName, accountName); Console.WriteLine("Primary key of account {0}:", accountName); Console.WriteLine(accountKeys.PrimaryKey); Console.WriteLine("Secondary key of account {0}:", accountName); Console.WriteLine(accountKeys.SecondaryKey); Console.WriteLine(); // Regenerate account key Console.WriteLine("Regenerating the primary key of account {0} ....", accountName); BatchAccountRegenerateKeyResponse newKeys = await batchManagementClient.Accounts.RegenerateKeyAsync(ResourceGroupName, accountName, new BatchAccountRegenerateKeyParameters() { KeyName = AccountKeyType.Primary }); Console.WriteLine("New primary key of account {0}:", accountName); Console.WriteLine(newKeys.PrimaryKey); Console.WriteLine("Secondary key of account {0}:", accountName); Console.WriteLine(newKeys.SecondaryKey); Console.WriteLine(); // List accounts Console.WriteLine("Listing all Batch accounts under subscription id {0} ...", context.Subscription.Id); BatchAccountListResponse listResponse = await batchManagementClient.Accounts.ListAsync(new AccountListParameters()); IList <AccountResource> accounts = listResponse.Accounts; Console.WriteLine("Total number of Batch accounts under subscription id {0}: {1}", context.Subscription.Id, accounts.Count); for (int i = 0; i < accounts.Count; i++) { Console.WriteLine(" {0} - {1}", i + 1, accounts[i].Name); } Console.WriteLine(); // Delete account Console.WriteLine("Deleting account {0} ...", accountName); await batchManagementClient.Accounts.DeleteAsync(ResourceGroupName, accountName); Console.WriteLine("Account {0} deleted", accountName); Console.WriteLine(); } }
/// <summary> /// Performs various Batch account operations using the Batch Management library. /// </summary> /// <param name="creds">The <see cref="Microsoft.Azure.TokenCloudCredentials"/> containing information about the user's /// Azure account and subscription.</param> /// <param name="location">The location where the Batch account will be created.</param> /// <returns>A <see cref="System.Threading.Tasks.Task"/> object that represents the asynchronous operation.</returns> private static async Task PerformBatchAccountOperationsAsync(TokenCloudCredentials creds, string location) { using (BatchManagementClient batchManagementClient = new BatchManagementClient(creds)) { // Get the account quota for the subscription SubscriptionQuotasGetResponse quotaResponse = await batchManagementClient.Subscriptions.GetSubscriptionQuotasAsync(location); Console.WriteLine("Your subscription can create {0} account(s) in the {1} region.", quotaResponse.AccountQuota, location); Console.WriteLine(); // Create account string accountName = PromptUserForAccountName(); Console.WriteLine("Creating account {0}...", accountName); await batchManagementClient.Accounts.CreateAsync(ResourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location }); Console.WriteLine("Account {0} created", accountName); Console.WriteLine(); // Get account Console.WriteLine("Getting account {0}...", accountName); BatchAccountGetResponse getResponse = await batchManagementClient.Accounts.GetAsync(ResourceGroupName, accountName); AccountResource account = getResponse.Resource; Console.WriteLine("Got account {0}:", account.Name); Console.WriteLine(" Account location: {0}", account.Location); Console.WriteLine(" Account resource type: {0}", account.Type); Console.WriteLine(" Account id: {0}", account.Id); Console.WriteLine(); // Print account quotas Console.WriteLine("Quotas for account {0}:", account.Name); Console.WriteLine(" Core quota: {0}", account.Properties.CoreQuota); Console.WriteLine(" Pool quota: {0}", account.Properties.PoolQuota); Console.WriteLine(" Active job and job schedule quota: {0}", account.Properties.ActiveJobAndJobScheduleQuota); Console.WriteLine(); // Get account keys Console.WriteLine("Getting account keys of account {0}...", account.Name); BatchAccountListKeyResponse accountKeys = await batchManagementClient.Accounts.ListKeysAsync(ResourceGroupName, account.Name); Console.WriteLine(" Primary key of account {0}: {1}", account.Name, accountKeys.PrimaryKey); Console.WriteLine(" Secondary key of account {0}: {1}", account.Name, accountKeys.SecondaryKey); Console.WriteLine(); // Regenerate primary account key Console.WriteLine("Regenerating the primary key of account {0}...", account.Name); BatchAccountRegenerateKeyResponse newKeys = await batchManagementClient.Accounts.RegenerateKeyAsync( ResourceGroupName, account.Name, new BatchAccountRegenerateKeyParameters() { KeyName = AccountKeyType.Primary }); Console.WriteLine(" New primary key of account {0}: {1}", account.Name, newKeys.PrimaryKey); Console.WriteLine(" Secondary key of account {0}: {1}", account.Name, newKeys.SecondaryKey); Console.WriteLine(); // Print subscription quota information BatchAccountListResponse listResponse = await batchManagementClient.Accounts.ListAsync(new AccountListParameters()); IList <AccountResource> accounts = listResponse.Accounts; Console.WriteLine("Total number of Batch accounts under subscription id {0}: {1}", creds.SubscriptionId, accounts.Count); // Determine how many additional accounts can be created in the target region int numAccountsInRegion = accounts.Count(o => o.Location == account.Location); Console.WriteLine("Accounts in {0}: {1}", account.Location, numAccountsInRegion); Console.WriteLine("You can create {0} more accounts in the {1} region.", quotaResponse.AccountQuota - numAccountsInRegion, account.Location); Console.WriteLine(); // List accounts in the subscription Console.WriteLine("Listing all Batch accounts under subscription id {0}...", creds.SubscriptionId); foreach (AccountResource acct in accounts) { Console.WriteLine(" {0} - {1} | Location: {2}", accounts.IndexOf(acct) + 1, acct.Name, acct.Location); } Console.WriteLine(); // Delete account Console.Write("Hit ENTER to delete account {0}: ", account.Name); Console.ReadLine(); Console.WriteLine("Deleting account {0}...", account.Name); await batchManagementClient.Accounts.DeleteAsync(ResourceGroupName, account.Name); Console.WriteLine("Account {0} deleted", account.Name); Console.WriteLine(); } }
// Performs various Batch account operations private static async Task PerformBatchAccountOperationsAsync(AzureProfile profile, string location) { using (IBatchManagementClient batchManagementClient = AzureSession.ClientFactory.CreateClient <BatchManagementClient>(profile, AzureEnvironment.Endpoint.ResourceManager)) { Console.WriteLine("The Batch account name must be 3 to 24 characters, and it must only contain lowercase letters and numbers."); Console.WriteLine("Please input the account name you want to create: "); string accountName = Console.ReadLine(); Console.WriteLine(); // Create account Console.WriteLine("Creating account {0} ...", accountName); await batchManagementClient.Accounts.CreateAsync(ResourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location }); Console.WriteLine("Account {0} created", accountName); Console.WriteLine(); // Get acount Console.WriteLine("Getting account {0} ...", accountName); BatchAccountGetResponse getRespone = await batchManagementClient.Accounts.GetAsync(ResourceGroupName, accountName); AccountResource account = getRespone.Resource; Console.WriteLine("Got account {0}:", accountName); Console.WriteLine(" Account location: {0}", account.Location); Console.WriteLine(" Account resource type: {0}", account.Type); Console.WriteLine(" Account id: {0}", account.Id); Console.WriteLine(); // Get account keys Console.WriteLine("Getting Account keys of account {0} ....", accountName); BatchAccountListKeyResponse accountKeys = await batchManagementClient.Accounts.ListKeysAsync(ResourceGroupName, accountName); Console.WriteLine("Primary key of account {0}:", accountName); Console.WriteLine(accountKeys.PrimaryKey); Console.WriteLine("Secondary key of account {0}:", accountName); Console.WriteLine(accountKeys.SecondaryKey); Console.WriteLine(); // Regenerate account key Console.WriteLine("Regenerating the primary key of account {0} ....", accountName); BatchAccountRegenerateKeyResponse newKeys = await batchManagementClient.Accounts.RegenerateKeyAsync(ResourceGroupName, accountName, new BatchAccountRegenerateKeyParameters() { KeyName = AccountKeyType.Primary }); Console.WriteLine("New primary key of account {0}:", accountName); Console.WriteLine(newKeys.PrimaryKey); Console.WriteLine("Secondary key of account {0}:", accountName); Console.WriteLine(newKeys.SecondaryKey); Console.WriteLine(); // list accounts Console.WriteLine("Listing all Batch accounts under subscription id {0} ...", profile.DefaultSubscription.Id); BatchAccountListResponse listResponse = await batchManagementClient.Accounts.ListAsync(new AccountListParameters()); IList <AccountResource> accounts = listResponse.Accounts; Console.WriteLine("Total number of Batch accounts under subscription id {0}: {1}", profile.DefaultSubscription.Id, accounts.Count); for (int i = 0; i < accounts.Count; i++) { Console.WriteLine(" {0} - {1}", i + 1, accounts[i].Name); } Console.WriteLine(); // delete account Console.WriteLine("Deleting account {0} ...", accountName); await batchManagementClient.Accounts.DeleteAsync(ResourceGroupName, accountName); Console.WriteLine("Account {0} deleted", accountName); Console.WriteLine(); } }