Search in sources :

Example 56 with QueryTask

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);
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 57 with QueryTask

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));
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 58 with QueryTask

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;
    }
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Operation(com.vmware.xenon.common.Operation)

Example 59 with QueryTask

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;
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 60 with QueryTask

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;
    });
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)

Aggregations

QueryTask (com.vmware.xenon.services.common.QueryTask)147 Query (com.vmware.xenon.services.common.QueryTask.Query)62 Operation (com.vmware.xenon.common.Operation)61 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)39 ArrayList (java.util.ArrayList)26 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)20 List (java.util.List)20 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)19 UriUtils (com.vmware.xenon.common.UriUtils)18 Utils (com.vmware.xenon.common.Utils)17 HashSet (java.util.HashSet)16 HashMap (java.util.HashMap)14 QueryOption (com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption)12 TimeUnit (java.util.concurrent.TimeUnit)12 ComputeEnumerateResourceRequest (com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest)11 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)11 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)11 ServiceTypeCluster (com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster)11 URI (java.net.URI)11 Set (java.util.Set)11