Search in sources :

Example 26 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class CreateSnapshotFromVMSnapshotCmd method execute.

@Override
public void execute() {
    s_logger.info("CreateSnapshotFromVMSnapshotCmd with vm snapshot id:" + getVMSnapshotId() + " and snapshot id:" + getEntityId() + " starts:" + System.currentTimeMillis());
    CallContext.current().setEventDetails("Vm Snapshot Id: " + getVMSnapshotId());
    Snapshot snapshot = null;
    try {
        snapshot = _snapshotService.backupSnapshotFromVmSnapshot(getEntityId(), getVmId(), getVolumeId(), getVMSnapshotId());
        if (snapshot != null) {
            SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
            response.setResponseName(getCommandName());
            this.setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot from vm snapshot " + getVMSnapshotId());
        }
    } catch (final InvalidParameterValueException ex) {
        throw ex;
    } catch (final Exception e) {
        s_logger.debug("Failed to create snapshot", e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot from vm snapshot " + getVMSnapshotId());
    } finally {
        if (snapshot == null) {
            try {
                _snapshotService.deleteSnapshot(getEntityId());
            } catch (final Exception e) {
                s_logger.debug("Failed to clean failed snapshot" + getEntityId());
            }
        }
    }
}
Also used : VMSnapshot(com.cloud.vm.snapshot.VMSnapshot) Snapshot(com.cloud.storage.Snapshot) ServerApiException(com.cloud.api.ServerApiException) VMSnapshotResponse(com.cloud.api.response.VMSnapshotResponse) SnapshotResponse(com.cloud.api.response.SnapshotResponse) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ServerApiException(com.cloud.api.ServerApiException) InvalidParameterValueException(com.cloud.utils.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 27 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class ListSnapshotsCmd method execute.

@Override
public void execute() {
    final Pair<List<? extends Snapshot>, Integer> result = _snapshotService.listSnapshots(this);
    final ListResponse<SnapshotResponse> response = new ListResponse<>();
    final List<SnapshotResponse> snapshotResponses = new ArrayList<>();
    for (final Snapshot snapshot : result.first()) {
        final SnapshotResponse snapshotResponse = _responseGenerator.createSnapshotResponse(snapshot);
        snapshotResponse.setObjectName("snapshot");
        snapshotResponses.add(snapshotResponse);
    }
    response.setResponses(snapshotResponses, result.second());
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : Snapshot(com.cloud.storage.Snapshot) ListResponse(com.cloud.api.response.ListResponse) SnapshotResponse(com.cloud.api.response.SnapshotResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 28 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class RevertSnapshotCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Snapshot Id: " + getId());
    final Snapshot snapshot = _snapshotService.revertSnapshot(getId());
    if (snapshot != null) {
        final SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to revert snapshot");
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) ServerApiException(com.cloud.api.ServerApiException) SnapshotResponse(com.cloud.api.response.SnapshotResponse)

Example 29 with Snapshot

use of com.cloud.storage.Snapshot in project cosmic by MissionCriticalCloud.

the class SnapshotStateListener method pubishOnEventBus.

private void pubishOnEventBus(final String event, final String status, final Snapshot vo, final State oldState, final State newState) {
    final String configKey = Config.PublishResourceStateEvent.key();
    final String value = s_configDao.getValue(configKey);
    final boolean configValue = Boolean.parseBoolean(value);
    if (!configValue) {
        return;
    }
    try {
        s_eventBus = ComponentContext.getComponent(EventBus.class);
    } catch (final NoSuchBeanDefinitionException nbe) {
        // no provider is configured to provide events bus, so just return
        return;
    }
    final String resourceName = getEntityFromClassName(Snapshot.class.getName());
    final com.cloud.framework.events.Event eventMsg = new com.cloud.framework.events.Event(ManagementService.Name, EventCategory.RESOURCE_STATE_CHANGE_EVENT.getName(), event, resourceName, vo.getUuid());
    final Map<String, String> eventDescription = new HashMap<>();
    eventDescription.put("resource", resourceName);
    eventDescription.put("id", vo.getUuid());
    eventDescription.put("old-state", oldState.name());
    eventDescription.put("new-state", newState.name());
    final String eventDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    eventDescription.put("eventDateTime", eventDate);
    eventMsg.setDescription(eventDescription);
    try {
        s_eventBus.publish(eventMsg);
    } catch (final EventBusException e) {
        s_logger.warn("Failed to publish state change event on the the event bus.");
    }
}
Also used : HashMap(java.util.HashMap) EventBus(com.cloud.framework.events.EventBus) Date(java.util.Date) Snapshot(com.cloud.storage.Snapshot) Event(com.cloud.storage.Snapshot.Event) EventBusException(com.cloud.framework.events.EventBusException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 30 with Snapshot

use of com.cloud.storage.Snapshot in project cloudstack by apache.

the class CreateSnapshotCmd method execute.

@Override
public void execute() {
    Snapshot snapshot;
    try {
        snapshot = _volumeService.takeSnapshot(getVolumeId(), getPolicyId(), getEntityId(), _accountService.getAccount(getEntityOwnerId()), getQuiescevm(), getLocationType(), getAsyncBackup(), getTags());
        if (snapshot != null) {
            SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Snapshot from volume [%s] was not found in database.", getVolumeUuid()));
        }
    } catch (Exception e) {
        String errorMessage = "Failed to create snapshot due to an internal error creating snapshot for volume " + getVolumeUuid();
        s_logger.error(errorMessage, e);
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, errorMessage);
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) ServerApiException(org.apache.cloudstack.api.ServerApiException) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Aggregations

Snapshot (com.cloud.storage.Snapshot)43 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)19 Test (org.junit.Test)11 ServerApiException (org.apache.cloudstack.api.ServerApiException)10 ServerApiException (com.cloud.api.ServerApiException)8 Volume (com.cloud.storage.Volume)8 VolumeVO (com.cloud.storage.VolumeVO)8 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)7 SnapshotResponse (com.cloud.api.response.SnapshotResponse)6 Account (com.cloud.user.Account)6 SnapshotResponse (org.apache.cloudstack.api.response.SnapshotResponse)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)4 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)4 DataStoreRole (com.cloud.storage.DataStoreRole)4 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)4 InvalidParameterValueException (com.cloud.utils.exception.InvalidParameterValueException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SnapshotInfo (org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo)3