From cd72e2afeef0e74db499963971720ff4a8dbd3af Mon Sep 17 00:00:00 2001 From: Wei Zhou Date: Mon, 21 Oct 2013 14:05:53 +0200 Subject: [PATCH] fix a bug in listServiceOffering with vmId --- .../com/cloud/api/query/QueryManagerImpl.java | 59 +++++++++++-------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/server/src/com/cloud/api/query/QueryManagerImpl.java b/server/src/com/cloud/api/query/QueryManagerImpl.java index 8edc2ebd1cf..cbe5e247535 100644 --- a/server/src/com/cloud/api/query/QueryManagerImpl.java +++ b/server/src/com/cloud/api/query/QueryManagerImpl.java @@ -2423,6 +2423,27 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { } } + if (vmId != null) { + UserVmVO vmInstance = _userVmDao.findById(vmId); + if ((vmInstance == null) || (vmInstance.getRemoved() != null)) { + InvalidParameterValueException ex = new InvalidParameterValueException( + "unable to find a virtual machine with specified id"); + ex.addProxyObject(vmId.toString(), "vmId"); + throw ex; + } + + _accountMgr.checkAccess(caller, null, true, vmInstance); + + ServiceOfferingVO offering = _srvOfferingDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId()); + sc.addAnd("id", SearchCriteria.Op.NEQ, offering.getId()); + + // Only return offerings with the same Guest IP type and storage + // pool preference + // sc.addAnd("guestIpType", SearchCriteria.Op.EQ, + // offering.getGuestIpType()); + sc.addAnd("useLocalStorage", SearchCriteria.Op.EQ, offering.getUseLocalStorage()); + } + // boolean includePublicOfferings = false; if ((caller.getType() == Account.ACCOUNT_TYPE_NORMAL || caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || caller.getType() == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) { @@ -2432,11 +2453,20 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { } // find all domain Id up to root domain for this account List domainIds = new ArrayList(); - DomainVO domainRecord = _domainDao.findById(caller.getDomainId()); - if (domainRecord == null) { - s_logger.error("Could not find the domainId for account:" + caller.getAccountName()); - throw new CloudAuthenticationException("Could not find the domainId for account:" - + caller.getAccountName()); + DomainVO domainRecord; + if (vmId != null) { + UserVmVO vmInstance = _userVmDao.findById(vmId); + domainRecord = _domainDao.findById(vmInstance.getDomainId()); + if ( domainRecord == null ){ + s_logger.error("Could not find the domainId for vmId:" + vmId); + throw new CloudAuthenticationException("Could not find the domainId for vmId:" + vmId); + } + } else { + domainRecord = _domainDao.findById(caller.getDomainId()); + if ( domainRecord == null ){ + s_logger.error("Could not find the domainId for account:" + caller.getAccountName()); + throw new CloudAuthenticationException("Could not find the domainId for account:" + caller.getAccountName()); + } } domainIds.add(domainRecord.getId()); while (domainRecord.getParent() != null) { @@ -2466,25 +2496,6 @@ public class QueryManagerImpl extends ManagerBase implements QueryService { ssc.addOr("name", SearchCriteria.Op.LIKE, "%" + keyword + "%"); sc.addAnd("name", SearchCriteria.Op.SC, ssc); - } else if (vmId != null) { - UserVmVO vmInstance = _userVmDao.findById(vmId); - if ((vmInstance == null) || (vmInstance.getRemoved() != null)) { - InvalidParameterValueException ex = new InvalidParameterValueException( - "unable to find a virtual machine with specified id"); - ex.addProxyObject(vmId.toString(), "vmId"); - throw ex; - } - - _accountMgr.checkAccess(caller, null, true, vmInstance); - - ServiceOfferingVO offering = _srvOfferingDao.findByIdIncludingRemoved(vmInstance.getServiceOfferingId()); - sc.addAnd("id", SearchCriteria.Op.NEQ, offering.getId()); - - // Only return offerings with the same Guest IP type and storage - // pool preference - // sc.addAnd("guestIpType", SearchCriteria.Op.EQ, - // offering.getGuestIpType()); - sc.addAnd("useLocalStorage", SearchCriteria.Op.EQ, offering.getUseLocalStorage()); } if (id != null) {