Search in sources :

Example 1 with Release

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

the class ActivationKeyTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ActivationKeyDTO populate(ModelTranslator modelTranslator, ActivationKey source, ActivationKeyDTO dest) {
    dest = super.populate(modelTranslator, source, dest);
    dest.setId(source.getId()).setName(source.getName()).setDescription(source.getDescription()).setServiceLevel(source.getServiceLevel()).setAutoAttach(source.isAutoAttach());
    // Process nested objects if we have a model translator to use to the translation...
    if (modelTranslator != null) {
        Owner owner = source.getOwner();
        dest.setOwner(owner != null ? modelTranslator.translate(owner, OwnerDTO.class) : null);
        Set<ActivationKeyPool> pools = source.getPools();
        if (pools != null && !pools.isEmpty()) {
            for (ActivationKeyPool poolEntry : pools) {
                if (poolEntry != null) {
                    dest.addPool(new ActivationKeyDTO.ActivationKeyPoolDTO(poolEntry.getPool().getId(), poolEntry.getQuantity()));
                }
            }
        } else {
            dest.setPools(Collections.<ActivationKeyDTO.ActivationKeyPoolDTO>emptySet());
        }
        Set<Product> products = source.getProducts();
        if (products != null && !products.isEmpty()) {
            for (Product prod : products) {
                if (prod != null) {
                    dest.addProductId(prod.getId());
                }
            }
        } else {
            dest.setProductIds(Collections.<String>emptySet());
        }
        Set<ActivationKeyContentOverride> overrides = source.getContentOverrides();
        if (overrides != null && !overrides.isEmpty()) {
            for (ActivationKeyContentOverride override : overrides) {
                if (override != null) {
                    dest.addContentOverride(new ActivationKeyDTO.ActivationKeyContentOverrideDTO(override.getContentLabel(), override.getName(), override.getValue()));
                }
            }
        } else {
            dest.setContentOverrides(Collections.<ActivationKeyDTO.ActivationKeyContentOverrideDTO>emptySet());
        }
        Release release = source.getReleaseVer();
        if (release != null) {
            dest.setReleaseVersion(release.getReleaseVer());
        }
    }
    return dest;
}
Also used : Owner(org.candlepin.model.Owner) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Product(org.candlepin.model.Product) Release(org.candlepin.model.Release) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride)

Example 2 with Release

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

the class ActivationKeyTranslatorTest method verifyOutput.

@Override
protected void verifyOutput(ActivationKey source, ActivationKeyDTO dest, boolean childrenGenerated) {
    if (source != null) {
        assertEquals(source.getId(), dest.getId());
        assertEquals(source.getName(), dest.getName());
        assertEquals(source.getDescription(), dest.getDescription());
        assertEquals(source.getServiceLevel(), dest.getServiceLevel());
        assertEquals(source.isAutoAttach(), dest.isAutoAttach());
        if (childrenGenerated) {
            this.ownerTranslatorTest.verifyOutput(source.getOwner(), dest.getOwner(), true);
            for (Product prod : source.getProducts()) {
                for (String prodDto : dest.getProductIds()) {
                    assertNotNull(prodDto);
                    if (prodDto.equals(prod.getId())) {
                    // Nothing else to assert on, since prodDto only holds the product id.
                    }
                }
            }
            for (ActivationKeyPool akPool : source.getPools()) {
                for (ActivationKeyDTO.ActivationKeyPoolDTO akPoolDto : dest.getPools()) {
                    assertNotNull(akPoolDto);
                    if (akPoolDto.getPoolId().equals(akPool.getId())) {
                        assertEquals(akPool.getQuantity(), akPoolDto.getQuantity());
                    }
                }
            }
            for (ActivationKeyContentOverride akOverride : source.getContentOverrides()) {
                for (ActivationKeyDTO.ActivationKeyContentOverrideDTO akOverrideDto : dest.getContentOverrides()) {
                    assertNotNull(akOverrideDto);
                    if (akOverrideDto.getName().equals(akOverride.getName())) {
                        assertEquals(akOverrideDto.getContentLabel(), akOverride.getContentLabel());
                        assertEquals(akOverrideDto.getValue(), akOverride.getValue());
                    }
                }
            }
            Release releaseSource = source.getReleaseVer();
            String releaseDestination = dest.getReleaseVersion();
            if (releaseSource != null) {
                assertEquals(releaseSource.getReleaseVer(), releaseDestination);
            } else {
                assertNull(releaseDestination);
            }
        } else {
            assertNull(dest.getOwner());
            assertNull(dest.getProductIds());
            assertNull(dest.getPools());
            assertNull(dest.getContentOverrides());
            assertNull(dest.getReleaseVersion());
        }
    } else {
        assertNull(dest);
    }
}
Also used : Product(org.candlepin.model.Product) ActivationKeyPool(org.candlepin.model.activationkeys.ActivationKeyPool) Release(org.candlepin.model.Release) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride)

Example 3 with Release

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

the class ActivationKeyResourceTest method testCreateReadDelete.

@Test(expected = BadRequestException.class)
public void testCreateReadDelete() {
    ActivationKey key = new ActivationKey();
    key.setOwner(owner);
    key.setName("dd");
    key.setServiceLevel("level1");
    key.setReleaseVer(new Release("release1"));
    activationKeyCurator.create(key);
    assertNotNull(key.getId());
    ActivationKeyDTO output = activationKeyResource.getActivationKey(key.getId());
    assertNotNull(output);
    output.setName("JarJarBinks");
    output.setServiceLevel("level2");
    output.setReleaseVersion("release2");
    activationKeyResource.updateActivationKey(key.getId(), output);
    output = activationKeyResource.getActivationKey(key.getId());
    assertEquals("JarJarBinks", output.getName());
    assertEquals("level2", output.getServiceLevel());
    assertEquals("release2", output.getReleaseVersion());
    activationKeyResource.deleteActivationKey(key.getId());
    output = activationKeyResource.getActivationKey(key.getId());
    assertNull(output);
}
Also used : ActivationKeyDTO(org.candlepin.dto.api.v1.ActivationKeyDTO) ActivationKey(org.candlepin.model.activationkeys.ActivationKey) Release(org.candlepin.model.Release) Test(org.junit.Test)

Example 4 with Release

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

the class OwnerResource method populateEntity.

/**
 * Populates the specified entity with data from the provided DTO. This method will not set the
 * ID field.
 *
 * @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(ActivationKey entity, ActivationKeyDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("the activation key model entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("the activation key dto is null");
    }
    if (dto.getName() != null) {
        entity.setName(dto.getName());
    }
    if (dto.getDescription() != null) {
        entity.setDescription(dto.getDescription());
    }
    if (dto.getServiceLevel() != null) {
        entity.setServiceLevel(dto.getServiceLevel());
    }
    if (dto.getOwner() != null) {
        entity.setOwner(lookupOwnerFromDto(dto.getOwner()));
    }
    if (dto.getServiceLevel() != null) {
        if (dto.getServiceLevel().isEmpty()) {
            entity.setServiceLevel(null);
        } else {
            entity.setServiceLevel(dto.getServiceLevel());
        }
    }
    if (dto.getContentOverrides() != null) {
        if (dto.getContentOverrides().isEmpty()) {
            entity.setContentOverrides(new HashSet<>());
        } else {
            for (ActivationKeyDTO.ActivationKeyContentOverrideDTO overrideDTO : dto.getContentOverrides()) {
                if (overrideDTO != null) {
                    entity.addContentOverride(new ActivationKeyContentOverride(entity, overrideDTO.getContentLabel(), overrideDTO.getName(), overrideDTO.getValue()));
                }
            }
        }
    }
    if (dto.getReleaseVersion() != null) {
        entity.setReleaseVer(new Release(dto.getReleaseVersion()));
    }
    if (dto.getPools() != null) {
        if (dto.getPools().isEmpty()) {
            entity.setPools(new HashSet<>());
        } else {
            for (ActivationKeyDTO.ActivationKeyPoolDTO poolDTO : dto.getPools()) {
                if (poolDTO != null) {
                    Pool pool = findPool(poolDTO.getPoolId());
                    entity.addPool(pool, poolDTO.getQuantity());
                }
            }
        }
    }
    if (dto.getProductIds() != null) {
        if (dto.getProductIds().isEmpty()) {
            entity.setProducts(new HashSet<>());
        } else {
            for (String productId : dto.getProductIds()) {
                if (productId != null) {
                    Product product = findProduct(entity.getOwner(), productId);
                    entity.addProduct(product);
                }
            }
        }
    }
}
Also used : ActivationKeyDTO(org.candlepin.dto.api.v1.ActivationKeyDTO) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Release(org.candlepin.model.Release) ActivationKeyContentOverride(org.candlepin.model.activationkeys.ActivationKeyContentOverride)

Example 5 with Release

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

the class ConsumerResource method performConsumerUpdates.

@Transactional
public boolean performConsumerUpdates(ConsumerDTO updated, Consumer toUpdate, GuestMigration guestMigration, boolean isIdCert) {
    log.debug("Updating consumer: {}", toUpdate.getUuid());
    // We need a representation of the consumer before making any modifications.
    // If nothing changes we won't send.  The new entity needs to be correct though,
    // so we should get a Jsonstring now, and finish it off if we're going to send
    EventBuilder eventBuilder = eventFactory.getEventBuilder(Target.CONSUMER, Type.MODIFIED).setEventData(toUpdate);
    // version changed on non-checked in consumer, or list of capabilities
    // changed on checked in consumer
    boolean changesMade = updateCapabilities(toUpdate, updated);
    changesMade = checkForFactsUpdate(toUpdate, updated) || changesMade;
    changesMade = checkForInstalledProductsUpdate(toUpdate, updated) || changesMade;
    changesMade = checkForHypervisorIdUpdate(toUpdate, updated) || changesMade;
    changesMade = guestMigration.isMigrationPending() || changesMade;
    if (updated.getContentTags() != null && !updated.getContentTags().equals(toUpdate.getContentTags())) {
        log.info("   Updating content tags.");
        toUpdate.setContentTags(updated.getContentTags());
        changesMade = true;
    }
    // Allow optional setting of the autoheal attribute:
    if (updated.getAutoheal() != null && !updated.getAutoheal().equals(toUpdate.isAutoheal())) {
        log.info("   Updating consumer autoheal setting.");
        toUpdate.setAutoheal(updated.getAutoheal());
        changesMade = true;
    }
    if (updated.getReleaseVersion() != null && !updated.getReleaseVersion().equals(toUpdate.getReleaseVer() == null ? null : toUpdate.getReleaseVer().getReleaseVer())) {
        log.info("   Updating consumer releaseVer setting.");
        toUpdate.setReleaseVer(new Release(updated.getReleaseVersion()));
        changesMade = true;
    }
    // Allow optional setting of the service level attribute:
    String level = updated.getServiceLevel();
    if (level != null && !level.equals(toUpdate.getServiceLevel())) {
        log.info("   Updating consumer service level setting.");
        consumerBindUtil.validateServiceLevel(toUpdate.getOwnerId(), level);
        toUpdate.setServiceLevel(level);
        changesMade = true;
    }
    String environmentId = updated.getEnvironment() == null ? null : updated.getEnvironment().getId();
    if (environmentId != null && (toUpdate.getEnvironmentId() == null || !toUpdate.getEnvironmentId().equals(environmentId))) {
        Environment e = environmentCurator.find(environmentId);
        if (e == null) {
            throw new NotFoundException(i18n.tr("Environment with ID \"{0}\" could not be found.", environmentId));
        }
        log.info("Updating environment to: {}", environmentId);
        toUpdate.setEnvironment(e);
        // lazily regenerate certs, so the client can still work
        poolManager.regenerateCertificatesOf(toUpdate, true);
        changesMade = true;
    }
    // it should remain the same
    if (updated.getName() != null && !toUpdate.getName().equals(updated.getName())) {
        checkConsumerName(updated);
        log.info("Updating consumer name: {} -> {}", toUpdate.getName(), updated.getName());
        toUpdate.setName(updated.getName());
        changesMade = true;
        // get the new name into the id cert if we are using the cert
        if (isIdCert) {
            IdentityCertificate ic = generateIdCert(toUpdate, true);
            toUpdate.setIdCert(ic);
        }
    }
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(toUpdate);
    if (updated.getContentAccessMode() != null && !updated.getContentAccessMode().equals(toUpdate.getContentAccessMode()) && ctype.isManifest()) {
        Owner toUpdateOwner = ownerCurator.findOwnerById(toUpdate.getOwnerId());
        if (!toUpdateOwner.isAllowedContentAccessMode(updated.getContentAccessMode())) {
            throw new BadRequestException(i18n.tr("The consumer cannot use the supplied content access mode."));
        }
        toUpdate.setContentAccessMode(updated.getContentAccessMode());
        changesMade = true;
    }
    if (!StringUtils.isEmpty(updated.getContentAccessMode()) && !ctype.isManifest()) {
        throw new BadRequestException(i18n.tr("The consumer cannot be assigned a content access mode."));
    }
    if (updated.getLastCheckin() != null) {
        log.info("Updating to specific last checkin time: {}", updated.getLastCheckin());
        toUpdate.setLastCheckin(updated.getLastCheckin());
        changesMade = true;
    }
    if (changesMade) {
        log.debug("Consumer {} updated.", toUpdate.getUuid());
        // Set the updated date here b/c @PreUpdate will not get fired
        // since only the facts table will receive the update.
        toUpdate.setUpdated(new Date());
        // this should update compliance on toUpdate, but not call the curator
        complianceRules.getStatus(toUpdate, null, false, false);
        Event event = eventBuilder.setEventData(toUpdate).buildEvent();
        sink.queueEvent(event);
    }
    return changesMade;
}
Also used : Owner(org.candlepin.model.Owner) EventBuilder(org.candlepin.audit.EventBuilder) Environment(org.candlepin.model.Environment) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Event(org.candlepin.audit.Event) ConsumerType(org.candlepin.model.ConsumerType) Release(org.candlepin.model.Release) Date(java.util.Date) IdentityCertificate(org.candlepin.model.IdentityCertificate) Transactional(com.google.inject.persist.Transactional)

Aggregations

Release (org.candlepin.model.Release)11 Environment (org.candlepin.model.Environment)4 Owner (org.candlepin.model.Owner)4 Product (org.candlepin.model.Product)4 ActivationKey (org.candlepin.model.activationkeys.ActivationKey)4 ActivationKeyContentOverride (org.candlepin.model.activationkeys.ActivationKeyContentOverride)4 ActivationKeyDTO (org.candlepin.dto.api.v1.ActivationKeyDTO)3 ConsumerCapability (org.candlepin.model.ConsumerCapability)3 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)3 ConsumerType (org.candlepin.model.ConsumerType)3 ActivationKeyPool (org.candlepin.model.activationkeys.ActivationKeyPool)3 Date (java.util.Date)2 HashSet (java.util.HashSet)2 NotFoundException (org.candlepin.common.exceptions.NotFoundException)2 GuestId (org.candlepin.model.GuestId)2 Pool (org.candlepin.model.Pool)2 Test (org.junit.Test)2 Transactional (com.google.inject.persist.Transactional)1 ApiOperation (io.swagger.annotations.ApiOperation)1 ApiResponses (io.swagger.annotations.ApiResponses)1