Search in sources :

Example 46 with QueryTask

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

the class TestAWSSetupUtils method getInternalTagsByType.

public static ServiceDocumentQueryResult getInternalTagsByType(VerificationHost host, String type) {
    Query query = Query.Builder.create().addKindFieldClause(TagService.TagState.class).addFieldClause(TagService.TagState.FIELD_NAME_KEY, PhotonModelConstants.TAG_KEY_TYPE).addFieldClause(TagService.TagState.FIELD_NAME_VALUE, type).build();
    Query externalQuery = new Query().setTermPropertyName(TagService.TagState.FIELD_NAME_EXTERNAL).setTermMatchValue(Boolean.FALSE.toString());
    externalQuery.occurance = Query.Occurance.SHOULD_OCCUR;
    Query originQuery = new Query().addBooleanClause(Query.Builder.create().addCollectionItemClause(TagService.TagState.FIELD_NAME_ORIGIN, DISCOVERED.toString(), Occurance.MUST_NOT_OCCUR).addCollectionItemClause(TagService.TagState.FIELD_NAME_ORIGIN, SYSTEM.toString(), Query.Occurance.SHOULD_OCCUR).addCollectionItemClause(TagService.TagState.FIELD_NAME_ORIGIN, USER_DEFINED.toString(), Query.Occurance.SHOULD_OCCUR).build()).setOccurance(Query.Occurance.SHOULD_OCCUR);
    Query originOrExternalQuery = new Query().addBooleanClause(externalQuery).addBooleanClause(originQuery).setOccurance(Query.Occurance.MUST_OCCUR);
    query.addBooleanClause(originOrExternalQuery);
    QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QuerySpecification.QueryOption.TOP_RESULTS).setResultLimit(getQueryResultLimit()).build();
    queryTask.documentSelfLink = UUID.randomUUID().toString();
    host.createQueryTaskService(queryTask, false, true, queryTask, null);
    ServiceDocumentQueryResult results = queryTask.results;
    return results;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask) Query(com.vmware.xenon.services.common.QueryTask.Query) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Example 47 with QueryTask

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

the class TestResourceDeduplication method getDocumentLinks.

private List<String> getDocumentLinks(Class resourceState, String endpointLink) {
    QueryTask.Query.Builder qBuilder = QueryTask.Query.Builder.create().addKindFieldClause(resourceState);
    if (endpointLink != null && !endpointLink.equals("")) {
        qBuilder.addFieldClause(ENDPOINT_LINKS_FIELD_CLAUSE, endpointLink);
    } else {
        qBuilder.addFieldClause(ENDPOINT_LINKS_FIELD_CLAUSE, "*", QueryTask.QueryTerm.MatchType.WILDCARD, QueryTask.Query.Occurance.MUST_NOT_OCCUR);
    }
    QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).setQuery(qBuilder.build()).build();
    this.host.createQueryTaskService(queryTask, false, true, queryTask, null);
    return queryTask.results.documentLinks;
}
Also used : QueryTask(com.vmware.xenon.services.common.QueryTask)

Example 48 with QueryTask

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

the class TestAzureStatsCollection method getResourceMetrics.

/**
 * Query to get ResourceMetrics document for a specific resource containing a specific metric.
 * @param resourceLink Link to the resource on which stats are being collected.
 * @param metricKey Metric name.
 * @return ResourceMetrics document.
 */
private ResourceMetrics getResourceMetrics(String resourceLink, String metricKey) {
    QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setQuery(QueryTask.Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(resourceLink)), QueryTask.QueryTerm.MatchType.PREFIX).addRangeClause(QueryTask.QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricKey), QueryTask.NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true)).build()).build();
    Operation op = QueryUtils.createQueryTaskOperation(this.host, qt, ServiceTypeCluster.METRIC_SERVICE).setReferer(this.host.getUri()).setBody(qt).setCompletion((o, e) -> {
        if (e != null) {
            this.host.log(Level.WARNING, e.toString());
        }
    });
    Operation result = this.host.waitForResponse(op);
    QueryTask qtResult = result.getBody(QueryTask.class);
    ResourceMetrics resourceMetric = null;
    if (qtResult.results.documentLinks.size() > 0) {
        String documentLink = qtResult.results.documentLinks.get(0);
        resourceMetric = Utils.fromJson(qtResult.results.documents.get(documentLink), ResourceMetrics.class);
    }
    return resourceMetric;
}
Also used : ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) Operation(com.vmware.xenon.common.Operation)

Example 49 with QueryTask

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

the class AzureSubscriptionsEnumerationServiceTest method testAddFirstAzureSubscription.

private void testAddFirstAzureSubscription() throws Throwable {
    // Fail if this.computeLink is null
    Assert.assertNotNull("Root computeLink of Azure EA account is null", this.computeLink);
    // Fail if this.compute is null
    Assert.assertNotNull("Root compute of Azure EA account is null", this.compute);
    // Request for creating computes for 1 Azure Subscriptions
    AzureSubscription subscription = getAzureSubscription(SUBSCRIPTION_ID_1, ACCOUNT_EMAIL_ID_1);
    AzureSubscription existingSubscription1 = getAzureSubscription(SUBSCRIPTION_EXISTING_1, ACCOUNT_EMAIL_ID_1);
    createAzureCostComputesForSubscriptions(Arrays.asList(subscription, existingSubscription1));
    // Query for Azure Computes created with CLIENT_ID as enrollment Number
    QueryTask task = createQueryTaskForAzureComputes(ENROLLMENT_NUMNBER, Collections.singletonList(TENANT_ID));
    QueryTask queryTaskResponse = executQuerySynchronously(task);
    assertQueryTaskResponse(queryTaskResponse, 3);
    // Remove compute for existing subscription
    ComputeState existingSubsCs = Utils.fromJson(queryTaskResponse.results.documents.remove(this.existingSubsComputeLink1), ComputeState.class);
    Assert.assertNotNull(existingSubsCs);
    Assert.assertNull(existingSubsCs.customProperties.get(PhotonModelConstants.AUTO_DISCOVERED_ENTITY));
    // Get and assert the returned compute
    queryTaskResponse.results.documents.values().forEach(resultCompute -> {
        ComputeState cs = Utils.fromJson(resultCompute, ComputeState.class);
        assertPropertiesOfComputeState(cs, ENROLLMENT_NUMNBER, cs.customProperties.get(AzureConstants.AZURE_SUBSCRIPTION_ID_KEY), ACCOUNT_EMAIL_ID_1, this.compute.endpointLink, this.compute.tenantLinks);
        this.createdComputeLinks.add(cs.documentSelfLink);
    });
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

Example 50 with QueryTask

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

the class AzureSubscriptionsEnumerationServiceTest method testAddSameAzureSubscriptions.

private void testAddSameAzureSubscriptions() throws Throwable {
    // Request for creating computes for existing Azure Subscriptions
    AzureSubscription subscription1 = getAzureSubscription(SUBSCRIPTION_ID_1, ACCOUNT_EMAIL_ID_1);
    AzureSubscription subscription2 = getAzureSubscription(SUBSCRIPTION_ID_2, ACCOUNT_EMAIL_ID_2);
    AzureSubscription existingSubscription2 = getAzureSubscription(SUBSCRIPTION_EXISTING_2, ACCOUNT_EMAIL_ID_2);
    createAzureCostComputesForSubscriptions(Arrays.asList(subscription1, subscription2, existingSubscription2));
    // Query for Azure Computes created with CLIENT_ID as enrollment Number
    QueryTask task = createQueryTaskForAzureComputes(ENROLLMENT_NUMNBER, Collections.singletonList(TENANT_ID));
    QueryTask queryTaskResponse = executQuerySynchronously(task);
    assertQueryTaskResponse(queryTaskResponse, 6);
    // Remove compute for existing subscription
    ComputeState existingSubsCs1 = Utils.fromJson(queryTaskResponse.results.documents.remove(this.existingSubsComputeLink1), ComputeState.class);
    Assert.assertNotNull(existingSubsCs1);
    Assert.assertNull(existingSubsCs1.customProperties.get(PhotonModelConstants.AUTO_DISCOVERED_ENTITY));
    ComputeState existingSubsCs2 = Utils.fromJson(queryTaskResponse.results.documents.remove(this.existingSubsComputeLink2), ComputeState.class);
    Assert.assertNotNull(existingSubsCs2);
    Assert.assertNull(existingSubsCs1.customProperties.get(PhotonModelConstants.AUTO_DISCOVERED_ENTITY));
    // Remove the already asserted computes
    this.createdComputeLinks.stream().forEach(computeLnk -> {
        queryTaskResponse.results.documents.remove(computeLnk);
    });
    // Get and assert the returned compute, which is created newly
    ComputeState cs = Utils.fromJson(queryTaskResponse.results.documents.values().iterator().next(), ComputeState.class);
    assertPropertiesOfComputeState(cs, ENROLLMENT_NUMNBER, SUBSCRIPTION_EXISTING_2, ACCOUNT_EMAIL_ID_2, this.compute.endpointLink, this.compute.tenantLinks);
    this.createdComputeLinks.add(cs.documentSelfLink);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) QueryTask(com.vmware.xenon.services.common.QueryTask) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription)

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