CLOUDSTACK-5997:

Template state changes side affects
This commit is contained in:
Nitin Mehta 2014-01-30 17:59:55 -08:00
parent d84a6937d7
commit 0e2e6995b7
4 changed files with 30 additions and 27 deletions

View File

@ -28,6 +28,7 @@ import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import com.cloud.template.VirtualMachineTemplate;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@ -147,7 +148,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
sb.and("format", sb.entity().getFormat(), SearchCriteria.Op.EQ);
sb.and("type", sb.entity().getTemplateType(), SearchCriteria.Op.EQ);
sb.and("bootable", sb.entity().isBootable(), SearchCriteria.Op.EQ);
sb.and("removed", sb.entity().getRemoved(), SearchCriteria.Op.EQ);
sb.and("state", sb.entity().getState(), SearchCriteria.Op.EQ);
SearchBuilder<ResourceTagVO> tagSearch = _tagsDao.createSearchBuilder();
for (int count = 0; count < tags.size(); count++) {
@ -170,7 +171,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
}
if (!listRemoved) {
sc.setParameters("removed", (Object)null);
sc.setParameters("state", VirtualMachineTemplate.State.Active);
}
if (tags != null && !tags.isEmpty()) {
@ -197,7 +198,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
sc.setParameters("type", TemplateType.USER.toString());
if (!listRemoved) {
sc.setParameters("removed", (Object)null);
sc.setParameters("state", VirtualMachineTemplate.State.Active);
}
return listBy(sc);
@ -217,7 +218,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
String sql =
"select * from template_host_ref as thr INNER JOIN vm_template as t ON t.id=thr.template_id "
+ "where thr.host_id=? and t.public=0 and t.featured=0 and t.type='USER' and t.removed is NULL";
+ "where thr.host_id=? and t.public=0 and t.featured=0 and t.type='USER' and t.state='Active'";
List<Long> l = new ArrayList<Long>();
@ -265,6 +266,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
public List<VMTemplateVO> listByAccountId(long accountId) {
SearchCriteria<VMTemplateVO> sc = AccountIdSearch.create();
sc.setParameters("accountId", accountId);
sc.setParameters("state", VirtualMachineTemplate.State.Active);
return listBy(sc);
}
@ -312,12 +314,12 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
PublicIsoSearch.and("format", PublicIsoSearch.entity().getFormat(), SearchCriteria.Op.EQ);
PublicIsoSearch.and("type", PublicIsoSearch.entity().getTemplateType(), SearchCriteria.Op.EQ);
PublicIsoSearch.and("bootable", PublicIsoSearch.entity().isBootable(), SearchCriteria.Op.EQ);
PublicIsoSearch.and("removed", PublicIsoSearch.entity().getRemoved(), SearchCriteria.Op.EQ);
PublicIsoSearch.and("state", PublicIsoSearch.entity().getState(), SearchCriteria.Op.EQ);
UserIsoSearch = createSearchBuilder();
UserIsoSearch.and("format", UserIsoSearch.entity().getFormat(), SearchCriteria.Op.EQ);
UserIsoSearch.and("type", UserIsoSearch.entity().getTemplateType(), SearchCriteria.Op.EQ);
UserIsoSearch.and("removed", UserIsoSearch.entity().getRemoved(), SearchCriteria.Op.EQ);
UserIsoSearch.and("state", UserIsoSearch.entity().getState(), SearchCriteria.Op.EQ);
tmpltTypeHyperSearch = createSearchBuilder();
tmpltTypeHyperSearch.and("templateType", tmpltTypeHyperSearch.entity().getTemplateType(), SearchCriteria.Op.EQ);
@ -332,7 +334,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
tmpltTypeHyperSearch.done();
readySystemTemplateSearch = createSearchBuilder();
readySystemTemplateSearch.and("removed", readySystemTemplateSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
readySystemTemplateSearch.and("state", readySystemTemplateSearch.entity().getState(), SearchCriteria.Op.EQ);
readySystemTemplateSearch.and("templateType", readySystemTemplateSearch.entity().getTemplateType(), SearchCriteria.Op.EQ);
SearchBuilder<TemplateDataStoreVO> templateDownloadSearch = _templateDataStoreDao.createSearchBuilder();
templateDownloadSearch.and("downloadState", templateDownloadSearch.entity().getDownloadState(), SearchCriteria.Op.EQ);
@ -355,13 +357,13 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
tmpltTypeHyperSearch2.and("templateName", tmpltTypeHyperSearch2.entity().getName(), SearchCriteria.Op.EQ);
tmpltTypeSearch = createSearchBuilder();
tmpltTypeSearch.and("removed", tmpltTypeSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
tmpltTypeSearch.and("state", tmpltTypeSearch.entity().getState(), SearchCriteria.Op.EQ);
tmpltTypeSearch.and("templateType", tmpltTypeSearch.entity().getTemplateType(), SearchCriteria.Op.EQ);
AccountIdSearch = createSearchBuilder();
AccountIdSearch.and("accountId", AccountIdSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
AccountIdSearch.and("publicTemplate", AccountIdSearch.entity().isPublicTemplate(), SearchCriteria.Op.EQ);
AccountIdSearch.and("removed", AccountIdSearch.entity().getRemoved(), SearchCriteria.Op.NULL); // only list not removed templates for this account
AccountIdSearch.and("state", AccountIdSearch.entity().getState(), SearchCriteria.Op.EQ); // only list not removed templates for this account
AccountIdSearch.done();
SearchBuilder<VMTemplateZoneVO> tmpltZoneSearch = _templateZoneDao.createSearchBuilder();
@ -369,7 +371,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
tmpltZoneSearch.and("zoneId", tmpltZoneSearch.entity().getZoneId(), SearchCriteria.Op.EQ);
TmpltsInZoneSearch = createSearchBuilder();
TmpltsInZoneSearch.and("removed", TmpltsInZoneSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
TmpltsInZoneSearch.and("state", TmpltsInZoneSearch.entity().getState(), SearchCriteria.Op.EQ);
TmpltsInZoneSearch.and().op("avoidtype", TmpltsInZoneSearch.entity().getTemplateType(), SearchCriteria.Op.NEQ);
TmpltsInZoneSearch.or("templateType", TmpltsInZoneSearch.entity().getTemplateType(), SearchCriteria.Op.NULL);
TmpltsInZoneSearch.cp();
@ -378,12 +380,12 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
TmpltsInZoneSearch.done();
ActiveTmpltSearch = createSearchBuilder();
ActiveTmpltSearch.and("removed", ActiveTmpltSearch.entity().getRemoved(), SearchCriteria.Op.NULL);
ActiveTmpltSearch.and("state", ActiveTmpltSearch.entity().getState(), SearchCriteria.Op.EQ);
CountTemplatesByAccount = createSearchBuilder(Long.class);
CountTemplatesByAccount.select(null, Func.COUNT, null);
CountTemplatesByAccount.and("account", CountTemplatesByAccount.entity().getAccountId(), SearchCriteria.Op.EQ);
CountTemplatesByAccount.and("removed", CountTemplatesByAccount.entity().getRemoved(), SearchCriteria.Op.NULL);
CountTemplatesByAccount.and("state", CountTemplatesByAccount.entity().getState(), SearchCriteria.Op.EQ);
CountTemplatesByAccount.done();
// updateStateSearch = this.createSearchBuilder();
@ -774,6 +776,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
public List<VMTemplateVO> listAllInZone(long dataCenterId) {
SearchCriteria<VMTemplateVO> sc = TmpltsInZoneSearch.create();
sc.setParameters("avoidtype", TemplateType.PERHOST.toString());
sc.setParameters("state", VirtualMachineTemplate.State.Active);
sc.setJoinParameters("tmpltzone", "zoneId", dataCenterId);
return listBy(sc);
}
@ -781,6 +784,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
@Override
public List<VMTemplateVO> listAllActive() {
SearchCriteria<VMTemplateVO> sc = ActiveTmpltSearch.create();
sc.setParameters("state", VirtualMachineTemplate.State.Active.toString());
return listBy(sc);
}
@ -788,6 +792,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
public List<VMTemplateVO> listDefaultBuiltinTemplates() {
SearchCriteria<VMTemplateVO> sc = tmpltTypeSearch.create();
sc.setParameters("templateType", Storage.TemplateType.BUILTIN);
sc.setParameters("state", VirtualMachineTemplate.State.Active);
return listBy(sc);
}
@ -813,6 +818,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
public VMTemplateVO findSystemVMReadyTemplate(long zoneId, HypervisorType hypervisorType) {
SearchCriteria<VMTemplateVO> sc = readySystemTemplateSearch.create();
sc.setParameters("templateType", Storage.TemplateType.SYSTEM);
sc.setParameters("state", VirtualMachineTemplate.State.Active);
sc.setJoinParameters("tmplHyper", "type", Host.Type.Routing);
sc.setJoinParameters("tmplHyper", "zoneId", zoneId);
sc.setJoinParameters("vmTemplateJoinTemplateStoreRef", "downloadState", VMTemplateStorageResourceAssoc.Status.DOWNLOADED);
@ -873,6 +879,7 @@ public class VMTemplateDaoImpl extends GenericDaoBase<VMTemplateVO, Long> implem
public Long countTemplatesForAccount(long accountId) {
SearchCriteria<Long> sc = CountTemplatesByAccount.create();
sc.setParameters("account", accountId);
sc.setParameters("state", VirtualMachineTemplate.State.Active.toString());
return customSearch(sc, null).get(0);
}

View File

@ -382,14 +382,16 @@ public class HypervisorTemplateAdapter extends TemplateAdapterBase {
// find all eligible image stores for this template
List<DataStore> iStores = templateMgr.getImageStoreByTemplate(template.getId(), null);
if (iStores == null || iStores.size() == 0) {
// remove template from vm_templates table
if (_tmpltDao.remove(template.getId())) {
// Decrement the number of templates and total secondary storage
// space used by the account
Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
_resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
_resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
}
// Mark template as Inactive.
template.setState(VirtualMachineTemplate.State.Inactive);
_tmpltDao.update(template.getId(), template);
// Decrement the number of templates and total secondary storage
// space used by the account
Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
_resourceLimitMgr.decrementResourceCount(template.getAccountId(), ResourceType.template);
_resourceLimitMgr.recalculateResourceCount(template.getAccountId(), account.getDomainId(), ResourceType.secondary_storage.getOrdinal());
}
}
return success;

View File

@ -1084,9 +1084,6 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
throw new InvalidParameterValueException("Please specify a valid template.");
}
template.setState(VirtualMachineTemplate.State.Inactive);
_tmpltDao.update(template.getId(), template);
TemplateAdapter adapter = getAdapter(template.getHypervisorType());
TemplateProfile profile = adapter.prepareDelete(cmd);
return adapter.delete(profile);
@ -1119,9 +1116,6 @@ public class TemplateManagerImpl extends ManagerBase implements TemplateManager,
throw new InvalidParameterValueException("Failed to find a secondary storage store in the specified zone.");
}
template.setState(VirtualMachineTemplate.State.Inactive);
_tmpltDao.update(template.getId(), template);
TemplateAdapter adapter = getAdapter(template.getHypervisorType());
TemplateProfile profile = adapter.prepareDelete(cmd);
boolean result = adapter.delete(profile);

View File

@ -299,7 +299,7 @@ CREATE VIEW `cloud`.`template_view` AS
left join
`cloud`.`vm_template` source_template ON source_template.id = vm_template.source_template_id
left join
`cloud`.`template_store_ref` ON template_store_ref.template_id = vm_template.id and template_store_ref.store_role = 'Image'
`cloud`.`template_store_ref` ON template_store_ref.template_id = vm_template.id and template_store_ref.store_role = 'Image' and template_store_ref.destroyed=0
left join
`cloud`.`image_store` ON image_store.removed is NULL AND template_store_ref.store_id is not NULL AND image_store.id = template_store_ref.store_id
left join