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;
}
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;
}
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;
}
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);
});
}
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);
}
Aggregations