use of com.microsoft.azure.management.batch.ApplicationPackage in project azure-sdk-for-java by Azure.
the class Utils method print.
/**
* Prints batch account.
* @param batchAccount a Batch Account
*/
public static void print(BatchAccount batchAccount) {
StringBuilder applicationsOutput = new StringBuilder().append("\n\tapplications: ");
if (batchAccount.applications().size() > 0) {
for (Map.Entry<String, Application> applicationEntry : batchAccount.applications().entrySet()) {
Application application = applicationEntry.getValue();
StringBuilder applicationPackages = new StringBuilder().append("\n\t\t\tapplicationPackages : ");
for (Map.Entry<String, ApplicationPackage> applicationPackageEntry : application.applicationPackages().entrySet()) {
ApplicationPackage applicationPackage = applicationPackageEntry.getValue();
StringBuilder 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");
}
StringBuilder 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");
}
}
System.out.println(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(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());
}
use of com.microsoft.azure.management.batch.ApplicationPackage in project azure-sdk-for-java by Azure.
the class ManageBatchAccount method runSample.
/**
* Main function which runs the actual sample.
* @param azure instance of the azure client
* @return true if sample runs successfully
*/
public static boolean runSample(Azure azure) {
final String batchAccountName = Utils.createRandomName("ba");
final String storageAccountName = Utils.createRandomName("sa");
final String applicationName = "application";
final String applicationDisplayName = "My application display name";
final String applicationPackageName = "app_package";
final String batchAccountName2 = Utils.createRandomName("ba2");
final String rgName = Utils.createRandomName("rgBAMB");
final Region region = Region.AUSTRALIA_SOUTHEAST;
final Region region2 = Region.US_WEST;
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.
List<BatchAccount> batchAccounts = azure.batchAccounts().list();
int batchAccountsAtSpecificRegion = 0;
for (BatchAccount batchAccount : batchAccounts) {
if (batchAccount.region() == region) {
batchAccountsAtSpecificRegion++;
}
}
if (batchAccountsAtSpecificRegion >= allowedNumberOfBatchAccounts) {
System.out.println("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 false;
}
// ============================================================
// Create a batch account
System.out.println("Creating a batch Account");
BatchAccount batchAccount = azure.batchAccounts().define(batchAccountName).withRegion(region).withNewResourceGroup(rgName).defineNewApplication(applicationName).defineNewApplicationPackage(applicationPackageName).withAllowUpdates(true).withDisplayName(applicationDisplayName).attach().withNewStorageAccount(storageAccountName).create();
System.out.println("Created a batch Account:");
Utils.print(batchAccount);
// ============================================================
// Get | regenerate batch account access keys
System.out.println("Getting batch account access keys");
BatchAccountKeys batchAccountKeys = batchAccount.getKeys();
Utils.print(batchAccountKeys);
System.out.println("Regenerating primary batch account primary access key");
batchAccountKeys = batchAccount.regenerateKeys(AccountKeyType.PRIMARY);
Utils.print(batchAccountKeys);
// ============================================================
// Regenerate the keys for storage account
StorageAccount storageAccount = azure.storageAccounts().getByResourceGroup(rgName, storageAccountName);
List<StorageAccountKey> storageAccountKeys = storageAccount.getKeys();
Utils.print(storageAccountKeys);
System.out.println("Regenerating first storage account access key");
storageAccountKeys = storageAccount.regenerateKey(storageAccountKeys.get(0).keyName());
Utils.print(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();
Utils.print(batchAccount);
// ============================================================
// Create another batch account
System.out.println("Creating another Batch Account");
allowedNumberOfBatchAccounts = azure.batchAccounts().getBatchAccountQuotaByLocation(region2);
// ===========================================================
// List all the batch accounts in subscription.
batchAccounts = azure.batchAccounts().list();
batchAccountsAtSpecificRegion = 0;
for (BatchAccount batch : batchAccounts) {
if (batch.region() == region2) {
batchAccountsAtSpecificRegion++;
}
}
BatchAccount batchAccount2 = null;
if (batchAccountsAtSpecificRegion < allowedNumberOfBatchAccounts) {
batchAccount2 = azure.batchAccounts().define(batchAccountName2).withRegion(region2).withExistingResourceGroup(rgName).withExistingStorageAccount(storageAccount).create();
System.out.println("Created second Batch Account:");
Utils.print(batchAccount2);
}
// ============================================================
// List batch accounts
System.out.println("Listing Batch accounts");
List<BatchAccount> accounts = azure.batchAccounts().listByResourceGroup(rgName);
BatchAccount ba;
for (int i = 0; i < accounts.size(); i++) {
ba = accounts.get(i);
System.out.println("Batch Account (" + i + ") " + ba.name());
}
// ============================================================
// Refresh a batch account.
batchAccount.refresh();
Utils.print(batchAccount);
// ============================================================
// Delete a batch account
System.out.println("Deleting a batch account - " + batchAccount.name());
for (Map.Entry<String, Application> applicationEntry : batchAccount.applications().entrySet()) {
for (Map.Entry<String, ApplicationPackage> applicationPackageEntry : applicationEntry.getValue().applicationPackages().entrySet()) {
System.out.println("Deleting a application package - " + applicationPackageEntry.getKey());
applicationPackageEntry.getValue().delete();
}
System.out.println("Deleting a application - " + applicationEntry.getKey());
batchAccount.update().withoutApplication(applicationEntry.getKey()).apply();
}
azure.batchAccounts().deleteById(batchAccount.id());
System.out.println("Deleted batch account");
if (batchAccount2 != null) {
System.out.println("Deleting second batch account - " + batchAccount2.name());
azure.batchAccounts().deleteById(batchAccount2.id());
System.out.println("Deleted second batch account");
}
return true;
} catch (Exception f) {
System.out.println(f.getMessage());
f.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().deleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (Exception e) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
}
}
return false;
}
Aggregations