use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class ResourceRemovalTaskService method deallocateIpAddressesForResources.
private void deallocateIpAddressesForResources(ResourceRemovalTaskState currentState, QueryTask queryTask, String subTaskLink) {
// handle empty pages
if (queryTask.results.documentCount == 0) {
sendSelfPatch(TaskState.TaskStage.FINISHED, SubStage.FINISHED, null);
return;
}
if (subTaskLink == null) {
createSubTaskForIpDeallocationCallbacks(currentState, queryTask, link -> deallocateIpAddressesForResources(currentState, queryTask, link));
return;
}
logFine("Starting ip deallocation of %d compute resources using sub task", queryTask.results.documentCount);
// for each compute resource link in the results, call ResourceIPDeallocationTaskService
for (String resourceLink : queryTask.results.documentLinks) {
ResourceIPDeallocationTaskState deallocateTask = new ResourceIPDeallocationTaskState();
deallocateTask.resourceLink = resourceLink;
deallocateTask.tenantLinks = currentState.tenantLinks;
deallocateTask.serviceTaskCallback = ServiceTaskCallback.create(UriUtils.buildPublicUri(getHost(), subTaskLink));
deallocateTask.serviceTaskCallback.onSuccessFinishTask();
// Similar to instance deletes - a failure for one network interface deallocate will fail the task
deallocateTask.serviceTaskCallback.onErrorFailTask();
this.sendRequest(Operation.createPost(this, ResourceIPDeallocationTaskService.FACTORY_LINK).setBody(deallocateTask).setCompletion((o, ex) -> {
if (ex != null) {
logSevere("Failure deallocating IPs for resource: [%s]: %s", resourceLink, ex);
ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.finish(resourceLink);
sendPatch(subTaskLink, subTaskPatchBody);
} else {
logFine("Sent deallocation IPs request for resource [%s]", resourceLink);
}
}));
}
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class ResourceRemovalTaskService method doInstanceDeletes.
private void doInstanceDeletes(ResourceRemovalTaskState currentState, QueryTask queryTask, String subTaskLink) {
// handle empty pages
if (queryTask.results.documentCount == 0) {
sendSelfPatch(currentState.taskInfo.stage, SubStage.DEALLOCATE_IP_ADDRESSES, s -> {
s.nextPageLink = null;
});
return;
}
if (subTaskLink == null) {
createSubTaskForDeleteCallbacks(currentState, queryTask, link -> doInstanceDeletes(currentState, queryTask, link));
return;
}
logFine(() -> String.format("Starting delete of %d compute resources using sub task %s", queryTask.results.documentLinks.size(), subTaskLink));
// a DELETE request to its associated instance service.
for (String resourceLink : queryTask.results.documentLinks) {
URI u = ComputeStateWithDescription.buildUri(UriUtils.buildUri(getHost(), resourceLink));
sendRequest(Operation.createGet(u).setCompletion((o, e) -> {
if (e != null) {
// we do not fail task if one delete failed ...
// send a FINISHED patch which is what a sub task
// would do. Since the
// current state
// is still at REMOVING_RESOURCES, we will just
// increment a counter
ResourceOperationResponse subTaskPatchBody = ResourceOperationResponse.fail(resourceLink, e);
sendPatch(subTaskLink, subTaskPatchBody);
return;
}
sendInstanceDelete(resourceLink, subTaskLink, o, currentState);
}));
}
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class TagGroomerTaskService method getDiscoveredNotDeletedTagStates.
/**
* Collect all tag states with origins ["DISCOVERED"] that have not been soft deleted, and
* perform soft deletion of stale ones by page.
*
* If the tag has multiple origins, do not delete the tag.
*/
private void getDiscoveredNotDeletedTagStates(TagDeletionRequest task, SubStage next) {
Query query = Query.Builder.create().addKindFieldClause(TagState.class).build();
Map<String, Query.Occurance> origin = new HashMap<>();
origin.put(DISCOVERED.toString(), Query.Occurance.MUST_OCCUR);
origin.put(SYSTEM.toString(), Query.Occurance.MUST_NOT_OCCUR);
origin.put(USER_DEFINED.toString(), Query.Occurance.MUST_NOT_OCCUR);
Query externalQuery = createOriginTagQuery(Boolean.TRUE, origin);
query.addBooleanClause(externalQuery);
query.addBooleanClause(new Query().setTermPropertyName(TagState.FIELD_NAME_DELETED).setTermMatchValue(Boolean.FALSE.toString()));
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(query).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(QUERY_RESULT_LIMIT).build();
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((response, e) -> {
if (e != null) {
task.failureMessage = e.getMessage();
task.subStage = SubStage.FAILED;
sendSelfPatch(task);
return;
}
if (response.results != null && response.results.nextPageLink != null) {
task.tagsNextPageLink = response.results.nextPageLink;
task.subStage = next;
} else {
task.subStage = SubStage.FINISHED;
}
sendSelfPatch(task);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class TagGroomerTaskService method queryDocumentsTaggedWithTag.
/**
* Issue query for documents that contain the specific tagLink.
*/
private void queryDocumentsTaggedWithTag(TagDeletionRequest task, SubStage next) {
if (task.tagsMap.isEmpty()) {
task.subStage = SubStage.FINISHED;
sendSelfPatch(task);
return;
}
Set<String> tagLinks = new HashSet<>();
task.tagsMap.forEach((k, v) -> tagLinks.add(k));
Query query = Query.Builder.create().addInCollectionItemClause(ResourceState.FIELD_NAME_TAG_LINKS, tagLinks).build();
// get all documents that contain the links.
QueryTask queryTask = QueryTask.Builder.createDirectTask().addOption(QueryOption.SELECT_LINKS).addLinkTerm(ResourceState.FIELD_NAME_TAG_LINKS).setResultLimit(MAX_QUERY_RESULT_LIMIT).setQuery(query).build();
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((response, e) -> {
if (e != null) {
task.failureMessage = e.getMessage();
task.subStage = SubStage.FAILED;
sendSelfPatch(task);
return;
}
if (response.results != null) {
task.docsNextPageLink = response.results.nextPageLink;
task.subStage = next;
} else {
task.subStage = SubStage.GET_TAGS_NEXT_PAGE;
}
sendSelfPatch(task);
});
}
use of com.vmware.xenon.services.common.QueryTask in project photon-model by vmware.
the class EndpointRemovalTaskService method disassociateResources.
/**
* Disassociate computes discovered with this endpoint.
*/
private void disassociateResources(EndpointRemovalTaskState state, SubStage next) {
Query.Builder qBuilder = Query.Builder.create().addKindFieldClause(ComputeState.class).addFieldClause(ComputeState.FIELD_NAME_PARENT_LINK, state.endpoint.computeLink);
QueryTask queryTask = QueryTask.Builder.createDirectTask().setQuery(qBuilder.build()).addOption(QueryOption.EXPAND_CONTENT).setResultLimit(QueryUtils.DEFAULT_RESULT_LIMIT).build();
queryTask.documentExpirationTimeMicros = state.documentExpirationTimeMicros;
queryTask.documentSelfLink = UUID.randomUUID().toString();
queryTask.tenantLinks = state.tenantLinks;
QueryUtils.startInventoryQueryTask(this, queryTask).whenComplete((responseTask, throwable) -> {
if (throwable != null) {
logWarning(() -> String.format("Failure retrieving query results: " + "%s", throwable.toString()));
sendFailureSelfPatch(throwable);
return;
}
if (responseTask.results.nextPageLink == null) {
logFine("Query returned no resources to update");
sendSelfPatch(TaskStage.STARTED, next);
return;
}
// process one page of resource links at a time
disassociateResourceHelper(responseTask.results.nextPageLink, next, state.endpointLink);
});
}
Aggregations