Search in sources :

Example 1 with SnapshotResponse

use of org.apache.cloudstack.api.response.SnapshotResponse in project cloudstack by apache.

the class ApiResponseHelper method createSnapshotResponse.

@Override
public SnapshotResponse createSnapshotResponse(Snapshot snapshot) {
    SnapshotResponse snapshotResponse = new SnapshotResponse();
    snapshotResponse.setId(snapshot.getUuid());
    populateOwner(snapshotResponse, snapshot);
    VolumeVO volume = findVolumeById(snapshot.getVolumeId());
    String snapshotTypeStr = snapshot.getRecurringType().name();
    snapshotResponse.setSnapshotType(snapshotTypeStr);
    if (volume != null) {
        snapshotResponse.setVolumeId(volume.getUuid());
        snapshotResponse.setVolumeName(volume.getName());
        snapshotResponse.setVolumeType(volume.getVolumeType().name());
        DataCenter zone = ApiDBUtils.findZoneById(volume.getDataCenterId());
        if (zone != null) {
            snapshotResponse.setZoneId(zone.getUuid());
        }
        if (volume.getVolumeType() == Volume.Type.ROOT) {
            //TODO combine lines and 489 into a join in the volume dao
            VMInstanceVO instance = ApiDBUtils.findVMInstanceById(volume.getInstanceId());
            if (instance != null) {
                GuestOS guestOs = ApiDBUtils.findGuestOSById(instance.getGuestOSId());
                if (guestOs != null) {
                    snapshotResponse.setOsTypeId(guestOs.getUuid());
                    snapshotResponse.setOsDisplayName(guestOs.getDisplayName());
                }
            }
        }
    }
    snapshotResponse.setCreated(snapshot.getCreated());
    snapshotResponse.setName(snapshot.getName());
    snapshotResponse.setIntervalType(ApiDBUtils.getSnapshotIntervalTypes(snapshot.getId()));
    snapshotResponse.setState(snapshot.getState());
    snapshotResponse.setLocationType(ApiDBUtils.getSnapshotLocationType(snapshot.getId()));
    SnapshotInfo snapshotInfo = null;
    if (snapshot instanceof SnapshotInfo) {
        snapshotInfo = (SnapshotInfo) snapshot;
    } else {
        DataStoreRole dataStoreRole = getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
        snapshotInfo = snapshotfactory.getSnapshot(snapshot.getId(), dataStoreRole);
    }
    if (snapshotInfo == null) {
        s_logger.debug("Unable to find info for image store snapshot with uuid " + snapshot.getUuid());
        snapshotResponse.setRevertable(false);
    } else {
        snapshotResponse.setRevertable(snapshotInfo.isRevertable());
        snapshotResponse.setPhysicaSize(snapshotInfo.getPhysicalSize());
    }
    // set tag information
    List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Snapshot, snapshot.getId());
    List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
    for (ResourceTag tag : tags) {
        ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
        CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
    }
    snapshotResponse.setTags(tagResponses);
    snapshotResponse.setObjectName("snapshot");
    return snapshotResponse;
}
Also used : ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) DataStoreRole(com.cloud.storage.DataStoreRole) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) DataCenter(com.cloud.dc.DataCenter) ResourceTag(com.cloud.server.ResourceTag) VolumeVO(com.cloud.storage.VolumeVO) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse) VMSnapshotResponse(org.apache.cloudstack.api.response.VMSnapshotResponse) ResourceTagResponse(org.apache.cloudstack.api.response.ResourceTagResponse) GuestOS(com.cloud.storage.GuestOS)

Example 2 with SnapshotResponse

use of org.apache.cloudstack.api.response.SnapshotResponse in project cloudstack by apache.

the class ListSnapshotsCmd method execute.

@Override
public void execute() {
    Pair<List<? extends Snapshot>, Integer> result = _snapshotService.listSnapshots(this);
    ListResponse<SnapshotResponse> response = new ListResponse<SnapshotResponse>();
    List<SnapshotResponse> snapshotResponses = new ArrayList<SnapshotResponse>();
    for (Snapshot snapshot : result.first()) {
        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(org.apache.cloudstack.api.response.ListResponse) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with SnapshotResponse

use of org.apache.cloudstack.api.response.SnapshotResponse in project cloudstack by apache.

the class RevertSnapshotCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("Snapshot Id: " + getId());
    Snapshot snapshot = _snapshotService.revertSnapshot(getId());
    if (snapshot != null) {
        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(org.apache.cloudstack.api.ServerApiException) SnapshotResponse(org.apache.cloudstack.api.response.SnapshotResponse)

Example 4 with SnapshotResponse

use of org.apache.cloudstack.api.response.SnapshotResponse 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(anyLong(), anyLong(), anyLong(), any(Account.class), anyBoolean(), isNull(Snapshot.LocationType.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) AccountService(com.cloud.user.AccountService) ServerApiException(org.apache.cloudstack.api.ServerApiException) ExpectedException(org.junit.rules.ExpectedException) Test(org.junit.Test)

Example 5 with SnapshotResponse

use of org.apache.cloudstack.api.response.SnapshotResponse 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());
        if (snapshot != null) {
            SnapshotResponse response = _responseGenerator.createSnapshotResponse(snapshot);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot for volume " + getVolumeId());
        }
    } catch (Exception e) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create snapshot due to an internal error creating snapshot for volume " + getVolumeId());
    }
}
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

SnapshotResponse (org.apache.cloudstack.api.response.SnapshotResponse)6 Snapshot (com.cloud.storage.Snapshot)5 ServerApiException (org.apache.cloudstack.api.ServerApiException)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)2 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)2 ArrayList (java.util.ArrayList)2 VMSnapshotResponse (org.apache.cloudstack.api.response.VMSnapshotResponse)2 DataCenter (com.cloud.dc.DataCenter)1 ResourceTag (com.cloud.server.ResourceTag)1 DataStoreRole (com.cloud.storage.DataStoreRole)1 GuestOS (com.cloud.storage.GuestOS)1 VolumeApiService (com.cloud.storage.VolumeApiService)1 VolumeVO (com.cloud.storage.VolumeVO)1 Account (com.cloud.user.Account)1 AccountService (com.cloud.user.AccountService)1 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)1 VMInstanceVO (com.cloud.vm.VMInstanceVO)1 VMSnapshot (com.cloud.vm.snapshot.VMSnapshot)1 List (java.util.List)1