Search in sources :

Example 6 with HypervisorId

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

the class ConsumerResource method createConsumerFromDTO.

public Consumer createConsumerFromDTO(ConsumerDTO consumer, ConsumerType type, Principal principal, String userName, String ownerKey, String activationKeys, boolean identityCertCreation) throws BadRequestException {
    // API:registerConsumer
    Set<String> keyStrings = splitKeys(activationKeys);
    // Only let NoAuth principals through if there are activation keys to consider:
    if ((principal instanceof NoAuthPrincipal) && keyStrings.isEmpty()) {
        throw new ForbiddenException(i18n.tr("Insufficient permissions"));
    }
    validateOnKeyStrings(keyStrings, ownerKey, userName);
    Owner owner = setupOwner(principal, ownerKey);
    // Raise an exception if none of the keys specified exist for this owner.
    List<ActivationKey> keys = checkActivationKeys(principal, owner, keyStrings);
    userName = setUserName(consumer, principal, userName);
    checkConsumerName(consumer);
    validateViaConsumerType(consumer, type, keys, owner, userName, principal);
    if (type.isType(ConsumerTypeEnum.SHARE)) {
        // Share consumers do not need identity certificates so refuse to create them.
        identityCertCreation = false;
        validateShareConsumer(consumer, principal, keys);
        // if there exists a share consumer between the two orgs, return it.
        Consumer existingShareConsumer = consumerCurator.getSharingConsumer(owner, consumer.getRecipientOwnerKey());
        if (existingShareConsumer != null) {
            return existingShareConsumer;
        }
        consumer.setAutoheal(false);
    } else {
        // this is the default
        consumer.setAutoheal(true);
        if (StringUtils.isNotEmpty(consumer.getRecipientOwnerKey())) {
            throw new BadRequestException(i18n.tr("Only share consumers can specify recipient owners"));
        }
    }
    if (consumer.getServiceLevel() == null) {
        consumer.setServiceLevel("");
    }
    // Sanitize the inbound facts
    this.sanitizeConsumerFacts(consumer);
    // If no service level was specified, and the owner has a default set, use it:
    if (consumer.getServiceLevel().equals("") && owner.getDefaultServiceLevel() != null && !type.isType(ConsumerTypeEnum.SHARE)) {
        consumer.setServiceLevel(owner.getDefaultServiceLevel());
    }
    Consumer consumerToCreate = new Consumer();
    consumerToCreate.setOwner(owner);
    populateEntity(consumerToCreate, consumer);
    consumerToCreate.setType(type);
    if (!type.isType(ConsumerTypeEnum.SHARE)) {
        consumerToCreate.setCanActivate(subAdapter.canActivateSubscription(consumerToCreate));
    }
    HypervisorId hvsrId = consumerToCreate.getHypervisorId();
    if (hvsrId != null && hvsrId.getHypervisorId() != null && !hvsrId.getHypervisorId().isEmpty()) {
        // If a hypervisorId is supplied, make sure the consumer and owner are correct
        hvsrId.setConsumer(consumerToCreate);
        hvsrId.setOwner(owner);
    }
    updateCapabilities(consumerToCreate, null);
    logNewConsumerDebugInfo(consumerToCreate, keys, type);
    validateContentAccessMode(consumerToCreate, owner);
    consumerBindUtil.validateServiceLevel(owner.getId(), consumerToCreate.getServiceLevel());
    try {
        Date createdDate = consumerToCreate.getCreated();
        Date lastCheckIn = consumerToCreate.getLastCheckin();
        // create sets created to current time.
        consumerToCreate = consumerCurator.create(consumerToCreate);
        // If we sent in a created date, we want it persisted at the update below
        if (createdDate != null) {
            consumerToCreate.setCreated(createdDate);
        }
        if (lastCheckIn != null) {
            log.info("Creating with specific last check-in time: {}", lastCheckIn);
            consumerToCreate.setLastCheckin(lastCheckIn);
        }
        if (identityCertCreation) {
            IdentityCertificate idCert = generateIdCert(consumerToCreate, false);
            consumerToCreate.setIdCert(idCert);
        }
        sink.emitConsumerCreated(consumerToCreate);
        if (keys.size() > 0) {
            consumerBindUtil.handleActivationKeys(consumerToCreate, keys, owner.isAutobindDisabled());
        }
        // Don't allow complianceRules to update entitlementStatus, because we're about to perform
        // an update unconditionally.
        complianceRules.getStatus(consumerToCreate, null, false, false);
        consumerCurator.update(consumerToCreate);
        log.info("Consumer {} created in org {}", consumerToCreate.getUuid(), consumerToCreate.getOwnerId());
        return consumerToCreate;
    } catch (CandlepinException ce) {
        // If it is one of ours, rethrow it.
        throw ce;
    } catch (Exception e) {
        log.error("Problem creating unit:", e);
        throw new BadRequestException(i18n.tr("Problem creating unit {0}", consumer));
    }
}
Also used : CandlepinException(org.candlepin.common.exceptions.CandlepinException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Owner(org.candlepin.model.Owner) NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Date(java.util.Date) GeneralSecurityException(java.security.GeneralSecurityException) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) IseException(org.candlepin.common.exceptions.IseException) AutobindDisabledForOwnerException(org.candlepin.controller.AutobindDisabledForOwnerException) CandlepinException(org.candlepin.common.exceptions.CandlepinException) IOException(java.io.IOException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ExportCreationException(org.candlepin.sync.ExportCreationException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) PropertyValidationException(org.candlepin.util.PropertyValidationException) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) HypervisorId(org.candlepin.model.HypervisorId) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 7 with HypervisorId

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

the class HypervisorResource method createConsumerForHypervisorId.

/*
     * Create a new hypervisor type consumer to represent the incoming hypervisorId
     */
private Consumer createConsumerForHypervisorId(String incHypervisorId, Owner owner, Principal principal) {
    Consumer consumer = new Consumer();
    consumer.setName(incHypervisorId);
    consumer.setType(this.hypervisorType);
    consumer.setFact("uname.machine", "x86_64");
    consumer.setGuestIds(new ArrayList<>());
    consumer.setOwner(owner);
    // Create HypervisorId
    HypervisorId hypervisorId = new HypervisorId(consumer, owner, incHypervisorId);
    hypervisorId.setOwner(owner);
    consumer.setHypervisorId(hypervisorId);
    // Create Consumer
    return consumerResource.createConsumerFromDTO(this.translator.translate(consumer, ConsumerDTO.class), this.hypervisorType, principal, null, owner.getKey(), null, false);
}
Also used : Consumer(org.candlepin.model.Consumer) ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) HypervisorId(org.candlepin.model.HypervisorId)

Example 8 with HypervisorId

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

the class HypervisorUpdateJob method createConsumerForHypervisorId.

/*
     * Create a new hypervisor type consumer to represent the incoming hypervisorId
     */
private Consumer createConsumerForHypervisorId(String incHypervisorId, String reporterId, Owner owner, Principal principal, Consumer incoming) {
    Consumer consumer = new Consumer();
    if (incoming.getName() != null) {
        consumer.setName(incoming.getName());
    } else {
        consumer.setName(sanitizeHypervisorId(incHypervisorId));
    }
    consumer.setType(hypervisorType);
    consumer.setFact("uname.machine", "x86_64");
    consumer.setGuestIds(new ArrayList<>());
    consumer.setLastCheckin(new Date());
    consumer.setOwner(owner);
    consumer.setAutoheal(true);
    consumer.setCanActivate(subAdapter.canActivateSubscription(consumer));
    if (owner.getDefaultServiceLevel() != null) {
        consumer.setServiceLevel(owner.getDefaultServiceLevel());
    } else {
        consumer.setServiceLevel("");
    }
    if (principal.getUsername() != null) {
        consumer.setUsername(principal.getUsername());
    }
    consumer.setEntitlementCount(0L);
    // TODO: Refactor this to not call resource methods directly
    consumerResource.sanitizeConsumerFacts(consumer);
    // Create HypervisorId
    HypervisorId hypervisorId = new HypervisorId(consumer, owner, incHypervisorId);
    hypervisorId.setReporterId(reporterId);
    consumer.setHypervisorId(hypervisorId);
    // TODO: Refactor this to not call resource methods directly
    consumerResource.checkForFactsUpdate(consumer, incoming);
    return consumer;
}
Also used : Consumer(org.candlepin.model.Consumer) HypervisorId(org.candlepin.model.HypervisorId) Date(java.util.Date)

Example 9 with HypervisorId

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

the class HypervisorUpdateJobTest method reporterIdOnUpdateTest.

@Test
public void reporterIdOnUpdateTest() 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, "updateReporterId");
    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);
    assertEquals("updateReporterId", hypervisor.getHypervisorId().getReporterId());
}
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)

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