use of com.microsoft.azure.management.resources.fluentcore.arm.Region in project azure-tools-for-java by Microsoft.
the class AzureNewDockerConfigPage method updateDockerLocationComboBox.
private void updateDockerLocationComboBox(Composite mainContainer, AzureDockerSubscription currentSubscription) {
if (currentSubscription != null && currentSubscription.locations != null) {
dockerLocationComboBox.removeAll();
if (currentSubscription.locations.size() > 0) {
String previousSelection = preferredLocation;
preferredLocation = null;
for (String region : currentSubscription.locations) {
Region regionObj = Region.findByLabelOrName(region);
dockerLocationComboBox.add(regionObj != null ? regionObj.label() : region);
if ((previousSelection != null && region.equals(previousSelection)) || (newHost.hostVM.region != null && region.equals(newHost.hostVM.region))) {
preferredLocation = region;
dockerLocationComboBox.select(dockerLocationComboBox.getItemCount() - 1);
}
}
if (preferredLocation == null) {
dockerLocationComboBox.add(SELECT_REGION, 0);
dockerLocationComboBox.select(0);
setPageComplete(false);
}
}
updateDockerHostVMSizeComboBox(mainContainer, dockerHostVMPreferredSizesCheckBox.getSelection());
}
}
use of com.microsoft.azure.management.resources.fluentcore.arm.Region in project cloudbreak by hortonworks.
the class AzureClient method getVmTypes.
public Set<VirtualMachineSize> getVmTypes(String region) throws ProviderAuthenticationFailedException {
return handleAuthException(() -> {
Set<VirtualMachineSize> resultList = new HashSet<>();
if (region == null) {
for (Region tmpRegion : Region.values()) {
PagedList<VirtualMachineSize> virtualMachineSizes = azure.virtualMachines().sizes().listByRegion(Region.findByLabelOrName(tmpRegion.label()));
getAllElement(virtualMachineSizes, resultList);
}
}
PagedList<VirtualMachineSize> virtualMachineSizes = azure.virtualMachines().sizes().listByRegion(Region.findByLabelOrName(region));
getAllElement(virtualMachineSizes, resultList);
return resultList;
});
}
use of com.microsoft.azure.management.resources.fluentcore.arm.Region in project cloudbreak by hortonworks.
the class AzureClient method getCustomImageId.
public String getCustomImageId(String resourceGroup, String fromVhdUri, String region) {
String vhdName = fromVhdUri.substring(fromVhdUri.lastIndexOf('/') + 1);
String imageName = vhdName + '-' + region.toLowerCase().replaceAll("\\s", "");
PagedList<VirtualMachineCustomImage> customImageList = getCustomImageList(resourceGroup);
Optional<VirtualMachineCustomImage> virtualMachineCustomImage = customImageList.stream().filter(customImage -> customImage.name().equals(imageName) && customImage.region().label().equals(region)).findFirst();
if (virtualMachineCustomImage.isPresent()) {
LOGGER.info("custom image found in '{}' resource group with name '{}'", resourceGroup, imageName);
return virtualMachineCustomImage.get().id();
} else {
LOGGER.info("custom image NOT found in '{}' resource group with name '{}'", resourceGroup, imageName);
VirtualMachineCustomImage customImage = createCustomImage(imageName, resourceGroup, fromVhdUri, region);
return customImage.id();
}
}
use of com.microsoft.azure.management.resources.fluentcore.arm.Region 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);
}
use of com.microsoft.azure.management.resources.fluentcore.arm.Region in project azure-sdk-for-java by Azure.
the class CreateVirtualMachinesInParallel 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 rgName = SdkContext.randomResourceName("rgCOPD", 24);
final String userName = "tirekicker";
final String sshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.com";
Map<Region, Integer> virtualMachinesByLocation = new HashMap<Region, Integer>();
// debug target
/**
virtualMachinesByLocation.put(Region.US_EAST, 5);
virtualMachinesByLocation.put(Region.US_SOUTH_CENTRAL, 5);
*/
// final demo target
virtualMachinesByLocation.put(Region.US_EAST, 12);
virtualMachinesByLocation.put(Region.US_SOUTH_CENTRAL, 12);
virtualMachinesByLocation.put(Region.US_WEST, 12);
virtualMachinesByLocation.put(Region.US_NORTH_CENTRAL, 12);
try {
//=============================================================
// Create a resource group (Where all resources gets created)
//
ResourceGroup resourceGroup = azure.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
System.out.println("Created a new resource group - " + resourceGroup.id());
List<String> publicIpCreatableKeys = new ArrayList<>();
// Prepare a batch of Creatable definitions
//
List<Creatable<VirtualMachine>> creatableVirtualMachines = new ArrayList<>();
for (Map.Entry<Region, Integer> entry : virtualMachinesByLocation.entrySet()) {
Region region = entry.getKey();
Integer vmCount = entry.getValue();
//=============================================================
// Create 1 network creatable per region
// Prepare Creatable Network definition (Where all the virtual machines get added to)
//
String networkName = SdkContext.randomResourceName("vnetCOPD-", 20);
Creatable<Network> networkCreatable = azure.networks().define(networkName).withRegion(region).withExistingResourceGroup(resourceGroup).withAddressSpace("172.16.0.0/16");
//=============================================================
// Create 1 storage creatable per region (For storing VMs disk)
//
String storageAccountName = SdkContext.randomResourceName("stgcopd", 20);
Creatable<StorageAccount> storageAccountCreatable = azure.storageAccounts().define(storageAccountName).withRegion(region).withExistingResourceGroup(resourceGroup);
String linuxVMNamePrefix = SdkContext.randomResourceName("vm-", 15);
for (int i = 1; i <= vmCount; i++) {
//=============================================================
// Create 1 public IP address creatable
//
Creatable<PublicIPAddress> publicIPAddressCreatable = azure.publicIPAddresses().define(String.format("%s-%d", linuxVMNamePrefix, i)).withRegion(region).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(SdkContext.randomResourceName("pip", 10));
publicIpCreatableKeys.add(publicIPAddressCreatable.key());
//=============================================================
// Create 1 virtual machine creatable
Creatable<VirtualMachine> virtualMachineCreatable = azure.virtualMachines().define(String.format("%s-%d", linuxVMNamePrefix, i)).withRegion(region).withExistingResourceGroup(resourceGroup).withNewPrimaryNetwork(networkCreatable).withPrimaryPrivateIPAddressDynamic().withNewPrimaryPublicIPAddress(publicIPAddressCreatable).withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(userName).withSsh(sshKey).withSize(VirtualMachineSizeTypes.STANDARD_DS3_V2).withNewStorageAccount(storageAccountCreatable);
creatableVirtualMachines.add(virtualMachineCreatable);
}
}
//=============================================================
// Create !!
StopWatch stopwatch = new StopWatch();
System.out.println("Creating the virtual machines");
stopwatch.start();
CreatedResources<VirtualMachine> virtualMachines = azure.virtualMachines().create(creatableVirtualMachines);
stopwatch.stop();
System.out.println("Created virtual machines");
for (VirtualMachine virtualMachine : virtualMachines.values()) {
System.out.println(virtualMachine.id());
}
System.out.println("Virtual Machines created: (took " + (stopwatch.getTime() / 1000) + " seconds to create) == " + virtualMachines.size() + " == virtual machines");
List<String> publicIpResourceIds = new ArrayList<>();
for (String publicIpCreatableKey : publicIpCreatableKeys) {
PublicIPAddress pip = (PublicIPAddress) virtualMachines.createdRelatedResource(publicIpCreatableKey);
publicIpResourceIds.add(pip.id());
}
//=============================================================
// Create 1 Traffic Manager Profile
//
String trafficManagerName = SdkContext.randomResourceName("tra", 15);
TrafficManagerProfile.DefinitionStages.WithEndpoint profileWithEndpoint = azure.trafficManagerProfiles().define(trafficManagerName).withExistingResourceGroup(resourceGroup).withLeafDomainLabel(trafficManagerName).withPerformanceBasedRouting();
int endpointPriority = 1;
TrafficManagerProfile.DefinitionStages.WithCreate profileWithCreate = null;
for (String publicIpResourceId : publicIpResourceIds) {
String endpointName = String.format("azendpoint-%d", endpointPriority);
if (endpointPriority == 1) {
profileWithCreate = profileWithEndpoint.defineAzureTargetEndpoint(endpointName).toResourceId(publicIpResourceId).withRoutingPriority(endpointPriority).attach();
} else {
profileWithCreate = profileWithCreate.defineAzureTargetEndpoint(endpointName).toResourceId(publicIpResourceId).withRoutingPriority(endpointPriority).attach();
}
endpointPriority++;
}
System.out.println("Creating a traffic manager profile for the VMs");
stopwatch.reset();
stopwatch.start();
TrafficManagerProfile trafficManagerProfile = profileWithCreate.create();
stopwatch.stop();
System.out.println("Created a traffic manager profile (took " + (stopwatch.getTime() / 1000) + " seconds to create): " + trafficManagerProfile.id());
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 (NullPointerException npe) {
System.out.println("Did not create any resources in Azure. No clean up is necessary");
} catch (Exception g) {
g.printStackTrace();
}
}
return false;
}
Aggregations