Search in sources :

Example 1 with EndpointResourceDeletionRequest

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);
}
Also used : Service(com.vmware.xenon.common.Service) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) ResourceGroupState(com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState) Utils(com.vmware.xenon.common.Utils) TaskFactoryService(com.vmware.xenon.services.common.TaskFactoryService) LoadBalancerState(com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState) RouterState(com.vmware.photon.controller.model.resources.RouterService.RouterState) SubnetState(com.vmware.photon.controller.model.resources.SubnetService.SubnetState) EndpointService(com.vmware.photon.controller.model.resources.EndpointService) Map(java.util.Map) StorageDescription(com.vmware.photon.controller.model.resources.StorageDescriptionService.StorageDescription) CUSTOM_PROP_ENDPOINT_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) ComputeDescription(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription) NetworkInterfaceState(com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState) EndpointResourceDeletionRequest(com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) DiskState(com.vmware.photon.controller.model.resources.DiskService.DiskState) Occurance(com.vmware.xenon.services.common.QueryTask.Query.Occurance) ResourceUtils(com.vmware.photon.controller.model.resources.ResourceUtils) ResourceGroupService(com.vmware.photon.controller.model.resources.ResourceGroupService) List(java.util.List) SOURCE_TASK_LINK(com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) FactoryService(com.vmware.xenon.common.FactoryService) ServiceStateMapUpdateRequest(com.vmware.xenon.common.ServiceStateMapUpdateRequest) TaskService(com.vmware.xenon.services.common.TaskService) HashMap(java.util.HashMap) PhotonModelUtils(com.vmware.photon.controller.model.resources.util.PhotonModelUtils) ArrayList(java.util.ArrayList) SecurityGroupState(com.vmware.photon.controller.model.resources.SecurityGroupService.SecurityGroupState) ServiceUriPaths(com.vmware.xenon.services.common.ServiceUriPaths) HashSet(java.util.HashSet) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) Query(com.vmware.xenon.services.common.QueryTask.Query) UriPaths(com.vmware.photon.controller.model.UriPaths) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) PropertyUsageOption(com.vmware.xenon.common.ServiceDocumentDescription.PropertyUsageOption) ResourceState(com.vmware.photon.controller.model.resources.ResourceState) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ImageState(com.vmware.photon.controller.model.resources.ImageService.ImageState) NetworkState(com.vmware.photon.controller.model.resources.NetworkService.NetworkState) Collections(java.util.Collections) OperationJoin(com.vmware.xenon.common.OperationJoin) ArrayList(java.util.ArrayList) Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Operation(com.vmware.xenon.common.Operation)

Example 2 with EndpointResourceDeletionRequest

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());
}
Also used : EndpointResourceDeletionRequest(com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest) Operation(com.vmware.xenon.common.Operation) Test(org.junit.Test)

Example 3 with EndpointResourceDeletionRequest

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.");
}
Also used : EndpointResourceDeletionRequest(com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest) Operation(com.vmware.xenon.common.Operation) Test(org.junit.Test)

Example 4 with EndpointResourceDeletionRequest

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());
}
Also used : EndpointResourceDeletionRequest(com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest) Operation(com.vmware.xenon.common.Operation) Test(org.junit.Test)

Example 5 with EndpointResourceDeletionRequest

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;
}
Also used : EndpointResourceDeletionRequest(com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest) Operation(com.vmware.xenon.common.Operation)

Aggregations

EndpointResourceDeletionRequest (com.vmware.photon.controller.model.tasks.ResourceGroomerTaskService.EndpointResourceDeletionRequest)8 Operation (com.vmware.xenon.common.Operation)5 Test (org.junit.Test)3 UriPaths (com.vmware.photon.controller.model.UriPaths)1 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)1 CUSTOM_PROP_ENDPOINT_LINK (com.vmware.photon.controller.model.constants.PhotonModelConstants.CUSTOM_PROP_ENDPOINT_LINK)1 SOURCE_TASK_LINK (com.vmware.photon.controller.model.constants.PhotonModelConstants.SOURCE_TASK_LINK)1 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)1 ComputeDescription (com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription)1 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)1 DiskState (com.vmware.photon.controller.model.resources.DiskService.DiskState)1 EndpointService (com.vmware.photon.controller.model.resources.EndpointService)1 EndpointState (com.vmware.photon.controller.model.resources.EndpointService.EndpointState)1 ImageState (com.vmware.photon.controller.model.resources.ImageService.ImageState)1 LoadBalancerState (com.vmware.photon.controller.model.resources.LoadBalancerService.LoadBalancerState)1 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)1 NetworkState (com.vmware.photon.controller.model.resources.NetworkService.NetworkState)1 ResourceGroupService (com.vmware.photon.controller.model.resources.ResourceGroupService)1 ResourceGroupState (com.vmware.photon.controller.model.resources.ResourceGroupService.ResourceGroupState)1 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)1