@@ -6158,10 +6158,6 @@ public String finalizeUserData(String userData, Long userDataId, VirtualMachineT
61586158 }
61596159
61606160 private void verifyServiceOffering(BaseDeployVMCmd cmd, ServiceOffering serviceOffering) {
6161- if (serviceOffering == null) {
6162- throw new InvalidParameterValueException("Unable to find service offering: " + serviceOffering.getId());
6163- }
6164-
61656161 if (ServiceOffering.State.Inactive.equals(serviceOffering.getState())) {
61666162 throw new InvalidParameterValueException(String.format("Service offering is inactive: [%s].", serviceOffering.getUuid()));
61676163 }
@@ -6181,10 +6177,6 @@ private void verifyServiceOffering(BaseDeployVMCmd cmd, ServiceOffering serviceO
61816177 }
61826178
61836179 private void verifyTemplate(BaseDeployVMCmd cmd, VirtualMachineTemplate template, Long serviceOfferingId) {
6184- // Make sure a valid template ID was specified
6185- if (template == null) {
6186- throw new InvalidParameterValueException("Unable to use template " + template.getId());
6187- }
61886180 if (TemplateType.VNF.equals(template.getTemplateType())) {
61896181 vnfTemplateManager.validateVnfApplianceNics(template, cmd.getNetworkIds());
61906182 } else if (cmd instanceof DeployVnfApplianceCmd) {
@@ -6231,6 +6223,9 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
62316223 Long overrideDiskOfferingId = cmd.getOverrideDiskOfferingId();
62326224
62336225 ServiceOffering serviceOffering = _entityMgr.findById(ServiceOffering.class, serviceOfferingId);
6226+ if (serviceOffering == null) {
6227+ throw new InvalidParameterValueException("Unable to find service offering: " + serviceOffering.getId());
6228+ }
62346229 verifyServiceOffering(cmd, serviceOffering);
62356230
62366231 Account caller = CallContext.current().getCallingAccount();
@@ -6269,6 +6264,10 @@ public UserVm createVirtualMachine(DeployVMCmd cmd) throws InsufficientCapacityE
62696264 (!(HypervisorType.KVM.equals(template.getHypervisorType()) || HypervisorType.KVM.equals(cmd.getHypervisor())))) {
62706265 throw new InvalidParameterValueException("Deploying a virtual machine with existing volume/snapshot is supported only from KVM hypervisors");
62716266 }
6267+ // Make sure a valid template ID was specified
6268+ if (template == null) {
6269+ throw new InvalidParameterValueException("Unable to use template " + templateId);
6270+ }
62726271 verifyTemplate(cmd, template, serviceOfferingId);
62736272
62746273 Long diskOfferingId = cmd.getDiskOfferingId();
@@ -9401,6 +9400,15 @@ private void updateDetailsWithRootDiskAttributes(Map<String, String> details, Di
94019400 }
94029401 }
94039402
9403+ private void checkRootDiskSizeAgainstBackup(Long instanceVolumeSize,DiskOffering rootDiskOffering, Long backupVolumeSize) {
9404+ Long instanceRootDiskSize = rootDiskOffering.isCustomized() ? instanceVolumeSize : rootDiskOffering.getDiskSize() / GiB_TO_BYTES;
9405+ if (instanceRootDiskSize < backupVolumeSize) {
9406+ throw new InvalidParameterValueException(
9407+ String.format("Instance volume root disk size %d[GiB] cannot be less than the backed-up volume size %d[GiB].",
9408+ instanceVolumeSize, backupVolumeSize));
9409+ }
9410+ }
9411+
94049412 @Override
94059413 public UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws InsufficientCapacityException, ResourceAllocationException, ResourceUnavailableException {
94069414 //Verify that all objects exist before passing them to the service
@@ -9430,6 +9438,9 @@ public UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws Insufficien
94309438 ServiceOffering serviceOffering;
94319439 if (serviceOfferingId != null) {
94329440 serviceOffering = serviceOfferingDao.findById(serviceOfferingId);
9441+ if (serviceOffering == null) {
9442+ throw new InvalidParameterValueException("Unable to find service offering: " + serviceOffering.getId());
9443+ }
94339444 } else {
94349445 String serviceOfferingUuid = backup.getDetail(ApiConstants.SERVICE_OFFERING_ID);
94359446 if (serviceOfferingUuid == null) {
@@ -9443,12 +9454,20 @@ public UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws Insufficien
94439454 verifyServiceOffering(cmd, serviceOffering);
94449455
94459456 Long templateId;
9457+ VirtualMachineTemplate template;
94469458 if (cmd.getTemplateId() != null) {
94479459 templateId = cmd.getTemplateId();
9460+ template = _templateDao.findById(templateId);
9461+ if (template == null) {
9462+ throw new InvalidParameterValueException("Unable to use template " + templateId);
9463+ }
94489464 } else {
94499465 templateId = backupVm.getTemplateId();
9466+ template = _templateDao.findById(templateId);
9467+ if (template == null) {
9468+ throw new CloudRuntimeException("Unable to find template associated with the backup. Please specify a valid template while creating instance");
9469+ }
94509470 }
9451- VirtualMachineTemplate template = _templateDao.findById(templateId);
94529471 verifyTemplate(cmd, template, serviceOffering.getId());
94539472
94549473 Long size = cmd.getSize();
@@ -9470,38 +9489,28 @@ public UserVm allocateVMFromBackup(CreateVMFromBackupCmd cmd) throws Insufficien
94709489
94719490 Long overrideDiskOfferingId = cmd.getOverrideDiskOfferingId();
94729491
9473- DiskOfferingInfo rootDiskOfferingInfo = backupManager.getRootDiskOfferingInfoFromBackup(backup);
9492+ DiskOfferingInfo rootDiskOfferingInfoFromBackup = backupManager.getRootDiskOfferingInfoFromBackup(backup);
9493+
94749494 if (isIso) {
94759495 if (diskOfferingId == null) {
9476- if (rootDiskOfferingInfo == null) {
9477- throw new CloudRuntimeException("Unable to find root disk offering with the uuid stored in backup. Please specify a valid root disk offering id while creating instance");
9478- }
9479- diskOfferingId = rootDiskOfferingInfo.getDiskOffering().getId();
9480- updateDetailsWithRootDiskAttributes(cmd.getDetails(), rootDiskOfferingInfo);
9481- size = rootDiskOfferingInfo.getSize();
9496+ diskOfferingId = rootDiskOfferingInfoFromBackup.getDiskOffering().getId();
9497+ updateDetailsWithRootDiskAttributes(cmd.getDetails(), rootDiskOfferingInfoFromBackup);
9498+ size = rootDiskOfferingInfoFromBackup.getSize();
94829499 } else {
94839500 DiskOffering rootDiskOffering = _diskOfferingDao.findById(diskOfferingId);
9484- Long rootDiskSize = rootDiskOffering.isCustomized() ? size : rootDiskOffering.getDiskSize() / GiB_TO_BYTES;
9485- if (rootDiskOfferingInfo != null && rootDiskSize < rootDiskOfferingInfo.getSize()) {
9486- throw new InvalidParameterValueException(
9487- String.format("Instance volume size %d[GiB] cannot be less than the backed-up volume size %d[GiB].",
9488- rootDiskSize, rootDiskOfferingInfo.getSize()));
9489- }
9501+ checkRootDiskSizeAgainstBackup(size, rootDiskOffering, rootDiskOfferingInfoFromBackup.getSize());
94909502 }
94919503 } else {
94929504 if (overrideDiskOfferingId == null) {
9493- if (rootDiskOfferingInfo != null && serviceOffering.getDiskOfferingId() != rootDiskOfferingInfo.getDiskOffering().getId()) {
9494- overrideDiskOfferingId = rootDiskOfferingInfo.getDiskOffering().getId();
9495- updateDetailsWithRootDiskAttributes(cmd.getDetails(), rootDiskOfferingInfo);
9496- }
9505+ overrideDiskOfferingId = serviceOffering.getDiskOfferingId();
9506+ updateDetailsWithRootDiskAttributes(cmd.getDetails(), rootDiskOfferingInfoFromBackup);
94979507 } else {
94989508 DiskOffering overrideDiskOffering = _diskOfferingDao.findById(overrideDiskOfferingId);
9499- String diskSizeDetail = cmd.getDetails().get(VmDetailConstants.ROOT_DISK_SIZE);
9500- Long diskSize = diskSizeDetail != null ? Long.parseLong(diskSizeDetail) : overrideDiskOffering.getDiskSize() / GiB_TO_BYTES;
9501- if (rootDiskOfferingInfo != null && diskSize < rootDiskOfferingInfo.getSize()) {
9502- throw new InvalidParameterValueException(
9503- String.format("Instance volume size %d[GiB] cannot be less than the backed-up volume size %d[GiB].",
9504- diskSize, rootDiskOfferingInfo.getSize()));
9509+ if (overrideDiskOffering.isComputeOnly()) {
9510+ updateDetailsWithRootDiskAttributes(cmd.getDetails(), rootDiskOfferingInfoFromBackup);
9511+ } else {
9512+ Long rootDiskSize = Long.parseLong(cmd.getDetails().getOrDefault(VmDetailConstants.ROOT_DISK_SIZE, null));
9513+ checkRootDiskSizeAgainstBackup(rootDiskSize, overrideDiskOffering, rootDiskOfferingInfoFromBackup.getSize());
95059514 }
95069515 }
95079516 }
0 commit comments