Skip to content

Commit cd367dc

Browse files
author
Priyank Parihar
committed
CLOUDSTACK-9607: Preventing template deletion when template is in use.
Reviewed-by: Koushik Das
1 parent c6bb8c6 commit cd367dc

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

api/src/org/apache/cloudstack/api/command/user/template/DeleteTemplateCmd.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class DeleteTemplateCmd extends BaseAsyncCmd {
5252
@Parameter(name = ApiConstants.ZONE_ID, type = CommandType.UUID, entityType = ZoneResponse.class, description = "the ID of zone of the template")
5353
private Long zoneId;
5454

55+
@Parameter(name = ApiConstants.FORCED, type = CommandType.BOOLEAN, required = false, description = "Force delete a template.")
56+
private Boolean forced;
57+
5558
/////////////////////////////////////////////////////
5659
/////////////////// Accessors ///////////////////////
5760
/////////////////////////////////////////////////////
@@ -64,6 +67,9 @@ public Long getZoneId() {
6467
return zoneId;
6568
}
6669

70+
public boolean isForced() {
71+
return (forced != null) ? forced : false;
72+
}
6773
/////////////////////////////////////////////////////
6874
/////////////// API Implementation///////////////////
6975
/////////////////////////////////////////////////////

engine/schema/src/com/cloud/vm/dao/VMInstanceDao.java

100644100755
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<
5353
*/
5454
List<VMInstanceVO> listByPodId(long podId);
5555

56+
/**
57+
* Lists non-expunged VMs by templateId
58+
* @param templateId
59+
* @return list of VMInstanceVO deployed from the specified template, that are not expunged
60+
*/
61+
public List<VMInstanceVO> listNonExpungedByTemplate(long templateId);
62+
63+
5664
/**
5765
* Lists non-expunged VMs by zone ID and templateId
5866
* @param zoneId

engine/schema/src/com/cloud/vm/dao/VMInstanceDaoImpl.java

100644100755
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implem
7272
protected SearchBuilder<VMInstanceVO> IdStatesSearch;
7373
protected SearchBuilder<VMInstanceVO> AllFieldsSearch;
7474
protected SearchBuilder<VMInstanceVO> ZoneTemplateNonExpungedSearch;
75+
protected SearchBuilder<VMInstanceVO> TemplateNonExpungedSearch;
7576
protected SearchBuilder<VMInstanceVO> NameLikeSearch;
7677
protected SearchBuilder<VMInstanceVO> StateChangeSearch;
7778
protected SearchBuilder<VMInstanceVO> TransitionSearch;
@@ -164,6 +165,12 @@ protected void init() {
164165
ZoneTemplateNonExpungedSearch.and("state", ZoneTemplateNonExpungedSearch.entity().getState(), Op.NEQ);
165166
ZoneTemplateNonExpungedSearch.done();
166167

168+
169+
TemplateNonExpungedSearch = createSearchBuilder();
170+
TemplateNonExpungedSearch.and("template", TemplateNonExpungedSearch.entity().getTemplateId(), Op.EQ);
171+
TemplateNonExpungedSearch.and("state", TemplateNonExpungedSearch.entity().getState(), Op.NEQ);
172+
TemplateNonExpungedSearch.done();
173+
167174
NameLikeSearch = createSearchBuilder();
168175
NameLikeSearch.and("name", NameLikeSearch.entity().getHostName(), Op.LIKE);
169176
NameLikeSearch.done();
@@ -327,6 +334,15 @@ public List<VMInstanceVO> listByZoneIdAndType(long zoneId, VirtualMachine.Type t
327334
return listBy(sc);
328335
}
329336

337+
@Override
338+
public List<VMInstanceVO> listNonExpungedByTemplate(long templateId) {
339+
SearchCriteria<VMInstanceVO> sc = TemplateNonExpungedSearch.create();
340+
341+
sc.setParameters("template", templateId);
342+
sc.setParameters("state", State.Expunging);
343+
return listBy(sc);
344+
}
345+
330346
@Override
331347
public List<VMInstanceVO> listNonExpungedByZoneAndTemplate(long zoneId, long templateId) {
332348
SearchCriteria<VMInstanceVO> sc = ZoneTemplateNonExpungedSearch.create();

server/src/com/cloud/template/TemplateManagerImpl.java

100644100755
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,23 @@ public boolean deleteTemplate(DeleteTemplateCmd cmd) {
11761176
throw new InvalidParameterValueException("unable to find template with id " + templateId);
11771177
}
11781178

1179+
List<VMInstanceVO> vmInstanceVOList;
1180+
if(cmd.getZoneId() != null) {
1181+
vmInstanceVOList = _vmInstanceDao.listNonExpungedByZoneAndTemplate(cmd.getZoneId(), templateId);
1182+
}
1183+
else {
1184+
vmInstanceVOList = _vmInstanceDao.listNonExpungedByTemplate(templateId);
1185+
}
1186+
if(!cmd.isForced() && CollectionUtils.isNotEmpty(vmInstanceVOList)) {
1187+
StringBuilder s = new StringBuilder("Unable to delete template with id: " + templateId + " because some VM instances are using it. ");
1188+
for (VMInstanceVO elm : vmInstanceVOList) {
1189+
s.append(elm.getInstanceName() + ", ");
1190+
}
1191+
1192+
s_logger.warn(s.substring(0,s.length()-2));
1193+
throw new InvalidParameterValueException(s.substring(0,s.length()-2));
1194+
}
1195+
11791196
_accountMgr.checkAccess(caller, AccessType.OperateEntry, true, template);
11801197

11811198
if (template.getFormat() == ImageFormat.ISO) {

0 commit comments

Comments
 (0)