Fix VM import & VM delete with custom offering (#8813)

This commit is contained in:
Vishesh 2024-04-10 14:59:29 +05:30 committed by GitHub
parent c7626ebfd6
commit c24c1a5c00
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 12 additions and 14 deletions

View File

@ -60,6 +60,8 @@ public interface VirtualMachineProfile {
void setConfigDriveLocation(NetworkElement.Location location);
void setServiceOffering(ServiceOffering offering);
public static class Param {
public static final Param VmPassword = new Param("VmPassword");

View File

@ -26,7 +26,6 @@ import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.element.NetworkElement;
import com.cloud.offering.ServiceOffering;
import com.cloud.service.ServiceOfferingVO;
import com.cloud.template.VirtualMachineTemplate;
import com.cloud.template.VirtualMachineTemplate.BootloaderType;
import com.cloud.user.Account;
@ -260,7 +259,8 @@ public class VirtualMachineProfileImpl implements VirtualMachineProfile {
return _params;
}
public void setServiceOffering(ServiceOfferingVO offering) {
@Override
public void setServiceOffering(ServiceOffering offering) {
_offering = offering;
}

View File

@ -896,6 +896,7 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
if (!hostSupportsServiceOffering(sourceHost, serviceOffering)) {
LOGGER.debug(String.format("VM %s needs to be migrated", vm.getUuid()));
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm, template, serviceOffering, owner, null);
profile.setServiceOffering(serviceOfferingDao.findById(vm.getId(), serviceOffering.getId()));
DeploymentPlanner.ExcludeList excludeList = new DeploymentPlanner.ExcludeList();
excludeList.addHost(sourceHost.getId());
final DataCenterDeployment plan = new DataCenterDeployment(sourceHost.getDataCenterId(), sourceHost.getPodId(), sourceHost.getClusterId(), null, null, null);
@ -2131,11 +2132,6 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
UserVm userVm = null;
Map<String, String> allDetails = new HashMap<>(details);
if (serviceOffering.isDynamic()) {
allDetails.put(VmDetailConstants.CPU_NUMBER, String.valueOf(serviceOffering.getCpu()));
allDetails.put(VmDetailConstants.MEMORY, String.valueOf(serviceOffering.getRamSize()));
allDetails.put(VmDetailConstants.CPU_SPEED, String.valueOf(serviceOffering.getSpeed()));
}
// Check disks and supplied disk offerings
List<UnmanagedInstanceTO.Disk> unmanagedInstanceDisks = unmanagedInstance.getDisks();
@ -2189,6 +2185,8 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
}
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(userVm, template, serviceOffering, owner, null);
ServiceOfferingVO dummyOffering = serviceOfferingDao.findById(userVm.getId(), serviceOffering.getId());
profile.setServiceOffering(dummyOffering);
DeploymentPlanner.ExcludeList excludeList = new DeploymentPlanner.ExcludeList();
final DataCenterDeployment plan = new DataCenterDeployment(zone.getId(), null, null, null, null, null);
DeployDestination dest = null;
@ -2240,7 +2238,7 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
cleanupFailedImportVM(userVm);
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to import NICs while importing vm: %s. %s", instanceName, StringUtils.defaultString(e.getMessage())));
}
publishVMUsageUpdateResourceCount(userVm, serviceOffering);
publishVMUsageUpdateResourceCount(userVm, dummyOffering);
return userVm;
}
@ -2252,11 +2250,6 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
UserVm userVm = null;
Map<String, String> allDetails = new HashMap<>(details);
if (serviceOffering.isDynamic()) {
allDetails.put(VmDetailConstants.CPU_NUMBER, String.valueOf(serviceOffering.getCpu()));
allDetails.put(VmDetailConstants.MEMORY, String.valueOf(serviceOffering.getRamSize()));
allDetails.put(VmDetailConstants.CPU_SPEED, String.valueOf(serviceOffering.getSpeed()));
}
VirtualMachine.PowerState powerState = VirtualMachine.PowerState.PowerOff;
@ -2323,6 +2316,8 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
DiskProfile diskProfile = volumeManager.allocateRawVolume(Volume.Type.ROOT, rootVolumeName, diskOffering, null, null, null, userVm, template, owner, null);
final VirtualMachineProfile profile = new VirtualMachineProfileImpl(userVm, template, serviceOffering, owner, null);
ServiceOfferingVO dummyOffering = serviceOfferingDao.findById(userVm.getId(), serviceOffering.getId());
profile.setServiceOffering(dummyOffering);
DeploymentPlanner.ExcludeList excludeList = new DeploymentPlanner.ExcludeList();
final DataCenterDeployment plan = new DataCenterDeployment(zone.getId(), null, null, hostId, poolId, null);
DeployDestination dest = null;
@ -2374,7 +2369,7 @@ public class UnmanagedVMsManagerImpl implements UnmanagedVMsManager {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, String.format("Failed to import volumes while importing vm: %s. %s", instanceName, StringUtils.defaultString(e.getMessage())));
}
networkOrchestrationService.importNic(macAddress,0,network, true, userVm, requestedIpPair, zone, true);
publishVMUsageUpdateResourceCount(userVm, serviceOffering);
publishVMUsageUpdateResourceCount(userVm, dummyOffering);
return userVm;
}

View File

@ -332,6 +332,7 @@ public class UnmanagedVMsManagerImplTest {
when(serviceOffering.getRamSize()).thenReturn(instance.getMemory());
when(serviceOffering.getSpeed()).thenReturn(instance.getCpuSpeed());
when(serviceOfferingDao.findById(Mockito.anyLong())).thenReturn(serviceOffering);
when(serviceOfferingDao.findById(anyLong(), anyLong())).thenReturn(Mockito.mock(ServiceOfferingVO.class));
DiskOfferingVO diskOfferingVO = Mockito.mock(DiskOfferingVO.class);
when(diskOfferingDao.findById(Mockito.anyLong())).thenReturn(diskOfferingVO);
UserVmVO userVm = Mockito.mock(UserVmVO.class);