Search in sources :

Example 1 with StorageAccounts

use of com.microsoft.azure.management.storage.StorageAccounts in project azure-sdk-for-java by Azure.

the class ManageStorageAccountAsync 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(final Azure azure) {
    final String storageAccountName = Utils.createRandomName("sa");
    final String storageAccountName2 = Utils.createRandomName("sa2");
    final String rgName = Utils.createRandomName("rgSTMS");
    try {
        // ============================================================
        // Create storage accounts
        System.out.println("Creating a Storage Accounts");
        Observable.merge(azure.storageAccounts().define(storageAccountName).withRegion(Region.US_EAST).withNewResourceGroup(rgName).createAsync(), azure.storageAccounts().define(storageAccountName2).withRegion(Region.US_EAST).withNewResourceGroup(rgName).createAsync()).map(new Func1<Indexable, Indexable>() {

            @Override
            public Indexable call(Indexable indexable) {
                if (indexable instanceof StorageAccount) {
                    StorageAccount storageAccount = (StorageAccount) indexable;
                    System.out.println("Created a Storage Account:");
                    Utils.print(storageAccount);
                }
                return indexable;
            }
        }).toBlocking().last();
        // ============================================================
        // List storage accounts and regenerate storage account access keys
        System.out.println("Listing storage accounts");
        StorageAccounts storageAccounts = azure.storageAccounts();
        storageAccounts.listByResourceGroupAsync(rgName).flatMap(new Func1<StorageAccount, Observable<List<StorageAccountKey>>>() {

            @Override
            public Observable<List<StorageAccountKey>> call(final StorageAccount storageAccount) {
                System.out.println("Getting storage account access keys for Storage Account " + storageAccount.name() + " created @ " + storageAccount.creationTime());
                return storageAccount.getKeysAsync().flatMap(new Func1<List<StorageAccountKey>, Observable<List<StorageAccountKey>>>() {

                    @Override
                    public Observable<List<StorageAccountKey>> call(List<StorageAccountKey> storageAccountKeys) {
                        System.out.println("Regenerating first storage account access key");
                        return storageAccount.regenerateKeyAsync(storageAccountKeys.get(0).keyName());
                    }
                });
            }
        }).map(new Func1<List<StorageAccountKey>, List<StorageAccountKey>>() {

            @Override
            public List<StorageAccountKey> call(List<StorageAccountKey> storageAccountKeys) {
                Utils.print(storageAccountKeys);
                return storageAccountKeys;
            }
        }).toBlocking().last();
        // ============================================================
        // Delete storage accounts
        storageAccounts.listByResourceGroupAsync(rgName).flatMap(new Func1<StorageAccount, Observable<Void>>() {

            @Override
            public Observable<Void> call(StorageAccount storageAccount) {
                System.out.println("Deleting a storage account - " + storageAccount.name() + " created @ " + storageAccount.creationTime());
                return azure.storageAccounts().deleteByIdAsync(storageAccount.id()).toObservable();
            }
        }).toCompletable().await();
        return true;
    } catch (Exception f) {
        System.out.println(f.getMessage());
        f.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().deleteByNameAsync(rgName).await();
            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;
}
Also used : Observable(rx.Observable) StorageAccount(com.microsoft.azure.management.storage.StorageAccount) StorageAccountKey(com.microsoft.azure.management.storage.StorageAccountKey) Indexable(com.microsoft.azure.management.resources.fluentcore.model.Indexable) List(java.util.List) StorageAccounts(com.microsoft.azure.management.storage.StorageAccounts) Func1(rx.functions.Func1)

Example 2 with StorageAccounts

use of com.microsoft.azure.management.storage.StorageAccounts in project azure-sdk-for-java by Azure.

the class ManageStorageAccount 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 storageAccountName = Utils.createRandomName("sa");
    final String storageAccountName2 = Utils.createRandomName("sa2");
    final String rgName = Utils.createRandomName("rgSTMS");
    try {
        // ============================================================
        // Create a storage account
        System.out.println("Creating a Storage Account");
        StorageAccount storageAccount = azure.storageAccounts().define(storageAccountName).withRegion(Region.US_EAST).withNewResourceGroup(rgName).create();
        System.out.println("Created a Storage Account:");
        Utils.print(storageAccount);
        // ============================================================
        // Get | regenerate storage account access keys
        System.out.println("Getting storage account access keys");
        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);
        // ============================================================
        // Create another storage account
        System.out.println("Creating a 2nd Storage Account");
        StorageAccount storageAccount2 = azure.storageAccounts().define(storageAccountName2).withRegion(Region.US_EAST).withNewResourceGroup(rgName).create();
        System.out.println("Created a Storage Account:");
        Utils.print(storageAccount2);
        // ============================================================
        // List storage accounts
        System.out.println("Listing storage accounts");
        StorageAccounts storageAccounts = azure.storageAccounts();
        List<StorageAccount> accounts = storageAccounts.listByResourceGroup(rgName);
        StorageAccount sa;
        for (int i = 0; i < accounts.size(); i++) {
            sa = (StorageAccount) accounts.get(i);
            System.out.println("Storage Account (" + i + ") " + sa.name() + " created @ " + sa.creationTime());
        }
        // ============================================================
        // Delete a storage account
        System.out.println("Deleting a storage account - " + storageAccount.name() + " created @ " + storageAccount.creationTime());
        azure.storageAccounts().deleteById(storageAccount.id());
        System.out.println("Deleted storage 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;
}
Also used : StorageAccount(com.microsoft.azure.management.storage.StorageAccount) StorageAccountKey(com.microsoft.azure.management.storage.StorageAccountKey) StorageAccounts(com.microsoft.azure.management.storage.StorageAccounts)

Aggregations

StorageAccount (com.microsoft.azure.management.storage.StorageAccount)2 StorageAccountKey (com.microsoft.azure.management.storage.StorageAccountKey)2 StorageAccounts (com.microsoft.azure.management.storage.StorageAccounts)2 Indexable (com.microsoft.azure.management.resources.fluentcore.model.Indexable)1 List (java.util.List)1 Observable (rx.Observable)1 Func1 (rx.functions.Func1)1