use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class GuestIdResource method populateEntities.
/**
* Populates the specified entities with data from the provided DTOs.
*
* @param entities
* The entities instance to populate
*
* @param dtos
* The DTO containing the data with which to populate the entity
*
* @throws IllegalArgumentException
* if either entity or dto are null
*/
protected void populateEntities(List<GuestId> entities, List<GuestIdDTO> dtos) {
if (entities == null) {
throw new IllegalArgumentException("the guestId model entity is null");
}
if (dtos == null) {
throw new IllegalArgumentException("the guestId dto is null");
}
for (GuestIdDTO dto : dtos) {
if (dto == null) {
continue;
}
GuestId guestId = new GuestId();
populateEntity(guestId, dto);
entities.add(guestId);
}
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class GuestIdResource method updateGuest.
@ApiOperation(notes = "Updates a single Guest on a Consumer. Allows virt-who to avoid uploading" + " an entire list of guests", value = "updateGuest")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{guest_id}")
public void updateGuest(@ApiParam("consumer who owns or hosts the guest in question") @PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @ApiParam("guest virtual uuid") @PathParam("guest_id") String guestId, @ApiParam(name = "updated", required = true, value = "updated guest data to use") GuestIdDTO updatedDTO) {
// I'm not sure this can happen
if (guestId == null || guestId.isEmpty()) {
throw new BadRequestException(i18n.tr("Please supply a valid guest id"));
}
if (updatedDTO == null) {
// If they're not sending attributes, we can get the guestId from the url
updatedDTO = new GuestIdDTO().setGuestId(guestId);
}
// Allow the id to be left out in this case, we can use the path param
if (updatedDTO.getGuestId() == null) {
updatedDTO.setGuestId(guestId);
}
// If the guest uuids do not match, something is wrong
if (!guestId.equalsIgnoreCase(updatedDTO.getGuestId())) {
throw new BadRequestException(i18n.tr("Guest ID in json \"{0}\" does not match path guest ID \"{1}\"", updatedDTO.getGuestId(), guestId));
}
Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
GuestId guestIdEntity = new GuestId();
populateEntity(guestIdEntity, updatedDTO);
guestIdEntity.setConsumer(consumer);
GuestId toUpdate = guestIdCurator.findByGuestIdAndOrg(guestId, consumer.getOwnerId());
if (toUpdate != null) {
guestIdEntity.setId(toUpdate.getId());
}
guestIdCurator.merge(guestIdEntity);
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class HypervisorUpdateJob method parseHypervisorList.
private void parseHypervisorList(HypervisorList hypervisorList, Set<String> hosts, Set<String> guests, Map<String, Consumer> incomingHosts) {
int emptyGuestIdCount = 0;
int emptyHypervisorIdCount = 0;
List<Consumer> l = hypervisorList.getHypervisors();
for (Iterator<Consumer> hypervisors = l.iterator(); hypervisors.hasNext(); ) {
Consumer hypervisor = hypervisors.next();
HypervisorId idWrapper = hypervisor.getHypervisorId();
if (idWrapper == null) {
continue;
}
String id = idWrapper.getHypervisorId();
if (id == null) {
continue;
}
if ("".equals(id)) {
hypervisors.remove();
emptyHypervisorIdCount++;
continue;
}
incomingHosts.put(id, hypervisor);
hosts.add(id);
List<GuestId> guestsIdList = hypervisor.getGuestIds();
if (guestsIdList == null || guestsIdList.isEmpty()) {
continue;
}
for (Iterator<GuestId> guestIds = guestsIdList.iterator(); guestIds.hasNext(); ) {
GuestId guestId = guestIds.next();
if (StringUtils.isEmpty(guestId.getGuestId())) {
guestIds.remove();
emptyGuestIdCount++;
} else {
guests.add(guestId.getGuestId());
}
}
}
if (emptyHypervisorIdCount > 0) {
log.debug("Ignoring {} hypervisors with empty hypervisor IDs", emptyHypervisorIdCount);
}
if (emptyGuestIdCount > 0) {
log.debug("Ignoring {} empty/null guestId(s)", emptyGuestIdCount);
}
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class HypervisorResourceTest method hypervisorCheckInUpdatesGuestIdsWhenHostConsumerExists.
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@Test
public void hypervisorCheckInUpdatesGuestIdsWhenHostConsumerExists() throws Exception {
Owner owner = new Owner("owner-id", "Owner Id");
Map<String, List<GuestIdDTO>> hostGuestMap = new HashMap<>();
String hypervisorId = "test-host";
hostGuestMap.put(hypervisorId, new ArrayList(Arrays.asList(TestUtil.createGuestIdDTO("GUEST_B"))));
Owner o = new Owner("owner-id", "Owner ID");
o.setId("owner-id");
Consumer existing = new Consumer();
existing.setUuid("test-host");
existing.setOwner(o);
existing.addGuestId(new GuestId("GUEST_A"));
existing.setType(this.hypervisorType);
when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
// Force update
when(consumerCurator.getHostConsumersMap(any(Owner.class), any(Set.class))).thenReturn(mockHypervisorConsumerMap(hypervisorId, existing));
when(consumerCurator.getGuestConsumersMap(any(String.class), any(Set.class))).thenReturn(new VirtConsumerMap());
HypervisorCheckInResult result = hypervisorResource.hypervisorUpdate(hostGuestMap, principal, owner.getKey(), true);
List<Consumer> updated = new ArrayList<>(result.getUpdated());
assertEquals(1, updated.size());
Consumer c1 = updated.get(0);
assertEquals("test-host", c1.getUuid());
assertEquals(1, c1.getGuestIds().size());
assertEquals("GUEST_B", c1.getGuestIds().get(0).getGuestId());
}
use of org.candlepin.model.GuestId in project candlepin by candlepin.
the class HypervisorResourceTest method treatNullGuestListsAsEmptyGuestLists.
@SuppressWarnings({ "rawtypes", "unchecked", "deprecation" })
@Test
public void treatNullGuestListsAsEmptyGuestLists() throws Exception {
Owner owner = new Owner("admin");
Map<String, List<GuestIdDTO>> hostGuestMap = new HashMap<>();
hostGuestMap.put("HYPERVISOR_A", null);
when(ownerCurator.lookupByKey(eq(owner.getKey()))).thenReturn(owner);
when(consumerCurator.getHostConsumersMap(any(Owner.class), any(Set.class))).thenReturn(new VirtConsumerMap());
when(consumerCurator.getGuestConsumersMap(any(String.class), any(Set.class))).thenReturn(new VirtConsumerMap());
when(principal.canAccess(eq(owner), eq(SubResource.CONSUMERS), eq(Access.CREATE))).thenReturn(true);
when(idCertService.generateIdentityCert(any(Consumer.class))).thenReturn(new IdentityCertificate());
HypervisorCheckInResult result = hypervisorResource.hypervisorUpdate(hostGuestMap, principal, owner.getKey(), true);
assertNotNull(result);
assertNotNull(result.getCreated());
List<Consumer> created = new ArrayList<>(result.getCreated());
assertEquals(1, created.size());
List<GuestId> gids = created.get(0).getGuestIds();
assertEquals(0, gids.size());
}
Aggregations