use of com.microsoft.azure.management.storage.StorageAccount in project azure-sdk-for-java by Azure.
the class ManageLinuxWebAppStorageAccountConnection 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) {
// New resources
final String suffix = ".azurewebsites.net";
final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
final String app1Url = app1Name + suffix;
final String storageName = SdkContext.randomResourceName("jsdkstore", 20);
final String containerName = SdkContext.randomResourceName("jcontainer", 20);
final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
try {
//============================================================
// Create a storage account for the web app to use
System.out.println("Creating storage account " + storageName + "...");
StorageAccount storageAccount = azure.storageAccounts().define(storageName).withRegion(Region.US_WEST).withNewResourceGroup(rgName).create();
String accountKey = storageAccount.getKeys().get(0).value();
String connectionString = String.format("DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s", storageAccount.name(), accountKey);
System.out.println("Created storage account " + storageAccount.name());
//============================================================
// Upload a few files to the storage account blobs
System.out.println("Uploading 2 blobs to container " + containerName + "...");
CloudBlobContainer container = setUpStorageAccount(connectionString, containerName);
uploadFileToContainer(container, "helloworld.war", ManageLinuxWebAppStorageAccountConnection.class.getResource("/helloworld.war").getPath());
uploadFileToContainer(container, "install_apache.sh", ManageLinuxWebAppStorageAccountConnection.class.getResource("/install_apache.sh").getPath());
System.out.println("Uploaded 2 blobs to container " + container.getName());
//============================================================
// Create a web app with a new app service plan
System.out.println("Creating web app " + app1Name + "...");
WebApp app1 = azure.webApps().define(app1Name).withRegion(Region.US_WEST).withExistingResourceGroup(rgName).withNewLinuxPlan(PricingTier.STANDARD_S1).withPublicDockerHubImage("tomcat:8-jre8").withStartUpCommand("/bin/bash -c \"sed -ie 's/appBase=\\\"webapps\\\"/appBase=\\\"\\\\/home\\\\/site\\\\/wwwroot\\\\/webapps\\\"/g' conf/server.xml && catalina.sh run\"").withConnectionString("storage.connectionString", connectionString, ConnectionStringType.CUSTOM).withAppSetting("storage.containerName", containerName).withAppSetting("PORT", "8080").create();
System.out.println("Created web app " + app1.name());
Utils.print(app1);
//============================================================
// Deploy a web app that connects to the storage account
// Source code: https://github.com/jianghaolu/azure-samples-blob-explorer
System.out.println("Deploying azure-samples-blob-traverser.war to " + app1Name + " through FTP...");
Utils.uploadFileToFtp(app1.getPublishingProfile(), "azure-samples-blob-traverser.war", ManageLinuxWebAppStorageAccountConnection.class.getResourceAsStream("/azure-samples-blob-traverser.war"));
System.out.println("Deployment azure-samples-blob-traverser.war to web app " + app1.name() + " completed");
Utils.print(app1);
// warm up
System.out.println("Warming up " + app1Url + "/azure-samples-blob-traverser...");
curl("http://" + app1Url + "/azure-samples-blob-traverser");
Thread.sleep(5000);
System.out.println("CURLing " + app1Url + "/azure-samples-blob-traverser...");
System.out.println(curl("http://" + app1Url + "/azure-samples-blob-traverser"));
return true;
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
} finally {
try {
System.out.println("Deleting Resource Group: " + rgName);
azure.resourceGroups().beginDeleteByName(rgName);
System.out.println("Deleted Resource Group: " + rgName);
} catch (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
use of com.microsoft.azure.management.storage.StorageAccount 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;
}
use of com.microsoft.azure.management.storage.StorageAccount 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;
}
use of com.microsoft.azure.management.storage.StorageAccount in project azure-sdk-for-java by Azure.
the class VirtualMachineScaleSetOperationsTests method canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings.
@Test
public void canUpdateVirtualMachineScaleSetWithExtensionProtectedSettings() throws Exception {
final String vmssName = generateRandomResourceName("vmss", 10);
final String uname = "jvuser";
final String password = "123OData!@#123";
ResourceGroup resourceGroup = this.resourceManager.resourceGroups().define(RG_NAME).withRegion(REGION).create();
StorageAccount storageAccount = this.storageManager.storageAccounts().define(generateRandomResourceName("stg", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).create();
List<StorageAccountKey> keys = storageAccount.getKeys();
Assert.assertNotNull(keys);
Assert.assertTrue(keys.size() > 0);
String storageAccountKey = keys.get(0).value();
final String storageConnectionString = String.format("DefaultEndpointsProtocol=http;AccountName=%s;AccountKey=%s", storageAccount.name(), storageAccountKey);
// Get the script to upload
//
InputStream scriptFileAsStream = VirtualMachineScaleSetOperationsTests.class.getResourceAsStream("/install_apache.sh");
// Get the size of the stream
//
int fileSize;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[256];
int bytesRead;
while ((bytesRead = scriptFileAsStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileSize = outputStream.size();
outputStream.close();
// Upload the script file as block blob
//
URI fileUri;
if (IS_MOCKED) {
fileUri = new URI("http://nonexisting.blob.core.windows.net/scripts/install_apache.sh");
} else {
CloudStorageAccount account = CloudStorageAccount.parse(storageConnectionString);
CloudBlobClient cloudBlobClient = account.createCloudBlobClient();
CloudBlobContainer container = cloudBlobClient.getContainerReference("scripts");
container.createIfNotExists();
CloudBlockBlob blob = container.getBlockBlobReference("install_apache.sh");
blob.upload(scriptFileAsStream, fileSize);
fileUri = blob.getUri();
}
List<String> fileUris = new ArrayList<>();
fileUris.add(fileUri.toString());
Network network = this.networkManager.networks().define(generateRandomResourceName("vmssvnet", 15)).withRegion(REGION).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0.0.0/28").withSubnet("subnet1", "10.0.0.0/28").create();
VirtualMachineScaleSet virtualMachineScaleSet = this.computeManager.virtualMachineScaleSets().define(vmssName).withRegion(REGION).withExistingResourceGroup(resourceGroup).withSku(VirtualMachineScaleSetSkuTypes.STANDARD_A0).withExistingPrimaryNetworkSubnet(network, "subnet1").withoutPrimaryInternetFacingLoadBalancer().withoutPrimaryInternalLoadBalancer().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(uname).withRootPassword(password).withUnmanagedDisks().withNewStorageAccount(generateRandomResourceName("stg", 15)).withExistingStorageAccount(storageAccount).defineNewExtension("CustomScriptForLinux").withPublisher("Microsoft.OSTCExtensions").withType("CustomScriptForLinux").withVersion("1.4").withMinorVersionAutoUpgrade().withPublicSetting("fileUris", fileUris).withProtectedSetting("commandToExecute", "bash install_apache.sh").withProtectedSetting("storageAccountName", storageAccount.name()).withProtectedSetting("storageAccountKey", storageAccountKey).attach().create();
// Validate extensions after create
//
Map<String, VirtualMachineScaleSetExtension> extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
VirtualMachineScaleSetExtension extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
// Retrieve scale set
VirtualMachineScaleSet scaleSet = this.computeManager.virtualMachineScaleSets().getById(virtualMachineScaleSet.id());
// Validate extensions after get
//
extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
// Update VMSS capacity
//
int newCapacity = (int) (scaleSet.capacity() + 1);
virtualMachineScaleSet.update().withCapacity(newCapacity).apply();
// Validate extensions after update
//
extensions = virtualMachineScaleSet.extensions();
Assert.assertNotNull(extensions);
Assert.assertEquals(1, extensions.size());
Assert.assertTrue(extensions.containsKey("CustomScriptForLinux"));
extension = extensions.get("CustomScriptForLinux");
Assert.assertNotNull(extension.publicSettings());
Assert.assertEquals(1, extension.publicSettings().size());
Assert.assertNotNull(extension.publicSettingsAsJsonString());
}
use of com.microsoft.azure.management.storage.StorageAccount in project azure-sdk-for-java by Azure.
the class VirtualMachineOperationsTests method canDeleteRelatedResourcesFromFailedParallelVMCreations.
@Test
@Ignore("Can't be played from recording for some reason...")
public void canDeleteRelatedResourcesFromFailedParallelVMCreations() {
final int desiredVMCount = 40;
final Region region = Region.US_EAST;
final String resourceGroupName = RG_NAME;
// Create one resource group for everything, to ensure no reliance on resource groups
ResourceGroup resourceGroup = resourceManager.resourceGroups().define(resourceGroupName).withRegion(region).create();
// Needed for tracking related resources
final Map<String, Collection<Creatable<? extends Resource>>> vmNonNicResourceDefinitions = new HashMap<>();
// Tracking NICs separately because they have to be deleted first
final Map<String, Creatable<NetworkInterface>> nicDefinitions = new HashMap<>();
final Map<String, Creatable<VirtualMachine>> vmDefinitions = new HashMap<>();
final Map<String, String> createdResourceIds = new HashMap<>();
final List<Throwable> errors = new ArrayList<>();
// Prepare a number of VM definitions along with their related resource definitions
for (int i = 0; i < desiredVMCount; i++) {
Collection<Creatable<? extends Resource>> relatedDefinitions = new ArrayList<>();
// Define a network for each VM
String networkName = SdkContext.randomResourceName("net", 14);
Creatable<Network> networkDefinition = networkManager.networks().define(networkName).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("10.0." + i + ".0/29");
relatedDefinitions.add(networkDefinition);
// Define a PIP for each VM
String pipName = SdkContext.randomResourceName("pip", 14);
PublicIPAddress.DefinitionStages.WithCreate pipDefinition = this.networkManager.publicIPAddresses().define(pipName).withRegion(region).withExistingResourceGroup(resourceGroup);
relatedDefinitions.add(pipDefinition);
// Define a NIC for each VM
String nicName = SdkContext.randomResourceName("nic", 14);
Creatable<NetworkInterface> nicDefinition = networkManager.networkInterfaces().define(nicName).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork(networkDefinition).withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(pipDefinition);
// Define a storage account for each VM
String storageAccountName = SdkContext.randomResourceName("st", 14);
Creatable<StorageAccount> storageAccountDefinition = storageManager.storageAccounts().define(storageAccountName).withRegion(region).withExistingResourceGroup(resourceGroup);
relatedDefinitions.add(storageAccountDefinition);
// Define an availability set for each VM
String availabilitySetName = SdkContext.randomResourceName("as", 14);
Creatable<AvailabilitySet> availabilitySetDefinition = computeManager.availabilitySets().define(availabilitySetName).withRegion(region).withExistingResourceGroup(resourceGroup);
relatedDefinitions.add(availabilitySetDefinition);
String vmName = SdkContext.randomResourceName("vm", 14);
// Define a VM
String userName;
if (i == desiredVMCount / 2) {
// Intentionally cause a failure in one of the VMs
userName = "";
} else {
userName = "tester";
}
Creatable<VirtualMachine> vmDefinition = computeManager.virtualMachines().define(vmName).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetworkInterface(nicDefinition).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withRootPassword("Abcdef.123456!").withNewStorageAccount(storageAccountDefinition).withSize(VirtualMachineSizeTypes.STANDARD_DS1_V2).withNewAvailabilitySet(availabilitySetDefinition);
// Keep track of all the related resource definitions based on the VM definition
vmNonNicResourceDefinitions.put(vmDefinition.key(), relatedDefinitions);
nicDefinitions.put(vmDefinition.key(), nicDefinition);
vmDefinitions.put(vmDefinition.key(), vmDefinition);
}
// Start the parallel creation of everything
computeManager.virtualMachines().createAsync(new ArrayList<>(vmDefinitions.values())).map(new Func1<Indexable, Indexable>() {
@Override
public Indexable call(Indexable createdResource) {
if (createdResource instanceof Resource) {
Resource resource = (Resource) createdResource;
System.out.println("Created: " + resource.id());
if (resource instanceof VirtualMachine) {
VirtualMachine virtualMachine = (VirtualMachine) resource;
// Record that this VM was created successfully
vmDefinitions.remove(virtualMachine.key());
// Remove the associated resources from cleanup list
vmNonNicResourceDefinitions.remove(virtualMachine.key());
// Remove the associated NIC from cleanup list
nicDefinitions.remove(virtualMachine.key());
} else {
// Add this related resource to potential cleanup list
createdResourceIds.put(resource.key(), resource.id());
}
}
return createdResource;
}
}).onErrorReturn(new Func1<Throwable, Indexable>() {
@Override
public Indexable call(Throwable throwable) {
errors.add(throwable);
return null;
}
}).toBlocking().last();
// Delete remaining successfully created NICs of failed VM creations
Collection<String> nicIdsToDelete = new ArrayList<>();
for (Creatable<NetworkInterface> nicDefinition : nicDefinitions.values()) {
String nicId = createdResourceIds.get(nicDefinition.key());
if (nicId != null) {
nicIdsToDelete.add(nicId);
}
}
if (!nicIdsToDelete.isEmpty()) {
networkManager.networkInterfaces().deleteByIds(nicIdsToDelete);
}
// Delete remaining successfully created resources of failed VM creations
Collection<Completable> deleteObservables = new ArrayList<>();
for (Collection<Creatable<? extends Resource>> relatedResources : vmNonNicResourceDefinitions.values()) {
for (Creatable<? extends Resource> resource : relatedResources) {
String createdResourceId = createdResourceIds.get(resource.key());
if (createdResourceId != null) {
deleteObservables.add(resourceManager.genericResources().deleteByIdAsync(createdResourceId));
}
}
}
// Delete as much as possible, postponing the errors till the end
Completable.mergeDelayError(deleteObservables).await();
// Show any errors
for (Throwable error : errors) {
System.out.println("\n### ERROR ###\n");
if (error instanceof CloudException) {
CloudException ce = (CloudException) error;
System.out.println("CLOUD EXCEPTION: " + ce.getMessage());
} else {
error.printStackTrace();
}
}
System.out.println("Number of failed/cleaned up VM creations: " + vmNonNicResourceDefinitions.size());
// Verifications
final int successfulVMCount = desiredVMCount - vmNonNicResourceDefinitions.size();
final int actualVMCount = computeManager.virtualMachines().listByResourceGroup(resourceGroupName).size();
System.out.println("Number of actual successful VMs: " + actualVMCount);
Assert.assertEquals(successfulVMCount, actualVMCount);
final int actualNicCount = networkManager.networkInterfaces().listByResourceGroup(resourceGroupName).size();
Assert.assertEquals(successfulVMCount, actualNicCount);
final int actualNetworkCount = networkManager.networks().listByResourceGroup(resourceGroupName).size();
Assert.assertEquals(successfulVMCount, actualNetworkCount);
final int actualPipCount = networkManager.publicIPAddresses().listByResourceGroup(resourceGroupName).size();
Assert.assertEquals(successfulVMCount, actualPipCount);
final int actualAvailabilitySetCount = computeManager.availabilitySets().listByResourceGroup(resourceGroupName).size();
Assert.assertEquals(successfulVMCount, actualAvailabilitySetCount);
final int actualStorageAccountCount = storageManager.storageAccounts().listByResourceGroup(resourceGroupName).size();
Assert.assertEquals(successfulVMCount, actualStorageAccountCount);
// Verify that at least one VM failed.
// TODO: Ideally only one, but today the internal RX logic terminates eagerly -- need to change that for parallel creation to terminate more "lazily" in the future
Assert.assertTrue(successfulVMCount < desiredVMCount);
}
Aggregations