Search in sources :

Example 16 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class AzureCostStatsService method getServiceMetadata.

private void getServiceMetadata(Context context, Stages next) {
    Query.Builder builder = Query.Builder.create();
    builder.addKindFieldClause(ResourceMetrics.class);
    builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, ResourceMetrics.PROPERTY_RESOURCE_LINK, context.computeHostDesc.documentSelfLink);
    builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, PhotonModelConstants.CONTAINS_BILL_PROCESSED_TIME_STAT, Boolean.TRUE.toString());
    QueryTask.Builder queryTaskBuilder = QueryTask.Builder.createDirectTask().addOption(QueryOption.SORT).addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).addOption(QueryOption.EXPAND_CONTENT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setResultLimit(1).setQuery(builder.build());
    if (!AzureCostConstants.SHOULD_REFRESH_INDEX) {
        queryTaskBuilder.addOption(QueryOption.DO_NOT_REFRESH);
    }
    QueryTask queryTask = queryTaskBuilder.build();
    queryTask.tenantLinks = context.computeHostDesc.tenantLinks;
    Operation queryTaskOp = QueryUtils.createQueryTaskOperation(this, queryTask, ServiceTypeCluster.METRIC_SERVICE).setExpiration(Utils.fromNowMicrosUtc(TimeUnit.SECONDS.toMicros(INTERNAL_REQUEST_TIMEOUT_SECONDS)));
    this.sendWithDeferredResult(queryTaskOp, QueryTask.class).whenComplete((response, exception) -> {
        if (exception != null) {
            handleError(context, Stages.GET_HISTORICAL_COSTS, exception, true);
            return;
        }
        if (response == null || response.results == null || response.results.documents == null) {
            context.billProcessedTimeMillis = 0;
            return;
        }
        Collection<Object> values = response.results.documents.values();
        if (!values.isEmpty()) {
            if (values.iterator().next() == null) {
                context.billProcessedTimeMillis = 0;
                return;
            }
            ResourceMetrics rawResourceMetrics = Utils.fromJson(values.iterator().next(), ResourceMetrics.class);
            context.billProcessedTimeMillis = rawResourceMetrics.entries.getOrDefault(PhotonModelConstants.CLOUD_ACCOUNT_COST_SYNC_MARKER_MILLIS, 0d).longValue();
            context.oldestBillProcessedMillis = rawResourceMetrics.entries.getOrDefault(AzureCostConstants.OLDEST_BILL_PROCESSED_MILLIS, 0d).longValue();
        }
        context.stage = next;
        handleRequest(context);
    });
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) Builder(com.vmware.xenon.services.common.QueryTask.Builder) Query(com.vmware.xenon.services.common.QueryTask.Query) Operation(com.vmware.xenon.common.Operation)

Example 17 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class AzureCostStatsService method getStoredEaUsageCost.

private void getStoredEaUsageCost(Context context, Stages next) {
    Query.Builder builder = Query.Builder.create();
    builder.addKindFieldClause(ResourceMetrics.class);
    builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, ResourceMetrics.PROPERTY_RESOURCE_LINK, context.computeHostDesc.documentSelfLink);
    builder.addRangeClause(QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, AzureCostConstants.USAGE_COST), QueryTask.NumericRange.createDoubleRange(0d, Double.MAX_VALUE, true, true));
    Builder queryTaskBuilder = Builder.createDirectTask().addOption(QueryOption.SORT).addOption(QueryOption.TOP_RESULTS).addOption(QueryOption.INCLUDE_ALL_VERSIONS).addOption(QueryOption.EXPAND_CONTENT).orderDescending(ResourceMetrics.FIELD_NAME_TIMESTAMP, TypeName.LONG).setResultLimit(1).setQuery(builder.build());
    if (!AzureCostConstants.SHOULD_REFRESH_INDEX) {
        queryTaskBuilder.addOption(QueryOption.DO_NOT_REFRESH);
    }
    QueryTask queryTask = queryTaskBuilder.build();
    QueryUtils.startQueryTask(this, queryTask, ServiceTypeCluster.METRIC_SERVICE).whenComplete((response, exception) -> {
        if (exception != null) {
            handleError(context, Stages.DOWNLOAD_DETAILED_BILL, exception, true);
            return;
        }
        if (response == null || response.results == null || response.results.documents == null) {
            context.storedCurrentMonthEaUsageCost = null;
            return;
        }
        Collection<Object> values = response.results.documents.values();
        if (!values.isEmpty()) {
            if (values.iterator().next() == null) {
                context.storedCurrentMonthEaUsageCost = null;
                return;
            }
            ResourceMetrics rawResourceMetrics = Utils.fromJson(values.iterator().next(), ResourceMetrics.class);
            context.storedCurrentMonthEaUsageCost = rawResourceMetrics.entries.get(AzureCostConstants.USAGE_COST);
        }
        context.stage = next;
        handleRequest(context);
    });
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) Builder(com.vmware.xenon.services.common.QueryTask.Builder)

Example 18 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class AzureEndpointAdapterService method checkIfAccountExistsAndGetExistingDocuments.

private void checkIfAccountExistsAndGetExistingDocuments(EndpointConfigRequest req, Operation op) {
    String accountId = req.endpointProperties.get(USER_LINK_KEY);
    if (accountId != null && !accountId.isEmpty() && req.tenantLinks != null && !req.tenantLinks.isEmpty()) {
        QueryTask queryTask = QueryUtils.createAccountQuery(accountId, PhotonModelConstants.EndpointType.azure.name(), req.tenantLinks);
        queryTask.tenantLinks = req.tenantLinks;
        QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((qrt, e) -> {
            if (e != null) {
                logSevere(() -> String.format("Failure retrieving query results for azure compute host corresponding to" + "the account ID: %s", e.toString()));
                op.fail(e);
                return;
            }
            if (qrt.results.documentCount > 0) {
                req.accountAlreadyExists = true;
                Object state = qrt.results.documents.values().iterator().next();
                ComputeState computeHost = Utils.fromJson(state, ComputeState.class);
                req.existingComputeState = computeHost;
                getComputeDescription(req, computeHost.descriptionLink, op);
            } else {
                req.accountAlreadyExists = false;
                op.setBody(req);
                op.complete();
                return;
            }
        });
    } else {
        req.accountAlreadyExists = false;
        op.setBody(req);
        op.complete();
    }
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 19 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class TestVSphereOvfProvisionTaskBase method findFirstOvfDescriptionLink.

protected String findFirstOvfDescriptionLink() throws Exception {
    QueryTask.Query q = QueryTask.Query.Builder.create().addFieldClause(ComputeService.ComputeState.FIELD_NAME_ID, "ovf-", QueryTask.QueryTerm.MatchType.PREFIX).build();
    QueryTask qt = QueryTask.Builder.createDirectTask().setQuery(q).build();
    Operation op = QueryUtils.createQueryTaskOperation(this.host, qt, ServiceTypeCluster.INVENTORY_SERVICE);
    QueryTask result = this.host.waitForResponse(op).getBody(QueryTask.class);
    return result.results.documentLinks.get(0);
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Operation(com.vmware.xenon.common.Operation)

Example 20 with QueryTask

use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.

the class TestVSphereProvisionWithStaticIpTask method findTemplate.

private ComputeState findTemplate() throws InterruptedException, ExecutionException, TimeoutException {
    String templateVmName = System.getProperty("vc.templateVmName");
    QuerySpecification qs = new QuerySpecification();
    qs.options.add(QueryOption.EXPAND_CONTENT);
    qs.query.addBooleanClause(Query.Builder.create().addFieldClause(ComputeState.FIELD_NAME_NAME, templateVmName).addFieldClause(ServiceDocument.FIELD_NAME_KIND, Utils.buildKind(ComputeState.class)).build());
    QueryTask qt = QueryTask.create(qs).setDirect(true);
    Operation op = QueryUtils.createQueryTaskOperation(this.host, qt, ServiceTypeCluster.INVENTORY_SERVICE);
    QueryTask result = this.host.sendWithFuture(op).thenApply(o -> o.getBody(QueryTask.class)).get(10, TimeUnit.SECONDS);
    return Utils.fromJson(result.results.documents.values().iterator().next(), ComputeState.class);
}
Also used : PowerState(com.vmware.photon.controller.model.resources.ComputeService.PowerState) Arrays(java.util.Arrays) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) IpAssignment(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.IpAssignment) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) TimeoutException(java.util.concurrent.TimeoutException) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ComputeDescriptionService(com.vmware.photon.controller.model.resources.ComputeDescriptionService) ProvisionComputeTaskState(com.vmware.photon.controller.model.tasks.ProvisionComputeTaskService.ProvisionComputeTaskState) DiskType(com.vmware.photon.controller.model.resources.DiskService.DiskType) ArrayList(java.util.ArrayList) Utils(com.vmware.xenon.common.Utils) Query(com.vmware.xenon.services.common.QueryTask.Query) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) URI(java.net.URI) VirtualMachineGuestOsIdentifier(com.vmware.vim25.VirtualMachineGuestOsIdentifier) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) NetworkInterfaceDescriptionService(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) TestUtils(com.vmware.photon.controller.model.tasks.TestUtils) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Test(org.junit.Test) UUID(java.util.UUID) File(java.io.File) NetworkService(com.vmware.photon.controller.model.resources.NetworkService) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) IOUtils(org.apache.commons.io.IOUtils) BootConfig(com.vmware.photon.controller.model.resources.DiskService.DiskState.BootConfig) NetworkInterfaceService(com.vmware.photon.controller.model.resources.NetworkInterfaceService) Ignore(org.junit.Ignore) FileEntry(com.vmware.photon.controller.model.resources.DiskService.DiskState.BootConfig.FileEntry) UriUtils(com.vmware.xenon.common.UriUtils) ComputeService(com.vmware.photon.controller.model.resources.ComputeService) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) NetworkInterfaceDescription(com.vmware.photon.controller.model.resources.NetworkInterfaceDescriptionService.NetworkInterfaceDescription) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Assert(org.junit.Assert) DiskService(com.vmware.photon.controller.model.resources.DiskService) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) Assert.assertEquals(org.junit.Assert.assertEquals) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) Operation(com.vmware.xenon.common.Operation)

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