use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method collectAllEndpointResources.
private DeferredResult<Set<String>> collectAllEndpointResources(ComputeEnumerateResourceRequest req, String parentLink) {
QueryTask.Query combinedClause = new QueryTask.Query().setOccurance(Occurance.MUST_OCCUR);
// The next clauses are the boolean clauses that will be added to the
// combinedClause. At the top, all further queries should have SHOULD_OCCUR
QueryTask.Query resourceClause = QueryTask.Query.Builder.create().addFieldClause(ResourceState.FIELD_NAME_ENDPOINT_LINK, req.endpointLink).addFieldClause(ComputeService.ComputeState.FIELD_NAME_LIFECYCLE_STATE, ComputeService.LifecycleState.PROVISIONING.toString(), QueryTask.QueryTerm.MatchType.TERM, QueryTask.Query.Occurance.MUST_NOT_OCCUR).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, parentLink, QueryTask.Query.Occurance.MUST_NOT_OCCUR).addInClause(ServiceDocument.FIELD_NAME_KIND, Arrays.asList(Utils.buildKind(ComputeService.ComputeState.class), Utils.buildKind(NetworkService.NetworkState.class), Utils.buildKind(StorageDescriptionService.StorageDescription.class), Utils.buildKind(SubnetService.SubnetState.class))).build().setOccurance(Occurance.SHOULD_OCCUR);
// The below two queries are added to get the Folders and Datacenters that are enumerated
// They are persisted as ResourceGroupState documents, and there are other documents of the same
// kind (which have a different lifecycle), we're filtering on the "__computeType" property
// Adding these documents here will enable automatic deletion of "untouched" resources in
// the further logic
QueryTask.Query folderClause = QueryTask.Query.Builder.create().addCompositeFieldClause(ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, CustomProperties.TYPE, VimNames.TYPE_FOLDER).addKindFieldClause(ResourceGroupState.class).build().setOccurance(Occurance.SHOULD_OCCUR);
QueryTask.Query datacenterClause = QueryTask.Query.Builder.create().addCompositeFieldClause(ResourceState.FIELD_NAME_CUSTOM_PROPERTIES, CustomProperties.TYPE, VimNames.TYPE_DATACENTER).addKindFieldClause(ResourceGroupState.class).build().setOccurance(Occurance.SHOULD_OCCUR);
// Add all the clauses to the combined clause
// The query structure is now --> MUST(SHOULD A + SHOULD B + SHOULD C)
// where A, B, C are independent queries
combinedClause.addBooleanClause(resourceClause).addBooleanClause(folderClause).addBooleanClause(datacenterClause);
QueryTask task = QueryTask.Builder.createDirectTask().setQuery(combinedClause).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).build();
DeferredResult<Set<String>> res = new DeferredResult<>();
Set<String> links = new ConcurrentSkipListSet<>();
QueryUtils.startInventoryQueryTask(this, task).whenComplete((result, e) -> {
if (e != null) {
res.complete(new HashSet<>());
return;
}
if (result.results.nextPageLink == null) {
res.complete(links);
return;
}
Operation.createGet(PhotonModelUriUtils.createInventoryUri(getHost(), result.results.nextPageLink)).setCompletion(makeCompletion(links, res)).sendWith(this);
});
return res;
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class VSphereIncrementalEnumerationService method updateServerDisks.
private void updateServerDisks(EnumerationProgress ctx, HostSystemOverlay hostSystemOverlay) {
QueryTask queryTask = queryForServerDisks(ctx, hostSystemOverlay);
withTaskResults(this, queryTask, serviceDocumentQueryResult -> {
if (!serviceDocumentQueryResult.documentLinks.isEmpty()) {
DiskService.DiskState oldDocument = convertOnlyResultToDocument(serviceDocumentQueryResult, DiskService.DiskState.class);
updateDiskState(ctx, oldDocument, hostSystemOverlay);
}
});
}
use of com.vmware.xenon.services.common.QueryTask 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.xenon.services.common.QueryTask in project photon-model by vmware.
the class VSphereVMSnapshotEnumerationHelper method enumerateSnapshots.
static void enumerateSnapshots(VSphereIncrementalEnumerationService service, EnumerationProgress enumerationProgress, List<VmOverlay> vms) {
if (CollectionUtils.isEmpty(vms)) {
return;
}
vms.forEach(vm -> {
// process only those VMs that have snapshots attached.
if (vm.getRootSnapshotList() != null) {
ComputeEnumerateResourceRequest request = enumerationProgress.getRequest();
QueryTask task = VSphereVirtualMachineEnumerationHelper.queryForVm(enumerationProgress, request.resourceLink(), vm.getInstanceUuid(), null);
VsphereEnumerationHelper.withTaskResults(service, task, result -> {
ComputeState computeState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, ComputeState.class);
processSnapshots(service, enumerationProgress, vm, computeState.documentSelfLink);
});
}
});
enumerationProgress.getSnapshotTracker().arriveAndDeregister();
}
use of com.vmware.xenon.services.common.QueryTask 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