|
54 | 54 | import javax.xml.parsers.DocumentBuilder; |
55 | 55 | import javax.xml.parsers.ParserConfigurationException; |
56 | 56 |
|
57 | | -import com.cloud.resourcelimit.ReservationHelper; |
58 | 57 | import org.apache.cloudstack.acl.ControlledEntity; |
59 | 58 | import org.apache.cloudstack.acl.ControlledEntity.ACLType; |
60 | 59 | import org.apache.cloudstack.acl.SecurityChecker.AccessType; |
|
135 | 134 | import org.apache.cloudstack.userdata.UserDataManager; |
136 | 135 | import org.apache.cloudstack.utils.bytescale.ByteScaleUtils; |
137 | 136 | import org.apache.cloudstack.utils.security.ParserUtils; |
138 | | -import org.apache.cloudstack.vm.schedule.VMScheduleManager; |
139 | 137 | import org.apache.cloudstack.vm.UnmanagedVMsManager; |
| 138 | +import org.apache.cloudstack.vm.schedule.VMScheduleManager; |
140 | 139 | import org.apache.commons.collections.CollectionUtils; |
141 | 140 | import org.apache.commons.collections.MapUtils; |
142 | 141 | import org.apache.commons.lang.math.NumberUtils; |
|
297 | 296 | import com.cloud.resource.ResourceManager; |
298 | 297 | import com.cloud.resource.ResourceState; |
299 | 298 | import com.cloud.resourcelimit.CheckedReservation; |
| 299 | +import com.cloud.resourcelimit.ReservationHelper; |
300 | 300 | import com.cloud.server.ManagementService; |
301 | 301 | import com.cloud.server.ResourceTag; |
302 | 302 | import com.cloud.server.StatsCollector; |
@@ -1285,46 +1285,45 @@ private void validateOfferingMaxResource(ServiceOfferingVO offering) { |
1285 | 1285 |
|
1286 | 1286 | @Override |
1287 | 1287 | public void validateCustomParameters(ServiceOfferingVO serviceOffering, Map<String, String> customParameters) { |
1288 | | - //TODO need to validate custom cpu, and memory against min/max CPU/Memory ranges from service_offering_details table |
1289 | | - if (customParameters.size() != 0) { |
1290 | | - Map<String, String> offeringDetails = serviceOfferingDetailsDao.listDetailsKeyPairs(serviceOffering.getId()); |
1291 | | - if (serviceOffering.getCpu() == null) { |
1292 | | - int minCPU = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MIN_CPU_NUMBER), 1); |
1293 | | - int maxCPU = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MAX_CPU_NUMBER), Integer.MAX_VALUE); |
1294 | | - int cpuNumber = NumbersUtil.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuNumber.name()), -1); |
1295 | | - Integer maxCPUCores = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES.value() == 0 ? Integer.MAX_VALUE: ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES.value(); |
1296 | | - if (cpuNumber < minCPU || cpuNumber > maxCPU || cpuNumber > maxCPUCores) { |
1297 | | - throw new InvalidParameterValueException(String.format("Invalid CPU cores value, specify a value between %d and %d", minCPU, Math.min(maxCPUCores, maxCPU))); |
1298 | | - } |
1299 | | - } else if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuNumber.name())) { |
1300 | | - throw new InvalidParameterValueException("The CPU cores of this offering id:" + serviceOffering.getUuid() |
1301 | | - + " is not customizable. This is predefined in the Template."); |
1302 | | - } |
1303 | | - |
1304 | | - if (serviceOffering.getSpeed() == null) { |
1305 | | - String cpuSpeed = customParameters.get(UsageEventVO.DynamicParameters.cpuSpeed.name()); |
1306 | | - if ((cpuSpeed == null) || (NumbersUtil.parseInt(cpuSpeed, -1) <= 0)) { |
1307 | | - throw new InvalidParameterValueException("Invalid CPU speed value, specify a value between 1 and " + Integer.MAX_VALUE); |
1308 | | - } |
1309 | | - } else if (!serviceOffering.isCustomCpuSpeedSupported() && customParameters.containsKey(UsageEventVO.DynamicParameters.cpuSpeed.name())) { |
1310 | | - throw new InvalidParameterValueException("The CPU speed of this offering id:" + serviceOffering.getUuid() |
1311 | | - + " is not customizable. This is predefined in the Template."); |
1312 | | - } |
1313 | | - |
1314 | | - if (serviceOffering.getRamSize() == null) { |
1315 | | - int minMemory = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MIN_MEMORY), 32); |
1316 | | - int maxMemory = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MAX_MEMORY), Integer.MAX_VALUE); |
1317 | | - int memory = NumbersUtil.parseInt(customParameters.get(UsageEventVO.DynamicParameters.memory.name()), -1); |
1318 | | - Integer maxRAMSize = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE.value() == 0 ? Integer.MAX_VALUE: ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE.value(); |
1319 | | - if (memory < minMemory || memory > maxMemory || memory > maxRAMSize) { |
1320 | | - throw new InvalidParameterValueException(String.format("Invalid memory value, specify a value between %d and %d", minMemory, Math.min(maxRAMSize, maxMemory))); |
1321 | | - } |
1322 | | - } else if (customParameters.containsKey(UsageEventVO.DynamicParameters.memory.name())) { |
1323 | | - throw new InvalidParameterValueException("The memory of this offering id:" + serviceOffering.getUuid() + " is not customizable. This is predefined in the Template."); |
1324 | | - } |
1325 | | - } else { |
| 1288 | + if (MapUtils.isEmpty(customParameters) && serviceOffering.isDynamic()) { |
1326 | 1289 | throw new InvalidParameterValueException("Need to specify custom parameter values cpu, cpu speed and memory when using custom offering"); |
1327 | 1290 | } |
| 1291 | + Map<String, String> offeringDetails = serviceOfferingDetailsDao.listDetailsKeyPairs(serviceOffering.getId()); |
| 1292 | + if (serviceOffering.getCpu() == null) { |
| 1293 | + int minCPU = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MIN_CPU_NUMBER), 1); |
| 1294 | + int maxCPU = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MAX_CPU_NUMBER), Integer.MAX_VALUE); |
| 1295 | + int cpuNumber = NumbersUtil.parseInt(customParameters.get(UsageEventVO.DynamicParameters.cpuNumber.name()), -1); |
| 1296 | + int maxCPUCores = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES.value() == 0 ? Integer.MAX_VALUE: ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_CPU_CORES.value(); |
| 1297 | + if (cpuNumber < minCPU || cpuNumber > maxCPU || cpuNumber > maxCPUCores) { |
| 1298 | + throw new InvalidParameterValueException(String.format("Invalid CPU cores value, specify a value between %d and %d", minCPU, Math.min(maxCPUCores, maxCPU))); |
| 1299 | + } |
| 1300 | + } else if (customParameters.containsKey(UsageEventVO.DynamicParameters.cpuNumber.name())) { |
| 1301 | + throw new InvalidParameterValueException("The CPU cores of this offering id:" + serviceOffering.getUuid() |
| 1302 | + + " is not customizable. This is predefined in the Template."); |
| 1303 | + } |
| 1304 | + |
| 1305 | + if (serviceOffering.getSpeed() == null) { |
| 1306 | + String cpuSpeed = customParameters.get(UsageEventVO.DynamicParameters.cpuSpeed.name()); |
| 1307 | + if ((cpuSpeed == null) || (NumbersUtil.parseInt(cpuSpeed, -1) <= 0)) { |
| 1308 | + throw new InvalidParameterValueException("Invalid CPU speed value, specify a value between 1 and " + Integer.MAX_VALUE); |
| 1309 | + } |
| 1310 | + } else if (!serviceOffering.isCustomCpuSpeedSupported() && customParameters.containsKey(UsageEventVO.DynamicParameters.cpuSpeed.name())) { |
| 1311 | + throw new InvalidParameterValueException(String.format("The CPU speed of this offering id:%s" |
| 1312 | + + " is not customizable. This is predefined as %d MHz.", |
| 1313 | + serviceOffering.getUuid(), serviceOffering.getSpeed())); |
| 1314 | + } |
| 1315 | + |
| 1316 | + if (serviceOffering.getRamSize() == null) { |
| 1317 | + int minMemory = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MIN_MEMORY), 32); |
| 1318 | + int maxMemory = NumbersUtil.parseInt(offeringDetails.get(ApiConstants.MAX_MEMORY), Integer.MAX_VALUE); |
| 1319 | + int memory = NumbersUtil.parseInt(customParameters.get(UsageEventVO.DynamicParameters.memory.name()), -1); |
| 1320 | + int maxRAMSize = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE.value() == 0 ? Integer.MAX_VALUE: ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE.value(); |
| 1321 | + if (memory < minMemory || memory > maxMemory || memory > maxRAMSize) { |
| 1322 | + throw new InvalidParameterValueException(String.format("Invalid memory value, specify a value between %d and %d", minMemory, Math.min(maxRAMSize, maxMemory))); |
| 1323 | + } |
| 1324 | + } else if (customParameters.containsKey(UsageEventVO.DynamicParameters.memory.name())) { |
| 1325 | + throw new InvalidParameterValueException("The memory of this offering id:" + serviceOffering.getUuid() + " is not customizable. This is predefined in the Template."); |
| 1326 | + } |
1328 | 1327 | } |
1329 | 1328 |
|
1330 | 1329 | private UserVm upgradeStoppedVirtualMachine(Long vmId, Long svcOffId, Map<String, String> customParameters) throws ResourceAllocationException { |
@@ -2785,10 +2784,16 @@ protected void verifyVmLimits(UserVmVO vmInstance, Map<String, String> details) |
2785 | 2784 | Map<String, String> customParameters = new HashMap<>(); |
2786 | 2785 | customParameters.put(VmDetailConstants.CPU_NUMBER, String.valueOf(newCpu)); |
2787 | 2786 | customParameters.put(VmDetailConstants.MEMORY, String.valueOf(newMemory)); |
2788 | | - if (svcOffering.isCustomCpuSpeedSupported()) { |
| 2787 | + if (details.containsKey(VmDetailConstants.CPU_SPEED)) { |
2789 | 2788 | customParameters.put(VmDetailConstants.CPU_SPEED, details.get(VmDetailConstants.CPU_SPEED)); |
2790 | 2789 | } |
2791 | 2790 | validateCustomParameters(svcOffering, customParameters); |
| 2791 | + } else { |
| 2792 | + if (details.containsKey(VmDetailConstants.CPU_NUMBER) || details.containsKey(VmDetailConstants.MEMORY) || |
| 2793 | + details.containsKey(VmDetailConstants.CPU_SPEED)) { |
| 2794 | + throw new InvalidParameterValueException("CPU number, Memory and CPU speed cannot be updated for a " + |
| 2795 | + "non-dynamic offering"); |
| 2796 | + } |
2792 | 2797 | } |
2793 | 2798 | if (VirtualMachineManager.ResourceCountRunningVMsonly.value()) { |
2794 | 2799 | return; |
|
0 commit comments