Search in sources :

Example 11 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class RouterDeploymentDefinitionTest method testListByDataCenterIdVMTypeAndStates.

@Test
public void testListByDataCenterIdVMTypeAndStates() {
    // Prepare
    final VMInstanceVO vmInstanceVO = mock(VMInstanceVO.class);
    final SearchBuilder<VMInstanceVO> vmInstanceSearch = mock(SearchBuilder.class);
    when(mockVmDao.createSearchBuilder()).thenReturn(vmInstanceSearch);
    when(vmInstanceSearch.entity()).thenReturn(vmInstanceVO);
    when(vmInstanceVO.getType()).thenReturn(VirtualMachine.Type.Instance);
    when(vmInstanceVO.getState()).thenReturn(VirtualMachine.State.Stopped);
    when(vmInstanceVO.getPodIdToDeployIn()).thenReturn(POD_ID1);
    final SearchBuilder<HostPodVO> podIdSearch = mock(SearchBuilder.class);
    when(mockPodDao.createSearchBuilder()).thenReturn(podIdSearch);
    final SearchCriteria<HostPodVO> sc = mock(SearchCriteria.class);
    final HostPodVO hostPodVO = mock(HostPodVO.class);
    when(podIdSearch.entity()).thenReturn(hostPodVO);
    when(hostPodVO.getId()).thenReturn(POD_ID1);
    when(hostPodVO.getDataCenterId()).thenReturn(DATA_CENTER_ID);
    when(podIdSearch.create()).thenReturn(sc);
    final List<HostPodVO> expectedPods = mock(List.class);
    when(mockPodDao.search(sc, null)).thenReturn(expectedPods);
    // Execute
    final List<HostPodVO> pods = deployment.listByDataCenterIdVMTypeAndStates(DATA_CENTER_ID, VirtualMachine.Type.User, VirtualMachine.State.Starting, VirtualMachine.State.Running);
    // Assert
    assertNotNull(pods);
    assertEquals(expectedPods, pods);
    verify(sc, times(1)).setParameters("dc", DATA_CENTER_ID);
    verify(sc, times(1)).setJoinParameters("vmInstanceSearch", "type", VirtualMachine.Type.User);
    verify(sc, times(1)).setJoinParameters("vmInstanceSearch", "states", VirtualMachine.State.Starting, VirtualMachine.State.Running);
    verify(mockPodDao, times(1)).search(sc, null);
}
Also used : VMInstanceVO(com.cloud.vm.VMInstanceVO) HostPodVO(com.cloud.dc.HostPodVO) Test(org.junit.Test)

Example 12 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class SnapshotManagerImpl method backupSnapshotFromVmSnapshot.

@Override
public Snapshot backupSnapshotFromVmSnapshot(Long snapshotId, Long vmId, Long volumeId, Long vmSnapshotId) {
    VMInstanceVO vm = _vmDao.findById(vmId);
    if (vm == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to vm:" + vmId + " doesn't exist");
    }
    if (!HypervisorType.KVM.equals(vm.getHypervisorType())) {
        throw new InvalidParameterValueException("Unsupported hypervisor type " + vm.getHypervisorType() + ". This supports KVM only");
    }
    VMSnapshotVO vmSnapshot = _vmSnapshotDao.findById(vmSnapshotId);
    if (vmSnapshot == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to vmSnapshot:" + vmSnapshotId + " doesn't exist");
    }
    // check vmsnapshot permissions
    Account caller = CallContext.current().getCallingAccount();
    _accountMgr.checkAccess(caller, null, true, vmSnapshot);
    SnapshotVO snapshot = _snapshotDao.findById(snapshotId);
    if (snapshot == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to snapshot:" + snapshotId + " doesn't exist");
    }
    VolumeInfo volume = volFactory.getVolume(volumeId);
    if (volume == null) {
        throw new InvalidParameterValueException("Creating snapshot failed due to volume:" + volumeId + " doesn't exist");
    }
    if (volume.getState() != Volume.State.Ready) {
        throw new InvalidParameterValueException("VolumeId: " + volumeId + " is not in " + Volume.State.Ready + " state but " + volume.getState() + ". Cannot take snapshot.");
    }
    DataStore store = volume.getDataStore();
    SnapshotDataStoreVO parentSnapshotDataStoreVO = _snapshotStoreDao.findParent(store.getRole(), store.getId(), volumeId);
    if (parentSnapshotDataStoreVO != null) {
        //Double check the snapshot is removed or not
        SnapshotVO parentSnap = _snapshotDao.findById(parentSnapshotDataStoreVO.getSnapshotId());
        if (parentSnap != null && parentSnapshotDataStoreVO.getInstallPath() != null && parentSnapshotDataStoreVO.getInstallPath().equals(vmSnapshot.getName())) {
            throw new InvalidParameterValueException("Creating snapshot failed due to snapshot : " + parentSnap.getUuid() + " is created from the same vm snapshot");
        }
    }
    SnapshotInfo snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
    snapshotInfo = (SnapshotInfo) store.create(snapshotInfo);
    SnapshotDataStoreVO snapshotOnPrimaryStore = this._snapshotStoreDao.findBySnapshot(snapshot.getId(), store.getRole());
    snapshotOnPrimaryStore.setState(ObjectInDataStoreStateMachine.State.Ready);
    snapshotOnPrimaryStore.setInstallPath(vmSnapshot.getName());
    _snapshotStoreDao.update(snapshotOnPrimaryStore.getId(), snapshotOnPrimaryStore);
    snapshot.setState(Snapshot.State.CreatedOnPrimary);
    _snapshotDao.update(snapshot.getId(), snapshot);
    snapshotInfo = this.snapshotFactory.getSnapshot(snapshotId, store);
    Long snapshotOwnerId = vm.getAccountId();
    try {
        SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
        if (snapshotStrategy == null) {
            throw new CloudRuntimeException("Unable to find snaphot strategy to handle snapshot with id '" + snapshotId + "'");
        }
        snapshotInfo = snapshotStrategy.backupSnapshot(snapshotInfo);
    } catch (Exception e) {
        s_logger.debug("Failed to backup snapshot from vm snapshot", e);
        _resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.snapshot);
        _resourceLimitMgr.decrementResourceCount(snapshotOwnerId, ResourceType.secondary_storage, new Long(volume.getSize()));
        throw new CloudRuntimeException("Failed to backup snapshot from vm snapshot", e);
    }
    return snapshotInfo;
}
Also used : Account(com.cloud.user.Account) SnapshotDataStoreVO(org.apache.cloudstack.storage.datastore.db.SnapshotDataStoreVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) ConfigurationException(javax.naming.ConfigurationException) PermissionDeniedException(com.cloud.exception.PermissionDeniedException) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) VMSnapshotVO(com.cloud.vm.snapshot.VMSnapshotVO) SnapshotVO(com.cloud.storage.SnapshotVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy)

Example 13 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class VolumeApiServiceImpl method migrateVolumeThroughJobQueue.

public Outcome<Volume> migrateVolumeThroughJobQueue(final Long vmId, final long volumeId, final long destPoolId, final boolean liveMigrate) {
    final CallContext context = CallContext.current();
    final User callingUser = context.getCallingUser();
    final Account callingAccount = context.getCallingAccount();
    final VMInstanceVO vm = _vmInstanceDao.findById(vmId);
    VmWorkJobVO workJob = new VmWorkJobVO(context.getContextId());
    workJob.setDispatcher(VmWorkConstants.VM_WORK_JOB_DISPATCHER);
    workJob.setCmd(VmWorkMigrateVolume.class.getName());
    workJob.setAccountId(callingAccount.getId());
    workJob.setUserId(callingUser.getId());
    workJob.setStep(VmWorkJobVO.Step.Starting);
    workJob.setVmType(VirtualMachine.Type.Instance);
    workJob.setVmInstanceId(vm.getId());
    workJob.setRelated(AsyncJobExecutionContext.getOriginJobId());
    // save work context info (there are some duplications)
    VmWorkMigrateVolume workInfo = new VmWorkMigrateVolume(callingUser.getId(), callingAccount.getId(), vm.getId(), VolumeApiServiceImpl.VM_WORK_JOB_HANDLER, volumeId, destPoolId, liveMigrate);
    workJob.setCmdInfo(VmWorkSerializer.serialize(workInfo));
    _jobMgr.submitAsyncJob(workJob, VmWorkConstants.VM_WORK_QUEUE, vm.getId());
    AsyncJobExecutionContext.getCurrentExecutionContext().joinJob(workJob.getId());
    return new VmJobVolumeOutcome(workJob, volumeId);
}
Also used : Account(com.cloud.user.Account) User(com.cloud.user.User) VMInstanceVO(com.cloud.vm.VMInstanceVO) VmWorkMigrateVolume(com.cloud.vm.VmWorkMigrateVolume) CallContext(org.apache.cloudstack.context.CallContext) VmWorkJobVO(org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)

Example 14 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class ConfigurationManagerTest method checkIfPodIsDeletableFailureOnVmInstanceTest.

@Test(expected = CloudRuntimeException.class)
public void checkIfPodIsDeletableFailureOnVmInstanceTest() {
    HostPodVO hostPodVO = Mockito.mock(HostPodVO.class);
    Mockito.when(hostPodVO.getDataCenterId()).thenReturn(new Random().nextLong());
    Mockito.when(_podDao.findById(anyLong())).thenReturn(hostPodVO);
    VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class);
    ArrayList<VMInstanceVO> arrayList = new ArrayList<VMInstanceVO>();
    arrayList.add(vMInstanceVO);
    Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyLong(), anyBoolean())).thenReturn(0);
    Mockito.when(_volumeDao.findByPod(anyLong())).thenReturn(new ArrayList<VolumeVO>());
    Mockito.when(_hostDao.findByPodId(anyLong())).thenReturn(new ArrayList<HostVO>());
    Mockito.when(_vmInstanceDao.listByPodId(anyLong())).thenReturn(arrayList);
    Mockito.when(_clusterDao.listByPodId(anyLong())).thenReturn(new ArrayList<ClusterVO>());
    configurationMgr.checkIfPodIsDeletable(new Random().nextLong());
}
Also used : ClusterVO(com.cloud.dc.ClusterVO) Random(java.util.Random) VolumeVO(com.cloud.storage.VolumeVO) ArrayList(java.util.ArrayList) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) Test(org.junit.Test)

Example 15 with VMInstanceVO

use of com.cloud.vm.VMInstanceVO in project cloudstack by apache.

the class ConfigurationManagerTest method checkIfZoneIsDeletableFailureOnVmInstanceTest.

@Test(expected = CloudRuntimeException.class)
public void checkIfZoneIsDeletableFailureOnVmInstanceTest() {
    VMInstanceVO vMInstanceVO = Mockito.mock(VMInstanceVO.class);
    ArrayList<VMInstanceVO> arrayList = new ArrayList<VMInstanceVO>();
    arrayList.add(vMInstanceVO);
    Mockito.when(_hostDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostVO>());
    Mockito.when(_podDao.listByDataCenterId(anyLong())).thenReturn(new ArrayList<HostPodVO>());
    Mockito.when(_privateIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
    Mockito.when(_publicIpAddressDao.countIPs(anyLong(), anyBoolean())).thenReturn(0);
    Mockito.when(_vmInstanceDao.listByZoneId(anyLong())).thenReturn(arrayList);
    Mockito.when(_volumeDao.findByDc(anyLong())).thenReturn(new ArrayList<VolumeVO>());
    Mockito.when(_physicalNetworkDao.listByZone(anyLong())).thenReturn(new ArrayList<PhysicalNetworkVO>());
    configurationMgr.checkIfZoneIsDeletable(new Random().nextLong());
}
Also used : VolumeVO(com.cloud.storage.VolumeVO) Random(java.util.Random) ArrayList(java.util.ArrayList) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) VMInstanceVO(com.cloud.vm.VMInstanceVO) HostPodVO(com.cloud.dc.HostPodVO) HostVO(com.cloud.host.HostVO) Test(org.junit.Test)

Aggregations

VMInstanceVO (com.cloud.vm.VMInstanceVO)131 ArrayList (java.util.ArrayList)40 HostVO (com.cloud.host.HostVO)34 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)30 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)28 Account (com.cloud.user.Account)28 VolumeVO (com.cloud.storage.VolumeVO)24 Test (org.junit.Test)24 HostPodVO (com.cloud.dc.HostPodVO)15 HashMap (java.util.HashMap)14 NetworkVO (com.cloud.network.dao.NetworkVO)13 User (com.cloud.user.User)13 NicVO (com.cloud.vm.NicVO)13 VmWorkJobVO (org.apache.cloudstack.framework.jobs.impl.VmWorkJobVO)13 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)12 StoragePoolVO (org.apache.cloudstack.storage.datastore.db.StoragePoolVO)12 ActionEvent (com.cloud.event.ActionEvent)11 Random (java.util.Random)11 Answer (com.cloud.agent.api.Answer)10 ConcurrentOperationException (com.cloud.exception.ConcurrentOperationException)10