Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import javax.inject.Inject;

import com.cloud.domain.Domain;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.dao.VMInstanceDao;
import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer;
import org.apache.cloudstack.agent.directdownload.CheckUrlCommand;
import org.apache.cloudstack.annotation.AnnotationService;
Expand Down Expand Up @@ -142,6 +144,8 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
private TemplateDeployAsIsDetailsDao templateDeployAsIsDetailsDao;
@Inject
private AnnotationDao annotationDao;
@Inject
VMInstanceDao _vmInstanceDao;

@Override
public String getName() {
Expand Down Expand Up @@ -662,11 +666,7 @@ public boolean delete(TemplateProfile profile) {
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());
checkAndRemoveTemplateDetails(template);

// Remove comments (if any)
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
Expand All @@ -677,6 +677,23 @@ public boolean delete(TemplateProfile profile) {
return success;
}

/**
* removes details of the template and
* if the template is registered as deploy as is,
* then it also deletes the details related to deploy as is only if there are no VMs using the template
* @param template
*/
void checkAndRemoveTemplateDetails(VMTemplateVO template) {
templateDetailsDao.removeDetails(template.getId());

if (template.isDeployAsIs()) {
List<VMInstanceVO> vmInstanceVOList = _vmInstanceDao.listNonExpungedByTemplate(template.getId());
if (CollectionUtils.isEmpty(vmInstanceVOList)) {
templateDeployAsIsDetailsDao.removeDetails(template.getId());
}
}
}

@Override
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
TemplateProfile profile = super.prepareDelete(cmd);
Expand Down