use of com.cloud.legacymodel.exceptions.ResourceAllocationException in project cosmic by MissionCriticalCloud.
the class SnapshotManagerImpl method allocSnapshot.
@Override
public Snapshot allocSnapshot(final Long volumeId, final Long policyId, String snapshotName, final boolean fromVmSnapshot) throws ResourceAllocationException {
final Account caller = CallContext.current().getCallingAccount();
final VolumeInfo volume = this.volFactory.getVolume(volumeId);
if (!fromVmSnapshot) {
supportedByHypervisor(volume);
}
// Verify permissions
this._accountMgr.checkAccess(caller, null, true, volume);
final Type snapshotType = Type.MANUAL;
final Account owner = this._accountMgr.getAccount(volume.getAccountId());
try {
this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.snapshot);
this._resourceLimitMgr.checkResourceLimit(owner, ResourceType.secondary_storage, volume.getSize());
} catch (final ResourceAllocationException e) {
throw e;
}
// Determine the name for this snapshot
// Snapshot Name: VMInstancename + volumeName + timeString
final String timeString = DateUtil.getDateDisplayString(DateUtil.GMT_TIMEZONE, new Date(), DateUtil.YYYYMMDD_FORMAT);
final VMInstanceVO vmInstance = this._vmDao.findById(volume.getInstanceId());
String vmDisplayName = "detached";
if (vmInstance != null) {
vmDisplayName = vmInstance.getHostName();
}
if (snapshotName == null) {
snapshotName = vmDisplayName + "_" + volume.getName() + "_" + timeString;
}
HypervisorType hypervisorType;
final StoragePoolVO storagePool = this._storagePoolDao.findById(volume.getDataStore().getId());
if (storagePool.getScope() == ScopeType.ZONE) {
hypervisorType = storagePool.getHypervisor();
// at the time being, managed storage only supports XenServer, ESX(i), and KVM (i.e. not Hyper-V), so the VHD file type can be mapped to XenServer
if (storagePool.isManaged() && HypervisorType.Any.equals(hypervisorType) && ImageFormat.VHD.equals(volume.getFormat())) {
hypervisorType = HypervisorType.XenServer;
}
} else {
hypervisorType = volume.getHypervisorType();
}
final SnapshotVO snapshotVO = new SnapshotVO(volume.getDataCenterId(), volume.getAccountId(), volume.getDomainId(), volume.getId(), volume.getDiskOfferingId(), snapshotName, (short) snapshotType.ordinal(), snapshotType.name(), volume.getSize(), volume.getMinIops(), volume.getMaxIops(), hypervisorType);
final SnapshotVO snapshot = this._snapshotDao.persist(snapshotVO);
if (snapshot == null) {
throw new CloudRuntimeException("Failed to create snapshot for volume: " + volume.getId());
}
this._resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.snapshot);
this._resourceLimitMgr.incrementResourceCount(volume.getAccountId(), ResourceType.secondary_storage, new Long(volume.getSize()));
return snapshot;
}
use of com.cloud.legacymodel.exceptions.ResourceAllocationException in project cosmic by MissionCriticalCloud.
the class VolumeApiServiceImplTest method testResourceLimitCheckForUploadedVolume.
// The resource limit check for primary storage should not be skipped for Volume in 'Uploaded' state.
@Test
public void testResourceLimitCheckForUploadedVolume() throws NoSuchFieldException, IllegalAccessException, ResourceAllocationException {
doThrow(new ResourceAllocationException("primary storage resource limit check failed", Resource.ResourceType.primary_storage)).when(_svc._resourceLimitMgr).checkResourceLimit(any(AccountVO.class), any(Resource.ResourceType.class), any(Long.class));
final UserVmVO vm = Mockito.mock(UserVmVO.class);
final VolumeInfo volumeToAttach = Mockito.mock(VolumeInfo.class);
when(volumeToAttach.getId()).thenReturn(9L);
when(volumeToAttach.getDataCenterId()).thenReturn(34L);
when(volumeToAttach.getVolumeType()).thenReturn(VolumeType.DATADISK);
when(volumeToAttach.getInstanceId()).thenReturn(null);
when(_userVmDao.findById(anyLong())).thenReturn(vm);
when(vm.getType()).thenReturn(VirtualMachineType.User);
when(vm.getState()).thenReturn(State.Running);
when(vm.getDataCenterId()).thenReturn(34L);
when(_svc._volsDao.findByInstanceAndType(anyLong(), any(VolumeType.class))).thenReturn(new ArrayList(10));
when(_svc.volFactory.getVolume(9L)).thenReturn(volumeToAttach);
when(volumeToAttach.getState()).thenReturn(Volume.State.Uploaded);
final DataCenterVO zoneWithDisabledLocalStorage = Mockito.mock(DataCenterVO.class);
when(_svc._dcDao.findById(anyLong())).thenReturn(zoneWithDisabledLocalStorage);
try {
_svc.attachVolumeToVM(2L, 9L, null);
} catch (final InvalidParameterValueException e) {
Assert.assertEquals(e.getMessage(), ("primary storage resource limit check failed"));
}
}
Aggregations