Search in sources :

Example 31 with Snapshot

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

the class CreateTemplateCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Template Id: " + getEntityUuid() + ((getSnapshotId() == null) ? " from volume Id: " + this._uuidMgr.getUuid(Volume.class, getVolumeId()) : " from snapshot Id: " + this._uuidMgr.getUuid(Snapshot.class, getSnapshotId())));
    VirtualMachineTemplate template = _templateService.createPrivateTemplate(this);
    if (template != null) {
        List<TemplateResponse> templateResponses;
        if (isBareMetal()) {
            templateResponses = _responseGenerator.createTemplateResponses(getResponseView(), template.getId(), vmId);
        } else {
            templateResponses = _responseGenerator.createTemplateResponses(getResponseView(), template.getId(), snapshotId, volumeId, false);
        }
        TemplateResponse response = new TemplateResponse();
        if (templateResponses != null && !templateResponses.isEmpty()) {
            response = templateResponses.get(0);
        }
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create private template");
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) VirtualMachineTemplate(com.cloud.template.VirtualMachineTemplate) ServerApiException(org.apache.cloudstack.api.ServerApiException) Volume(com.cloud.storage.Volume) TemplateResponse(org.apache.cloudstack.api.response.TemplateResponse)

Example 32 with Snapshot

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

the class CreateVolumeCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Volume Id: " + getEntityUuid() + ((getSnapshotId() == null) ? "" : " from snapshot: " + this._uuidMgr.getUuid(Snapshot.class, getSnapshotId())));
    Volume volume = _volumeService.createVolume(this);
    if (volume != null) {
        VolumeResponse response = _responseGenerator.createVolumeResponse(getResponseView(), volume);
        // FIXME - have to be moved to ApiResponseHelper
        if (getSnapshotId() != null) {
            Snapshot snap = _entityMgr.findById(Snapshot.class, getSnapshotId());
            if (snap != null) {
                // if the volume was
                response.setSnapshotId(snap.getUuid());
            // created from a
            // snapshot,
            // snapshotId will
            // be set so we pass
            // it back in the
            // response
            }
        }
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a volume");
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) VolumeResponse(org.apache.cloudstack.api.response.VolumeResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) Volume(com.cloud.storage.Volume)

Example 33 with Snapshot

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

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: " + this._uuidMgr.getUuid(VMSnapshot.class, 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 (InvalidParameterValueException ex) {
        throw ex;
    } catch (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 (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(org.apache.cloudstack.api.ServerApiException) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse) VMSnapshotResponse(org.apache.cloudstack.api.response.VMSnapshotResponse) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException)

Example 34 with Snapshot

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

the class CreateSnapshotCmdTest method testCreateSuccess.

@Test
public void testCreateSuccess() {
    AccountService accountService = Mockito.mock(AccountService.class);
    Account account = Mockito.mock(Account.class);
    Mockito.when(accountService.getAccount(anyLong())).thenReturn(account);
    VolumeApiService volumeApiService = Mockito.mock(VolumeApiService.class);
    Snapshot snapshot = Mockito.mock(Snapshot.class);
    try {
        Mockito.when(volumeApiService.takeSnapshot(nullable(Long.class), nullable(Long.class), isNull(), nullable(Account.class), nullable(Boolean.class), nullable(Snapshot.LocationType.class), nullable(Boolean.class), nullable(Map.class))).thenReturn(snapshot);
    } catch (Exception e) {
        Assert.fail("Received exception when success expected " + e.getMessage());
    }
    responseGenerator = Mockito.mock(ResponseGenerator.class);
    SnapshotResponse snapshotResponse = Mockito.mock(SnapshotResponse.class);
    Mockito.when(responseGenerator.createSnapshotResponse(snapshot)).thenReturn(snapshotResponse);
    Mockito.doNothing().when(snapshotResponse).setAccountName(anyString());
    createSnapshotCmd._accountService = accountService;
    createSnapshotCmd._responseGenerator = responseGenerator;
    createSnapshotCmd._volumeService = volumeApiService;
    try {
        createSnapshotCmd.execute();
    } catch (Exception e) {
        Assert.fail("Received exception when success expected " + e.getMessage());
    }
}
Also used : Account(com.cloud.user.Account) Snapshot(com.cloud.storage.Snapshot) ResponseGenerator(org.apache.cloudstack.api.ResponseGenerator) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse) VolumeApiService(com.cloud.storage.VolumeApiService) Matchers.anyLong(org.mockito.Matchers.anyLong) AccountService(com.cloud.user.AccountService) HashMap(java.util.HashMap) Map(java.util.Map) ServerApiException(org.apache.cloudstack.api.ServerApiException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 35 with Snapshot

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

the class CreateVolumeCmdByAdmin method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Volume Id: " + getEntityId() + ((getSnapshotId() == null) ? "" : " from snapshot: " + getSnapshotId()));
    final Volume volume = _volumeService.createVolume(this);
    if (volume != null) {
        final VolumeResponse response = _responseGenerator.createVolumeResponse(ResponseView.Full, volume);
        // FIXME - have to be moved to ApiResponseHelper
        if (getSnapshotId() != null) {
            final Snapshot snap = _entityMgr.findById(Snapshot.class, getSnapshotId());
            if (snap != null) {
                // if the volume was
                response.setSnapshotId(snap.getUuid());
            // created from a
            // snapshot,
            // snapshotId will
            // be set so we pass
            // it back in the
            // response
            }
        }
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a volume");
    }
}
Also used : Snapshot(com.cloud.storage.Snapshot) VolumeResponse(com.cloud.api.response.VolumeResponse) ServerApiException(com.cloud.api.ServerApiException) Volume(com.cloud.storage.Volume)

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