use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class GCPEnumerationAdapterService method queryForComputeStates.
/**
* Query all compute states for the cluster filtered by the received set of instance Ids.
* @param ctx The Enumeration Context.
* @param vms The Map of VM IDs and VMs.
*/
private void queryForComputeStates(EnumerationContext ctx, Map<Long, GCPInstance> vms) {
logFine(() -> "Enumerating Local Compute States");
QueryTask.Query.Builder instanceIdFilterParentQuery = QueryTask.Query.Builder.create(QueryTask.Query.Occurance.MUST_OCCUR);
for (Long instanceId : vms.keySet()) {
QueryTask.Query instanceIdFilter = QueryTask.Query.Builder.create(QueryTask.Query.Occurance.SHOULD_OCCUR).addFieldClause(ComputeState.FIELD_NAME_ID, instanceId.toString()).build();
instanceIdFilterParentQuery.addClause(instanceIdFilter);
}
QueryTask.Query query = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, ctx.enumRequest.resourcePoolLink).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, ctx.enumRequest.resourceLink()).build().addBooleanClause(instanceIdFilterParentQuery.build());
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.INDEXED_METADATA).setQuery(query).build();
q.tenantLinks = ctx.computeHostDesc.tenantLinks;
QueryUtils.startInventoryQueryTask(this, q).whenComplete((queryTask, e) -> {
if (e != null) {
handleError(ctx, e);
return;
}
logFine(() -> String.format("Found %d matching compute states for GCP Instances", queryTask.results.documentCount));
// Otherwise, we can jump directly to create stage.
if (queryTask.results.documentCount > 0) {
for (Object s : queryTask.results.documents.values()) {
ComputeState computeState = Utils.fromJson(s, ComputeState.class);
ctx.computeStates.add(computeState);
}
ctx.subStage = EnumerationSubStages.UPDATE_COMPUTESTATE_COMPUTEDESCRIPTION_DISK;
} else {
ctx.subStage = EnumerationSubStages.CREATE_LOCAL_VMS;
}
handleSubStage(ctx);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class GCPEnumerationAdapterService method delete.
/**
* Deletes undiscovered resources.
*
* The logic works by recording a timestamp when enumeration starts. This timestamp is used to
* lookup resources which haven't been touched as part of current enumeration cycle. The other
* data point this method uses is the virtual machines discovered as part of list vm call.
*
* Finally, deletion on a resource is invoked only if it meets two criteria:
* - Timestamp older than current enumeration cycle.
* - VM not present on GCP.
*
* The method paginates through list of resources for deletion.
* @param ctx The Enumeration Context.
*/
private void delete(EnumerationContext ctx) {
QueryTask.Query query = QueryTask.Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_RESOURCE_POOL_LINK, ctx.enumRequest.resourcePoolLink).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, ctx.enumRequest.resourceLink()).addRangeClause(ComputeState.FIELD_NAME_UPDATE_TIME_MICROS, NumericRange.createLessThanRange(ctx.enumerationStartTimeInMicros)).build();
QueryTask q = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.INDEXED_METADATA).setResultLimit(QUERY_RESULT_LIMIT).setQuery(query).build();
q.tenantLinks = ctx.computeHostDesc.tenantLinks;
logFine(() -> "Querying compute resources for deletion");
QueryUtils.startInventoryQueryTask(this, q).whenComplete((o, e) -> deleteQueryCompletionHandler(ctx, o, e));
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class TestAWSEnumerationTask method removeS3BucketRegionFromDiskState.
private void removeS3BucketRegionFromDiskState() {
Query query = Query.Builder.create().addKindFieldClause(DiskState.class).addFieldClause(DiskState.FIELD_NAME_ID, TEST_BUCKET_NAME).build();
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).build();
Operation getTestBucketState = QueryUtils.createQueryTaskOperation(this.host, queryTask, ServiceTypeCluster.INVENTORY_SERVICE).setReferer(this.host.getUri());
Operation response = this.host.waitForResponse(getTestBucketState);
QueryTask queryTaskResponse = response.getBody(QueryTask.class);
DiskState testBucketDiskState = Utils.fromJson(queryTaskResponse.results.documents.get(queryTaskResponse.results.documentLinks.get(0)), DiskState.class);
testBucketDiskState.regionId = null;
Operation setNullRegionOp = Operation.createPatch(this.host, testBucketDiskState.documentSelfLink).setBody(testBucketDiskState).setReferer(this.host.getUri());
response = this.host.waitForResponse(setNullRegionOp);
// TODO : This test is broken - Setting the regionId to null does not do anything to the underlying state
if (response.hasPragmaDirective(Operation.PRAGMA_DIRECTIVE_STATE_NOT_MODIFIED)) {
this.isTestBucketPatched = false;
} else {
this.isTestBucketPatched = true;
}
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class VSphereAdapterSnapshotService method getChildSnapshots.
private DeferredResult<List<SnapshotState>> getChildSnapshots(String snapshotLink) {
DeferredResult<List<SnapshotState>> snapshotStates = new DeferredResult<>();
// find the child snapshots for the given snapshot document link
QueryTask qTask = getQueryWithFilters(snapshotLink, SnapshotState.FIELD_NAME_PARENT_LINK);
QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((o, e) -> {
if (e != null) {
logWarning(String.format("Failure retrieving the child snapshots %s", Utils.toString(e)));
snapshotStates.fail(e);
return;
}
QueryResultsProcessor rp = QueryResultsProcessor.create(o);
List<SnapshotState> snapshotsTemp = new ArrayList<>();
if (rp.hasResults()) {
snapshotsTemp = rp.streamDocuments(SnapshotState.class).collect(Collectors.toList());
}
snapshotStates.complete(snapshotsTemp);
});
return snapshotStates;
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class VSphereAdapterSnapshotService method querySnapshotStates.
private DeferredResult<SnapshotContext> querySnapshotStates(SnapshotContext context) {
// find if for the compute a snapshot already exist or not and if yes, get the current snapshot (among the snapshots that may exist)
QueryTask.Query snapshotQuery = QueryTask.Query.Builder.create().addKindFieldClause(SnapshotState.class).addFieldClause(SnapshotState.FIELD_NAME_COMPUTE_LINK, context.snapshotState.computeLink).addFieldClause(SnapshotState.FIELD_NAME_IS_CURRENT, "true").build();
QueryTask qTask = QueryTask.Builder.createDirectTask().setQuery(snapshotQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.INDEXED_METADATA).build();
return QueryUtils.startInventoryQueryTask(this, qTask).thenApply(op -> {
QueryResultsProcessor rp = QueryResultsProcessor.create(op);
if (rp.hasResults()) {
Optional<SnapshotState> snapshotStateOptional = rp.streamDocuments(SnapshotState.class).findFirst();
if (snapshotStateOptional.isPresent()) {
context.existingSnapshotState = snapshotStateOptional.get();
if (context.requestType == SnapshotService.SnapshotRequestType.CREATE) {
context.snapshotState.parentLink = context.existingSnapshotState.documentSelfLink;
}
}
}
return context;
});
}
Aggregations