Search in sources :

Example 1 with HypervisorId

use of org.candlepin.model.HypervisorId in project candlepin by candlepin.

the class HypervisorUpdateJobTest method hypervisorUpdateExecUpdate.

@Test
public void hypervisorUpdateExecUpdate() throws JobExecutionException {
    when(ownerCurator.lookupByKey(eq("joe"))).thenReturn(owner);
    Consumer hypervisor = new Consumer();
    String hypervisorId = "uuid_999";
    hypervisor.setHypervisorId(new HypervisorId(hypervisorId));
    VirtConsumerMap vcm = new VirtConsumerMap();
    vcm.add(hypervisorId, hypervisor);
    when(consumerCurator.getHostConsumersMap(eq(owner), any(Set.class))).thenReturn(vcm);
    JobDetail detail = HypervisorUpdateJob.forOwner(owner, hypervisorJson, true, principal, null);
    JobExecutionContext ctx = mock(JobExecutionContext.class);
    when(ctx.getMergedJobDataMap()).thenReturn(detail.getJobDataMap());
    HypervisorUpdateJob job = new HypervisorUpdateJob(ownerCurator, consumerCurator, consumerTypeCurator, consumerResource, i18n, subAdapter, complianceRules);
    injector.injectMembers(job);
    job.execute(ctx);
    verify(consumerResource).checkForFactsUpdate(any(Consumer.class), any(Consumer.class));
    verify(consumerCurator).update(any(Consumer.class), eq(false));
}
Also used : JobDetail(org.quartz.JobDetail) Set(java.util.Set) Consumer(org.candlepin.model.Consumer) VirtConsumerMap(org.candlepin.model.VirtConsumerMap) HypervisorId(org.candlepin.model.HypervisorId) JobExecutionContext(org.quartz.JobExecutionContext) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 2 with HypervisorId

use of org.candlepin.model.HypervisorId in project candlepin by candlepin.

the class ConsumerResource method checkForHypervisorIdUpdate.

private boolean checkForHypervisorIdUpdate(Consumer existing, ConsumerDTO incoming) {
    HypervisorIdDTO incomingId = incoming.getHypervisorId();
    if (incomingId != null) {
        HypervisorId existingId = existing.getHypervisorId();
        if (incomingId.getHypervisorId() == null || incomingId.getHypervisorId().isEmpty()) {
            // Allow hypervisorId to be removed
            existing.setHypervisorId(null);
        } else {
            if (existingId != null) {
                if (existingId.getHypervisorId() != null && !existingId.getHypervisorId().equals(incomingId.getHypervisorId())) {
                    existingId.setHypervisorId(incomingId.getHypervisorId());
                    Owner owner = ownerCurator.findOwnerById(existing.getOwnerId());
                    existingId.setOwner(owner);
                } else {
                    return false;
                }
            } else {
                // Safer to build a new clean HypervisorId object
                Owner owner = ownerCurator.findOwnerById(existing.getOwnerId());
                HypervisorId hypervisorId = new HypervisorId(incomingId.getHypervisorId());
                hypervisorId.setOwner(owner);
                existing.setHypervisorId(hypervisorId);
            }
        }
        return true;
    }
    return false;
}
Also used : HypervisorIdDTO(org.candlepin.dto.api.v1.HypervisorIdDTO) Owner(org.candlepin.model.Owner) HypervisorId(org.candlepin.model.HypervisorId)

Example 3 with HypervisorId

use of org.candlepin.model.HypervisorId in project candlepin by candlepin.

the class ConsumerResource method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO, during consumer creation (not update).
 * This method will not set the ID, entitlementStatus, complianceStatusHash, idCert, entitlements,
 * keyPair and canActivate, because clients are not allowed to create or update those properties.
 *
 * while autoheal is populated, it is overridden in create method.
 *
 * owner is not populated because create populates it differently.
 *
 * @param entity
 *  The entity instance to populate
 *
 * @param dto
 *  The DTO containing the data with which to populate the entity
 *
 * @throws IllegalArgumentException
 *  if either entity or dto are null
 */
protected void populateEntity(Consumer entity, ConsumerDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("the consumer model entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("the consumer dto is null");
    }
    if (dto.getCreated() != null) {
        entity.setCreated(dto.getCreated());
    }
    if (dto.getName() != null) {
        entity.setName(dto.getName());
    }
    if (dto.getUuid() != null) {
        entity.setUuid(dto.getUuid());
    }
    if (dto.getFacts() != null) {
        entity.setFacts(dto.getFacts());
    }
    if (dto.getUsername() != null) {
        entity.setUsername(dto.getUsername());
    }
    if (dto.getServiceLevel() != null) {
        entity.setServiceLevel(dto.getServiceLevel());
    }
    if (dto.getReleaseVersion() != null) {
        entity.setReleaseVer(new Release(dto.getReleaseVersion()));
    }
    if (dto.getEnvironment() != null) {
        Environment env = environmentCurator.find(dto.getEnvironment().getId());
        if (env == null) {
            throw new NotFoundException(i18n.tr("Environment \"{0}\" could not be found.", dto.getEnvironment().getId()));
        }
        entity.setEnvironment(env);
    }
    if (dto.getLastCheckin() != null) {
        entity.setLastCheckin(dto.getLastCheckin());
    }
    if (dto.getCapabilities() != null) {
        Set<ConsumerCapability> capabilities = populateCapabilities(entity, dto);
        entity.setCapabilities(capabilities);
    }
    if (dto.getGuestIds() != null) {
        List<GuestId> guestIds = new ArrayList<>();
        for (GuestIdDTO guestIdDTO : dto.getGuestIds()) {
            if (guestIdDTO != null) {
                guestIds.add(new GuestId(guestIdDTO.getGuestId(), entity, guestIdDTO.getAttributes()));
            }
        }
        entity.setGuestIds(guestIds);
    }
    if (dto.getHypervisorId() != null && entity.getOwnerId() != null) {
        HypervisorId hypervisorId = new HypervisorId(entity, ownerCurator.findOwnerById(entity.getOwnerId()), dto.getHypervisorId().getHypervisorId(), dto.getHypervisorId().getReporterId());
        entity.setHypervisorId(hypervisorId);
    }
    if (dto.getHypervisorId() == null && dto.getFact("system_uuid") != null && !"true".equals(dto.getFact("virt.is_guest")) && entity.getOwnerId() != null) {
        HypervisorId hypervisorId = new HypervisorId(entity, ownerCurator.findOwnerById(entity.getOwnerId()), dto.getFact("system_uuid"));
        entity.setHypervisorId(hypervisorId);
    }
    if (dto.getContentTags() != null) {
        entity.setContentTags(dto.getContentTags());
    }
    if (dto.getAutoheal() != null) {
        entity.setAutoheal(dto.getAutoheal());
    }
    if (dto.getContentAccessMode() != null) {
        entity.setContentAccessMode(dto.getContentAccessMode());
    }
    if (dto.getRecipientOwnerKey() != null) {
        entity.setRecipientOwnerKey(dto.getRecipientOwnerKey());
    }
    if (dto.getAnnotations() != null) {
        entity.setAnnotations(dto.getAnnotations());
    }
    if (dto.getInstalledProducts() != null) {
        Set<ConsumerInstalledProduct> installedProducts = populateInstalledProducts(entity, dto);
        entity.setInstalledProducts(installedProducts);
    }
}
Also used : GuestIdDTO(org.candlepin.dto.api.v1.GuestIdDTO) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ArrayList(java.util.ArrayList) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ConsumerCapability(org.candlepin.model.ConsumerCapability) GuestId(org.candlepin.model.GuestId) Environment(org.candlepin.model.Environment) HypervisorId(org.candlepin.model.HypervisorId) Release(org.candlepin.model.Release)

Example 4 with HypervisorId

use of org.candlepin.model.HypervisorId 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 5 with HypervisorId

use of org.candlepin.model.HypervisorId in project candlepin by candlepin.

the class HypervisorIdTranslatorTest method initSourceObject.

@Override
protected HypervisorId initSourceObject() {
    HypervisorId source = new HypervisorId();
    source.setId("test_id");
    source.setHypervisorId("test_hypervisor_id");
    source.setReporterId("test_reporter_id");
    return source;
}
Also used : HypervisorId(org.candlepin.model.HypervisorId)

Aggregations

HypervisorId (org.candlepin.model.HypervisorId)9 Consumer (org.candlepin.model.Consumer)6 Date (java.util.Date)2 Set (java.util.Set)2 NotFoundException (org.candlepin.common.exceptions.NotFoundException)2 GuestId (org.candlepin.model.GuestId)2 Owner (org.candlepin.model.Owner)2 VirtConsumerMap (org.candlepin.model.VirtConsumerMap)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 JobDetail (org.quartz.JobDetail)2 JobExecutionContext (org.quartz.JobExecutionContext)2 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)1 BadRequestException (org.candlepin.common.exceptions.BadRequestException)1 CandlepinException (org.candlepin.common.exceptions.CandlepinException)1 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)1 IseException (org.candlepin.common.exceptions.IseException)1