public Vm get() { // First we need to check if the affinity label does contain the virtual machine: Label label = BackendAffinityLabelHelper.getLabel(this, labelId); if (!label.getVms().contains(guid)) { throw new WebApplicationException(Response.Status.NOT_FOUND); } // Then we return a link to the virtual machine: return BackendAffinityLabelHelper.makeVmLink(guid); }
@Override public Vms list() { Vms vms = new Vms(); List<Vm> list = vms.getVms(); Label label = BackendAffinityLabelHelper.getLabel(this, labelId); label.getVms().stream() .map(BackendAffinityLabelHelper::makeVmLink) .forEach(list::add); return vms; }
public void init() { startProgress(); AsyncDataProvider.getInstance().getVmListByClusterName(new AsyncQuery<>(vmList -> { Set<Guid> vmIds = getAffinityLabel().getVms(); getVmsSelectionModel().init(vmList, vmIds != null ? new ArrayList<>(vmIds) : new ArrayList<>()); stopProgressOnVmsAndHostsInit(); }), clusterName); AsyncDataProvider.getInstance().getHostListByClusterId(new AsyncQuery<>(hostList -> { Set<Guid> hostIds = getAffinityLabel().getHosts(); Collections.sort(hostList, new NameableComparator()); getHostsSelectionModel().init(hostList, hostIds != null ? new ArrayList<>(hostIds) : new ArrayList<>()); stopProgressOnVmsAndHostsInit(); }), clusterId); }
public Response remove() { // First we need to check if the affinity label does contain the virtual machine: Label label = BackendAffinityLabelHelper.getLabel(this, labelId); if (!label.getVms().contains(guid)) { throw new WebApplicationException(Response.Status.NOT_FOUND); } // Remove the virtual machine from the label: VM entity = new VM(); entity.setId(guid); Label updatedLabel = new LabelBuilder(label) .removeEntity(entity) .build(); return performAction(ActionType.UpdateLabel, new LabelActionParameters(updatedLabel)); } }
public LabelBuilder(Label label) { id = label.getId(); name = label.getName(); vmIds(label.getVms()); hostIds(label.getHosts()); readOnly = label.isReadOnly(); }
@Override public void update(Label label) { MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() .addValue("label_id", label.getId()) .addValue("label_name", label.getName()) .addValue("readonly", label.isReadOnly()) .addValue("vms", createArrayOf("uuid", label.getVms().toArray())) .addValue("hosts", createArrayOf("uuid", label.getHosts().toArray())); getCallsHandler() .executeModification("UpdateLabel", parameterSource); }
@Override public void save(Label label) { Guid id = label.getId(); if (Guid.isNullOrEmpty(id)) { id = Guid.newGuid(); label.setId(id); } MapSqlParameterSource parameterSource = getCustomMapSqlParameterSource() .addValue("label_id", label.getId()) .addValue("label_name", label.getName()) .addValue("readonly", label.isReadOnly()) .addValue("vms", createArrayOf("uuid", label.getVms().toArray())) .addValue("hosts", createArrayOf("uuid", label.getHosts().toArray())); getCallsHandler().executeModification("CreateLabel", parameterSource); }
@Test public void testCreateAndGetWithVM() { Guid guid = Guid.newGuid(); VmStatic vm = createAndSaveVm(); Label label = new LabelBuilder() .name("test label") .id(guid) .vm(vm.getId()) .build(); dao.save(label); Label read = dao.get(guid); assertNotNull(read); assertEquals(guid, read.getId()); assertEquals(label.getName(), read.getName()); assertNotNull(read.getVms()); assertEquals(1, read.getVms().size()); assertEquals(0, read.getHosts().size()); assertEquals(vm.getId(), read.getVms().iterator().next()); }
@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()); }