Search in sources :

Example 1 with SnapshotState

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

the class EndpointRemovalTaskServiceTest method createSnapshotState.

private static void createSnapshotState(BaseModelTest test, String endpointLink, List<String> tenantLinks) throws Throwable {
    SnapshotState snapshot = new SnapshotState();
    snapshot.name = "snapshot";
    snapshot.parentLink = null;
    snapshot.computeLink = "vmLink";
    snapshot.tenantLinks = tenantLinks;
    snapshot.endpointLinks = new HashSet<String>();
    snapshot.endpointLinks.add(endpointLink);
    snapshot.regionId = "region-id";
    test.postServiceSynchronously(SnapshotService.FACTORY_LINK, snapshot, SnapshotState.class);
}
Also used : SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)

Example 2 with SnapshotState

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

the class VSphereAdapterSnapshotService method getChildSnapshots.

private DeferredResult<List<SnapshotState>> getChildSnapshots(String snapshotLink) {
    DeferredResult<List<SnapshotState>> snapshotStates = new DeferredResult<>();
    // find the child snapshots for the given snapshot document link
    QueryTask qTask = getQueryWithFilters(snapshotLink, SnapshotState.FIELD_NAME_PARENT_LINK);
    QueryUtils.startInventoryQueryTask(this, qTask).whenComplete((o, e) -> {
        if (e != null) {
            logWarning(String.format("Failure retrieving the child snapshots %s", Utils.toString(e)));
            snapshotStates.fail(e);
            return;
        }
        QueryResultsProcessor rp = QueryResultsProcessor.create(o);
        List<SnapshotState> snapshotsTemp = new ArrayList<>();
        if (rp.hasResults()) {
            snapshotsTemp = rp.streamDocuments(SnapshotState.class).collect(Collectors.toList());
        }
        snapshotStates.complete(snapshotsTemp);
    });
    return snapshotStates;
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) DeferredResult(com.vmware.xenon.common.DeferredResult)

Example 3 with SnapshotState

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

the class VSphereAdapterSnapshotService method querySnapshotStates.

private DeferredResult<SnapshotContext> querySnapshotStates(SnapshotContext context) {
    // find if for the compute a snapshot already exist or not and if yes, get the current snapshot (among the snapshots that may exist)
    QueryTask.Query snapshotQuery = QueryTask.Query.Builder.create().addKindFieldClause(SnapshotState.class).addFieldClause(SnapshotState.FIELD_NAME_COMPUTE_LINK, context.snapshotState.computeLink).addFieldClause(SnapshotState.FIELD_NAME_IS_CURRENT, "true").build();
    QueryTask qTask = QueryTask.Builder.createDirectTask().setQuery(snapshotQuery).addOption(QueryTask.QuerySpecification.QueryOption.EXPAND_CONTENT).addOption(QueryTask.QuerySpecification.QueryOption.INDEXED_METADATA).build();
    return QueryUtils.startInventoryQueryTask(this, qTask).thenApply(op -> {
        QueryResultsProcessor rp = QueryResultsProcessor.create(op);
        if (rp.hasResults()) {
            Optional<SnapshotState> snapshotStateOptional = rp.streamDocuments(SnapshotState.class).findFirst();
            if (snapshotStateOptional.isPresent()) {
                context.existingSnapshotState = snapshotStateOptional.get();
                if (context.requestType == SnapshotService.SnapshotRequestType.CREATE) {
                    context.snapshotState.parentLink = context.existingSnapshotState.documentSelfLink;
                }
            }
        }
        return context;
    });
}
Also used : QueryResultsProcessor(com.vmware.xenon.common.QueryResultsProcessor) QueryTask(com.vmware.xenon.services.common.QueryTask) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState)

Example 4 with SnapshotState

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

the class VSphereAdapterSnapshotService method deleteSnapshot.

private void deleteSnapshot(SnapshotContext context, Connection connection, DeferredResult<SnapshotContext> deferredResult) {
    final SnapshotState snapshot = context.snapshotState;
    // 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 removed", snapshotMoref)));
        return;
    }
    ManagedObjectReference task;
    TaskInfo info;
    try {
        logInfo("Deleting snapshot with name %s", context.snapshotState.name);
        task = connection.getVimPort().removeSnapshotTask(snapshotMoref, REMOVE_CHILDREN, SNAPSHOT_CONSOLIDATION);
        info = VimUtils.waitTaskEnd(connection, task);
        if (info.getState() != TaskInfoState.SUCCESS) {
            VimUtils.rethrow(info.getError());
        }
    } catch (Exception e) {
        logSevere("Deleting the snapshot %s failed", context.snapshotState.name);
        deferredResult.fail(e);
        return;
    }
    logInfo("Deleted the snapshot with name %s successfully", context.snapshotState.name);
    // Once the actual snapshot delete is successful, process the update of the children
    // snapshot states and the next current snapshot
    final List<SnapshotState> childSnapshots = new ArrayList<>();
    DeferredResult<List<SnapshotState>> dr = getChildSnapshots(snapshot.documentSelfLink);
    List<SnapshotState> updatedChildSnapshots = new ArrayList<>();
    dr.whenComplete((o, e) -> {
        if (e != null) {
            logSevere("Retrieving the details of children snapshots failed");
            deferredResult.fail(e);
            return;
        }
        childSnapshots.addAll(o);
        logInfo("Retrieving the details of %s children snapshots ", childSnapshots.size());
        // Update the children
        if (!childSnapshots.isEmpty()) {
            logInfo("Updating the state of child snapshots of: %s", snapshot.name);
            childSnapshots.forEach(c -> {
                c.parentLink = snapshot.parentLink;
                updatedChildSnapshots.add(c);
            });
            context.snapshotOperations = updatedChildSnapshots.stream().map(childSnapshot -> Operation.createPatch(PhotonModelUriUtils.createInventoryUri(getHost(), childSnapshot.documentSelfLink)).setBody(childSnapshot).setReferer(getUri())).collect(Collectors.toList());
        }
        processNextStepsForDeleteOperation(context, deferredResult);
    });
}
Also used : TaskInfo(com.vmware.vim25.TaskInfo) SnapshotState(com.vmware.photon.controller.model.resources.SnapshotService.SnapshotState) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

Example 5 with SnapshotState

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

the class VSphereAdapterSnapshotService method createSnapshot.

private void createSnapshot(Connection connection, SnapshotContext context, DeferredResult<SnapshotContext> deferredResult) {
    ManagedObjectReference vmMoRef = CustomProperties.of(context.computeDescription).getMoRef(CustomProperties.MOREF);
    if (vmMoRef == null) {
        deferredResult.fail(new IllegalStateException("Cannot find VM to snapshot"));
        return;
    }
    ManagedObjectReference task;
    TaskInfo info;
    try {
        logInfo("Creating snapshot for compute resource %s", context.computeDescription.name);
        task = connection.getVimPort().createSnapshotTask(vmMoRef, context.snapshotState.name, context.snapshotState.description, context.snapshotMemory, false);
        info = VimUtils.waitTaskEnd(connection, task);
        if (info.getState() != TaskInfoState.SUCCESS) {
            VimUtils.rethrow(info.getError());
        }
    } catch (Exception e) {
        deferredResult.fail(e);
        return;
    }
    CustomProperties.of(context.snapshotState).put(CustomProperties.MOREF, (ManagedObjectReference) info.getResult());
    // mark this as current snapshot
    context.snapshotState.isCurrent = true;
    // create a new xenon SnapshotState
    logInfo(String.format("Creating a new snapshot state for compute : %s", context.computeDescription.name));
    Operation createSnapshotState = Operation.createPost(PhotonModelUriUtils.createInventoryUri(getHost(), SnapshotService.FACTORY_LINK)).setBody(context.snapshotState);
    if (context.existingSnapshotState != null) {
        // un-mark old snapshot as current snapshot
        context.existingSnapshotState.isCurrent = false;
        Operation patchOldSnapshot = Operation.createPatch(UriUtils.buildUri(getHost(), context.existingSnapshotState.documentSelfLink)).setBody(context.existingSnapshotState);
        OperationSequence.create(createSnapshotState).next(patchOldSnapshot).setCompletion((o, e) -> {
            if (e != null && !e.isEmpty()) {
                deferredResult.fail(e.values().iterator().next());
            }
            deferredResult.complete(context);
        }).sendWith(this);
    } else {
        context.computeDescription.customProperties.put(ComputeProperties.CUSTOM_PROP_COMPUTE_HAS_SNAPSHOTS, "true");
        // patch compute adding the property '_hasSnapshots' with true
        Operation patchCompute = Operation.createPatch(UriUtils.buildUri(getHost(), context.snapshotState.computeLink)).setBody(context.computeDescription);
        OperationSequence.create(createSnapshotState).next(patchCompute).setCompletion((o, e) -> {
            if (e != null && !e.isEmpty()) {
                deferredResult.fail(e.values().iterator().next());
            }
            logInfo(String.format("Created a new snapshot state for compute : %s", context.computeDescription.name));
            deferredResult.complete(context);
        }).sendWith(this);
    }
}
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) Operation(com.vmware.xenon.common.Operation) ResourceOperation(com.vmware.photon.controller.model.adapters.registry.operations.ResourceOperation) ManagedObjectReference(com.vmware.vim25.ManagedObjectReference)

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