use of com.microsoft.azure.management.compute.VirtualMachine in project photon-model by vmware.
the class AzureDiskEnumerationServiceTest method testVMAndDiskEnumeration.
@Test
public void testVMAndDiskEnumeration() throws Throwable {
// Exit if it is mock
if (this.isMock) {
return;
}
this.resourceGroup = SdkContext.randomResourceName(RESOURCE_GROUP_NAME, RESOURCE_GROUP_NAME.length() + 5);
// Create a vm with one additional disk
VirtualMachine vm = getAzureSdkClients().getComputeManager().virtualMachines().define("TestVM").withRegion(Region.US_WEST).withNewResourceGroup(this.resourceGroup).withNewPrimaryNetwork("10.0.0.0/28").withPrimaryPrivateIPAddressDynamic().withoutPrimaryPublicIPAddress().withPopularLinuxImage(KnownLinuxVirtualMachineImage.UBUNTU_SERVER_16_04_LTS).withRootUsername(AzureTestUtil.AZURE_ADMIN_USERNAME).withRootPassword(AzureTestUtil.AZURE_ADMIN_PASSWORD).withNewDataDisk(5).create();
assertNotNull(vm);
DataDisk dataDisk = vm.storageProfile().dataDisks().get(0);
assertNotNull(dataDisk);
ComputeEnumerateResourceRequest resourceRequest = kickOffEnumeration();
ComputeEnumerateAdapterRequest request = new ComputeEnumerateAdapterRequest(resourceRequest, this.authState, this.computeHost);
// Run VM enumeration
patchServiceSynchronously(AzureComputeEnumerationAdapterService.SELF_LINK, request);
// Run disk enumeration
patchServiceSynchronously(AzureDiskEnumerationAdapterService.SELF_LINK, request);
// Verify disk state is created in local store
Map<String, DiskState> diskStateMap = ProvisioningUtils.getResourceStates(getHost(), DiskService.FACTORY_LINK, DiskState.class);
assertTrue(diskStateMap.keySet().stream().anyMatch(s -> s.equalsIgnoreCase(dataDisk.managedDisk().id())));
// Detach disk
vm.update().withoutDataDisk(dataDisk.lun()).apply();
// Run Disk enumeration
patchServiceSynchronously(AzureDiskEnumerationAdapterService.SELF_LINK, request);
// Verify if the status of disk state is updated to Available state or not
diskStateMap = ProvisioningUtils.getResourceStates(getHost(), DiskService.FACTORY_LINK, DiskState.class);
diskStateMap.values().forEach(diskState -> {
if (diskState.name.equalsIgnoreCase(dataDisk.name())) {
assertTrue("Status of disk state should be Available", diskState.status.equals(DiskService.DiskStatus.AVAILABLE));
}
});
}
use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.
the class VMArmModule method refreshItems.
@Override
protected void refreshItems() throws AzureCmdException {
List<Pair<String, String>> failedSubscriptions = new ArrayList<>();
try {
AzureManager azureManager = AuthMethodManager.getInstance().getAzureManager();
// not signed in
if (azureManager == null) {
return;
}
SubscriptionManager subscriptionManager = azureManager.getSubscriptionManager();
Set<String> sidList = subscriptionManager.getSelectedSubscriptionDetails().stream().filter(subscriptionDetail -> Objects.nonNull(subscriptionDetail) && subscriptionDetail.isSelected()).map(subscriptionDetail -> subscriptionDetail.getSubscriptionId()).collect(Collectors.toSet());
for (String sid : sidList) {
try {
Azure azure = azureManager.getAzure(sid);
List<VirtualMachine> virtualMachines = azure.virtualMachines().list();
for (VirtualMachine vm : virtualMachines) {
addChildNode(new VMNode(this, sid, vm));
}
} catch (Exception ex) {
failedSubscriptions.add(new ImmutablePair<>(sid, ex.getMessage()));
continue;
}
}
} catch (Exception ex) {
DefaultLoader.getUIHelper().logError("An error occurred when trying to load Virtual Machines\n\n" + ex.getMessage(), ex);
}
if (!failedSubscriptions.isEmpty()) {
StringBuilder errorMessage = new StringBuilder("An error occurred when trying to load Storage Accounts for the subscriptions:\n\n");
for (Pair error : failedSubscriptions) {
errorMessage.append(error.getKey()).append(": ").append(error.getValue()).append("\n");
}
DefaultLoader.getUIHelper().logError("An error occurred when trying to load Storage Accounts\n\n" + errorMessage.toString(), null);
}
}
use of com.microsoft.azure.management.compute.VirtualMachine in project azure-tools-for-java by Microsoft.
the class CreateVMWizard method performFinish.
@Override
public boolean performFinish() {
Operation operation = TelemetryManager.createOperation(VM, CREATE_VM);
AzureTaskManager.getInstance().runInBackground("Creating virtual machine " + name + "...", new Runnable() {
@Override
public void run() {
try {
operation.start();
byte[] certData = new byte[0];
if (!certificate.isEmpty()) {
File certFile = new File(certificate);
if (certFile.exists()) {
try (FileInputStream certStream = new FileInputStream(certFile)) {
certData = new byte[(int) certFile.length()];
if (certStream.read(certData) != certData.length) {
throw new Exception("Unable to process certificate: " + "stream longer than informed size.");
}
} finally {
}
}
}
// create storage account when use choose to create new one
if (Objects.nonNull(newStorageAccount)) {
storageAccount = new CreateStorageAccountTask(newStorageAccount).execute();
}
VirtualMachine vm = AzureSDKManager.createVirtualMachine(subscription.getId(), name, resourceGroupName, isNewResourceGroup, size.name(), region.getName(), virtualMachineImage, knownMachineImage, isKnownMachineImage, storageAccount, virtualNetwork, newNetwork, isNewNetwork, subnet, publicIpAddress, withNewPip, availabilitySet, withNewAvailabilitySet, userName, password, certData.length > 0 ? new String(certData) : null);
// update resource groups cache if new resource group was created when creating storage account
DefaultLoader.getIdeHelper().invokeLater(new Runnable() {
@Override
public void run() {
try {
node.addChildNode(new VMNode(node, subscription.getId(), vm));
} catch (AzureCmdException e) {
PluginUtil.displayErrorDialogWithAzureMsg(PluginUtil.getParentShell(), Messages.err, "An error occurred while refreshing the list of virtual machines.", e);
}
}
});
} catch (Exception e) {
EventUtil.logError(operation, ErrorType.userError, e, null, null);
DefaultLoader.getIdeHelper().invokeLater(new Runnable() {
public void run() {
PluginUtil.displayErrorDialogWithAzureMsg(PluginUtil.getParentShell(), "Error Creating Virtual Machine", "An error occurred while trying to create the specified virtual machine", e);
}
});
} finally {
operation.complete();
}
}
});
return true;
}
Aggregations