/** * Static method that converts the persistence {@link VirtualMachine} object to transfer {@link * VirtualMachineDto} object. It also adds REST self-discover {@link RESTLink}s * * @param vm Input persistence object. * @param restBuilder Context-injected {@link RESTLink} builder to create the links. * @return the corresponding transfer object. * @throws Exception Serialization unhandled exception. */ public static VirtualMachineDto createTransferObject( final Integer datacenterId, final Integer rackId, final Integer machineId, final VirtualMachine vm, final VirtualAppliance vapp, final IRESTBuilder restBuilder) throws Exception { // TODO: Try with enterprise and user != null VirtualMachineDto vmDto = ModelTransformer.transportFromPersistence(VirtualMachineDto.class, vm); if (vm.getIdType() == 0) { vmDto.setIdType(com.abiquo.server.core.cloud.VirtualMachine.NOT_MANAGED); } else { vmDto.setIdType(com.abiquo.server.core.cloud.VirtualMachine.MANAGED); } Integer enterpriseId = null; Integer userId = null; if (vm.getEnterprise() != null) { enterpriseId = vm.getEnterprise().getId(); } if (vm.getUser() != null) { userId = vm.getUser().getId(); } vmDto.setLinks( restBuilder.buildVirtualMachineAdminLinks( datacenterId, rackId, machineId, enterpriseId, userId, vm.getHypervisor().getType(), vapp, vm.getId())); final VirtualMachineTemplate vmtemplate = vm.getVirtualMachineTemplate(); if (vmtemplate != null) { if (vmtemplate.getRepository() != null) { vmDto.addLink( restBuilder.buildVirtualMachineTemplateLink( vmtemplate.getEnterprise().getId(), vmtemplate.getRepository().getDatacenter().getId(), vmtemplate.getId())); } else { // imported virtual machines vmDto.addLink( restBuilder.buildVirtualMachineTemplateLink( vmtemplate.getEnterprise().getId(), vm.getHypervisor().getMachine().getRack().getDatacenter().getId(), vmtemplate.getId())); } } return vmDto; }
@Test public void testDeleteMachineWithVirtualMachinesDeployed() { Hypervisor hypervisor = hypervisorGenerator.createUniqueInstance(); Datacenter datacenter = hypervisor.getMachine().getDatacenter(); VirtualDatacenter vdc = vdcGenerator.createInstance(datacenter); RemoteService rm = remoteServiceGenerator.createInstance(RemoteServiceType.VIRTUAL_SYSTEM_MONITOR, datacenter); VirtualImage image = virtualImageGenerator.createInstance(vdc.getEnterprise()); VirtualAppliance vapp = virtualApplianceGenerator.createInstance(vdc); VirtualMachine vm = vmGenerator.createInstance(image, vdc.getEnterprise(), hypervisor, "vm_test"); vm.setState(VirtualMachineState.RUNNING); vm.setIdType(VirtualMachine.MANAGED); NodeVirtualImage node = new NodeVirtualImage("node_test", vapp, image, vm); hypervisor.getMachine().setHypervisor(hypervisor); List<Object> entitiesToPersist = new ArrayList<Object>(); entitiesToPersist.add(vdc.getEnterprise()); entitiesToPersist.add(datacenter); entitiesToPersist.add(rm); entitiesToPersist.add(hypervisor.getMachine().getRack()); entitiesToPersist.add(hypervisor.getMachine()); entitiesToPersist.add(hypervisor); entitiesToPersist.add(vdc); entitiesToPersist.add(image); entitiesToPersist.add(vapp); for (Privilege p : vm.getUser().getRole().getPrivileges()) { entitiesToPersist.add(p); } entitiesToPersist.add(vm.getUser().getRole()); entitiesToPersist.add(vm.getUser()); entitiesToPersist.add(vm); entitiesToPersist.add(node); setup(entitiesToPersist.toArray()); int machineId = hypervisor.getMachine().getId(); EntityManager em = getEntityManager(); EntityManagerHelper.beginReadWriteTransaction(em); MachineService service = new MachineService(em); service.setVsm(new VsmServiceStubMock()); // Must use the mocked VSM service.removeMachine(machineId); EntityManagerHelper.commit(em); EntityManagerHelper.beginRollbackTransaction(em); service = new MachineService(em); try { Machine m = service.getMachine(machineId); } catch (NotFoundException e) { org.testng.Assert.assertEquals( e.getErrors().iterator().next().getMessage(), "The requested machine does not exist"); } VirtualMachineService vmService = new VirtualMachineService(em); VirtualMachine virtualMachine = vmService.getVirtualMachine(vdc.getId(), vapp.getId(), vm.getId()); org.testng.Assert.assertNull(virtualMachine.getHypervisor()); org.testng.Assert.assertNull(virtualMachine.getDatastore()); org.testng.Assert.assertEquals(virtualMachine.getState(), VirtualMachineState.NOT_DEPLOYED); }