use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method handlePatch.
@Override
public void handlePatch(Operation patch) {
// complete the patch immediately.
patch.complete();
logInfo("Received PATCH for incremental enumeration!");
VSphereIncrementalEnumerationRequest enumerationRequest = patch.getBody(VSphereIncrementalEnumerationRequest.class);
ComputeEnumerateResourceRequest request = enumerationRequest.request;
URI parentUri = ComputeService.ComputeStateWithDescription.buildUri(PhotonModelUriUtils.createInventoryUri(getHost(), request.resourceReference));
logInfo("Creating task manager!");
TaskManager mgr = new TaskManager(this, request.taskReference, request.resourceLink());
logInfo(" Requesting GET on compute state with description!.");
Operation.createGet(parentUri).setCompletion(o -> {
logInfo("Submitting job to threadpool!");
VsphereEnumerationHelper.submitWorkToVSpherePool(this, () -> {
logInfo("Incremental enumeration job started for endpoint %s", enumerationRequest.request.endpointLink);
ComputeStateWithDescription computeStateWithDesc = o.getBody(ComputeStateWithDescription.class);
VapiConnection vapiConnection = VapiConnection.createFromVimConnection(this.connection);
logInfo("Establishing VAPI connection for endpoint %s", enumerationRequest.request.endpointLink);
try {
vapiConnection.login();
} catch (IOException | RpcException rpce) {
logWarning(() -> String.format("Cannot login into vAPI endpoint: %s", Utils.toString(rpce)));
mgr.patchTaskToFailure(rpce);
// self delete service so that full enumeration kicks in next invocation.
selfDeleteService();
return;
}
try {
// Get instanceUuid of the vCenter
AboutInfo vCenter = this.connection.getServiceContent().getAbout();
for (CollectorDetails collectorDetails : this.collectors) {
logInfo("Retrieving resources incremental data for data center: %s", collectorDetails.datacenter);
EnumerationProgress enumerationProgress = new EnumerationProgress(new HashSet<>(), request, computeStateWithDesc, vapiConnection, collectorDetails.datacenter, vCenter.getInstanceUuid());
EnumerationClient client = new EnumerationClient(this.connection, computeStateWithDesc, VimUtils.convertStringToMoRef(collectorDetails.datacenter));
List<ObjectUpdate> resourcesUpdates = collectResourcesData(collectorDetails);
List<ObjectUpdate> vmUpdates = collectVMData(collectorDetails);
logInfo("Received resources updates for datacenter: %s : %s", collectorDetails.datacenter, resourcesUpdates.size());
logInfo("Received vm updates for datacenter: %s : %s", collectorDetails.datacenter, vmUpdates.size());
logInfo("Resources Updates: %s", Utils.toJson(resourcesUpdates));
logInfo("VM Updates: %s", Utils.toJson(vmUpdates));
if (!resourcesUpdates.isEmpty()) {
SegregatedOverlays segregatedOverlays = segregateObjectUpdates(enumerationProgress, resourcesUpdates);
this.logInfo("Processing incremental changes for folders for datacenter [%s]!", collectorDetails.datacenter);
VsphereFolderEnumerationHelper.handleFolderChanges(this, segregatedOverlays.folders, enumerationProgress, client);
logInfo("Processing incremental changes for networks for datacenter [%s]!", collectorDetails.datacenter);
VSphereNetworkEnumerationHelper.handleNetworkChanges(this, segregatedOverlays.networks, enumerationProgress, client);
logInfo("Processing incremental changes for Datastores for datacenter [%s]!", collectorDetails.datacenter);
VsphereDatastoreEnumerationHelper.handleDatastoreChanges(this, segregatedOverlays.datastores, enumerationProgress);
logInfo("Processing incremental changes for compute resource for datacenter [%s]!", collectorDetails.datacenter);
VsphereComputeResourceEnumerationHelper.handleComputeResourceChanges(this, segregatedOverlays.clusters, enumerationProgress, client);
logInfo("Processing incremental changes for host system for datacenter [%s]!", collectorDetails.datacenter);
VSphereHostSystemEnumerationHelper.handleHostSystemChanges(this, segregatedOverlays.hosts, enumerationProgress, client);
logInfo("Processing incremental changes for resource pool for datacenter [%s]!", collectorDetails.datacenter);
VSphereResourcePoolEnumerationHelper.handleResourcePoolChanges(this, segregatedOverlays.resourcePools, enumerationProgress, client);
}
if (!vmUpdates.isEmpty()) {
logInfo("Processing incremental changes for virtual machines for datacenter [%s]!", collectorDetails.datacenter);
VSphereVirtualMachineEnumerationHelper.handleVMChanges(this, vmUpdates, enumerationProgress, client);
}
// sync storage profiles
logInfo("Syncing storage profiles for datacenter [%s]!", collectorDetails.datacenter);
VsphereStoragePolicyEnumerationHelper.syncStorageProfiles(this, client, enumerationProgress);
}
mgr.patchTask(TaskStage.FINISHED);
} catch (Exception exception) {
String msg = "Error processing PropertyCollector results during incremental retrieval";
logWarning(() -> msg + ": " + exception.toString());
mgr.patchTaskToFailure(exception);
// self delete service so that full enumeration kicks in next invocation.
// TODO: This is not complete. We need to enable owner selection on this service.
selfDeleteService();
return;
} finally {
vapiConnection.close();
}
});
}, mgr).sendWith(this);
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class VSphereResourcePoolEnumerationHelper method handleResourcePoolChanges.
public static void handleResourcePoolChanges(VSphereIncrementalEnumerationService service, List<ResourcePoolOverlay> resourcePools, EnumerationProgress enumerationProgress, EnumerationClient client) {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
enumerationProgress.expectResourcePoolCount(resourcePools.size());
for (ResourcePoolOverlay resourcePool : resourcePools) {
// no need to collect the root resource pool
if (ObjectUpdateKind.ENTER.equals(resourcePool.getObjectUpdateKind()) && VimNames.TYPE_RESOURCE_POOL.equals(resourcePool.getParent().getType())) {
String ownerMoRefId = resourcePool.getOwner().getValue();
QueryTask task = queryForRPOwner(ownerMoRefId, enumerationProgress);
String selfLink = buildStableResourcePoolLink(resourcePool.getId(), request.endpointLink);
withTaskResults(service, task, result -> {
try {
if (!result.documentLinks.isEmpty()) {
ComputeState ownerDocument = convertOnlyResultToDocument(result, ComputeState.class);
createNewResourcePool(service, enumerationProgress, ownerDocument.name, selfLink, resourcePool, client);
} else {
// This happens for the resource pools within Host. The owner is a ComputeResource and
// is not currently enumerated in photon
createNewResourcePool(service, enumerationProgress, null, selfLink, resourcePool, client);
}
} catch (Exception e) {
enumerationProgress.getResourcePoolTracker().track();
}
});
} else {
String rpSelfLink = buildStableResourcePoolLink(resourcePool.getId(), request.endpointLink);
Operation.createGet(PhotonModelUriUtils.createInventoryUri(service.getHost(), rpSelfLink)).setCompletion((o, e) -> {
try {
if (e == null) {
ComputeState oldState = o.getBody(ComputeState.class);
String existingOwnerName = getOwnerNameFromResourcePoolName(oldState.name);
if (ObjectUpdateKind.MODIFY.equals(resourcePool.getObjectUpdateKind())) {
updateResourcePool(service, enumerationProgress, existingOwnerName, oldState.documentSelfLink, resourcePool, false, client);
} else {
Operation.createDelete(PhotonModelUriUtils.createInventoryUri(service.getHost(), rpSelfLink)).setCompletion(trackResourcePool(enumerationProgress, resourcePool)).sendWith(service);
}
} else {
enumerationProgress.getResourcePoolTracker().track();
}
} catch (Exception ex) {
enumerationProgress.getResourcePoolTracker().track();
}
}).sendWith(service);
}
}
try {
enumerationProgress.getResourcePoolTracker().await();
} catch (InterruptedException e) {
service.logSevere("Interrupted during incremental enumeration for resource pools!", e);
}
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class EnumerationClient method queryDisksAvailabilityinVSphere.
/**
* Utility method that crosschecks the availability of independent disks in vSphere.
*/
public List<String> queryDisksAvailabilityinVSphere(Map<String, Object> diskInfoInLocalIndex) {
final List<String> unAvailableDisks = new ArrayList<>();
diskInfoInLocalIndex.entrySet().stream().forEach(entry -> {
DiskService.DiskState diskState = Utils.fromJson(entry.getValue(), DiskService.DiskState.class);
String diskDirectoryPath = diskState.customProperties.get(CustomProperties.DISK_PARENT_DIRECTORY);
String datastoreName = diskState.customProperties.get(CustomProperties.DISK_DATASTORE_NAME);
HostDatastoreBrowserSearchSpec searchSpec = createHostDatastoreBrowserSearchSpecForDisk(diskState.id);
try {
this.getMoRef.entityProps(this.finder.datastore(datastoreName).object, "browser").entrySet().stream().forEach(item -> {
try {
ManagedObjectReference hostBrowser = (ManagedObjectReference) item.getValue();
ManagedObjectReference task = connection.getVimPort().searchDatastoreSubFoldersTask(hostBrowser, diskDirectoryPath, searchSpec);
TaskInfo info = VimUtils.waitTaskEnd(connection, task);
ArrayOfHostDatastoreBrowserSearchResults searchResult = (ArrayOfHostDatastoreBrowserSearchResults) info.getResult();
if (searchResult == null) {
// Folder is deleted.
unAvailableDisks.add(entry.getKey());
} else {
searchResult.getHostDatastoreBrowserSearchResults().stream().forEach(result -> {
// Folder is present but the vmdk file is deleted.
if (CollectionUtils.isEmpty(result.getFile())) {
unAvailableDisks.add(entry.getKey());
}
});
}
} catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg | InvalidCollectorVersionFaultMsg | FileFaultFaultMsg | InvalidDatastoreFaultMsg ex) {
logger.info("Unable to get the availability status for " + entry.getKey());
}
});
} catch (InvalidPropertyFaultMsg | RuntimeFaultFaultMsg | FinderException ex) {
logger.info("Unable to find the datastore : " + datastoreName);
}
});
return unAvailableDisks;
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class InstanceClient method customizeAfterClone.
private void customizeAfterClone() throws Exception {
VirtualMachineConfigSpec spec = new VirtualMachineConfigSpec();
// is takes precedence
if (this.ctx.child.description.cpuCount > 0) {
spec.setNumCPUs((int) this.ctx.child.description.cpuCount);
}
String gt = CustomProperties.of(this.ctx.child).getString(CustomProperties.GUEST_ID, null);
if (gt != null) {
spec.setGuestId(gt);
}
if (this.ctx.child.description.totalMemoryBytes > 0) {
spec.setMemoryMB(toMemoryMb(this.ctx.child.description.totalMemoryBytes));
}
recordTimestamp(spec.getExtraConfig());
// set ovf environment
ArrayOfVAppPropertyInfo infos = this.get.entityProp(this.vm, VimPath.vm_config_vAppConfig_property);
populateCloudConfig(spec, infos);
// remove nics and attach to proper networks if nics are configured
ArrayOfVirtualDevice devices = null;
if (this.ctx.nics != null && this.ctx.nics.size() > 0) {
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
devices.getVirtualDevice().stream().filter(d -> d instanceof VirtualEthernetCard).forEach(nic -> {
VirtualDeviceConfigSpec removeNicChange = new VirtualDeviceConfigSpec();
removeNicChange.setOperation(VirtualDeviceConfigSpecOperation.REMOVE);
removeNicChange.setDevice(nic);
spec.getDeviceChange().add(removeNicChange);
});
for (NetworkInterfaceStateWithDetails niState : this.ctx.nics) {
VirtualDevice nic = createNic(niState, null);
addDeviceToVm(spec, nic);
}
}
// Find whether it has HDD disk
if (this.imageDisks != null && !this.imageDisks.isEmpty()) {
// Iterate over each of the VirtualDisk and reconfigure if needed
if (devices == null) {
devices = this.get.entityProp(this.vm, VimPath.vm_config_hardware_device);
}
spec.getDeviceChange().addAll(getCustomizationConfigSpecs(devices, this.imageDisks));
}
// set the maximum snapshot limit if specified
final String snapshotLimit = CustomProperties.of(this.ctx.child).getString(CustomProperties.SNAPSHOT_MAXIMUM_LIMIT);
recordSnapshotLimit(spec.getExtraConfig(), snapshotLimit);
ManagedObjectReference task = getVimPort().reconfigVMTask(this.vm, spec);
TaskInfo info = waitTaskEnd(task);
if (info.getState() == TaskInfoState.ERROR) {
VimUtils.rethrow(info.getError());
}
// If there are any data disks then attach then to the VM
if (this.dataDisks != null && !this.dataDisks.isEmpty()) {
attachDisks(this.dataDisks, false);
}
// If there are any external disks, then attach then to the VM
attachExternalDisks();
}
use of com.vmware.vim25.mo.Task in project photon-model by vmware.
the class VSphereVirtualMachineEnumerationHelper method handleVMChanges.
public static void handleVMChanges(VSphereIncrementalEnumerationService service, List<ObjectUpdate> resourcesUpdates, EnumerationProgress enumerationProgress, EnumerationClient client) {
List<VmOverlay> vmOverlays = new ArrayList<>();
for (ObjectUpdate objectUpdate : resourcesUpdates) {
if (VimUtils.isVirtualMachine(objectUpdate.getObj())) {
VmOverlay vm = new VmOverlay(objectUpdate);
if (vm.getInstanceUuid() != null || !objectUpdate.getKind().equals(ObjectUpdateKind.ENTER)) {
vmOverlays.add(vm);
}
}
}
for (VmOverlay vmOverlay : vmOverlays) {
if (ObjectUpdateKind.ENTER == vmOverlay.getObjectUpdateKind()) {
createNewVm(service, enumerationProgress, vmOverlay);
} else {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
QueryTask task = queryForVm(enumerationProgress, request.resourceLink(), null, vmOverlay.getId());
VsphereEnumerationHelper.withTaskResults(service, task, result -> {
if (!result.documentLinks.isEmpty()) {
ComputeState oldDocument = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
if (ObjectUpdateKind.MODIFY == vmOverlay.getObjectUpdateKind()) {
updateVm(service, oldDocument, enumerationProgress, vmOverlay, false);
} else {
deleteVM(enumerationProgress, vmOverlay, service, oldDocument);
}
} else {
enumerationProgress.getVmTracker().arrive();
}
});
}
}
}
Aggregations