use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method queryForDiskStates.
/**
* Get all disk states related to given VMs
*/
private void queryForDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.size() == 0) {
logFine(() -> "No virtual machines found to be associated with local disks");
if (ctx.regions.isEmpty()) {
ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
} else {
ctx.subStage = ComputeEnumerationSubStages.UPDATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
}
}
ctx.diskStates.clear();
List<String> diskIdList = new ArrayList<>();
for (String instanceId : ctx.virtualMachines.keySet()) {
VirtualMachineInner virtualMachine = ctx.virtualMachines.get(instanceId);
String diskId = getVhdUri(virtualMachine);
if (diskId == null) {
continue;
}
diskIdList.add(diskId);
List<String> dataDiskIDList = getDataDisksID(virtualMachine, AzureUtils.isDiskManaged(virtualMachine));
if (null != dataDiskIDList && dataDiskIDList.size() > 0) {
diskIdList.addAll(dataDiskIDList);
}
}
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(DiskState.class).addInClause(DiskState.FIELD_NAME_ID, diskIdList, Occurance.SHOULD_OCCUR);
QueryByPages<DiskState> queryDiskStates = new QueryByPages<>(getHost(), qBuilder.build(), DiskState.class, ctx.parentCompute.tenantLinks, // endpointLink
null, ctx.parentCompute.documentSelfLink).setMaxPageSize(QueryUtils.MAX_RESULT_LIMIT).setClusterType(ServiceTypeCluster.INVENTORY_SERVICE);
queryDiskStates.collectDocuments(Collectors.toList()).whenComplete((diskStates, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
if (diskStates == null) {
return;
}
diskStates.forEach(diskState -> ctx.diskStates.put(diskState.id, diskState));
ctx.subStage = next;
handleSubStage(ctx);
});
}
use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method vmEnumerationCompletion.
/**
* Completion handler for VM enumeration call. For async calls to list VMs, Azure returns
* Observable<Page<VirtualMachineInner>>. The following completion subscribes to the Observable
* and Overrides call(<T>) to include logic to process pages of VMs when we receive them.
*/
private Action1<Page<VirtualMachineInner>> vmEnumerationCompletion(EnumerationContext ctx, ComputeEnumerationSubStages next) {
Action1<Page<VirtualMachineInner>> vmEnumCompletion = new Action1<Page<VirtualMachineInner>>() {
@Override
public void call(Page<VirtualMachineInner> virtualMachineInnerPage) {
List<VirtualMachineInner> virtualMachineInners = virtualMachineInnerPage.items();
ctx.enumNextPageLink = virtualMachineInnerPage.nextPageLink();
if (virtualMachineInners == null || virtualMachineInners.size() == 0) {
ctx.subStage = ComputeEnumerationSubStages.GET_COMPUTE_STATES;
handleSubStage(ctx);
return;
}
logInfo(() -> String.format("Retrieved %d VMs from Azure", virtualMachineInners.size()));
logFine(() -> String.format("Next page link %s", ctx.enumNextPageLink));
for (VirtualMachineInner virtualMachine : virtualMachineInners) {
// We don't want to process VMs that are being terminated.
if (AZURE_VM_TERMINATION_STATES.contains(virtualMachine.provisioningState())) {
logFine(() -> String.format("Not processing %s", virtualMachine.id()));
continue;
}
// Azure for some case changes the case of the vm id.
String vmId = virtualMachine.id().toLowerCase();
ctx.virtualMachines.put(vmId, virtualMachine);
ctx.vmIds.add(vmId);
}
logFine(() -> String.format("Processing %d VMs", ctx.vmIds.size()));
ctx.subStage = next;
handleSubStage(ctx);
}
};
return injectOperationContext(vmEnumCompletion);
}
use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method updateDiskStates.
/**
* Update disk states with additional custom properties
*/
private void updateDiskStates(EnumerationContext ctx, ComputeEnumerationSubStages next) {
if (ctx.virtualMachines.isEmpty()) {
logFine(() -> "No virtual machines found to be associated with local disks");
if (ctx.regions.isEmpty()) {
ctx.subStage = ComputeEnumerationSubStages.DISASSOCIATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
} else {
ctx.subStage = ComputeEnumerationSubStages.UPDATE_COMPUTE_STATES;
handleSubStage(ctx);
return;
}
}
Iterator<Entry<String, VirtualMachineInner>> iterator = ctx.virtualMachines.entrySet().iterator();
Collection<Operation> opCollection = new ArrayList<>();
while (iterator.hasNext()) {
Entry<String, VirtualMachineInner> vmEntry = iterator.next();
VirtualMachineInner virtualMachine = vmEntry.getValue();
if (virtualMachine.storageProfile() == null || virtualMachine.storageProfile().imageReference() == null) {
continue;
}
if (virtualMachine.storageProfile().osDisk() == null) {
logWarning(() -> "VM has empty OS disk.");
continue;
}
String osDiskUri = getVhdUri(virtualMachine);
if (osDiskUri == null) {
logFine(() -> String.format("OS Disk URI not found for vm: %s", virtualMachine.id()));
continue;
}
updateOSDiskProperties(ctx, virtualMachine, osDiskUri, opCollection);
// create data disk states and update their custom properties
updateDataDiskProperties(ctx, virtualMachine, opCollection);
}
if (opCollection.isEmpty()) {
logFine(() -> "No local disk states fount to update.");
ctx.subStage = next;
handleSubStage(ctx);
return;
}
OperationJoin.create(opCollection).setCompletion((ops, exs) -> {
if (exs != null) {
// TODO: https://jira-hzn.eng.vmware.com/browse/VSYM-3256
exs.values().forEach(ex -> logWarning(() -> String.format("Error: %s", ex.getMessage())));
}
logFine(() -> "Continue on to create network interfaces.");
ctx.subStage = next;
handleSubStage(ctx);
}).sendWith(this);
}
use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method patchVMInstanceDetails.
private DeferredResult<ComputeState> patchVMInstanceDetails(EnumerationContext ctx, VirtualMachinesInner vmOps, ComputeState computeState) {
String resourceGroupName = getResourceGroupName(computeState.id);
String vmName = computeState.name;
AzureDeferredResultServiceCallback<VirtualMachineInner> handler = new Default<>(this, "Load virtual machine instance view:" + vmName);
PhotonModelUtils.runInExecutor(this.executorService, () -> {
vmOps.getByResourceGroupAsync(resourceGroupName, vmName, InstanceViewTypes.INSTANCE_VIEW, handler);
}, handler::failure);
return handler.toDeferredResult().thenApply(vm -> {
for (InstanceViewStatus status : vm.instanceView().statuses()) {
if (computeState.creationTimeMicros == null && status.time() != null) {
computeState.creationTimeMicros = TimeUnit.MILLISECONDS.toMicros(status.time().getMillis());
}
if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_RUNNING)) {
computeState.powerState = PowerState.ON;
} else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_STOPPED)) {
computeState.powerState = PowerState.OFF;
} else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_DEALLOCATED)) {
computeState.powerState = PowerState.SUSPEND;
}
}
if (computeState.customProperties == null) {
computeState.customProperties = new HashMap<>();
}
computeState.customProperties.put(RESOURCE_GROUP_NAME, resourceGroupName);
computeState.endpointLinks.add(ctx.request.endpointLink);
computeState.type = ComputeType.VM_GUEST;
computeState.environmentName = ComputeDescription.ENVIRONMENT_NAME_AZURE;
return computeState;
});
}
use of com.microsoft.azure.management.compute.implementation.VirtualMachineInner in project photon-model by vmware.
the class AzurePowerServiceTest method assertVmCurrentPowerState.
private void assertVmCurrentPowerState(PowerState powerState) {
if (this.isMock) {
// return. Nothing provisioned on Azure so nothing to check
return;
}
try {
PowerState vmPowerState = PowerState.UNKNOWN;
VirtualMachineInner vm = AzureTestUtil.getAzureVirtualMachineWithExtension(this.computeManagementClient, azureVMName, azureVMName, EXPAND_INSTANCE_VIEW_PARAM);
for (InstanceViewStatus status : vm.instanceView().statuses()) {
if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_RUNNING)) {
vmPowerState = PowerState.ON;
} else if (status.code().equals(AzureConstants.AZURE_VM_POWER_STATE_STOPPED)) {
vmPowerState = PowerState.OFF;
}
}
assertEquals("VM current power state does not match expected value.", powerState, vmPowerState);
} catch (Exception e) {
fail("Unable to verify current Machine Power state on Azure");
e.printStackTrace();
}
}
Aggregations