Search in sources :

Example 1 with TemplateApiResult

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult in project cloudstack by apache.

the class TemplateManagerImpl method createPrivateTemplate.

@Override
@DB
@ActionEvent(eventType = EventTypes.EVENT_TEMPLATE_CREATE, eventDescription = "creating template", async = true)
public VirtualMachineTemplate createPrivateTemplate(CreateTemplateCmd command) throws CloudRuntimeException {
    final long templateId = command.getEntityId();
    Long volumeId = command.getVolumeId();
    Long snapshotId = command.getSnapshotId();
    VMTemplateVO privateTemplate = null;
    final Long accountId = CallContext.current().getCallingAccountId();
    SnapshotVO snapshot = null;
    VolumeVO volume = null;
    Account caller = CallContext.current().getCallingAccount();
    try {
        TemplateInfo tmplInfo = _tmplFactory.getTemplate(templateId, DataStoreRole.Image);
        long zoneId = 0;
        if (snapshotId != null) {
            snapshot = _snapshotDao.findById(snapshotId);
            zoneId = snapshot.getDataCenterId();
        } else if (volumeId != null) {
            volume = _volumeDao.findById(volumeId);
            zoneId = volume.getDataCenterId();
        }
        DataStore store = _dataStoreMgr.getImageStoreWithFreeCapacity(zoneId);
        if (store == null) {
            throw new CloudRuntimeException("cannot find an image store for zone " + zoneId);
        }
        AsyncCallFuture<TemplateApiResult> future = null;
        if (snapshotId != null) {
            DataStoreRole dataStoreRole = ApiResponseHelper.getDataStoreRole(snapshot, _snapshotStoreDao, _dataStoreMgr);
            SnapshotInfo snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
            if (dataStoreRole == DataStoreRole.Image) {
                if (snapInfo == null) {
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, DataStoreRole.Primary);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId);
                    }
                    // We need to copy the snapshot onto secondary.
                    SnapshotStrategy snapshotStrategy = _storageStrategyFactory.getSnapshotStrategy(snapshot, SnapshotOperation.BACKUP);
                    snapshotStrategy.backupSnapshot(snapInfo);
                    // Attempt to grab it again.
                    snapInfo = _snapshotFactory.getSnapshot(snapshotId, dataStoreRole);
                    if (snapInfo == null) {
                        throw new CloudRuntimeException("Cannot find snapshot " + snapshotId + " on secondary and could not create backup");
                    }
                }
                _accountMgr.checkAccess(caller, null, true, snapInfo);
                DataStore snapStore = snapInfo.getDataStore();
                if (snapStore != null) {
                    // pick snapshot image store to create template
                    store = snapStore;
                }
            }
            future = _tmpltSvr.createTemplateFromSnapshotAsync(snapInfo, tmplInfo, store);
        } else if (volumeId != null) {
            VolumeInfo volInfo = _volFactory.getVolume(volumeId);
            if (volInfo == null) {
                throw new InvalidParameterValueException("No such volume exist");
            }
            _accountMgr.checkAccess(caller, null, true, volInfo);
            future = _tmpltSvr.createTemplateFromVolumeAsync(volInfo, tmplInfo, store);
        } else {
            throw new CloudRuntimeException("Creating private Template need to specify snapshotId or volumeId");
        }
        CommandResult result = null;
        try {
            result = future.get();
            if (result.isFailed()) {
                privateTemplate = null;
                s_logger.debug("Failed to create template" + result.getResult());
                throw new CloudRuntimeException("Failed to create template" + result.getResult());
            }
            // create entries in template_zone_ref table
            if (_dataStoreMgr.isRegionStore(store)) {
                // template created on region store
                _tmpltSvr.associateTemplateToZone(templateId, null);
            } else {
                VMTemplateZoneVO templateZone = new VMTemplateZoneVO(zoneId, templateId, new Date());
                _tmpltZoneDao.persist(templateZone);
            }
            privateTemplate = _tmpltDao.findById(templateId);
            TemplateDataStoreVO srcTmpltStore = _tmplStoreDao.findByStoreTemplate(store.getId(), templateId);
            UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, privateTemplate.getAccountId(), zoneId, privateTemplate.getId(), privateTemplate.getName(), null, privateTemplate.getSourceTemplateId(), srcTmpltStore.getPhysicalSize(), privateTemplate.getSize());
            _usageEventDao.persist(usageEvent);
        } catch (InterruptedException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        } catch (ExecutionException e) {
            s_logger.debug("Failed to create template", e);
            throw new CloudRuntimeException("Failed to create template", e);
        }
    } finally {
        if (privateTemplate == null) {
            final VolumeVO volumeFinal = volume;
            final SnapshotVO snapshotFinal = snapshot;
            Transaction.execute(new TransactionCallbackNoReturn() {

                @Override
                public void doInTransactionWithoutResult(TransactionStatus status) {
                    // template_store_ref entries should have been removed using our
                    // DataObject.processEvent command in case of failure, but clean
                    // it up here to avoid
                    // some leftovers which will cause removing template from
                    // vm_template table fail.
                    _tmplStoreDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template_zone_ref record
                    _tmpltZoneDao.deletePrimaryRecordsForTemplate(templateId);
                    // Remove the template record
                    _tmpltDao.expunge(templateId);
                    // decrement resource count
                    if (accountId != null) {
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.template);
                        _resourceLimitMgr.decrementResourceCount(accountId, ResourceType.secondary_storage, new Long(volumeFinal != null ? volumeFinal.getSize() : snapshotFinal.getSize()));
                    }
                }
            });
        }
    }
    if (privateTemplate != null) {
        return privateTemplate;
    } else {
        throw new CloudRuntimeException("Failed to create a template");
    }
}
Also used : Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) VolumeInfo(org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo) UsageEventVO(com.cloud.event.UsageEventVO) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) DataStoreRole(com.cloud.storage.DataStoreRole) VolumeVO(com.cloud.storage.VolumeVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) SnapshotStrategy(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotStrategy) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) Date(java.util.Date) CommandResult(org.apache.cloudstack.storage.command.CommandResult) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) SnapshotInfo(org.apache.cloudstack.engine.subsystem.api.storage.SnapshotInfo) SnapshotVO(com.cloud.storage.SnapshotVO) ActionEvent(com.cloud.event.ActionEvent) DB(com.cloud.utils.db.DB)

Example 2 with TemplateApiResult

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult in project cloudstack by apache.

the class TemplateManagerImpl method evictTemplateFromStoragePool.

@Override
@DB
public void evictTemplateFromStoragePool(VMTemplateStoragePoolVO templatePoolVO) {
    // Need to hold the lock; otherwise, another thread may create a volume from the template at the same time.
    // Assumption here is that we will hold the same lock during create volume from template.
    VMTemplateStoragePoolVO templatePoolRef = _tmpltPoolDao.acquireInLockTable(templatePoolVO.getId());
    if (templatePoolRef == null) {
        s_logger.debug("Can't aquire the lock for template pool ref: " + templatePoolVO.getId());
        return;
    }
    PrimaryDataStore pool = (PrimaryDataStore) _dataStoreMgr.getPrimaryDataStore(templatePoolVO.getPoolId());
    TemplateInfo template = _tmplFactory.getTemplateOnPrimaryStorage(templatePoolRef.getTemplateId(), pool, templatePoolRef.getDeploymentOption());
    try {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("Evicting " + templatePoolVO);
        }
        if (pool.isManaged()) {
            // For managed store, just delete the template volume.
            AsyncCallFuture<TemplateApiResult> future = _tmpltSvr.deleteTemplateOnPrimary(template, pool);
            TemplateApiResult result = future.get();
            if (result.isFailed()) {
                s_logger.debug("Failed to delete template " + template.getId() + " from storage pool " + pool.getId());
            } else {
                // Remove the templatePoolVO.
                if (_tmpltPoolDao.remove(templatePoolVO.getId())) {
                    s_logger.debug("Successfully evicted template " + template.getName() + " from storage pool " + pool.getName());
                }
            }
        } else {
            DestroyCommand cmd = new DestroyCommand(pool, templatePoolVO);
            Answer answer = _storageMgr.sendToPool(pool, cmd);
            if (answer != null && answer.getResult()) {
                // Remove the templatePoolVO.
                if (_tmpltPoolDao.remove(templatePoolVO.getId())) {
                    s_logger.debug("Successfully evicted template " + template.getName() + " from storage pool " + pool.getName());
                }
            } else {
                s_logger.info("Will retry evict template " + template.getName() + " from storage pool " + pool.getName());
            }
        }
    } catch (StorageUnavailableException | InterruptedException | ExecutionException e) {
        s_logger.info("Storage is unavailable currently. Will retry evicte template " + template.getName() + " from storage pool " + pool.getName());
    } finally {
        _tmpltPoolDao.releaseFromLockTable(templatePoolRef.getId());
    }
}
Also used : VMTemplateStoragePoolVO(com.cloud.storage.VMTemplateStoragePoolVO) Answer(com.cloud.agent.api.Answer) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) StorageUnavailableException(com.cloud.exception.StorageUnavailableException) DestroyCommand(com.cloud.agent.api.storage.DestroyCommand) ExecutionException(java.util.concurrent.ExecutionException) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) PrimaryDataStore(org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore) DB(com.cloud.utils.db.DB)

Example 3 with TemplateApiResult

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult in project cloudstack by apache.

the class HypervisorTemplateAdapter method delete.

@Override
@DB
public boolean delete(TemplateProfile profile) {
    boolean success = false;
    VMTemplateVO template = profile.getTemplate();
    Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
    if (profile.getZoneIdList() != null && profile.getZoneIdList().size() > 1)
        throw new CloudRuntimeException("Operation is not supported for more than one zone id at a time");
    Long zoneId = null;
    if (profile.getZoneIdList() != null)
        zoneId = profile.getZoneIdList().get(0);
    // find all eligible image stores for this template
    List<DataStore> imageStores = templateMgr.getImageStoreByTemplate(template.getId(), zoneId);
    if (imageStores == null || imageStores.size() == 0) {
        // already destroyed on image stores
        success = true;
        s_logger.info("Unable to find image store still having template: " + template.getName() + ", so just mark the template removed");
    } else {
        // Make sure the template is downloaded to all found image stores
        for (DataStore store : imageStores) {
            long storeId = store.getId();
            List<TemplateDataStoreVO> templateStores = _tmpltStoreDao.listByTemplateStore(template.getId(), storeId);
            for (TemplateDataStoreVO templateStore : templateStores) {
                if (templateStore.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
                    String errorMsg = "Please specify a template that is not currently being downloaded.";
                    s_logger.debug("Template: " + template.getName() + " is currently being downloaded to secondary storage host: " + store.getName() + "; cant' delete it.");
                    throw new CloudRuntimeException(errorMsg);
                }
            }
        }
        String eventType = "";
        if (template.getFormat().equals(ImageFormat.ISO)) {
            eventType = EventTypes.EVENT_ISO_DELETE;
        } else {
            eventType = EventTypes.EVENT_TEMPLATE_DELETE;
        }
        for (DataStore imageStore : imageStores) {
            // publish zone-wide usage event
            Long sZoneId = ((ImageStoreEntity) imageStore).getDataCenterId();
            if (sZoneId != null) {
                UsageEventUtils.publishUsageEvent(eventType, template.getAccountId(), sZoneId, template.getId(), null, VirtualMachineTemplate.class.getName(), template.getUuid());
            }
            boolean dataDiskDeletetionResult = true;
            List<VMTemplateVO> dataDiskTemplates = templateDao.listByParentTemplatetId(template.getId());
            if (dataDiskTemplates != null && dataDiskTemplates.size() > 0) {
                s_logger.info("Template: " + template.getId() + " has Datadisk template(s) associated with it. Delete Datadisk templates before deleting the template");
                for (VMTemplateVO dataDiskTemplate : dataDiskTemplates) {
                    s_logger.info("Delete Datadisk template: " + dataDiskTemplate.getId() + " from image store: " + imageStore.getName());
                    AsyncCallFuture<TemplateApiResult> future = imageService.deleteTemplateAsync(imageFactory.getTemplate(dataDiskTemplate.getId(), imageStore));
                    try {
                        TemplateApiResult result = future.get();
                        dataDiskDeletetionResult = result.isSuccess();
                        if (!dataDiskDeletetionResult) {
                            s_logger.warn("Failed to delete datadisk template: " + dataDiskTemplate + " from image store: " + imageStore.getName() + " due to: " + result.getResult());
                            break;
                        }
                        // Remove from template_zone_ref
                        List<VMTemplateZoneVO> templateZones = templateZoneDao.listByZoneTemplate(sZoneId, dataDiskTemplate.getId());
                        if (templateZones != null) {
                            for (VMTemplateZoneVO templateZone : templateZones) {
                                templateZoneDao.remove(templateZone.getId());
                            }
                        }
                        // Mark datadisk template as Inactive
                        List<DataStore> iStores = templateMgr.getImageStoreByTemplate(dataDiskTemplate.getId(), null);
                        if (iStores == null || iStores.size() == 0) {
                            dataDiskTemplate.setState(VirtualMachineTemplate.State.Inactive);
                            _tmpltDao.update(dataDiskTemplate.getId(), dataDiskTemplate);
                        }
                        // Decrement total secondary storage space used by the account
                        _resourceLimitMgr.recalculateResourceCount(dataDiskTemplate.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
                    } catch (Exception e) {
                        s_logger.debug("Delete datadisk template failed", e);
                        throw new CloudRuntimeException("Delete datadisk template failed", e);
                    }
                }
            }
            // remove from template_zone_ref
            if (dataDiskDeletetionResult) {
                s_logger.info("Delete template: " + template.getId() + " from image store: " + imageStore.getName());
                AsyncCallFuture<TemplateApiResult> future = imageService.deleteTemplateAsync(imageFactory.getTemplate(template.getId(), imageStore));
                try {
                    TemplateApiResult result = future.get();
                    success = result.isSuccess();
                    if (!success) {
                        s_logger.warn("Failed to delete the template: " + template + " from the image store: " + imageStore.getName() + " due to: " + result.getResult());
                        break;
                    }
                    // remove from template_zone_ref
                    List<VMTemplateZoneVO> templateZones = templateZoneDao.listByZoneTemplate(sZoneId, template.getId());
                    if (templateZones != null) {
                        for (VMTemplateZoneVO templateZone : templateZones) {
                            templateZoneDao.remove(templateZone.getId());
                        }
                    }
                } catch (InterruptedException | ExecutionException e) {
                    s_logger.debug("Delete template Failed", e);
                    throw new CloudRuntimeException("Delete template Failed", e);
                }
            } else {
                s_logger.warn("Template: " + template.getId() + " won't be deleted from image store: " + imageStore.getName() + " because deletion of one of the Datadisk" + " templates that belonged to the template failed");
            }
        }
    }
    if (success) {
        if ((imageStores != null && imageStores.size() > 1) && (profile.getZoneIdList() != null)) {
            // if template is stored in more than one image stores, and the zone id is not null, then don't delete other templates.
            return cleanupTemplate(template, success);
        }
        // delete all cache entries for this template
        List<TemplateInfo> cacheTmpls = imageFactory.listTemplateOnCache(template.getId());
        for (TemplateInfo tmplOnCache : cacheTmpls) {
            s_logger.info("Delete template: " + tmplOnCache.getId() + " from image cache store: " + tmplOnCache.getDataStore().getName());
            tmplOnCache.delete();
        }
        // find all eligible image stores for this template
        List<DataStore> iStores = templateMgr.getImageStoreByTemplate(template.getId(), null);
        if (iStores == null || iStores.size() == 0) {
            // remove any references from template_zone_ref
            List<VMTemplateZoneVO> templateZones = templateZoneDao.listByTemplateId(template.getId());
            if (templateZones != null) {
                for (VMTemplateZoneVO templateZone : templateZones) {
                    templateZoneDao.remove(templateZone.getId());
                }
            }
            // Mark template as Inactive.
            template.setState(VirtualMachineTemplate.State.Inactive);
            _tmpltDao.remove(template.getId());
            _tmpltDao.update(template.getId(), template);
            // Decrement the number of templates and total secondary storage
            // space used by the account
            _resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
            _resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
        }
        // remove its related ACL permission
        Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
        _messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
        // Remove template details
        templateDetailsDao.removeDetails(template.getId());
        // Remove deploy-as-is details (if any)
        templateDeployAsIsDetailsDao.removeDetails(template.getId());
        // Remove comments (if any)
        AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ? AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE;
        annotationDao.removeByEntityType(entityType.name(), template.getUuid());
    }
    return success;
}
Also used : Account(com.cloud.user.Account) VMTemplateZoneVO(com.cloud.storage.VMTemplateZoneVO) VMTemplateVO(com.cloud.storage.VMTemplateVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) ExecutionException(java.util.concurrent.ExecutionException) Pair(com.cloud.utils.Pair) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ResourceAllocationException(com.cloud.exception.ResourceAllocationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) ImageStoreEntity(org.apache.cloudstack.storage.image.datastore.ImageStoreEntity) AnnotationService(org.apache.cloudstack.annotation.AnnotationService) DB(com.cloud.utils.db.DB)

Example 4 with TemplateApiResult

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult in project cloudstack by apache.

the class HypervisorTemplateAdapter method createTemplateAsyncCallBack.

protected Void createTemplateAsyncCallBack(AsyncCallbackDispatcher<HypervisorTemplateAdapter, TemplateApiResult> callback, CreateTemplateContext<TemplateApiResult> context) {
    TemplateApiResult result = callback.getResult();
    TemplateInfo template = context.template;
    if (result.isSuccess()) {
        VMTemplateVO tmplt = _tmpltDao.findById(template.getId());
        // need to grant permission for public templates
        if (tmplt.isPublicTemplate()) {
            _messageBus.publish(_name, TemplateManager.MESSAGE_REGISTER_PUBLIC_TEMPLATE_EVENT, PublishScope.LOCAL, tmplt.getId());
        }
        long accountId = tmplt.getAccountId();
        if (template.getSize() != null) {
            // publish usage event
            String etype = EventTypes.EVENT_TEMPLATE_CREATE;
            if (tmplt.getFormat() == ImageFormat.ISO) {
                etype = EventTypes.EVENT_ISO_CREATE;
            }
            // get physical size from template_store_ref table
            long physicalSize = 0;
            DataStore ds = template.getDataStore();
            TemplateDataStoreVO tmpltStore = _tmpltStoreDao.findByStoreTemplate(ds.getId(), template.getId());
            if (tmpltStore != null) {
                physicalSize = tmpltStore.getPhysicalSize();
            } else {
                s_logger.warn("No entry found in template_store_ref for template id: " + template.getId() + " and image store id: " + ds.getId() + " at the end of registering template!");
            }
            Scope dsScope = ds.getScope();
            if (dsScope.getScopeType() == ScopeType.ZONE) {
                if (dsScope.getScopeId() != null) {
                    UsageEventUtils.publishUsageEvent(etype, template.getAccountId(), dsScope.getScopeId(), template.getId(), template.getName(), null, null, physicalSize, template.getSize(), VirtualMachineTemplate.class.getName(), template.getUuid());
                } else {
                    s_logger.warn("Zone scope image store " + ds.getId() + " has a null scope id");
                }
            } else if (dsScope.getScopeType() == ScopeType.REGION) {
                // publish usage event for region-wide image store using a -1 zoneId for 4.2, need to revisit post-4.2
                UsageEventUtils.publishUsageEvent(etype, template.getAccountId(), -1, template.getId(), template.getName(), null, null, physicalSize, template.getSize(), VirtualMachineTemplate.class.getName(), template.getUuid());
            }
            _resourceLimitMgr.incrementResourceCount(accountId, ResourceType.secondary_storage, template.getSize());
        }
    }
    return null;
}
Also used : TemplateInfo(org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo) ZoneScope(org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope) Scope(org.apache.cloudstack.engine.subsystem.api.storage.Scope) PublishScope(org.apache.cloudstack.framework.messagebus.PublishScope) DataStore(org.apache.cloudstack.engine.subsystem.api.storage.DataStore) VMTemplateVO(com.cloud.storage.VMTemplateVO) TemplateDataStoreVO(org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO) TemplateApiResult(org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)

Example 5 with TemplateApiResult

use of org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult 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.getZoneIdList()).thenReturn(null);
    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)

Aggregations

TemplateApiResult (org.apache.cloudstack.engine.subsystem.api.storage.TemplateService.TemplateApiResult)14 DataStore (org.apache.cloudstack.engine.subsystem.api.storage.DataStore)13 TemplateInfo (org.apache.cloudstack.engine.subsystem.api.storage.TemplateInfo)13 ExecutionException (java.util.concurrent.ExecutionException)9 VMTemplateVO (com.cloud.storage.VMTemplateVO)8 TemplateDataStoreVO (org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO)7 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)6 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)5 DB (com.cloud.utils.db.DB)5 StorageUnavailableException (com.cloud.exception.StorageUnavailableException)4 PrimaryDataStore (org.apache.cloudstack.engine.subsystem.api.storage.PrimaryDataStore)4 VolumeInfo (org.apache.cloudstack.engine.subsystem.api.storage.VolumeInfo)4 Test (org.testng.annotations.Test)4 PermissionDeniedException (com.cloud.exception.PermissionDeniedException)3 ResourceAllocationException (com.cloud.exception.ResourceAllocationException)3 URISyntaxException (java.net.URISyntaxException)3 ZoneScope (org.apache.cloudstack.engine.subsystem.api.storage.ZoneScope)3 AsyncCallFuture (org.apache.cloudstack.framework.async.AsyncCallFuture)3 SnapshotVO (com.cloud.storage.SnapshotVO)2 VMTemplateStoragePoolVO (com.cloud.storage.VMTemplateStoragePoolVO)2