Skip to content

Commit 7e0ba1e

Browse files
Added VMs existance check
1 parent 4a78277 commit 7e0ba1e

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.inject.Inject;
3030

3131
import com.cloud.domain.Domain;
32+
import com.cloud.vm.VMInstanceVO;
3233
import org.apache.cloudstack.agent.directdownload.CheckUrlAnswer;
3334
import org.apache.cloudstack.agent.directdownload.CheckUrlCommand;
3435
import org.apache.cloudstack.annotation.AnnotationService;
@@ -665,6 +666,14 @@ public boolean delete(TemplateProfile profile) {
665666
// Remove template details
666667
templateDetailsDao.removeDetails(template.getId());
667668

669+
// Remove deploy-as-is details (if any and if there are no VMs using it)
670+
if (template.isDeployAsIs()) {
671+
List<VMInstanceVO> vmInstanceVOList = _vmInstanceDao.listNonExpungedByTemplate(template.getId());
672+
if (CollectionUtils.isEmpty(vmInstanceVOList)) {
673+
templateDeployAsIsDetailsDao.removeDetails(template.getId());
674+
}
675+
}
676+
668677
// Remove comments (if any)
669678
AnnotationService.EntityType entityType = template.getFormat().equals(ImageFormat.ISO) ?
670679
AnnotationService.EntityType.ISO : AnnotationService.EntityType.TEMPLATE;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.cloud.storage.upload.params.TemplateUploadParams;
2929
import com.cloud.storage.upload.params.UploadParams;
3030
import com.cloud.vm.VmDetailConstants;
31+
import com.cloud.vm.dao.VMInstanceDao;
3132
import org.apache.cloudstack.api.command.user.iso.GetUploadParamsForIsoCmd;
3233
import org.apache.cloudstack.api.command.user.template.GetUploadParamsForTemplateCmd;
3334
import org.apache.commons.collections.CollectionUtils;
@@ -123,6 +124,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
123124
ProjectManager _projectMgr;
124125
@Inject
125126
private TemplateDataStoreDao templateDataStoreDao;
127+
@Inject
128+
VMInstanceDao _vmInstanceDao;
126129

127130
@Override
128131
public boolean stop() {

0 commit comments

Comments
 (0)