Skip to content

Commit 8c9a24d

Browse files
author
Daan Hoogland
committed
only clean details and annotations when this tamplate no longer exists on any zone
1 parent f13cf59 commit 8c9a24d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

server/src/main/java/com/cloud/template/HypervisorTemplateAdapter.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ public VMTemplateVO create(TemplateProfile profile) {
295295
}
296296

297297
/**
298-
* For each zone ID in {@link TemplateProfile#zoneIdList}, verifies if there is active heuristic rules for allocating template and returns the
298+
* For each zone ID in {@link TemplateProfile#getZoneIdList()}, verifies if there is active heuristic rules for allocating template and returns the
299299
* {@link DataStore} returned by the heuristic rule. If there is not an active heuristic rule, then allocate it to a random {@link DataStore}, if the ISO/template is private
300300
* or allocate it to all {@link DataStore} in the zone, if it is public.
301301
* @param profile
@@ -456,10 +456,10 @@ public List<TemplateOrVolumePostUploadCommand> doInTransaction(TransactionStatus
456456

457457
/**
458458
* If the template/ISO is marked as private, then it is allocated to a random secondary storage; otherwise, allocates to every storage pool in every zone given by the
459-
* {@link TemplateProfile#zoneIdList}.
459+
* {@link TemplateProfile#getZoneIdList()}.
460460
*/
461461
private void postUploadAllocation(List<DataStore> imageStores, VMTemplateVO template, List<TemplateOrVolumePostUploadCommand> payloads) {
462-
Set<Long> zoneSet = new HashSet<Long>();
462+
Set<Long> zoneSet = new HashSet<>();
463463
Collections.shuffle(imageStores);
464464
for (DataStore imageStore : imageStores) {
465465
Long zoneId_is = imageStore.getScope().getScopeId();
@@ -702,8 +702,8 @@ public boolean delete(TemplateProfile profile) {
702702
}
703703

704704
// delete all cache entries for this template
705-
List<TemplateInfo> cacheTmpls = imageFactory.listTemplateOnCache(template.getId());
706-
for (TemplateInfo tmplOnCache : cacheTmpls) {
705+
List<TemplateInfo> cachedTemplates = imageFactory.listTemplateOnCache(template.getId());
706+
for (TemplateInfo tmplOnCache : cachedTemplates) {
707707
s_logger.info("Delete template: " + tmplOnCache.getId() + " from image cache store: " + tmplOnCache.getDataStore().getName());
708708
tmplOnCache.delete();
709709
}
@@ -732,27 +732,32 @@ public boolean delete(TemplateProfile profile) {
732732
}
733733

734734
// remove its related ACL permission
735-
Pair<Class<?>, Long> tmplt = new Pair<Class<?>, Long>(VirtualMachineTemplate.class, template.getId());
736-
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, tmplt);
737-
738-
checkAndRemoveTemplateDetails(template);
739-
740-
// Remove comments (if any)
741-
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
742-
AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE;
743-
annotationDao.removeByEntityType(entityType.name(), template.getUuid());
735+
Pair<Class<?>, Long> templateClassForId = new Pair<>(VirtualMachineTemplate.class, template.getId());
736+
_messageBus.publish(_name, EntityManager.MESSAGE_REMOVE_ENTITY_EVENT, PublishScope.LOCAL, templateClassForId);
744737

738+
List<VMTemplateZoneVO> zoneRegistrations = templateZoneDao.listByTemplateId(template.getId());
739+
if (zoneRegistrations.isEmpty()) {
740+
removeTemplateDetails(template);
741+
removeTemplateAnnotations(template);
742+
}
745743
}
746744
return success;
747745
}
748746

747+
private void removeTemplateAnnotations(VMTemplateVO template) {
748+
// Remove comments (if any)
749+
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
750+
AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE;
751+
annotationDao.removeByEntityType(entityType.name(), template.getUuid());
752+
}
753+
749754
/**
750755
* removes details of the template and
751756
* if the template is registered as deploy as is,
752757
* then it also deletes the details related to deploy as is only if there are no VMs using the template
753758
* @param template
754759
*/
755-
void checkAndRemoveTemplateDetails(VMTemplateVO template) {
760+
private void removeTemplateDetails(VMTemplateVO template) {
756761
templateDetailsDao.removeDetails(template.getId());
757762

758763
if (template.isDeployAsIs()) {

0 commit comments

Comments
 (0)