Search in sources :

Example 46 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class EventSinkImplTest method keyWithPoolsShouldEmitSuccessfully.

@Test
public void keyWithPoolsShouldEmitSuccessfully() throws Exception {
    ArrayList<Pool> pools = new ArrayList<>();
    pools.add(TestUtil.createPool(o, TestUtil.createProduct()));
    pools.add(TestUtil.createPool(o, TestUtil.createProduct()));
    ActivationKey key = TestUtil.createActivationKey(new Owner("deadbeef"), pools);
    eventSinkImpl.emitActivationKeyCreated(key);
    eventSinkImpl.sendEvents();
    verify(mockClientProducer).send(any(ClientMessage.class));
}
Also used : Owner(org.candlepin.model.Owner) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) ClientMessage(org.apache.activemq.artemis.api.core.client.ClientMessage) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Test(org.junit.Test)

Example 47 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ActivationKeyTranslatorTest method initSourceObject.

@Override
protected ActivationKey initSourceObject() {
    ActivationKey source = new ActivationKey();
    source.setId("key-id");
    source.setName("key-name");
    source.setDescription("key-description");
    source.setOwner(this.ownerTranslatorTest.initSourceObject());
    source.setReleaseVer(new Release("key-release-ver"));
    source.setServiceLevel("key-service-level");
    source.setAutoAttach(true);
    Product prod = new Product();
    prod.setId("prod-1-id");
    source.setProducts(new HashSet<>());
    source.addProduct(prod);
    Pool pool = new Pool();
    pool.setId("pool-1-id");
    source.setPools(new HashSet<>());
    source.addPool(pool, 1L);
    return source;
}
Also used : Product(org.candlepin.model.Product) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Pool(org.candlepin.model.Pool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Release(org.candlepin.model.Release) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride)

Example 48 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class PoolCurator method getActivationKeysForPool.

public List<ActivationKey> getActivationKeysForPool(Pool p) {
    List<ActivationKey> activationKeys = new ArrayList<>();
    List<ActivationKeyPool> activationKeyPools = currentSession().createCriteria(ActivationKeyPool.class).add(Restrictions.eq("pool", p)).list();
    for (ActivationKeyPool akp : activationKeyPools) {
        activationKeys.add(akp.getKey());
    }
    return activationKeys;
}
Also used : ArrayList(java.util.ArrayList) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) ActivationKey(org.candlepin.model.activationkeys.ActivationKey)

Example 49 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey 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 50 with ActivationKey

use of org.candlepin.model.activationkeys.ActivationKey in project candlepin by candlepin.

the class ConsumerResource method checkActivationKeys.

private List<ActivationKey> checkActivationKeys(Principal principal, Owner owner, Set<String> keyStrings) throws BadRequestException {
    List<ActivationKey> keys = new ArrayList<>();
    for (String keyString : keyStrings) {
        ActivationKey key = null;
        try {
            key = findKey(keyString, owner);
            keys.add(key);
        } catch (NotFoundException e) {
            log.warn(e.getMessage());
        }
    }
    if ((principal instanceof NoAuthPrincipal) && keys.isEmpty()) {
        throw new BadRequestException(i18n.tr("None of the activation keys specified exist for this org."));
    }
    return keys;
}
Also used : NoAuthPrincipal(org.candlepin.auth.NoAuthPrincipal) ArrayList(java.util.ArrayList) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) ActivationKey(org.candlepin.model.activationkeys.ActivationKey)

Aggregations

ActivationKey (org.candlepin.model.activationkeys.ActivationKey)76 Test (org.junit.Test)55 Pool (org.candlepin.model.Pool)38 Product (org.candlepin.model.Product)16 ActivationKeyPool (org.candlepin.model.activationkeys.ActivationKeyPool)15 ArrayList (java.util.ArrayList)14 ValidationResult (org.candlepin.policy.ValidationResult)14 Consumer (org.candlepin.model.Consumer)13 ActivationKeyCurator (org.candlepin.model.activationkeys.ActivationKeyCurator)13 PoolManager (org.candlepin.controller.PoolManager)11 ProductCachedSerializationModule (org.candlepin.jackson.ProductCachedSerializationModule)10 Owner (org.candlepin.model.Owner)10 ApiOperation (io.swagger.annotations.ApiOperation)9 ApiResponses (io.swagger.annotations.ApiResponses)9 Path (javax.ws.rs.Path)9 Produces (javax.ws.rs.Produces)9 BadRequestException (org.candlepin.common.exceptions.BadRequestException)9 Date (java.util.Date)8 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)8 HashSet (java.util.HashSet)6