From 4907a8f6faf17294f4ca58e05cee9faaf77a385b Mon Sep 17 00:00:00 2001 From: Harikrishna Patnala Date: Wed, 11 Dec 2013 10:30:39 -0800 Subject: [PATCH] CLOUDSTACK-4904: Unable to see a derieved template if the parent template is deleted Signed off by : nitin mehta --- .../apache/cloudstack/api/ApiConstants.java | 1 + .../api/command/user/iso/ListIsosCmd.java | 7 +++++ .../user/template/ListTemplatesCmd.java | 7 +++++ .../com/cloud/api/query/QueryManagerImpl.java | 27 +++++++++++++++---- .../cloud/api/query/dao/TemplateJoinDao.java | 5 ++++ .../api/query/dao/TemplateJoinDaoImpl.java | 8 ++++++ 6 files changed, 50 insertions(+), 5 deletions(-) diff --git a/api/src/org/apache/cloudstack/api/ApiConstants.java b/api/src/org/apache/cloudstack/api/ApiConstants.java index e08923c29ba..1f9948198e5 100755 --- a/api/src/org/apache/cloudstack/api/ApiConstants.java +++ b/api/src/org/apache/cloudstack/api/ApiConstants.java @@ -209,6 +209,7 @@ public class ApiConstants { public static final String SENT_BYTES = "sentbytes"; public static final String SERVICE_OFFERING_ID = "serviceofferingid"; public static final String SHOW_CAPACITIES = "showcapacities"; + public static final String SHOW_REMOVED = "showremoved"; public static final String SIZE = "size"; public static final String SNAPSHOT_ID = "snapshotid"; public static final String SNAPSHOT_POLICY_ID = "snapshotpolicyid"; diff --git a/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java b/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java index c3f558bd249..9b4b414cf53 100644 --- a/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/iso/ListIsosCmd.java @@ -79,6 +79,9 @@ public class ListIsosCmd extends BaseListTaggedResourcesCmd { description="the ID of the zone") private Long zoneId; + @Parameter(name=ApiConstants.SHOW_REMOVED, type=CommandType.BOOLEAN, description="show removed ISOs as well") + private Boolean showRemoved; + ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// @@ -116,6 +119,10 @@ public class ListIsosCmd extends BaseListTaggedResourcesCmd { return zoneId; } + public Boolean getShowRemoved() { + return (showRemoved != null ? showRemoved : false); + } + public boolean listInReadyState() { Account account = CallContext.current().getCallingAccount(); // It is account specific if account is admin type and domainId and accountName are not null diff --git a/api/src/org/apache/cloudstack/api/command/user/template/ListTemplatesCmd.java b/api/src/org/apache/cloudstack/api/command/user/template/ListTemplatesCmd.java index 4b349092125..b055bf243ab 100644 --- a/api/src/org/apache/cloudstack/api/command/user/template/ListTemplatesCmd.java +++ b/api/src/org/apache/cloudstack/api/command/user/template/ListTemplatesCmd.java @@ -70,6 +70,9 @@ public class ListTemplatesCmd extends BaseListTaggedResourcesCmd { @Parameter(name=ApiConstants.ZONE_ID, type=CommandType.UUID, entityType = ZoneResponse.class, description="list templates by zoneId") private Long zoneId; + + @Parameter(name=ApiConstants.SHOW_REMOVED, type=CommandType.BOOLEAN, description="show removed templates as well") + private Boolean showRemoved; ///////////////////////////////////////////////////// /////////////////// Accessors /////////////////////// ///////////////////////////////////////////////////// @@ -94,6 +97,10 @@ public class ListTemplatesCmd extends BaseListTaggedResourcesCmd { return zoneId; } + public Boolean getShowRemoved() { + return (showRemoved != null ? showRemoved : false); + } + public boolean listInReadyState() { Account account = CallContext.current().getCallingAccount(); diff --git a/server/src/com/cloud/api/query/QueryManagerImpl.java b/server/src/com/cloud/api/query/QueryManagerImpl.java index 58df38c296a..3830b2d0bfd 100644 --- a/server/src/com/cloud/api/query/QueryManagerImpl.java +++ b/server/src/com/cloud/api/query/QueryManagerImpl.java @@ -2793,6 +2793,7 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { TemplateFilter templateFilter = TemplateFilter.valueOf(cmd.getTemplateFilter()); Long id = cmd.getId(); Map tags = cmd.getTags(); + boolean showRemovedTmpl = cmd.getShowRemoved(); Account caller = CallContext.current().getCallingAccount(); boolean listAll = false; @@ -2820,14 +2821,14 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { return searchForTemplatesInternal(id, cmd.getTemplateName(), cmd.getKeyword(), templateFilter, false, null, cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, showDomr, - cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags); + cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedTmpl); } private Pair, Integer> searchForTemplatesInternal(Long templateId, String name, String keyword, TemplateFilter templateFilter, boolean isIso, Boolean bootable, Long pageSize, Long startIndex, Long zoneId, HypervisorType hyperType, boolean showDomr, boolean onlyReady, List permittedAccounts, Account caller, ListProjectResourcesCriteria listProjectResourcesCriteria, - Map tags) { + Map tags, boolean showRemovedTmpl) { // check if zone is configured, if not, just return empty list List hypers = null; @@ -2850,7 +2851,11 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { // verify templateId parameter and specially handle it if (templateId != null) { - template = _templateDao.findByIdIncludingRemoved(templateId); // Done for backward compatibility - Bug-5221 + if(showRemovedTmpl){ + template = _templateDao.findByIdIncludingRemoved(templateId); + } else { + template = _templateDao.findById(templateId); + } if (template == null) { throw new InvalidParameterValueException("Please specify a valid template ID."); }// If ISO requested then it should be ISO. @@ -2868,6 +2873,11 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { ex.addProxyObject(template.getUuid(), "templateId"); throw ex; } + // If template is removed and showRemoved flag not turned -> throw exception. findbyId returns removed template as well above. + if ((template == null) || ((template.getRemoved() != null) && !showRemovedTmpl)){ + s_logger.error("Please specify a valid template ID, template " + template.getUuid() + " is removed"); + throw new InvalidParameterValueException("Please specify a valid template ID " + template.getUuid()); + } // if template is not public, perform permission check here if (!template.isPublicTemplate() && caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { @@ -3051,7 +3061,13 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { // sc.addAnd("removed", SearchCriteria.Op.NULL); // search unique templates and find details by Ids - Pair, Integer> uniqueTmplPair = _templateJoinDao.searchAndCount(sc, searchFilter); + Pair, Integer> uniqueTmplPair = null; + if(showRemovedTmpl){ + uniqueTmplPair = _templateJoinDao.searchIncludingRemovedAndCount(sc, searchFilter); + } else { + uniqueTmplPair = _templateJoinDao.searchAndCount(sc, searchFilter); + } + Integer count = uniqueTmplPair.second(); if (count.intValue() == 0) { // empty result @@ -3088,6 +3104,7 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { TemplateFilter isoFilter = TemplateFilter.valueOf(cmd.getIsoFilter()); Long id = cmd.getId(); Map tags = cmd.getTags(); + boolean showRemovedISO = cmd.getShowRemoved(); Account caller = CallContext.current().getCallingAccount(); boolean listAll = false; @@ -3114,7 +3131,7 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { return searchForTemplatesInternal(cmd.getId(), cmd.getIsoName(), cmd.getKeyword(), isoFilter, true, cmd.isBootable(), cmd.getPageSizeVal(), cmd.getStartIndex(), cmd.getZoneId(), hypervisorType, true, - cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags); + cmd.listInReadyState(), permittedAccounts, caller, listProjectResourcesCriteria, tags, showRemovedISO); } @Override diff --git a/server/src/com/cloud/api/query/dao/TemplateJoinDao.java b/server/src/com/cloud/api/query/dao/TemplateJoinDao.java index f73f5bdea1e..e0c3fc053e6 100644 --- a/server/src/com/cloud/api/query/dao/TemplateJoinDao.java +++ b/server/src/com/cloud/api/query/dao/TemplateJoinDao.java @@ -18,6 +18,9 @@ package com.cloud.api.query.dao; import java.util.List; +import com.cloud.utils.Pair; +import com.cloud.utils.db.Filter; +import com.cloud.utils.db.SearchCriteria; import org.apache.cloudstack.api.response.TemplateResponse; import com.cloud.api.query.vo.TemplateJoinVO; @@ -42,4 +45,6 @@ public interface TemplateJoinDao extends GenericDao { List listActiveTemplates(long storeId); + public Pair, Integer> searchIncludingRemovedAndCount(final SearchCriteria sc, final Filter filter); + } diff --git a/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java b/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java index 468fb8314c0..a35b3349c10 100644 --- a/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java +++ b/server/src/com/cloud/api/query/dao/TemplateJoinDaoImpl.java @@ -24,6 +24,8 @@ import java.util.Map; import javax.ejb.Local; import javax.inject.Inject; +import com.cloud.utils.Pair; +import com.cloud.utils.db.Filter; import org.apache.cloudstack.api.BaseCmd; import org.apache.cloudstack.api.response.TemplateResponse; import org.apache.cloudstack.context.CallContext; @@ -445,6 +447,12 @@ public class TemplateJoinDaoImpl extends GenericDaoBase im return searchIncludingRemoved(sc, null, null, false); } + public Pair, Integer> searchIncludingRemovedAndCount(final SearchCriteria sc, final Filter filter) { + List objects = searchIncludingRemoved(sc, filter, null, false); + Integer count = getCount(sc); + return new Pair, Integer>(objects, count); + } + }