Search in sources :

Example 6 with EnvironmentContent

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

the class DatabaseTestFixture method createEnvironment.

protected Environment createEnvironment(Owner owner, String id, String name, String description, Collection<Consumer> consumers, Collection<Content> content) {
    Environment environment = new Environment(id, name, owner);
    environment.setDescription(description);
    if (content != null) {
        for (Content elem : content) {
            EnvironmentContent envContent = new EnvironmentContent(environment, elem, true);
            // Impl note:
            // At the time of writing, this line is redundant. But if we ever fix environment,
            // this will be good to have as a backup.
            environment.getEnvironmentContent().add(envContent);
        }
    }
    environment = this.environmentCurator.create(environment);
    // Update consumers to point to the new environment
    if (consumers != null) {
        for (Consumer consumer : consumers) {
            consumer.setEnvironmentId(environment.getId());
            this.consumerCurator.merge(consumer);
        }
    }
    return environment;
}
Also used : Consumer(org.candlepin.model.Consumer) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent) Environment(org.candlepin.model.Environment) EnvironmentContent(org.candlepin.model.EnvironmentContent)

Example 7 with EnvironmentContent

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

the class EnvironmentTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public EnvironmentDTO populate(ModelTranslator translator, Environment source, EnvironmentDTO dest) {
    dest = super.populate(translator, source, dest);
    dest.setId(source.getId());
    dest.setName(source.getName());
    dest.setDescription(source.getDescription());
    if (translator != null) {
        dest.setOwner(translator.translate(source.getOwner(), OwnerDTO.class));
        Set<EnvironmentContent> envContents = source.getEnvironmentContent();
        if (envContents != null) {
            Collection<EnvironmentContent> envContent = source.getEnvironmentContent();
            dest.setEnvironmentContent(Collections.<EnvironmentContentDTO>emptyList());
            if (envContent != null) {
                ObjectTranslator<Content, ContentDTO> contentTranslator = translator.findTranslatorByClass(Content.class, ContentDTO.class);
                for (EnvironmentContent ec : envContent) {
                    if (ec != null) {
                        ContentDTO dto = contentTranslator.translate(translator, ec.getContent());
                        if (dto != null) {
                            dest.addContent(dto, ec.getEnabled());
                        }
                    }
                }
            }
        }
    } else {
        dest.setOwner(null);
        dest.setEnvironmentContent(null);
    }
    return dest;
}
Also used : EnvironmentContentDTO(org.candlepin.dto.api.v1.EnvironmentDTO.EnvironmentContentDTO) EnvironmentContent(org.candlepin.model.EnvironmentContent) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent)

Example 8 with EnvironmentContent

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

the class EnvironmentTranslatorTest method verifyOutput.

@Override
protected void verifyOutput(Environment source, EnvironmentDTO dto, boolean childrenGenerated) {
    if (source != null) {
        assertEquals(source.getId(), dto.getId());
        assertEquals(source.getName(), dto.getName());
        assertEquals(source.getDescription(), dto.getDescription());
        if (childrenGenerated) {
            this.ownerTranslatorTest.verifyOutput(source.getOwner(), dto.getOwner(), childrenGenerated);
            assertNotNull(dto.getEnvironmentContent());
            for (EnvironmentContent ec : source.getEnvironmentContent()) {
                for (EnvironmentContentDTO ecdto : dto.getEnvironmentContent()) {
                    Content content = ec.getContent();
                    ContentDTO cdto = ecdto.getContent();
                    assertNotNull(cdto);
                    assertNotNull(cdto.getUuid());
                    if (cdto.getUuid().equals(content.getUuid())) {
                        assertEquals(ec.getEnabled(), ecdto.isEnabled());
                        // Pass the content off to the ContentTranslatorTest to verify it
                        this.contentTranslatorTest.verifyOutput(content, cdto, childrenGenerated);
                    }
                }
            }
        } else {
            assertNull(dto.getEnvironmentContent());
            assertNull(dto.getOwner());
        }
    } else {
        assertNull(dto);
    }
}
Also used : EnvironmentContentDTO(org.candlepin.dto.api.v1.EnvironmentDTO.EnvironmentContentDTO) EnvironmentContentDTO(org.candlepin.dto.api.v1.EnvironmentDTO.EnvironmentContentDTO) EnvironmentContent(org.candlepin.model.EnvironmentContent) Content(org.candlepin.model.Content) EnvironmentContent(org.candlepin.model.EnvironmentContent)

Example 9 with EnvironmentContent

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

the class EnvironmentResource method demoteContent.

@ApiOperation(notes = "Demotes a Content from an Environment. Consumer's registered to " + "this environment will no see this content in their entitlement certificates. (after" + " they are regenerated and synced to clients) This call accepts multiple content IDs" + " to demote at once, allowing us to mass demote, then trigger a cert regeneration." + " NOTE: This call expects the actual content IDs, *not* the ID created for each " + "EnvironmentContent object created after a promotion. This is to help integrate " + "with other management apps which should not have to track/lookup a specific ID " + "for the content to demote.", value = "demoteContent")
@ApiResponses({ @ApiResponse(code = 404, message = "When the content has already been demoted.") })
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Path("/{env_id}/content")
public JobDetail demoteContent(@PathParam("env_id") @Verify(Environment.class) String envId, @QueryParam("content") String[] contentIds, @QueryParam("lazy_regen") @DefaultValue("true") Boolean lazyRegen) {
    Environment e = lookupEnvironment(envId);
    Map<String, EnvironmentContent> demotedContent = new HashMap<>();
    // Step through and validate all given content IDs before deleting
    for (String contentId : contentIds) {
        EnvironmentContent envContent = envContentCurator.lookupByEnvironmentAndContent(e, contentId);
        if (envContent == null) {
            throw new NotFoundException(i18n.tr("Content does not exist in environment: {0}", contentId));
        }
        demotedContent.put(contentId, envContent);
    }
    try {
        envContentCurator.bulkDeleteTransactional(new ArrayList<>(demotedContent.values()));
        clearContentAccessCerts(e);
    } catch (RollbackException hibernateException) {
        if (rdbmsExceptionTranslator.isUpdateHadNoEffectException(hibernateException)) {
            log.info("Concurrent content demotion will cause this request to fail.", hibernateException);
            throw new NotFoundException(i18n.tr("One of the content does not exist in the environment anymore: {0}", demotedContent.values()));
        } else {
            throw hibernateException;
        }
    }
    // Impl note: Unfortunately, we have to make an additional set here, as the keySet isn't
    // serializable. Attempting to use it causes exceptions.
    Set<String> demotedContentIds = new HashSet<>(demotedContent.keySet());
    JobDataMap map = new JobDataMap();
    map.put(RegenEnvEntitlementCertsJob.ENV, e);
    map.put(RegenEnvEntitlementCertsJob.CONTENT, demotedContentIds);
    map.put(RegenEnvEntitlementCertsJob.LAZY_REGEN, lazyRegen);
    JobDetail detail = newJob(RegenEnvEntitlementCertsJob.class).withIdentity("regen_entitlement_cert_of_env" + Util.generateUUID()).usingJobData(map).build();
    return detail;
}
Also used : JobDataMap(org.quartz.JobDataMap) HashMap(java.util.HashMap) NotFoundException(org.candlepin.common.exceptions.NotFoundException) EnvironmentContent(org.candlepin.model.EnvironmentContent) RollbackException(javax.persistence.RollbackException) JobDetail(org.quartz.JobDetail) RegenEnvEntitlementCertsJob(org.candlepin.pinsetter.tasks.RegenEnvEntitlementCertsJob) Environment(org.candlepin.model.Environment) HashSet(java.util.HashSet) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 10 with EnvironmentContent

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

the class EnvironmentResource method batchCreate.

/**
 * To make promotion transactional
 * @param contentToPromote
 * @param env
 * @return contentIds Ids of the promoted content
 */
@Transactional
public Set<String> batchCreate(List<org.candlepin.model.dto.EnvironmentContent> contentToPromote, Environment env) {
    Set<String> contentIds = new HashSet<>();
    for (org.candlepin.model.dto.EnvironmentContent promoteMe : contentToPromote) {
        // Make sure the content exists:
        EnvironmentContent envcontent = new EnvironmentContent();
        envcontent.setEnvironment(env);
        envcontent.setContent(this.resolveContent(env, promoteMe.getContentId()));
        envcontent.setEnabled(promoteMe.getEnabled());
        envContentCurator.create(envcontent);
        env.getEnvironmentContent().add(envcontent);
        contentIds.add(promoteMe.getContentId());
    }
    return contentIds;
}
Also used : EnvironmentContent(org.candlepin.model.EnvironmentContent) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Aggregations

EnvironmentContent (org.candlepin.model.EnvironmentContent)17 HashSet (java.util.HashSet)9 Environment (org.candlepin.model.Environment)9 HashMap (java.util.HashMap)6 Content (org.candlepin.model.Content)6 Date (java.util.Date)4 Test (org.junit.Test)4 Matchers.anyString (org.mockito.Matchers.anyString)4 KeyPair (java.security.KeyPair)3 Entitlement (org.candlepin.model.Entitlement)3 Product (org.candlepin.model.Product)3 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 BigInteger (java.math.BigInteger)2 X509Certificate (java.security.cert.X509Certificate)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 Set (java.util.Set)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2