@Test
  public void testAddDisk() throws Exception {
    setUriInfo(setUpBasicUriExpectations());
    setUpHttpHeaderExpectations("Expect", "201-created");
    setUpEntityQueryExpectations(
        VdcQueryType.GetAllDisksByVmId,
        GetAllDisksByVmIdParameters.class,
        new String[] {"VmId"},
        new Object[] {PARENT_ID},
        asList(getEntity(0)));
    setUpCreationExpectations(
        VdcActionType.AddDiskToVm,
        AddDiskToVmParameters.class,
        new String[] {"VmId"},
        new Object[] {PARENT_ID},
        true,
        true,
        GUIDS[2], // VM snapshot ID
        asList(GUIDS[3]),
        asList(new AsyncTaskStatus(AsyncTaskStatusEnum.finished)),
        VdcQueryType.GetAllDisksByVmId,
        GetAllDisksByVmIdParameters.class,
        new String[] {"VmId"},
        new Object[] {PARENT_ID},
        asList(getEntity(0)));
    Disk model = getModel(0);
    model.setSize(1024 * 1024L);

    Response response = collection.add(model);
    assertEquals(201, response.getStatus());
    assertTrue(response.getEntity() instanceof Disk);
    verifyModel((Disk) response.getEntity(), 0);
    assertNull(((Disk) response.getEntity()).getCreationStatus());
  }
  private void doTestAddAsync(AsyncTaskStatusEnum asyncStatus, CreationStatus creationStatus)
      throws Exception {
    setUriInfo(setUpBasicUriExpectations());
    setUpCreationExpectations(
        VdcActionType.AddDiskToVm,
        AddDiskToVmParameters.class,
        new String[] {"VmId"},
        new Object[] {PARENT_ID},
        true,
        true,
        GUIDS[2], // VM snapshot ID
        asList(GUIDS[3]),
        asList(new AsyncTaskStatus(asyncStatus)),
        VdcQueryType.GetAllDisksByVmId,
        GetAllDisksByVmIdParameters.class,
        new String[] {"VmId"},
        new Object[] {PARENT_ID},
        asList(getEntity(0)));
    Disk model = getModel(0);
    model.setSize(1024 * 1024L);

    Response response = collection.add(model);
    assertEquals(202, response.getStatus());
    assertTrue(response.getEntity() instanceof Disk);
    verifyModel((Disk) response.getEntity(), 0);
    Disk created = (Disk) response.getEntity();
    assertNotNull(created.getCreationStatus());
    assertEquals(creationStatus.value(), created.getCreationStatus().getState());
  }
Beispiel #3
0
 @Override
 protected Disk postPopulate(Disk model) {
   model.setFormat(MappingTestHelper.shuffle(DiskFormat.class).value());
   model.setInterface(MappingTestHelper.shuffle(DiskInterface.class).value());
   model.setSgio(MappingTestHelper.shuffle(ScsiGenericIO.class).value());
   model.setStatus(StatusUtils.create(MappingTestHelper.shuffle(DiskStatus.class)));
   model.setLunStorage(null);
   return model;
 }
 @Test
 public void testAddIncompleteParameters() throws Exception {
   Disk model = new Disk();
   model.setName(NAMES[0]);
   setUriInfo(setUpBasicUriExpectations());
   control.replay();
   try {
     collection.add(model);
     fail("expected WebApplicationException on incomplete parameters");
   } catch (WebApplicationException wae) {
     verifyIncompleteException(wae, "Disk", "add", "size");
   }
 }
  @Override
  public Response add(Disk disk) {
    validateEnums(Disk.class, disk);

    if (disk.isSetId()) {
      return Response.fromResponse(attachDiskToVm(disk))
          .entity(map(lookupEntity(asGuid(disk.getId()))))
          .build();
    } else {
      validateDiskForCreation(disk);
      return performCreate(
          addAction, getAddParameters(map(disk), disk), getEntityIdResolver(disk.getName()));
    }
  }
 Disk addStatistics(
     Disk model,
     org.ovirt.engine.core.common.businessentities.Disk entity,
     UriInfo ui,
     HttpHeaders httpHeaders) {
   if (DetailHelper.include(httpHeaders, "statistics")) {
     model.setStatistics(new Statistics());
     DiskStatisticalQuery query = new DiskStatisticalQuery(newModel(model.getId()));
     List<Statistic> statistics = query.getStatistics(entity);
     for (Statistic statistic : statistics) {
       LinkHelper.addLinks(ui, statistic, query.getParentType());
     }
     model.getStatistics().getStatistics().addAll(statistics);
   }
   return model;
 }
Beispiel #7
0
  @Test
  public void testReadOnlyMapping() {
    Disk model = new Disk();
    model.setReadOnly(true);

    org.ovirt.engine.core.common.businessentities.Disk entity = DiskMapper.map(model, null);
    assertTrue(entity.getReadOnly());

    model.setReadOnly(false);
    entity = DiskMapper.map(model, null);
    assertFalse(entity.getReadOnly());

    model.setReadOnly(null);
    entity = DiskMapper.map(model, null);
    assertNull(entity.getReadOnly());
  }
Beispiel #8
0
 @Test
 public void testSizeMapping() throws Exception {
   Disk model = new Disk();
   // only <size>
   model.setSize((long) 576576);
   org.ovirt.engine.core.common.businessentities.Disk entity = DiskMapper.map(model, null);
   assertEquals(entity.getSize(), 576576);
   // <size> and <provisioned_size> - the latter should be dominant
   model.setProvisionedSize((long) 888888);
   entity = DiskMapper.map(model, null);
   assertEquals(entity.getSize(), 888888);
   // only <provisioned_size>
   model.setSize(null);
   entity = DiskMapper.map(model, null);
   assertEquals(entity.getSize(), 888888);
 }
  private Response attachDiskToVm(Disk disk) {
    boolean isDiskActive = BooleanUtils.toBooleanDefaultIfNull(disk.isActive(), false);
    boolean isDiskReadOnly = BooleanUtils.toBooleanDefaultIfNull(disk.isReadOnly(), false);
    AttachDetachVmDiskParameters params =
        new AttachDetachVmDiskParameters(
            parentId,
            Guid.createGuidFromStringDefaultEmpty(disk.getId()),
            isDiskActive,
            isDiskReadOnly);

    if (disk.isSetSnapshot()) {
      validateParameters(disk, "snapshot.id");
      params.setSnapshotId(asGuid(disk.getSnapshot().getId()));
    }

    return performAction(VdcActionType.AttachDiskToVm, params);
  }
  private void doTestBadAddDisk(boolean canDo, boolean success, String detail) throws Exception {
    setUriInfo(
        setUpActionExpectations(
            VdcActionType.AddDiskToVm,
            AddDiskToVmParameters.class,
            new String[] {"VmId"},
            new Object[] {PARENT_ID},
            canDo,
            success));
    Disk model = getModel(0);
    model.setSize(1024 * 1024L);

    try {
      collection.add(model);
      fail("expected WebApplicationException");
    } catch (WebApplicationException wae) {
      verifyFault(wae, detail);
    }
  }
  @Mapping(
      from = org.ovirt.engine.core.common.businessentities.storage.Disk.class,
      to = DiskSnapshot.class)
  public static DiskSnapshot map(
      org.ovirt.engine.core.common.businessentities.storage.Disk entity, DiskSnapshot template) {
    if (template == null) {
      template = new DiskSnapshot();
    }
    DiskSnapshot model = (DiskSnapshot) DiskMapper.map(entity, template);

    Disk disk = new Disk();
    disk.setId(entity.getId().toString());
    model.setDisk(disk);

    DiskImage diskImage = (DiskImage) entity;
    model.setId(diskImage.getImageId().toString());
    model.setImageId(null);

    return model;
  }
 protected void validateDiskForCreation(Disk disk) {
   validateParameters(disk, 3, "format", "interface");
   if (DiskResourceUtils.isLunDisk(disk)) {
     validateParameters(
         disk.getLunStorage(), 3, "type"); // when creating a LUN disk, user must specify type.
     StorageType storageType = StorageType.fromValue(disk.getLunStorage().getType());
     if (storageType != null && storageType == StorageType.ISCSI) {
       validateParameters(
           disk.getLunStorage().getLogicalUnits().get(0), 3, "address", "target", "port", "id");
     }
   } else if (disk.isSetLunStorage() && disk.getLunStorage().getLogicalUnits().isEmpty()) {
     // TODO: Implement nested entity existence validation infra for validateParameters()
     throw new WebFaultException(
         null,
         localize(Messages.INCOMPLETE_PARAMS_REASON),
         localize(Messages.INCOMPLETE_PARAMS_DETAIL_TEMPLATE, "LogicalUnit", "", "add"),
         Response.Status.BAD_REQUEST);
   } else {
     validateParameters(disk, 3, "provisionedSize|size"); // Non lun disks require size
   }
   validateEnums(Disk.class, disk);
 }
  @Override
  protected Disk addLinks(Disk model, String... subCollectionMembersToExclude) {
    Snapshot snapshotInfo = model.getSnapshot();
    model.setSnapshot(null);
    super.addLinks(model, subCollectionMembersToExclude);
    if (snapshotInfo != null) {
      org.ovirt.engine.core.common.businessentities.Snapshot snapshot =
          getEntity(
              org.ovirt.engine.core.common.businessentities.Snapshot.class,
              VdcQueryType.GetSnapshotBySnapshotId,
              new IdQueryParameters(asGuid(snapshotInfo.getId())),
              snapshotInfo.getId());
      VM vm = new VM();
      vm.setId(snapshot.getVmId().toString());
      snapshotInfo.setVm(vm);
      model.setSnapshot(snapshotInfo);
      LinkHelper.addLinks(getUriInfo(), snapshotInfo, null, false);
      model.setSnapshot(snapshotInfo);
    }

    return model;
  }
 @Override
 protected VdcActionParametersBase getAddParameters(
     org.ovirt.engine.core.common.businessentities.Disk entity, Disk disk) {
   AddDiskParameters parameters = new AddDiskParameters(parentId, entity);
   if (disk.isSetStorageDomains()
       && disk.getStorageDomains().getStorageDomains().get(0).isSetId()) {
     parameters.setStorageDomainId(
         asGuid(disk.getStorageDomains().getStorageDomains().get(0).getId()));
   } else if (disk.isSetStorageDomains()
       && disk.getStorageDomains().getStorageDomains().get(0).isSetName()) {
     Guid storageDomainId =
         getStorageDomainId(disk.getStorageDomains().getStorageDomains().get(0).getName());
     if (storageDomainId == null) {
       notFound(StorageDomain.class);
     } else {
       parameters.setStorageDomainId(storageDomainId);
     }
   }
   if (disk.isSetActive()) {
     parameters.setPlugDiskToVm(disk.isActive());
   }
   return parameters;
 }
Beispiel #15
0
 @Override
 protected void verify(Disk model, Disk transform) {
   assertNotNull(transform);
   assertEquals(model.getId(), transform.getId());
   assertEquals(model.getImageId(), transform.getImageId());
   assertEquals(model.getFormat(), transform.getFormat());
   assertEquals(model.getInterface(), transform.getInterface());
   assertEquals(model.isActive(), transform.isActive());
   assertEquals(model.isReadOnly(), transform.isReadOnly());
   assertEquals(model.getDescription(), transform.getDescription());
   assertEquals(model.getLogicalName(), transform.getLogicalName());
   assertNotNull(model.getSnapshot());
   assertEquals(model.getSnapshot().getId(), transform.getSnapshot().getId());
   assertEquals(
       "unexpected status", model.getStatus().getState(), transform.getStatus().getState());
   assertEquals("unexpected sparse", model.isSparse(), transform.isSparse());
   assertEquals("unexpected bootable", model.isBootable(), transform.isBootable());
   assertEquals(
       "unexpected propagate errors", model.isPropagateErrors(), transform.isPropagateErrors());
   assertEquals(
       "unexpected wipe after delete", model.isWipeAfterDelete(), transform.isWipeAfterDelete());
   assertEquals("unexpected shareable", model.isShareable(), transform.isShareable());
 }