use of com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest in project photon-model by vmware.
the class ResourceGroomerTaskService method deleteDisassociatePatchStaleDocuments.
/**
* Deletes documents that have no endpointLinks associated with them.
* Disassociate documents if they have invalid endpointLinks by sending a collection update
* patch.
*/
private void deleteDisassociatePatchStaleDocuments(EndpointResourceDeletionRequest task, SubStage next) {
if (task.documentsToBeDeletedLinks.isEmpty() && task.endpointLinksByDocumentsToBeDisassociated.isEmpty()) {
task.subStage = next;
sendSelfPatch(task);
return;
}
List<Operation> deletePatchOperations = new ArrayList<>();
task.documentsToBeDeletedLinks.stream().forEach(documentLink -> {
deletePatchOperations.add(Operation.createDelete(this.getHost(), documentLink).setReferer(this.getHost().getUri()));
});
task.endpointLinksByDocumentsToBeDisassociated.entrySet().stream().forEach(entry -> {
Map<String, Collection<Object>> itemsToRemove = Collections.singletonMap(ResourceState.FIELD_NAME_ENDPOINT_LINKS, new ArrayList<>(entry.getValue()));
ServiceStateCollectionUpdateRequest request = ServiceStateCollectionUpdateRequest.create(null, itemsToRemove);
deletePatchOperations.add(Operation.createPatch(this.getHost(), entry.getKey()).setBody(request).setReferer(this.getUri()));
logInfo("Removing endpointLinks: %s from resource: %s", entry.getValue(), entry.getKey());
});
task.endpointLinkToBePatchedByDocumentLinks.entrySet().stream().forEach(entry -> {
if (entry.getKey().startsWith(ResourceGroupService.FACTORY_LINK) || entry.getKey().startsWith(AuthCredentialsService.FACTORY_LINK)) {
deletePatchOperations.add(createResourceGroupEndpointLinkPatchOp(entry.getKey(), entry.getValue()));
} else {
deletePatchOperations.add(createEndpointLinkPatchOp(entry.getKey(), entry.getValue()));
}
logInfo("Changing endpointLink to %s from resource: %s", entry.getValue(), entry.getKey());
});
logInfo("Deleting stale documents that have invalid endpointLinks. [documentCount=%s]", task.documentsToBeDeletedLinks.size());
logInfo("Patching stale documents that have invalid endpointLinks list. [documentCount=%s]", task.endpointLinksByDocumentsToBeDisassociated.size());
logInfo("Patching stale documents that have invalid endpointLink. [documentCount=%s]", task.endpointLinkToBePatchedByDocumentLinks.size());
task.documentsDeletedCount += task.documentsToBeDeletedLinks.size();
task.endpointLinksPatchedCount += task.endpointLinksByDocumentsToBeDisassociated.size();
task.endpointLinkPatchedCount += task.endpointLinkToBePatchedByDocumentLinks.size();
OperationJoin.create(deletePatchOperations).setCompletion((o, e) -> {
if (e != null) {
task.failureMessage = e.values().iterator().next().getMessage();
task.subStage = SubStage.FAILED;
sendSelfFailurePatch(task, task.failureMessage);
return;
}
task.subStage = next;
sendSelfPatch(task);
}).sendWith(this, OPERATION_BATCH_SIZE);
}
use of com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest in project photon-model by vmware.
the class ResourceGroomerTaskServiceTest method testMalformedEndpointCleanup.
@Test
public void testMalformedEndpointCleanup() throws Throwable {
EndpointResourceDeletionRequest state = new EndpointResourceDeletionRequest();
state.documentSelfLink = UriUtils.buildUriPath(ResourceGroomerTaskService.FACTORY_LINK, UUID.randomUUID().toString());
state.tenantLinks = Collections.singleton(TENANT_LINK_1);
state.endpointLink = "some-random-link";
state.taskInfo = TaskState.createDirect();
Operation postOp = Operation.createPost(UriUtils.buildUri(this.host, ResourceGroomerTaskService.FACTORY_LINK)).setBody(state).setReferer(this.host.getUri());
Operation postResponse = this.host.waitForResponse(postOp);
assertEquals(400, postResponse.getStatusCode());
}
use of com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest in project photon-model by vmware.
the class ResourceGroomerTaskServiceTest method testValidEndpointCleanup.
@Test
public void testValidEndpointCleanup() throws Throwable {
if (!createDeleteEndpoints()) {
return;
}
EndpointResourceDeletionRequest state = new EndpointResourceDeletionRequest();
state.documentSelfLink = UriUtils.buildUriPath(ResourceGroomerTaskService.FACTORY_LINK, UUID.randomUUID().toString());
state.tenantLinks = Collections.singleton(TENANT_LINK_1);
state.endpointLink = VALID_ENDPOINT_LINK_1_TENANT_1;
state.taskInfo = TaskState.createDirect();
Operation postOp = Operation.createPost(UriUtils.buildUri(this.host, ResourceGroomerTaskService.FACTORY_LINK)).setBody(state).setReferer(this.host.getUri());
Operation postResponse = this.host.waitForResponse(postOp);
EndpointResourceDeletionRequest response = postResponse.getBody(EndpointResourceDeletionRequest.class);
assertEquals(200, postResponse.getStatusCode());
assertEquals(response.taskInfo.stage, TaskStage.FAILED);
assertEquals(response.failureMessage, "Deletion/Disassociation of documents for valid " + "endpoints is not supported.");
}
use of com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest in project photon-model by vmware.
the class ResourceGroomerTaskServiceTest method testNoTenantSpecified.
@Test
public void testNoTenantSpecified() throws Throwable {
if (!createDeleteEndpoints()) {
return;
}
EndpointResourceDeletionRequest state = new EndpointResourceDeletionRequest();
state.documentSelfLink = UriUtils.buildUriPath(ResourceGroomerTaskService.FACTORY_LINK, UUID.randomUUID().toString());
state.taskInfo = TaskState.createDirect();
Operation postOp = Operation.createPost(UriUtils.buildUri(this.host, ResourceGroomerTaskService.FACTORY_LINK)).setBody(state).setReferer(this.host.getUri());
Operation postResponse = this.host.waitForResponse(postOp);
assertEquals(400, postResponse.getStatusCode());
}
use of com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest in project photon-model by vmware.
the class ResourceGroomerTaskServiceTest method executeResourceGroomerTask.
/**
* Executes the groomer task on TENANT_LINK_1 and waits for it to finish. Returns the selfLink
* of the task after it finishes.
*/
public String executeResourceGroomerTask(String endpointLink) {
EndpointResourceDeletionRequest state = new EndpointResourceDeletionRequest();
state.tenantLinks = Collections.singleton(TENANT_LINK_1);
state.documentSelfLink = UriUtils.buildUriPath(ResourceGroomerTaskService.FACTORY_LINK, UUID.randomUUID().toString());
state.endpointLink = endpointLink;
Operation postOp = Operation.createPost(UriUtils.buildUri(this.host, ResourceGroomerTaskService.FACTORY_LINK)).setBody(state).setReferer(this.host.getUri());
this.host.waitForResponse(postOp);
this.host.waitForFinishedTask(EndpointResourceDeletionRequest.class, state.documentSelfLink);
return state.documentSelfLink;
}
Aggregations