@Override public Response add(Host model) { validateParameters(model, "id"); Label label = BackendAffinityLabelHelper.getLabel(this, labelId); VDS entity = new VDS(); entity.setId(GuidUtils.asGuid(model.getId())); Label updatedLabel = new LabelBuilder(label) .entity(entity) .build(); // The command used to add the host to the label returns the label, but we need to return the virtual machine, // so we ignore the result and return a link to the added host: LabelActionParameters updateParams = new LabelActionParameters(updatedLabel); try { doAction(ActionType.UpdateLabel, updateParams); } catch (BackendFailureException exception) { handleError(exception, false); } Host result = BackendAffinityLabelHelper.makeHostLink(entity.getId()); return Response.ok(Response.Status.CREATED).entity(result).build(); }
@Override public Response add(Vm model) { validateParameters(model, "id"); Label label = BackendAffinityLabelHelper.getLabel(this, labelId); VM entity = new VM(); entity.setId(GuidUtils.asGuid(model.getId())); Label updatedLabel = new LabelBuilder(label) .entity(entity) .build(); // The command used to add the virtual machine to the label returns the label, but we need to return the virtual // machine, so we ignore the result and return a link to the added virtual machine: LabelActionParameters updateParams = new LabelActionParameters(updatedLabel); try { doAction(ActionType.UpdateLabel, updateParams); } catch (BackendFailureException exception) { handleError(exception, false); } Vm result = BackendAffinityLabelHelper.makeVmLink(entity.getId()); return Response.ok(Response.Status.CREATED).entity(result).build(); }
@Override public Response add(AffinityLabel label) { validateParameters(label, "id"); IdQueryParameters parameters = new IdQueryParameters(GuidUtils.asGuid(label.getId())); org.ovirt.engine.core.common.businessentities.Label entity = getEntity( org.ovirt.engine.core.common.businessentities.Label.class, QueryType.GetLabelById, parameters, label.getId(), true); BusinessEntity<Guid> parent = constructor.create(); parent.setId(GuidUtils.asGuid(parentId)); org.ovirt.engine.core.common.businessentities.Label updatedLabel = new LabelBuilder(entity) .entity(parent) .build(); // Add the affinity label using the backend "update" operation. As the backend will return the added label as // the result of the operation, we can fetch it using a simple "identity" resolver, that just returns the same // value it is passed. LabelActionParameters updateParams = new LabelActionParameters(updatedLabel); return performCreate(ActionType.UpdateLabel, updateParams, (IResolver<Label, Label>) result -> result); }
@Test public void testCreateAndGetByIds() { Label label = new LabelBuilder() .name("test label") .id(Guid.newGuid()) .entity(host) .build(); dao.save(label); VmStatic vm = createAndSaveVm(); VmStatic vm2 = createAndSaveVm(); Label label2 = new LabelBuilder() .name("test label 2") .id(Guid.newGuid()) .entity(vm) .build(); dao.save(label2); label = new LabelBuilder() .name("test label not in result") .id(Guid.newGuid()) .entity(vm2) .build(); dao.save(label); List<Label> read = dao.getAllByIds(Lists.newArrayList(label.getId(), label2.getId())); assertNotNull(read); assertEquals(2, read.size()); }
@Test public void testCreateAndGetByReferencedId() { Label label = new LabelBuilder() .name("test label") .id(Guid.newGuid()) .entity(host) .build(); dao.save(label); VmStatic vm = createAndSaveVm(); label = new LabelBuilder() .name("test label 2") .id(Guid.newGuid()) .entity(vm) .build(); dao.save(label); VmStatic vm2 = createAndSaveVm(); label = new LabelBuilder() .name("test label not in result") .id(Guid.newGuid()) .entity(vm2) .build(); dao.save(label); List<Label> read = dao.getAllByEntityIds(Lists.newArrayList(host.getId(), vm.getId(), Guid.newGuid())); assertNotNull(read); assertEquals(2, read.size()); }
@Test public void testCreateAndGetWithHost() { Guid guid = Guid.newGuid(); Label label = new LabelBuilder() .name("test label") .id(guid) .entity(host) .build(); dao.save(label); Label read = dao.get(guid); assertNotNull(read); assertEquals(guid, read.getId()); assertEquals(label.getName(), read.getName()); assertNotNull(read.getVms()); assertEquals(0, read.getVms().size()); assertEquals(1, read.getHosts().size()); assertEquals(host.getId(), read.getHosts().iterator().next()); }