use of org.apache.cloudstack.storage.image.datastore.ImageStoreEntity in project cloudstack by apache.
the class HypervisorTemplateAdapterTest method testEmitDeleteEventUuid.
@Test
public void testEmitDeleteEventUuid() throws InterruptedException, ExecutionException, EventBusException {
//All the mocks required for this test to work.
ImageStoreEntity store = mock(ImageStoreEntity.class);
when(store.getId()).thenReturn(1l);
when(store.getDataCenterId()).thenReturn(1l);
when(store.getName()).thenReturn("Test Store");
TemplateDataStoreVO dataStoreVO = mock(TemplateDataStoreVO.class);
when(dataStoreVO.getDownloadState()).thenReturn(Status.DOWNLOADED);
TemplateInfo info = mock(TemplateInfo.class);
when(info.getDataStore()).thenReturn(store);
VMTemplateVO template = mock(VMTemplateVO.class);
when(template.getId()).thenReturn(1l);
when(template.getName()).thenReturn("Test Template");
when(template.getFormat()).thenReturn(ImageFormat.QCOW2);
when(template.getAccountId()).thenReturn(1l);
//TODO possibly return this from method for comparison, if things work how i want
when(template.getUuid()).thenReturn("Test UUID");
TemplateProfile profile = mock(TemplateProfile.class);
when(profile.getTemplate()).thenReturn(template);
when(profile.getZoneId()).thenReturn(1l);
TemplateApiResult result = mock(TemplateApiResult.class);
when(result.isSuccess()).thenReturn(true);
when(result.isFailed()).thenReturn(false);
@SuppressWarnings("unchecked") AsyncCallFuture<TemplateApiResult> future = mock(AsyncCallFuture.class);
when(future.get()).thenReturn(result);
AccountVO acct = mock(AccountVO.class);
when(acct.getId()).thenReturn(1l);
when(acct.getDomainId()).thenReturn(1l);
when(_templateMgr.getImageStoreByTemplate(anyLong(), anyLong())).thenReturn(Collections.singletonList((DataStore) store));
when(_templateStoreDao.listByTemplateStore(anyLong(), anyLong())).thenReturn(Collections.singletonList(dataStoreVO));
when(_dataFactory.getTemplate(anyLong(), any(DataStore.class))).thenReturn(info);
when(_dataFactory.listTemplateOnCache(anyLong())).thenReturn(Collections.singletonList(info));
when(_templateService.deleteTemplateAsync(any(TemplateInfo.class))).thenReturn(future);
when(_accountDao.findById(anyLong())).thenReturn(acct);
when(_accountDao.findByIdIncludingRemoved(anyLong())).thenReturn(acct);
//Test actually begins here.
setupUsageUtils();
_adapter.delete(profile);
Assert.assertNotNull(usageEvents);
Assert.assertNotNull(events);
Assert.assertEquals(1, events.size());
Event event = events.get(0);
Assert.assertNotNull(event);
Assert.assertNotNull(event.getResourceType());
Assert.assertEquals(VirtualMachineTemplate.class.getName(), event.getResourceType());
Assert.assertNotNull(event.getResourceUUID());
Assert.assertEquals("Test UUID", event.getResourceUUID());
Assert.assertEquals(EventTypes.EVENT_TEMPLATE_DELETE, event.getEventType());
cleanupUsageUtils();
}
use of org.apache.cloudstack.storage.image.datastore.ImageStoreEntity in project cloudstack by apache.
the class VolumeApiServiceImpl method orchestrateExtractVolume.
private String orchestrateExtractVolume(long volumeId, long zoneId) {
// get latest volume state to make sure that it is not updated by other parallel operations
VolumeVO volume = _volsDao.findById(volumeId);
if (volume == null || volume.getState() != Volume.State.Ready) {
throw new InvalidParameterValueException("Volume to be extracted has been removed or not in right state!");
}
// perform extraction
ImageStoreEntity secStore = (ImageStoreEntity) dataStoreMgr.getImageStore(zoneId);
String value = _configDao.getValue(Config.CopyVolumeWait.toString());
NumbersUtil.parseInt(value, Integer.parseInt(Config.CopyVolumeWait.getDefaultValue()));
// Copy volume from primary to secondary storage
VolumeInfo srcVol = volFactory.getVolume(volumeId);
AsyncCallFuture<VolumeApiResult> cvAnswer = volService.copyVolume(srcVol, secStore);
// Check if you got a valid answer.
VolumeApiResult cvResult = null;
try {
cvResult = cvAnswer.get();
} catch (InterruptedException e1) {
s_logger.debug("failed copy volume", e1);
throw new CloudRuntimeException("Failed to copy volume", e1);
} catch (ExecutionException e1) {
s_logger.debug("failed copy volume", e1);
throw new CloudRuntimeException("Failed to copy volume", e1);
}
if (cvResult == null || cvResult.isFailed()) {
String errorString = "Failed to copy the volume from the source primary storage pool to secondary storage.";
throw new CloudRuntimeException(errorString);
}
VolumeInfo vol = cvResult.getVolume();
String extractUrl = secStore.createEntityExtractUrl(vol.getPath(), vol.getFormat(), vol);
VolumeDataStoreVO volumeStoreRef = _volumeStoreDao.findByVolume(volumeId);
volumeStoreRef.setExtractUrl(extractUrl);
volumeStoreRef.setExtractUrlCreated(DateUtil.now());
_volumeStoreDao.update(volumeStoreRef.getId(), volumeStoreRef);
return extractUrl;
}
use of org.apache.cloudstack.storage.image.datastore.ImageStoreEntity in project cloudstack by apache.
the class StorageManagerImpl method cleanupDownloadUrls.
@Override
public void cleanupDownloadUrls() {
// Cleanup expired volume URLs
List<VolumeDataStoreVO> volumesOnImageStoreList = _volumeStoreDao.listVolumeDownloadUrls();
for (VolumeDataStoreVO volumeOnImageStore : volumesOnImageStoreList) {
try {
long downloadUrlCurrentAgeInSecs = DateUtil.getTimeDifference(DateUtil.now(), volumeOnImageStore.getExtractUrlCreated());
if (downloadUrlCurrentAgeInSecs < _downloadUrlExpirationInterval) {
// URL hasnt expired yet
continue;
}
s_logger.debug("Removing download url " + volumeOnImageStore.getExtractUrl() + " for volume id " + volumeOnImageStore.getVolumeId());
// Remove it from image store
ImageStoreEntity secStore = (ImageStoreEntity) _dataStoreMgr.getDataStore(volumeOnImageStore.getDataStoreId(), DataStoreRole.Image);
secStore.deleteExtractUrl(volumeOnImageStore.getInstallPath(), volumeOnImageStore.getExtractUrl(), Upload.Type.VOLUME);
// Now expunge it from DB since this entry was created only for download purpose
_volumeStoreDao.expunge(volumeOnImageStore.getId());
} catch (Throwable th) {
s_logger.warn("Caught exception while deleting download url " + volumeOnImageStore.getExtractUrl() + " for volume id " + volumeOnImageStore.getVolumeId(), th);
}
}
// Cleanup expired template URLs
List<TemplateDataStoreVO> templatesOnImageStoreList = _templateStoreDao.listTemplateDownloadUrls();
for (TemplateDataStoreVO templateOnImageStore : templatesOnImageStoreList) {
try {
long downloadUrlCurrentAgeInSecs = DateUtil.getTimeDifference(DateUtil.now(), templateOnImageStore.getExtractUrlCreated());
if (downloadUrlCurrentAgeInSecs < _downloadUrlExpirationInterval) {
// URL hasnt expired yet
continue;
}
s_logger.debug("Removing download url " + templateOnImageStore.getExtractUrl() + " for template id " + templateOnImageStore.getTemplateId());
// Remove it from image store
ImageStoreEntity secStore = (ImageStoreEntity) _dataStoreMgr.getDataStore(templateOnImageStore.getDataStoreId(), DataStoreRole.Image);
secStore.deleteExtractUrl(templateOnImageStore.getInstallPath(), templateOnImageStore.getExtractUrl(), Upload.Type.TEMPLATE);
// Now remove download details from DB.
templateOnImageStore.setExtractUrl(null);
templateOnImageStore.setExtractUrlCreated(null);
_templateStoreDao.update(templateOnImageStore.getId(), templateOnImageStore);
} catch (Throwable th) {
s_logger.warn("caught exception while deleting download url " + templateOnImageStore.getExtractUrl() + " for template id " + templateOnImageStore.getTemplateId(), th);
}
}
}
Aggregations