/// <summary> /// Creates an account and resource group for use with the Scenario tests /// </summary> public static BatchAccountContext CreateTestAccountAndResourceGroup(BatchController controller, string resourceGroupName, string accountName, string location) { controller.ResourceManagementClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup() { Location = location }); BatchAccount createResponse = controller.BatchManagementClient.BatchAccount.Create(resourceGroupName, accountName, new BatchAccountCreateParameters() { Location = location }); BatchAccountContext context = BatchAccountContext.ConvertAccountResourceToNewAccountContext(createResponse); BatchAccountKeys response = controller.BatchManagementClient.BatchAccount.GetKeys(resourceGroupName, accountName); context.PrimaryAccountKey = response.Primary; context.SecondaryAccountKey = response.Secondary; return(context); }
public async Task CanCRUDBatchAccounts() { using (var context = FluentMockContext.Start(this.GetType().FullName)) { var rgName = TestUtilities.GenerateName("rgstg"); var batchAccountName = TestUtilities.GenerateName("batchaccount"); var storageAccountName = TestUtilities.GenerateName("sa"); try { var batchManager = TestHelper.CreateBatchManager(); // Create var batchAccount = await batchManager.BatchAccounts .Define(batchAccountName) .WithRegion(Region.AsiaEast) .WithNewResourceGroup(rgName) .CreateAsync(); Assert.Equal(rgName, batchAccount.ResourceGroupName); Assert.Null(batchAccount.AutoStorage); // List var accounts = batchManager.BatchAccounts.ListByResourceGroup(rgName); Assert.Contains(accounts, account => StringComparer.OrdinalIgnoreCase.Equals(account.Name, batchAccountName)); // Get batchAccount = batchManager.BatchAccounts.GetByResourceGroup(rgName, batchAccountName); Assert.NotNull(batchAccount); // Get Keys BatchAccountKeys keys = batchAccount.GetKeys(); Assert.NotNull(keys.Primary); Assert.NotNull(keys.Secondary); BatchAccountKeys newKeys = batchAccount.RegenerateKeys(AccountKeyType.Primary); Assert.NotNull(newKeys.Primary); Assert.NotNull(newKeys.Secondary); Assert.NotEqual(newKeys.Primary, keys.Primary); Assert.Equal(newKeys.Secondary, keys.Secondary); batchAccount = batchAccount.Update() .WithNewStorageAccount(storageAccountName) .Apply(); Assert.NotNull(batchAccount.AutoStorage.StorageAccountId); var lastSync = batchAccount.AutoStorage.LastKeySync; batchAccount.SynchronizeAutoStorageKeys(); batchAccount.Refresh(); Assert.NotEqual(lastSync, batchAccount.AutoStorage.LastKeySync); // Test applications. var applicationId = TestUtilities.GenerateName("myApplication"); var applicationDisplayName = TestUtilities.GenerateName("displayName"); var applicationPackageName = TestUtilities.GenerateName("applicationPackage"); var updatesAllowed = true; batchAccount.Update() .DefineNewApplication(applicationId) .DefineNewApplicationPackage(applicationPackageName) .WithDisplayName(applicationDisplayName) .WithAllowUpdates(updatesAllowed) .Attach() .Apply(); Assert.True(batchAccount.Applications.ContainsKey(applicationId)); // Refresh to fetch batch account and application again. batchAccount.Refresh(); Assert.True(batchAccount.Applications.ContainsKey(applicationId)); var application = batchAccount.Applications[applicationId]; Assert.Equal(application.DisplayName, applicationDisplayName); Assert.Equal(application.UpdatesAllowed, updatesAllowed); Assert.Equal(1, application.ApplicationPackages.Count); var applicationPackage = application.ApplicationPackages[applicationPackageName]; Assert.Equal(applicationPackage.Name, applicationPackageName); // Delete application package directly. applicationPackage.Delete(); batchAccount .Update() .WithoutApplication(applicationId) .Apply(); batchAccount.Refresh(); Assert.False(batchAccount.Applications.ContainsKey(applicationId)); var applicationPackage1Name = TestUtilities.GenerateName("applicationPackage1"); var applicationPackage2Name = TestUtilities.GenerateName("applicationPackage2"); batchAccount.Update() .DefineNewApplication(applicationId) .DefineNewApplicationPackage(applicationPackage1Name) .DefineNewApplicationPackage(applicationPackage2Name) .WithDisplayName(applicationDisplayName) .WithAllowUpdates(updatesAllowed) .Attach() .Apply(); Assert.True(batchAccount.Applications.ContainsKey(applicationId)); application.Refresh(); Assert.Equal(2, application.ApplicationPackages.Count); var newApplicationDisplayName = "newApplicationDisplayName"; batchAccount .Update() .UpdateApplication(applicationId) .WithoutApplicationPackage(applicationPackage2Name) .WithDisplayName(newApplicationDisplayName) .Parent() .Apply(); application = batchAccount.Applications[applicationId]; Assert.Equal(application.DisplayName, newApplicationDisplayName); batchAccount.Refresh(); application = batchAccount.Applications[applicationId]; Assert.Equal(application.DisplayName, newApplicationDisplayName); Assert.Equal(1, application.ApplicationPackages.Count); applicationPackage = application.ApplicationPackages[applicationPackage1Name]; Assert.NotNull(applicationPackage); Assert.NotNull(applicationPackage.Id); Assert.Equal(applicationPackage.Name, applicationPackage1Name); Assert.Null(applicationPackage.Format); batchAccount .Update() .UpdateApplication(applicationId) .WithoutApplicationPackage(applicationPackage1Name) .Parent() .Apply(); try { await batchManager.BatchAccounts.DeleteByIdAsync(batchAccount.Id); } catch { } var batchAccounts = batchManager.BatchAccounts.ListByResourceGroup(rgName); Assert.Empty(batchAccounts); } finally { try { var resourceManager = TestHelper.CreateResourceManager(); resourceManager.ResourceGroups.DeleteByName(rgName); } catch { } } } }
public async Task BatchAccountEndToEndAsync() { using (MockContext context = StartMockContextAndInitializeClients(this.GetType().FullName)) { string resourceGroupName = TestUtilities.GenerateName(); string batchAccountName = TestUtilities.GenerateName(); ResourceGroup group = new ResourceGroup(this.Location); await this.ResourceManagementClient.ResourceGroups.CreateOrUpdateWithHttpMessagesAsync(resourceGroupName, group); try { // Check if the account exists var checkAvailabilityResult = await this.BatchManagementClient.Location.CheckNameAvailabilityAsync(this.Location, batchAccountName); Assert.True(checkAvailabilityResult.NameAvailable); // Create an account BatchAccountCreateParameters createParams = new BatchAccountCreateParameters(this.Location); await this.BatchManagementClient.BatchAccount.CreateAsync(resourceGroupName, batchAccountName, createParams); // Check if the account exists now checkAvailabilityResult = await this.BatchManagementClient.Location.CheckNameAvailabilityAsync(this.Location, batchAccountName); Assert.False(checkAvailabilityResult.NameAvailable); Assert.NotNull(checkAvailabilityResult.Message); Assert.NotNull(checkAvailabilityResult.Reason); // Get the account and verify some properties BatchAccount batchAccount = await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); Assert.Equal(batchAccountName, batchAccount.Name); Assert.True(batchAccount.DedicatedCoreQuota > 0); Assert.True(batchAccount.LowPriorityCoreQuota > 0); // Rotate a key BatchAccountKeys originalKeys = await this.BatchManagementClient.BatchAccount.GetKeysAsync(resourceGroupName, batchAccountName); BatchAccountKeys newKeys = await this.BatchManagementClient.BatchAccount.RegenerateKeyAsync(resourceGroupName, batchAccountName, AccountKeyType.Primary); Assert.NotEqual(originalKeys.Primary, newKeys.Primary); Assert.Equal(originalKeys.Secondary, newKeys.Secondary); // List accounts under the resource group IPage <BatchAccount> listResponse = await this.BatchManagementClient.BatchAccount.ListByResourceGroupAsync(resourceGroupName); List <BatchAccount> accounts = new List <BatchAccount>(listResponse); string nextLink = listResponse.NextPageLink; while (nextLink != null) { listResponse = await this.BatchManagementClient.BatchAccount.ListByResourceGroupNextAsync(nextLink); accounts.AddRange(listResponse); nextLink = listResponse.NextPageLink; } Assert.Single(accounts); Assert.Equal(batchAccountName, accounts.First().Name); // Delete the account try { await this.BatchManagementClient.BatchAccount.DeleteAsync(resourceGroupName, batchAccountName); } 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; } } // Verify account was deleted. A GET operation will return a 404 error and result in an exception try { await this.BatchManagementClient.BatchAccount.GetAsync(resourceGroupName, batchAccountName); } catch (CloudException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } } finally { await this.ResourceManagementClient.ResourceGroups.DeleteWithHttpMessagesAsync(resourceGroupName); } } }
public static void PrintBatchAccountKey(BatchAccountKeys batchAccountKeys) { Console.WriteLine("Primary Key (" + batchAccountKeys.Primary + ") Secondary key = (" + batchAccountKeys.Secondary + ")"); }
/// <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(" Dedicated core quota: {0}", account.DedicatedCoreQuota); Console.WriteLine(" Low priority core quota: {0}", account.LowPriorityCoreQuota); 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(); } }