Search in sources :

Example 1 with ServiceStateCollectionUpdateRequest

use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.

the class AWSComputeDescriptionEnumerationAdapterService method updateComputeDescriptionOperation.

private Operation updateComputeDescriptionOperation(InstanceDescKey cd, AWSComputeDescriptionCreationServiceContext context) {
    Map<String, Collection<Object>> collectionsToAddMap = Collections.singletonMap(ComputeDescription.FIELD_NAME_ENDPOINT_LINKS, Collections.singletonList(context.cdState.endpointLink));
    ServiceStateCollectionUpdateRequest updateEndpointLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAddMap, null);
    return Operation.createPatch(this.getHost(), context.localComputeDescriptionMap.get(cd).documentSelfLink).setReferer(this.getHost().getUri()).setBody(updateEndpointLinksRequest);
}
Also used : Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest)

Example 2 with ServiceStateCollectionUpdateRequest

use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.

the class AzureComputeDiskDay2Service method updateComputeState.

/**
 * Update the diskLink of DiskState in ComputeState
 */
private DeferredResult<Operation> updateComputeState(AzureComputeDiskDay2Context context) {
    Map<String, Collection<Object>> collectionsToModify = Collections.singletonMap(ComputeState.FIELD_NAME_DISK_LINKS, Collections.singletonList(context.diskState.documentSelfLink));
    Map<String, Collection<Object>> collectionsToAdd = null;
    Map<String, Collection<Object>> collectionsToRemove = null;
    ComputeState computeState = context.computeState;
    if (context.request.operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
        collectionsToAdd = collectionsToModify;
    } else if (context.request.operation.equals(ResourceOperation.DETACH_DISK.operation)) {
        collectionsToRemove = collectionsToModify;
    }
    ServiceStateCollectionUpdateRequest updateDiskLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAdd, collectionsToRemove);
    Operation computeStateOp = Operation.createPatch(createInventoryUri(this.getHost(), computeState.documentSelfLink)).setBody(updateDiskLinksRequest).setReferer(this.getUri());
    return this.sendWithDeferredResult(computeStateOp);
}
Also used : ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 3 with ServiceStateCollectionUpdateRequest

use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.

the class PhotonModelUtils method createRemoveEndpointLinksOperation.

/**
 * Utility method to create an operation to remove an endpointLink from the endpointLinks set of
 * the resourceState and also update the endpointLink property of the specific resourceState
 */
public static <T extends ResourceState> Operation createRemoveEndpointLinksOperation(Service service, String endpointLink, T resource) {
    if (resource.endpointLinks == null || !resource.endpointLinks.contains(endpointLink)) {
        return null;
    }
    Map<String, Collection<Object>> endpointsToRemoveMap = Collections.singletonMap(EndpointService.EndpointState.FIELD_NAME_ENDPOINT_LINKS, Collections.singleton(endpointLink));
    ServiceStateCollectionUpdateRequest serviceStateCollectionUpdateRequest = ServiceStateCollectionUpdateRequest.create(null, endpointsToRemoveMap);
    return Operation.createPatch(createInventoryUri(service.getHost(), resource.documentSelfLink)).setReferer(service.getUri()).setBody(serviceStateCollectionUpdateRequest).setCompletion((updateOp, exception) -> {
        if (exception != null) {
            service.getHost().log(Level.WARNING, () -> String.format("PATCH " + "to instance service %s, failed: %s", updateOp.getUri(), exception.toString()));
            return;
        }
        service.getHost().log(Level.FINE, () -> String.format("PATCH to " + "update endpointLink in endpointLinks " + "to instance service %s finished successfully", updateOp.getUri()));
        String resourceEndpointLink = getEndpointLink(resource);
        if (resourceEndpointLink != null) {
            // if the endpoint being deleted is the endpointLink of the
            // resourceState, then assign a new endpointLink
            updateEndpointLink(service, endpointLink, resource.documentSelfLink, resourceEndpointLink, resource.endpointLinks);
        }
    });
}
Also used : Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest)

Example 4 with ServiceStateCollectionUpdateRequest

use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest 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 5 with ServiceStateCollectionUpdateRequest

use of com.vmware.xenon.common.ServiceStateCollectionUpdateRequest in project photon-model by vmware.

the class AWSUtils method updateComputeState.

/**
 * Add or remove diskLink from ComputeState by sending a ServiceStateCollectionUpdateRequest.
 */
public static DeferredResult<Operation> updateComputeState(String computeStateLink, List<String> diskLinks, String operation, Service service) {
    Map<String, Collection<Object>> collectionsToModify = Collections.singletonMap(ComputeService.ComputeState.FIELD_NAME_DISK_LINKS, new ArrayList<>(diskLinks));
    Map<String, Collection<Object>> collectionsToAdd = null;
    Map<String, Collection<Object>> collectionsToRemove = null;
    if (operation.equals(ResourceOperation.ATTACH_DISK.operation)) {
        collectionsToAdd = collectionsToModify;
    } else {
        // DETACH case
        collectionsToRemove = collectionsToModify;
    }
    ServiceStateCollectionUpdateRequest updateDiskLinksRequest = ServiceStateCollectionUpdateRequest.create(collectionsToAdd, collectionsToRemove);
    Operation computeStateOp = Operation.createPatch(createInventoryUri(service.getHost(), computeStateLink)).setBody(updateDiskLinksRequest).setReferer(service.getUri());
    return service.sendWithDeferredResult(computeStateOp);
}
Also used : Collection(java.util.Collection) ServiceStateCollectionUpdateRequest(com.vmware.xenon.common.ServiceStateCollectionUpdateRequest) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) Operation(com.vmware.xenon.common.Operation)

Aggregations

ServiceStateCollectionUpdateRequest (com.vmware.xenon.common.ServiceStateCollectionUpdateRequest)13 Collection (java.util.Collection)13 Operation (com.vmware.xenon.common.Operation)10 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)5 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)4 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)4 NetworkInterfaceState (com.vmware.photon.controller.model.resources.NetworkInterfaceService.NetworkInterfaceState)4 ResourceState (com.vmware.photon.controller.model.resources.ResourceState)4 OperationJoin (com.vmware.xenon.common.OperationJoin)4 QueryTask (com.vmware.xenon.services.common.QueryTask)4 List (java.util.List)4 Map (java.util.Map)4 AdapterUtils.createPostOperation (com.vmware.photon.controller.model.adapters.util.AdapterUtils.createPostOperation)3 Utils (com.vmware.xenon.common.Utils)3 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)2 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)2 AdapterUtils (com.vmware.photon.controller.model.adapters.util.AdapterUtils)2