Search in sources :

Example 11 with SnapshotState

use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.

the class VSphereAdapterSnapshotService method populateAndGetSnapshotState.

private SnapshotState populateAndGetSnapshotState(ResourceOperationRequest request) {
    SnapshotState snapshotState = new SnapshotState();
    snapshotState.id = UUID.randomUUID().toString();
    snapshotState.computeLink = request.resourceLink();
    snapshotState.documentSelfLink = UriUtils.buildUriPath(SnapshotService.FACTORY_LINK, getHost().nextUUID());
    final String snapshotRequestName = request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_NAME);
    final String snapshotRequestDesc = request.payload.get(VSphereConstants.VSPHERE_SNAPSHOT_DESCRIPTION);
    if (snapshotRequestName == null || snapshotRequestName.isEmpty()) {
        snapshotState.name = "snapshot-" + Utils.getNowMicrosUtc();
    } else {
        snapshotState.name = snapshotRequestName;
    }
    if (snapshotRequestDesc == null || snapshotRequestDesc.isEmpty()) {
        snapshotState.description = "description - " + snapshotState.name;
    } else {
        snapshotState.description = snapshotRequestDesc;
    }
    return snapshotState;
}
Also used : SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)

Example 12 with SnapshotState

use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.

the class VSphereAdapterSnapshotService method processNextStepsForDeleteOperation.

private void processNextStepsForDeleteOperation(SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
    final SnapshotState snapshot = context.snapshotState;
    // Update the isCurrent
    if (snapshot.isCurrent && snapshot.parentLink != null) {
        logInfo("Updating the parent of the snapshot %s to current", snapshot.name);
        SnapshotState parentSnapshot = new SnapshotState();
        parentSnapshot.isCurrent = Boolean.TRUE;
        context.snapshotOperations.add(Operation.createPatch(PhotonModelUriUtils.createInventoryUri(this.getHost(), snapshot.parentLink)).setBody(parentSnapshot).setReferer(getUri()));
    }
    // Check if the deleted snapshot is the last available snapshot
    DeferredResult<Boolean> result = isLastSnapshotForCompute(context);
    Operation[] patchComputeOp = new Operation[1];
    result.whenComplete((b, e) -> {
        if (e != null) {
            logSevere(e);
            deferredResult.fail(e);
            return;
        }
        if (b) {
            ComputeStateWithDescription compute = context.computeDescription;
            compute.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, Boolean.FALSE.toString());
            // patch compute adding property that it _hasSnapshots
            logInfo("Updating the state of compute resource: %s", compute.name);
            patchComputeOp[0] = Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.computeLink)).setBody(compute).setReferer(getUri());
            context.snapshotOperations.add(patchComputeOp[0]);
        }
        OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
            if (exc != null) {
                this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
                deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
                return;
            }
            deferredResult.complete(context);
        };
        context.snapshotOperations.add(Operation.createDelete(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setReferer(getUri()));
        OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
        joinOp.setCompletion(joinCompletion);
        joinOp.sendWith(this.getHost());
    });
}
Also used : Service(com.vmware.xenon.common.Service) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) StringUtils(org.apache.commons.lang3.StringUtils) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType) BiConsumer(java.util.function.BiConsumer) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) TaskInfo(com.vmware.vim25.TaskInfo) OperationSequence(com.vmware.xenon.common.OperationSequence) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) MapUtils(org.apache.commons.collections.MapUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Optional(java.util.Optional) VSphereConstants(com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants) FactoryService(com.vmware.xenon.common.FactoryService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) OperationJoin(com.vmware.xenon.common.OperationJoin) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)

Example 13 with SnapshotState

use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.

the class VSphereAdapterSnapshotService method isLastSnapshotForCompute.

private DeferredResult<Boolean> isLastSnapshotForCompute(SnapshotContext context) {
    DeferredResult<Boolean> dr = new DeferredResult<>();
    // find if for the compute has only one snapshot (the one which is to be deleted)
    QueryTask qTask = getQueryWithFilters(context.snapshotState.computeLink, SnapshotState.FIELD_NAME_COMPUTE_LINK);
    QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((o, e) -> {
        if (e != null) {
            logInfo(String.format("Failure getting snapshot state: %s", Utils.toString(e)));
            dr.fail(e);
            return;
        }
        QueryResultsProcessor rp = QueryResultsProcessor.create(o);
        List<SnapshotState> snapshotsFinal;
        if (!rp.hasResults()) {
            dr.complete(Boolean.FALSE);
        } else {
            snapshotsFinal = rp.streamDocuments(SnapshotState.class).collect(Collectors.toList());
            dr.complete(snapshotsFinal.size() == 1);
        }
    });
    return dr;
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 14 with SnapshotState

use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.

the class VSphereAdapterSnapshotService method revertSnapshot.

private void revertSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
    final SnapshotState snapshot = context.snapshotState;
    SnapshotState existingSnapshotState = context.existingSnapshotState;
    // Physical snapshot processing
    ManagedObjectReference snapshotMoref = CustomProperties.of(snapshot).getMoRef(CustomProperties.MOREF);
    if (snapshotMoref == null) {
        deferredResult.fail(new IllegalStateException(String.format("Cannot find the snapshot %s to revert to", snapshotMoref)));
        return;
    }
    ManagedObjectReference task;
    TaskInfo info;
    try {
        logInfo("Reverting to  snapshot with name %s", context.snapshotState.name);
        task = connection.getVimPort().revertToSnapshotTask(snapshotMoref, null, false);
        info = VimUtils.waitTaskEnd(connection, task);
        if (info.getState() != TaskInfoState.SUCCESS) {
            VimUtils.rethrow(info.getError());
        }
    } catch (Exception e) {
        logSevere("Reverting to the snapshot %s failed", context.snapshotState.name);
        deferredResult.fail(e);
        return;
    }
    // cause concurrency issue
    if (snapshot.isCurrent) {
        deferredResult.complete(context);
    } else {
        snapshot.isCurrent = true;
        context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), snapshot.documentSelfLink)).setBody(snapshot).setReferer(getUri()));
        if (existingSnapshotState != null) {
            existingSnapshotState.isCurrent = false;
            context.snapshotOperations.add(Operation.createPatch(UriUtils.buildUri(getHost(), existingSnapshotState.documentSelfLink)).setBody(existingSnapshotState).setReferer(getUri()));
        }
        OperationJoin.JoinedCompletionHandler joinCompletion = (ox, exc) -> {
            if (exc != null) {
                this.logSevere(() -> String.format("Error updating the snapshot states: %s", Utils.toString(exc)));
                deferredResult.fail(new IllegalStateException("Error updating the snapshot states"));
                return;
            }
            deferredResult.complete(context);
        };
        OperationJoin joinOp = OperationJoin.create(context.snapshotOperations);
        joinOp.setCompletion(joinCompletion);
        joinOp.sendWith(this.getHost());
    }
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) Service(com.vmware.xenon.common.Service) ComputeProperties(com.vmware.photon.controller.model.ComputeProperties) SessionUtil(com.vmware.photon.controller.model.resources.SessionUtil) ResourceOperationUtils.handleAdapterResourceOperationRegistration(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration) PhotonModelUriUtils(com.vmware.photon.controller.model.util.PhotonModelUriUtils) QueryTask(com.vmware.xenon.services.common.QueryTask) ResourceOperationSpecService(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) StringUtils(org.apache.commons.lang3.StringUtils) ResourceOperationRequest(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest) ArrayList(java.util.ArrayList) TargetCriteria(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria) Utils(com.vmware.xenon.common.Utils) AuthCredentialsService(com.vmware.xenon.services.common.AuthCredentialsService) SnapshotRequestType(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotRequestType) BiConsumer(java.util.function.BiConsumer) Connection(com.vmware.photon.controller.model.adapters.vsphere.util.connection.Connection) URI(java.net.URI) TaskInfo(com.vmware.vim25.TaskInfo) OperationSequence(com.vmware.xenon.common.OperationSequence) QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) MapUtils(org.apache.commons.collections.MapUtils) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) Operation(com.vmware.xenon.common.Operation) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SnapshotService(com.vmware.photon.controller.model.resources.SnapshotService) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference) TaskStage(com.vmware.xenon.common.TaskState.TaskStage) List(java.util.List) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) Optional(java.util.Optional) VSphereConstants(com.vmware.photon.controller.model.adapters.vsphere.constants.VSphereConstants) FactoryService(com.vmware.xenon.common.FactoryService) IAAS_API_ENABLED(com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED) TaskInfoState(com.vmware.vim25.TaskInfoState) OperationJoin(com.vmware.xenon.common.OperationJoin) PhotonModelUriUtils.createInventoryUri(com.vmware.photon.controller.model.util.PhotonModelUriUtils.createInventoryUri) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) OperationJoin(com.vmware.xenon.common.OperationJoin) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 15 with SnapshotState

use of com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState in project photon-model by vmware.

the class VSphereVMSnapshotEnumerationHelper method processSnapshot.

static void processSnapshot(VSphereIncrementalEnumerationService service, VirtualMachineSnapshotTree current, String parentLink, EnumerationProgress enumerationProgress, VmOverlay vm, String vmSelfLink) {
    enumerationProgress.getSnapshotTracker().register();
    QueryTask task = queryForSnapshot(enumerationProgress, current.getId().toString(), vmSelfLink);
    VsphereEnumerationHelper.withTaskResults(service, task, (ServiceDocumentQueryResult result) -> {
        VsphereEnumerationHelper.submitWorkToVSpherePool(service, () -> {
            SnapshotState snapshotState = constructSnapshot(service, current, parentLink, vmSelfLink, enumerationProgress, vm);
            if (result.documentLinks.isEmpty()) {
                VSphereVirtualMachineEnumerationHelper.createSnapshot(service, snapshotState).thenCompose(createdSnapshotState -> trackAndProcessChildSnapshots(service, current, enumerationProgress, vm, vmSelfLink, createdSnapshotState)).whenComplete((ss, e) -> {
                    if (e != null) {
                        service.log(Level.SEVERE, "Creation of snapshot with name {%s} failed.", snapshotState.name);
                    }
                    enumerationProgress.getSnapshotTracker().arrive();
                });
            } else {
                SnapshotState oldState = VsphereEnumerationHelper.convertOnlyResultToDocument(result, SnapshotState.class);
                updateSnapshot(service, enumerationProgress, vm, oldState, snapshotState, current.getId().toString()).thenCompose(updatedSnapshotState -> trackAndProcessChildSnapshots(service, current, enumerationProgress, vm, vmSelfLink, updatedSnapshotState)).whenComplete((ss, e) -> {
                    if (e != null) {
                        service.logSevere("Updating of snapshot with name {%s}, selfLink {%s} failed", snapshotState.name, oldState.documentSelfLink);
                    }
                    enumerationProgress.getSnapshotTracker().arrive();
                });
            }
        });
    });
}
Also used : ComputeEnumerateResourceRequest(com.vmware.photon.controller.model.adapterapi.ComputeEnumerateResourceRequest) Operation(com.vmware.xenon.common.Operation) QueryTask(com.vmware.xenon.services.common.QueryTask) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) Level(java.util.logging.Level) HashSet(java.util.HashSet) List(java.util.List) CollectionUtils(org.apache.commons.collections.CollectionUtils) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) VirtualMachineSnapshotTree(com.vmware.vim25.VirtualMachineSnapshotTree) DeferredResult(com.vmware.xenon.common.DeferredResult) UriUtils(com.vmware.xenon.common.UriUtils) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) Builder(com.vmware.xenon.services.common.QueryTask.Query.Builder) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult)

Aggregations

SnapshotState (com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)17 Operation (com.vmware.xenon.common.Operation)10 QueryTask (com.vmware.xenon.services.common.QueryTask)9 ResourceOperation (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation)8 QueryResultsProcessor (com.vmware.xenon.common.QueryResultsProcessor)8 DeferredResult (com.vmware.xenon.common.DeferredResult)7 ArrayList (java.util.ArrayList)7 ResourceOperationRequest (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationRequest)6 List (java.util.List)6 TestRequestSender (com.vmware.xenon.common.test.TestRequestSender)5 QueryUtils (com.vmware.photon.controller.model.query.QueryUtils)4 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)4 ManagedObjectReference (com.vmware.vim25.ManagedObjectReference)4 TaskInfo (com.vmware.vim25.TaskInfo)4 UriUtils (com.vmware.xenon.common.UriUtils)4 ComputeProperties (com.vmware.photon.controller.model.ComputeProperties)3 IAAS_API_ENABLED (com.vmware.photon.controller.model.UriPaths.IAAS_API_ENABLED)3 ResourceOperationSpecService (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationSpecService)3 TargetCriteria (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.TargetCriteria)3 ResourceOperationUtils.handleAdapterResourceOperationRegistration (com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperationUtils.handleAdapterResourceOperationRegistration)3