Prevent NPE if guest OS mapping is missing while prioritizing hosts (#2577)

This commit is contained in:
Khosrow Moossavi 2018-04-20 09:10:38 -04:00 committed by dahn
parent 561630e449
commit 995775d588

View File

@ -29,6 +29,8 @@ import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import com.google.common.base.Strings;
import com.cloud.agent.manager.allocator.HostAllocator;
import com.cloud.capacity.CapacityManager;
import com.cloud.capacity.CapacityVO;
@ -417,6 +419,10 @@ public class FirstFitAllocator extends AdapterBase implements HostAllocator {
// Determine the guest OS category of the template
String templateGuestOSCategory = getTemplateGuestOSCategory(template);
if (Strings.isNullOrEmpty(templateGuestOSCategory)) {
return hosts;
}
List<Host> prioritizedHosts = new ArrayList<Host>();
List<Host> noHvmHosts = new ArrayList<Host>();
@ -537,6 +543,11 @@ public class FirstFitAllocator extends AdapterBase implements HostAllocator {
protected String getTemplateGuestOSCategory(VMTemplateVO template) {
long guestOSId = template.getGuestOSId();
GuestOSVO guestOS = _guestOSDao.findById(guestOSId);
if (guestOS == null) {
return null;
}
long guestOSCategoryId = guestOS.getCategoryId();
GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId);
return guestOSCategory.getName();