Example #1
0
        /// <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();
            }
        }
Example #2
0
        /// <summary>
        /// Performs various Batch account operations using the Batch Management library.
        /// </summary>
        /// <param name="accessToken">The access token to use for authentication.</param>
        /// <param name="subscriptionId">The subscription id to use for creating the Batch management client</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(string accessToken, string subscriptionId, string location)
        {
            using (BatchManagementClient batchManagementClient = new BatchManagementClient(new TokenCredentials(accessToken)))
            {
                batchManagementClient.SubscriptionId = subscriptionId;

                // Get the account quota for the subscription
                BatchLocationQuota quotaResponse = await batchManagementClient.Location.GetQuotasAsync(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.BatchAccount.CreateAsync(ResourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location });
                Console.WriteLine("Account {0} created", accountName);
                Console.WriteLine();

                // Get account
                Console.WriteLine("Getting account {0}...", accountName);
                BatchAccount account = await batchManagementClient.BatchAccount.GetAsync(ResourceGroupName, accountName);
                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.CoreQuota);
                Console.WriteLine("  Pool quota: {0}", account.PoolQuota);
                Console.WriteLine("  Active job and job schedule quota: {0}", account.ActiveJobAndJobScheduleQuota);
                Console.WriteLine();

                // Get account keys
                Console.WriteLine("Getting account keys of account {0}...", account.Name);
                BatchAccountKeys accountKeys = await batchManagementClient.BatchAccount.GetKeysAsync(ResourceGroupName, account.Name);
                Console.WriteLine("  Primary key of account {0}:   {1}", account.Name, accountKeys.Primary);
                Console.WriteLine("  Secondary key of account {0}: {1}", account.Name, accountKeys.Secondary);
                Console.WriteLine();

                // Regenerate primary account key
                Console.WriteLine("Regenerating the primary key of account {0}...", account.Name);
                BatchAccountKeys newKeys = await batchManagementClient.BatchAccount.RegenerateKeyAsync(
                    ResourceGroupName, account.Name, 
                    AccountKeyType.Primary);
                Console.WriteLine("  New primary key of account {0}: {1}", account.Name, newKeys.Primary);
                Console.WriteLine("  Secondary key of account {0}:   {1}", account.Name, newKeys.Secondary);
                Console.WriteLine();

                // List total number of accounts under the subscription id
                IPage<BatchAccount> listResponse = await batchManagementClient.BatchAccount.ListAsync();
                var accounts = new List<BatchAccount>();
                accounts.AddRange(listResponse);

                var nextLink = listResponse.NextPageLink;
                while (nextLink != null)
                {
                    listResponse = await batchManagementClient.BatchAccount.ListNextAsync(nextLink);
                    accounts.AddRange(listResponse);
                    nextLink = listResponse.NextPageLink;
                }

                Console.WriteLine("Total number of Batch accounts under subscription id {0}:  {1}", batchManagementClient.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}...", batchManagementClient.SubscriptionId);
                foreach (BatchAccount 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);

                try
                {
                    await batchManagementClient.BatchAccount.DeleteAsync(ResourceGroupName, account.Name);
                }
                catch (CloudException ex)
                {
                    /*  Account deletion is a long running operation. This .DeleteAsync() method will submit the account deletion request and
                     *  poll for the status of the long running operation until the account is deleted. Currently, querying for the operation
                     *  status after the account is deleted will return a 404 error, so we have to add this catch statement. This behavior will
                     *  be fixed in a future service release.
                     */
                    if (ex.Response.StatusCode != HttpStatusCode.NotFound)
                    {
                        throw;
                    }
                }

                Console.WriteLine("Account {0} deleted", account.Name);
                Console.WriteLine();
            }
        }