Search in sources :

Example 26 with GuestId

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);
    }
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) GuestId(org.candlepin.model.GuestId)

Example 27 with 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);
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) Consumer(org.candlepin.model.Consumer) GuestId(org.candlepin.model.GuestId) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT)

Example 28 with GuestId

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);
    }
}
Also used : Consumer(org.candlepin.model.Consumer) GuestId(org.candlepin.model.GuestId) HypervisorId(org.candlepin.model.HypervisorId)

Example 29 with GuestId

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());
}
Also used : Owner(org.candlepin.model.Owner) Set(java.util.Set) Consumer(org.candlepin.model.Consumer) HashMap(java.util.HashMap) GuestId(org.candlepin.model.GuestId) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) HypervisorCheckInResult(org.candlepin.resource.dto.HypervisorCheckInResult) Test(org.junit.Test)

Example 30 with GuestId

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());
}
Also used : Owner(org.candlepin.model.Owner) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) GuestId(org.candlepin.model.GuestId) List(java.util.List) ArrayList(java.util.ArrayList) IdentityCertificate(org.candlepin.model.IdentityCertificate) HypervisorCheckInResult(org.candlepin.resource.dto.HypervisorCheckInResult) Test(org.junit.Test)

Aggregations

GuestId (org.candlepin.model.GuestId)49 Consumer (org.candlepin.model.Consumer)37 Test (org.junit.Test)34 LinkedList (java.util.LinkedList)15 Entitlement (org.candlepin.model.Entitlement)14 GuestIdDTO (org.candlepin.dto.api.v1.GuestIdDTO)11 Date (java.util.Date)10 HashMap (java.util.HashMap)8 Product (org.candlepin.model.Product)8 ArrayList (java.util.ArrayList)7 Owner (org.candlepin.model.Owner)7 VirtConsumerMap (org.candlepin.model.VirtConsumerMap)7 HashSet (java.util.HashSet)6 Set (java.util.Set)6 List (java.util.List)5 Pool (org.candlepin.model.Pool)5 ApiOperation (io.swagger.annotations.ApiOperation)4 Produces (javax.ws.rs.Produces)4 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)4