use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class MetricsClusterTest method getDocumentCount.
private Long getDocumentCount(VerificationHost host, String factoryLink) {
QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryOption.INCLUDE_ALL_VERSIONS).addOption(QueryOption.COUNT).setQuery(Query.Builder.create().addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, factoryLink, MatchType.PREFIX).build()).build();
host.createQueryTaskService(queryTask, false, true, queryTask, null);
return queryTask.results.documentCount;
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSCostStatsService method createQueryForComputeStatesByAccount.
/**
* Method creates a query operation to get the compute states corresponding to the specified account ID.
* The list of the resultant compute states is then passed to the specified handler for processing.
*
* @param context
* @param accountId
* @param queryResultConsumer
* @return operation object representing the query
*/
protected Operation createQueryForComputeStatesByAccount(AWSCostStatsCreationContext context, String accountId, Consumer<List<ComputeState>> queryResultConsumer) {
Query awsAccountsQuery = Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_TYPE, ComputeType.ENDPOINT_HOST).addCompositeFieldClause(ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, EndpointAllocationTaskService.CUSTOM_PROP_ENPOINT_TYPE, PhotonModelConstants.EndpointType.aws.name()).addCompositeFieldClause(ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, AWS_ACCOUNT_ID_KEY, accountId).addInCollectionItemClause(ComputeState.FIELD_NAME_TENANT_LINKS, context.computeDesc.tenantLinks).build();
QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).setQuery(awsAccountsQuery).build();
queryTask.setDirect(true);
queryTask.tenantLinks = context.computeDesc.tenantLinks;
return QueryUtils.createQueryTaskOperation(this, queryTask, ServiceTypeCluster.INVENTORY_SERVICE).setCompletion((o, e) -> {
if (e != null) {
getFailureConsumer(context).accept(e);
return;
}
QueryTask responseTask = o.getBody(QueryTask.class);
List<ComputeState> accountComputeStates = responseTask.results.documents.values().stream().map(s -> Utils.fromJson(s, ComputeState.class)).filter(cs -> cs.parentLink == null && cs.endpointLink != null && !CollectionUtils.isEmpty(cs.endpointLinks)).collect(Collectors.toList());
queryResultConsumer.accept(accountComputeStates);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSCostStatsService method getQueryTaskForMetric.
private QueryTask getQueryTaskForMetric(ComputeState accountComputeState) {
Query.Builder builder = Query.Builder.create();
builder.addKindFieldClause(ResourceMetrics.class);
builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, ResourceMetrics.PROPERTY_RESOURCE_LINK, accountComputeState.documentSelfLink);
builder.addCompositeFieldClause(ResourceMetrics.FIELD_NAME_CUSTOM_PROPERTIES, PhotonModelConstants.CONTAINS_BILL_PROCESSED_TIME_STAT, Boolean.TRUE.toString());
QueryTask.Builder qTaskBuilder = 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 (!AWSConstants.SHOULD_REFRESH_INDEX) {
qTaskBuilder.addOption(QueryOption.DO_NOT_REFRESH);
}
QueryTask qTask = qTaskBuilder.build();
qTask.documentExpirationTimeMicros = Utils.fromNowMicrosUtc(QueryUtils.TEN_MINUTES_IN_MICROS);
return qTask;
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSNetworkStateEnumerationAdapterService method getLocalSubnetStates.
/**
* Gets the Subnet information from the local database to perform updates to existing subnet
* states.
*/
private void getLocalSubnetStates(AWSNetworkStateCreationContext context, AWSNetworkStateCreationStage next) {
if (context.subnets.isEmpty()) {
handleNetworkStateChanges(context, next);
return;
}
QueryTask q = createQueryToGetExistingSubnetStatesFilteredByDiscoveredSubnets(context.subnets.keySet(), context.request.parentComputeLink, context.request.request.endpointLink, context.request.regionId, context.request.tenantLinks);
// create the query to find resources
QueryUtils.startInventoryQueryTask(this, q).whenComplete((queryTask, e) -> {
if (e != null) {
logSevere(() -> String.format("Failed retrieving query results: %s", e.toString()));
finishWithFailure(context, e);
return;
}
if (queryTask.results.documents != null) {
for (Object s : queryTask.results.documents.values()) {
SubnetState subnetState = Utils.fromJson(s, SubnetState.class);
context.localSubnetStateMap.put(subnetState.id, subnetState);
}
}
logFine(() -> String.format("%d subnet states found.", queryTask.results.documentCount));
handleNetworkStateChanges(context, next);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class AWSEnumerationUtils method getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery.
/**
* Get all the compute descriptions already in the system that correspond to virtual machine and filter by.
* This query is primarily used during instance discovery to find compute descriptions that exist in the system
* to match the instances received from AWS.
* The query filters out compute descriptions that represent compute hosts and also checks for other conditions
* as below:
* - Environment name(AWS),
* - id (instance type),
* - ZoneId(placement).
* - Created from the enumeration task.
* Compute hosts are modeled to support VM guests.So excluding them from the query to get
* compute descriptions for VMs.
*/
public static QueryTask getCDsRepresentingVMsInLocalSystemCreatedByEnumerationQuery(Set<InstanceDescKey> descriptionsSet, List<String> tenantLinks, String regionId, String parentComputeLink, String endpointLink) {
String sourceTaskName = QueryTask.QuerySpecification.buildCompositeFieldName(ComputeState.FIELD_NAME_CUSTOM_PROPERTIES, SOURCE_TASK_LINK);
Query query = Query.Builder.create().addKindFieldClause(ComputeDescription.class).addFieldClause(ComputeDescription.FIELD_NAME_ENVIRONMENT_NAME, ComputeDescription.ENVIRONMENT_NAME_AWS).addFieldClause(ComputeDescription.FIELD_NAME_REGION_ID, regionId).addFieldClause(ComputeDescription.FIELD_NAME_COMPUTE_HOST_LINK, parentComputeLink).addFieldClause(sourceTaskName, ResourceEnumerationTaskService.FACTORY_LINK).build();
// Instance type and zone should fall in one of the passed in values
Query groupFilter = new Query();
groupFilter.occurance = Occurance.MUST_OCCUR;
for (InstanceDescKey key : descriptionsSet) {
Query itf = new Query().setTermPropertyName(ComputeDescription.FIELD_NAME_ID).setTermMatchValue(key.instanceType);
itf.occurance = Occurance.MUST_OCCUR;
Query zf = new Query().setTermPropertyName(ComputeDescription.FIELD_NAME_ZONE_ID).setTermMatchValue(key.zoneId);
zf.occurance = Occurance.MUST_OCCUR;
Query d = new Query();
d.occurance = Occurance.SHOULD_OCCUR;
d.addBooleanClause(itf);
d.addBooleanClause(zf);
groupFilter.addBooleanClause(d);
}
query.addBooleanClause(groupFilter);
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.INDEXED_METADATA).addOption(QueryOption.TOP_RESULTS).setResultLimit(getQueryResultLimit()).build();
queryTask.documentSelfLink = UUID.randomUUID().toString();
queryTask.tenantLinks = tenantLinks;
return queryTask;
}
Aggregations