Search in sources :

Example 6 with ImageStoreEntity

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();
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) Event(org.apache.cloudstack.framework.events.Event) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) AccountVO(com.cloud.user.AccountVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) TemplateProfile(com.cloud.storage.TemplateProfile) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with ImageStoreEntity

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;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) VolumeApiResult(org.apache.cloudstack.engine.subsystem.api.storage.VolumeService.VolumeApiResult) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with ImageStoreEntity

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);
        }
    }
}
Also used : VolumeDataStoreVO(org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)

Aggregations

ImageStoreEntity (org.apache.cloudstack.storage.image.datastore.ImageStoreEntity)8 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)5 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)4 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)4 VMTemplateVO (com.cloud.storage.VMTemplateVO)3 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 ExecutionException (java.util.concurrent.ExecutionException)2 DataObject (org.apache.cloudstack.engine.subsystem.api.storage.DataObject)2 TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)2 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)2 VolumeDataStoreVO (org.apache.cloudstack.storage.datastore.db.VolumeDataStoreVO)2 Answer (com.cloud.agent.api.Answer)1 MigrateVolumeAnswer (com.cloud.agent.api.storage.MigrateVolumeAnswer)1 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)1 TemplateProfile (com.cloud.storage.TemplateProfile)1 VMTemplateZoneVO (com.cloud.storage.VMTemplateZoneVO)1 Account (com.cloud.user.Account)1 AccountVO (com.cloud.user.AccountVO)1 Pair (com.cloud.utils.Pair)1