mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	server: fix listnetworkofferings with domain, refactor listvpofferings (#6748)
This commit is contained in:
		
							parent
							
								
									d74f64a2e1
								
							
						
					
					
						commit
						2dd29558fe
					
				| @ -23,6 +23,7 @@ import org.apache.cloudstack.api.APICommand; | |||||||
| import org.apache.cloudstack.api.ApiConstants; | import org.apache.cloudstack.api.ApiConstants; | ||||||
| import org.apache.cloudstack.api.BaseListCmd; | import org.apache.cloudstack.api.BaseListCmd; | ||||||
| import org.apache.cloudstack.api.Parameter; | import org.apache.cloudstack.api.Parameter; | ||||||
|  | import org.apache.cloudstack.api.response.DomainResponse; | ||||||
| import org.apache.cloudstack.api.response.ListResponse; | import org.apache.cloudstack.api.response.ListResponse; | ||||||
| import org.apache.cloudstack.api.response.VpcOfferingResponse; | import org.apache.cloudstack.api.response.VpcOfferingResponse; | ||||||
| import org.apache.cloudstack.api.response.ZoneResponse; | import org.apache.cloudstack.api.response.ZoneResponse; | ||||||
| @ -60,10 +61,17 @@ public class ListVPCOfferingsCmd extends BaseListCmd { | |||||||
|     @Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "list VPC offerings by state") |     @Parameter(name = ApiConstants.STATE, type = CommandType.STRING, description = "list VPC offerings by state") | ||||||
|     private String state; |     private String state; | ||||||
| 
 | 
 | ||||||
|  |     @Parameter(name = ApiConstants.DOMAIN_ID, | ||||||
|  |             type = CommandType.UUID, | ||||||
|  |             entityType = DomainResponse.class, | ||||||
|  |             description = "list VPC offerings available for VPC creation in specific domain", | ||||||
|  |             since = "4.18") | ||||||
|  |     private Long domainId; | ||||||
|  | 
 | ||||||
|     @Parameter(name = ApiConstants.ZONE_ID, |     @Parameter(name = ApiConstants.ZONE_ID, | ||||||
|             type = CommandType.UUID, |             type = CommandType.UUID, | ||||||
|             entityType = ZoneResponse.class, |             entityType = ZoneResponse.class, | ||||||
|             description = "id of zone disk offering is associated with", |             description = "id of zone VPC offering is associated with", | ||||||
|             since = "4.13") |             since = "4.13") | ||||||
|     private Long zoneId; |     private Long zoneId; | ||||||
| 
 | 
 | ||||||
| @ -94,6 +102,10 @@ public class ListVPCOfferingsCmd extends BaseListCmd { | |||||||
|         return state; |         return state; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     public Long getDomainId() { | ||||||
|  |         return domainId; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public Long getZoneId() { |     public Long getZoneId() { | ||||||
|         return zoneId; |         return zoneId; | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -20,6 +20,7 @@ import java.util.List; | |||||||
| import java.util.Set; | import java.util.Set; | ||||||
| 
 | 
 | ||||||
| import com.cloud.domain.DomainVO; | import com.cloud.domain.DomainVO; | ||||||
|  | import com.cloud.user.Account; | ||||||
| import com.cloud.utils.db.GenericDao; | import com.cloud.utils.db.GenericDao; | ||||||
| 
 | 
 | ||||||
| public interface DomainDao extends GenericDao<DomainVO, Long> { | public interface DomainDao extends GenericDao<DomainVO, Long> { | ||||||
| @ -40,4 +41,6 @@ public interface DomainDao extends GenericDao<DomainVO, Long> { | |||||||
|     Set<Long> getDomainParentIds(long domainId); |     Set<Long> getDomainParentIds(long domainId); | ||||||
| 
 | 
 | ||||||
|     List<Long> getDomainChildrenIds(String path); |     List<Long> getDomainChildrenIds(String path); | ||||||
|  | 
 | ||||||
|  |     boolean domainIdListContainsAccessibleDomain(String domainIdList, Account caller, Long domainId); | ||||||
| } | } | ||||||
|  | |||||||
| @ -24,11 +24,13 @@ import java.util.List; | |||||||
| import java.util.Set; | import java.util.Set; | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
|  | import org.apache.commons.lang3.StringUtils; | ||||||
| import org.apache.log4j.Logger; | import org.apache.log4j.Logger; | ||||||
| import org.springframework.stereotype.Component; | import org.springframework.stereotype.Component; | ||||||
| 
 | 
 | ||||||
| import com.cloud.domain.Domain; | import com.cloud.domain.Domain; | ||||||
| import com.cloud.domain.DomainVO; | import com.cloud.domain.DomainVO; | ||||||
|  | import com.cloud.user.Account; | ||||||
| import com.cloud.utils.db.DB; | import com.cloud.utils.db.DB; | ||||||
| import com.cloud.utils.db.GenericDaoBase; | import com.cloud.utils.db.GenericDaoBase; | ||||||
| import com.cloud.utils.db.GenericSearchBuilder; | import com.cloud.utils.db.GenericSearchBuilder; | ||||||
| @ -290,4 +292,27 @@ public class DomainDaoImpl extends GenericDaoBase<DomainVO, Long> implements Dom | |||||||
| 
 | 
 | ||||||
|         return parentDomains; |         return parentDomains; | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     @Override | ||||||
|  |     public boolean domainIdListContainsAccessibleDomain(String domainIdList, Account caller, Long domainId) { | ||||||
|  |         if (StringUtils.isEmpty(domainIdList)) { | ||||||
|  |             return false; | ||||||
|  |         } | ||||||
|  |         String[] domainIdsArray = domainIdList.split(","); | ||||||
|  |         for (String domainIdString : domainIdsArray) { | ||||||
|  |             try { | ||||||
|  |                 Long dId = Long.valueOf(domainIdString.trim()); | ||||||
|  |                 if (!Account.Type.ADMIN.equals(caller.getType()) && | ||||||
|  |                         isChildDomain(dId, caller.getDomainId())) { | ||||||
|  |                     return true; | ||||||
|  |                 } | ||||||
|  |                 if (domainId != null && isChildDomain(dId, domainId)) { | ||||||
|  |                     return true; | ||||||
|  |                 } | ||||||
|  |             } catch (NumberFormatException nfe) { | ||||||
|  |                 s_logger.debug(String.format("Unable to parse %s as domain ID from the list of domain IDs: %s", domainIdList.trim(), domainIdList), nfe); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |         return false; | ||||||
|  |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -6705,31 +6705,18 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati | |||||||
| 
 | 
 | ||||||
|         final List<NetworkOfferingJoinVO> offerings = networkOfferingJoinDao.search(sc, searchFilter); |         final List<NetworkOfferingJoinVO> offerings = networkOfferingJoinDao.search(sc, searchFilter); | ||||||
|         // Remove offerings that are not associated with caller's domain or domainId passed |         // Remove offerings that are not associated with caller's domain or domainId passed | ||||||
|         if ((caller.getType() != Account.Type.ADMIN || domainId != null) && CollectionUtils.isNotEmpty(offerings)) { |         if ((!Account.Type.ADMIN.equals(caller.getType()) || domainId != null) && CollectionUtils.isNotEmpty(offerings)) { | ||||||
|             ListIterator<NetworkOfferingJoinVO> it = offerings.listIterator(); |             ListIterator<NetworkOfferingJoinVO> it = offerings.listIterator(); | ||||||
|             while (it.hasNext()) { |             while (it.hasNext()) { | ||||||
|                 NetworkOfferingJoinVO offering = it.next(); |                 NetworkOfferingJoinVO offering = it.next(); | ||||||
|                 if (StringUtils.isNotEmpty(offering.getDomainId())) { |                 if (StringUtils.isEmpty(offering.getDomainId())) { | ||||||
|                     boolean toRemove = false; |                     continue; | ||||||
|                     String[] domainIdsArray = offering.getDomainId().split(","); |  | ||||||
|                     for (String domainIdString : domainIdsArray) { |  | ||||||
|                         Long dId = Long.valueOf(domainIdString.trim()); |  | ||||||
|                         if (caller.getType() != Account.Type.ADMIN && |  | ||||||
|                                 !_domainDao.isChildDomain(dId, caller.getDomainId())) { |  | ||||||
|                             toRemove = true; |  | ||||||
|                             break; |  | ||||||
|                 } |                 } | ||||||
|                         if (domainId != null && !_domainDao.isChildDomain(dId, domainId)) { |                 if (!_domainDao.domainIdListContainsAccessibleDomain(offering.getDomainId(), caller, domainId)) { | ||||||
|                             toRemove = true; |  | ||||||
|                             break; |  | ||||||
|                         } |  | ||||||
|                     } |  | ||||||
|                     if (toRemove) { |  | ||||||
|                     it.remove(); |                     it.remove(); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         } |  | ||||||
|         final Boolean sourceNatSupported = cmd.getSourceNatSupported(); |         final Boolean sourceNatSupported = cmd.getSourceNatSupported(); | ||||||
|         final List<String> pNtwkTags = new ArrayList<String>(); |         final List<String> pNtwkTags = new ArrayList<String>(); | ||||||
|         boolean checkForTags = false; |         boolean checkForTags = false; | ||||||
|  | |||||||
| @ -81,6 +81,7 @@ import com.cloud.dc.VlanVO; | |||||||
| import com.cloud.dc.dao.DataCenterDao; | import com.cloud.dc.dao.DataCenterDao; | ||||||
| import com.cloud.dc.dao.VlanDao; | import com.cloud.dc.dao.VlanDao; | ||||||
| import com.cloud.deploy.DeployDestination; | import com.cloud.deploy.DeployDestination; | ||||||
|  | import com.cloud.domain.Domain; | ||||||
| import com.cloud.domain.dao.DomainDao; | import com.cloud.domain.dao.DomainDao; | ||||||
| import com.cloud.event.ActionEvent; | import com.cloud.event.ActionEvent; | ||||||
| import com.cloud.event.EventTypes; | import com.cloud.event.EventTypes; | ||||||
| @ -703,6 +704,19 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis | |||||||
|         return serviceProviderMap; |         return serviceProviderMap; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private void verifyDomainId(Long domainId, Account caller) { | ||||||
|  |         if (domainId == null) { | ||||||
|  |             return; | ||||||
|  |         } | ||||||
|  |         Domain domain = _entityMgr.findById(Domain.class, domainId); | ||||||
|  |         if (domain == null) { | ||||||
|  |             throw new InvalidParameterValueException("Unable to find the domain by id=" + domainId); | ||||||
|  |         } | ||||||
|  |         if (!domainDao.isChildDomain(caller.getDomainId(), domainId)) { | ||||||
|  |             throw new InvalidParameterValueException(String.format("Unable to list VPC offerings for domain: %s as caller does not have access for it", domain.getUuid())); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Override |     @Override | ||||||
|     public Pair<List<? extends VpcOffering>, Integer> listVpcOfferings(ListVPCOfferingsCmd cmd) { |     public Pair<List<? extends VpcOffering>, Integer> listVpcOfferings(ListVPCOfferingsCmd cmd) { | ||||||
|         Account caller = CallContext.current().getCallingAccount(); |         Account caller = CallContext.current().getCallingAccount(); | ||||||
| @ -715,11 +729,14 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis | |||||||
|         final String state = cmd.getState(); |         final String state = cmd.getState(); | ||||||
|         final Long startIndex = cmd.getStartIndex(); |         final Long startIndex = cmd.getStartIndex(); | ||||||
|         final Long pageSizeVal = cmd.getPageSizeVal(); |         final Long pageSizeVal = cmd.getPageSizeVal(); | ||||||
|  |         final Long domainId = cmd.getDomainId(); | ||||||
|         final Long zoneId = cmd.getZoneId(); |         final Long zoneId = cmd.getZoneId(); | ||||||
|         final Filter searchFilter = new Filter(VpcOfferingJoinVO.class, "sortKey", QueryService.SortKeyAscending.value(), null, null); |         final Filter searchFilter = new Filter(VpcOfferingJoinVO.class, "sortKey", QueryService.SortKeyAscending.value(), null, null); | ||||||
|         searchFilter.addOrderBy(VpcOfferingJoinVO.class, "id", true); |         searchFilter.addOrderBy(VpcOfferingJoinVO.class, "id", true); | ||||||
|         final SearchCriteria<VpcOfferingJoinVO> sc = vpcOfferingJoinDao.createSearchCriteria(); |         final SearchCriteria<VpcOfferingJoinVO> sc = vpcOfferingJoinDao.createSearchCriteria(); | ||||||
| 
 | 
 | ||||||
|  |         verifyDomainId(domainId, caller); | ||||||
|  | 
 | ||||||
|         if (keyword != null) { |         if (keyword != null) { | ||||||
|             final SearchCriteria<VpcOfferingJoinVO> ssc = vpcOfferingJoinDao.createSearchCriteria(); |             final SearchCriteria<VpcOfferingJoinVO> ssc = vpcOfferingJoinDao.createSearchCriteria(); | ||||||
|             ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%"); |             ssc.addOr("displayText", SearchCriteria.Op.LIKE, "%" + keyword + "%"); | ||||||
| @ -760,28 +777,19 @@ public class VpcManagerImpl extends ManagerBase implements VpcManager, VpcProvis | |||||||
| 
 | 
 | ||||||
|         final List<VpcOfferingJoinVO> offerings = vpcOfferingJoinDao.search(sc, searchFilter); |         final List<VpcOfferingJoinVO> offerings = vpcOfferingJoinDao.search(sc, searchFilter); | ||||||
| 
 | 
 | ||||||
|         // Remove offerings that are not associated with caller's domain |         // Remove offerings that are not associated with caller's domain or domainId passed | ||||||
|         // TODO: Better approach |         if ((!Account.Type.ADMIN.equals(caller.getType()) || domainId != null) && CollectionUtils.isNotEmpty(offerings)) { | ||||||
|         if (caller.getType() != Account.Type.ADMIN && CollectionUtils.isNotEmpty(offerings)) { |  | ||||||
|             ListIterator<VpcOfferingJoinVO> it = offerings.listIterator(); |             ListIterator<VpcOfferingJoinVO> it = offerings.listIterator(); | ||||||
|             while (it.hasNext()) { |             while (it.hasNext()) { | ||||||
|                 VpcOfferingJoinVO offering = it.next(); |                 VpcOfferingJoinVO offering = it.next(); | ||||||
|                 if(org.apache.commons.lang3.StringUtils.isNotEmpty(offering.getDomainId())) { |                 if (org.apache.commons.lang3.StringUtils.isEmpty(offering.getDomainId())) { | ||||||
|                     boolean toRemove = true; |                     continue; | ||||||
|                     String[] domainIdsArray = offering.getDomainId().split(","); |  | ||||||
|                     for (String domainIdString : domainIdsArray) { |  | ||||||
|                         Long dId = Long.valueOf(domainIdString.trim()); |  | ||||||
|                         if (domainDao.isChildDomain(dId, caller.getDomainId())) { |  | ||||||
|                             toRemove = false; |  | ||||||
|                             break; |  | ||||||
|                 } |                 } | ||||||
|                     } |                 if (!domainDao.domainIdListContainsAccessibleDomain(offering.getDomainId(), caller, domainId)) { | ||||||
|                     if (toRemove) { |  | ||||||
|                     it.remove(); |                     it.remove(); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         } |  | ||||||
|         // filter by supported services |         // filter by supported services | ||||||
|         final boolean listBySupportedServices = supportedServicesStr != null && !supportedServicesStr.isEmpty() && !offerings.isEmpty(); |         final boolean listBySupportedServices = supportedServicesStr != null && !supportedServicesStr.isEmpty() && !offerings.isEmpty(); | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user