public static void PrintBatchAccount(IBatchAccount batchAccount) { var applicationsOutput = new StringBuilder().Append("\n\tapplications: "); if (batchAccount.Applications.Count > 0) { foreach (var applicationEntry in batchAccount.Applications) { var application = applicationEntry.Value; var applicationPackages = new StringBuilder().Append("\n\t\t\tapplicationPackages : "); foreach (var applicationPackageEntry in application.ApplicationPackages) { var applicationPackage = applicationPackageEntry.Value; var singleApplicationPackage = new StringBuilder().Append("\n\t\t\t\tapplicationPackage : " + applicationPackage.Name); singleApplicationPackage.Append("\n\t\t\t\tapplicationPackageState : " + applicationPackage.State); applicationPackages.Append(singleApplicationPackage); singleApplicationPackage.Append("\n"); } var singleApplication = new StringBuilder().Append("\n\t\tapplication: " + application.Name); singleApplication.Append("\n\t\tdisplayName: " + application.DisplayName); singleApplication.Append("\n\t\tdefaultVersion: " + application.DefaultVersion); singleApplication.Append(applicationPackages); applicationsOutput.Append(singleApplication); applicationsOutput.Append("\n"); } } Console.WriteLine(new StringBuilder().Append("BatchAccount: ").Append(batchAccount.Id) .Append("Name: ").Append(batchAccount.Name) .Append("\n\tResource group: ").Append(batchAccount.ResourceGroupName) .Append("\n\tRegion: ").Append(batchAccount.Region) .Append("\n\tTags: ").Append(FormatDictionary(batchAccount.Tags)) .Append("\n\tAccountEndpoint: ").Append(batchAccount.AccountEndpoint) .Append("\n\tPoolQuota: ").Append(batchAccount.PoolQuota) .Append("\n\tActiveJobAndJobScheduleQuota: ").Append(batchAccount.ActiveJobAndJobScheduleQuota) .Append("\n\tStorageAccount: ").Append(batchAccount.AutoStorage == null ? "No storage account attached" : batchAccount.AutoStorage.StorageAccountId) .Append(applicationsOutput) .ToString()); }
/** * 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"); int allowedNumberOfBatchAccounts = 0; int batchAccountsAtSpecificRegion = 0; try { // =========================================================== // Get how many batch accounts can be created in specified region. allowedNumberOfBatchAccounts = azure.BatchAccounts.GetBatchAccountQuotaByLocation(Region); // =========================================================== // List all the batch accounts in subscription. var batchAccounts = azure.BatchAccounts.List(); 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 { if (batchAccountsAtSpecificRegion >= allowedNumberOfBatchAccounts) { Utilities.Log("Deleting Resource Group: " + rgName); azure.ResourceGroups.DeleteByName(rgName); Utilities.Log("Deleted Resource Group: " + rgName); } } catch (Exception exc) { Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); } } }
public static void Main(string[] args) { try { //================================================================= // Authenticate AzureCredentials credentials = AzureCredentials.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); var azure = Azure .Configure() .WithLogLevel(HttpLoggingDelegatingHandler.Level.BASIC) .Authenticate(credentials) .WithDefaultSubscription(); // Print selected subscription Console.WriteLine("Selected subscription: " + azure.SubscriptionId); 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) { Console.WriteLine("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 Console.WriteLine("Creating a batch Account"); var batchAccount = azure.BatchAccounts.Define(batchAccountName) .WithRegion(region) .WithNewResourceGroup(rgName) .DefineNewApplication(applicationName) .DefineNewApplicationPackage(applicationPackageName) .WithAllowUpdates(true) .WithDisplayName(applicationDisplayName) .Attach() .WithNewStorageAccount(storageAccountName) .Create(); Console.WriteLine("Created a batch Account:"); Utilities.PrintBatchAccount(batchAccount); // ============================================================ // Get | regenerate batch account access keys Console.WriteLine("Getting batch account access keys"); var batchAccountKeys = batchAccount.GetKeys(); Utilities.PrintBatchAccountKey(batchAccountKeys); Console.WriteLine("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.GetByGroup(rgName, storageAccountName); var storageAccountKeys = storageAccount.GetKeys(); Utilities.PrintStorageAccountKeys(storageAccountKeys); Console.WriteLine("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(applicationName) .WithDisplayName("New application display name") .Parent() .Apply(); batchAccount.Refresh(); Utilities.PrintBatchAccount(batchAccount); // ============================================================ // Create another batch account Console.WriteLine("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(); Console.WriteLine("Created second Batch Account:"); Utilities.PrintBatchAccount(batchAccount2); } // ============================================================ // List batch accounts Console.WriteLine("Listing Batch accounts"); var accounts = azure.BatchAccounts.ListByGroup(rgName); IBatchAccount ba; foreach (var account in accounts) { Console.WriteLine("Batch Account - " + account.Name); } // ============================================================ // Refresh a batch account. batchAccount.Refresh(); Utilities.PrintBatchAccount(batchAccount); // ============================================================ // Delete a batch account Console.WriteLine("Deleting a batch account - " + batchAccount.Name); foreach (var applicationEntry in batchAccount.Applications) { foreach (var applicationPackageEntry in applicationEntry.Value.ApplicationPackages) { Console.WriteLine("Deleting a application package - " + applicationPackageEntry.Key); applicationPackageEntry.Value.Delete(); } Console.WriteLine("Deleting a application - " + applicationEntry.Key); batchAccount.Update().WithoutApplication(applicationEntry.Key).Apply(); } try { azure.BatchAccounts.Delete(batchAccount.Id); } catch { } Console.WriteLine("Deleted batch account"); if (batchAccount2 != null) { Console.WriteLine("Deleting second batch account - " + batchAccount2.Name); try { azure.BatchAccounts.Delete(batchAccount2.Id); } catch { } Console.WriteLine("Deleted second batch account"); } } catch (Exception ex) { Console.WriteLine(ex); } finally { try { Console.WriteLine("Deleting Resource Group: " + rgName); azure.ResourceGroups.Delete(rgName); Console.WriteLine("Deleted Resource Group: " + rgName); } catch (Exception) { Console.WriteLine("Did not create any resources in Azure. No clean up is necessary"); } } } catch (Exception ex) { Console.WriteLine(ex); } }