Beispiel #1
0
  @Test
  public void testIsNotChefEnabled() {
    VirtualMachine nochef = eg().createUniqueInstance();
    assertFalse(nochef.isChefEnabled());

    Enterprise enterprise = enterpriseGenerator.createChefInstance();
    VirtualMachineTemplate image = virtualImageGenerator.createInstance(enterprise);
    image.setChefEnabled(true);

    VirtualMachine chef =
        ((VirtualMachineGenerator) eg()).createInstance(image, enterprise, "Test");
    assertTrue(chef.isChefEnabled());
  }
  /**
   * 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;
  }
  public List<IpPoolManagement> findIpsByVirtualMachine(final VirtualMachine virtualMachine) {
    Query query = getSession().createQuery(BY_VIRTUAL_MACHINE);
    query.setParameter("vm_id", virtualMachine.getId());

    List<IpPoolManagement> ips = query.list();
    return ips;
  }
  /**
   * Returns a list of virtual machines builded from a virtual system list
   *
   * @param vsc VirtualSystemCollectionDto list to transform
   * @return list of virtual machines builded from a virtual system list
   */
  private List<VirtualMachine> transportVSCollectionToVMs(final VirtualSystemCollectionDto vsc) {
    long MEGABYTE = 1024L * 1024L;

    List<VirtualMachine> vms = new ArrayList<VirtualMachine>();

    for (VirtualSystemDto vs : vsc.getVirtualSystems()) {
      VirtualMachine vm =
          new VirtualMachine(
              vs.getName(),
              null,
              null,
              null,
              null,
              UUID.fromString(vs.getUuid()),
              VirtualMachine.NOT_MANAGED);

      vm.setCpu(new Long(vs.getCpu()).intValue());
      vm.setRam(new Long(vs.getRam() / MEGABYTE).intValue());
      vm.setVdrpPort(new Long(vs.getVport()).intValue());
      vm.setState(VirtualMachineState.valueOf(vs.getStatus().value()));
      for (ResourceType rt : vs.getResources()) {
        if (rt.getResourceType().equals(ResourceEnumType.STORAGE_DISK)) {
          long bytesHD = rt.getUnits();
          vm.setHdInBytes(bytesHD);

          if (StringUtils.hasText(rt.getAddress()) && StringUtils.hasText(rt.getConnection())) {
            Datastore ds = new Datastore();
            ds.setDirectory(rt.getAddress());
            ds.setRootPath(rt.getConnection());
            ds.setName(rt.getElementName());
            vm.setDatastore(ds);
          }

          if (rt.getResourceSubType() != null) {

            VirtualImage vi = new VirtualImage(null);
            VirtualDiskEnumType diskFormatType =
                VirtualDiskEnumType.fromValue(rt.getResourceSubType().toString());
            if (diskFormatType.equals(VirtualDiskEnumType.STATEFUL)) {
              vi.setStateful(1);
            }
            vi.setDiskFormatType(DiskFormatType.fromURI(diskFormatType.value()));
            vm.setVirtualImage(vi);
          }
        }
      }

      vms.add(vm);
    }

    return vms;
  }
Beispiel #5
0
  @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);
  }