Search in sources :

Example 11 with Environment

use of org.candlepin.model.Environment 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 12 with Environment

use of org.candlepin.model.Environment 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 13 with Environment

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

the class EnvironmentResource method deleteEnv.

@ApiOperation(notes = "Deletes an environment. WARNING: this will delete all consumers in the environment and " + "revoke their entitlement certificates.", value = "deleteEnv")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@DELETE
@Produces(MediaType.WILDCARD)
@Path("/{env_id}")
public void deleteEnv(@PathParam("env_id") @Verify(Environment.class) String envId) {
    Environment e = envCurator.find(envId);
    if (e == null) {
        throw new NotFoundException(i18n.tr("No such environment: {0}", envId));
    }
    CandlepinQuery<Consumer> consumers = this.envCurator.getEnvironmentConsumers(e);
    // Cleanup all consumers and their entitlements:
    log.info("Deleting consumers in environment {}", e);
    for (Consumer c : consumers.list()) {
        log.info("Deleting consumer: {}", c);
        // We're about to delete these consumers; no need to regen/dirty their dependent
        // entitlements or recalculate status.
        poolManager.revokeAllEntitlements(c, false);
        consumerCurator.delete(c);
    }
    log.info("Deleting environment: {}", e);
    envCurator.delete(e);
}
Also used : Consumer(org.candlepin.model.Consumer) Environment(org.candlepin.model.Environment) NotFoundException(org.candlepin.common.exceptions.NotFoundException) 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 14 with Environment

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

the class DefaultEntitlementCertServiceAdapterTest method testPrefixExpandsEnvIfConsumerHasOne.

@Test
@SuppressWarnings("checkstyle:indentation")
public void testPrefixExpandsEnvIfConsumerHasOne() throws Exception {
    owner.setContentPrefix("/someorg/$env/");
    // Setup an environment for the consumer:
    Environment e = this.mockEnvironment(new Environment("env1", "Awesome Environment #1", owner));
    e.getEnvironmentContent().add(new EnvironmentContent(e, content, true));
    this.consumer.setEnvironment(e);
    certServiceAdapter.createX509Certificate(consumer, owner, pool, entitlement, product, new HashSet<>(), getProductModels(product, new HashSet<>(), "prefix", entitlement), new BigInteger("1234"), keyPair, true);
    verify(mockedPKI).createX509Certificate(any(String.class), argThat(new ListContainsContentUrl("/someorg/Awesome+Environment+%231" + CONTENT_URL, CONTENT_ID)), any(Set.class), any(Date.class), any(Date.class), any(KeyPair.class), any(BigInteger.class), any(String.class));
}
Also used : KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) Set(java.util.Set) HashSet(java.util.HashSet) Environment(org.candlepin.model.Environment) BigInteger(java.math.BigInteger) EnvironmentContent(org.candlepin.model.EnvironmentContent) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with Environment

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

the class DefaultEntitlementCertServiceAdapterTest method testURLEncoding.

@Test
@SuppressWarnings("checkstyle:indentation")
public void testURLEncoding() throws Exception {
    owner.setContentPrefix("/some org/$env/");
    // Setup an environment for the consumer:
    Environment e = this.mockEnvironment(new Environment("env1", "Awesome Environment #1", owner));
    e.getEnvironmentContent().add(new EnvironmentContent(e, content, true));
    this.consumer.setEnvironment(e);
    certServiceAdapter.createX509Certificate(consumer, owner, pool, entitlement, product, new HashSet<>(), getProductModels(product, new HashSet<>(), "prefix", entitlement), new BigInteger("1234"), keyPair, true);
    verify(mockedPKI).createX509Certificate(any(String.class), argThat(new ListContainsContentUrl("/some+org/Awesome+Environment+%231" + CONTENT_URL, CONTENT_ID)), any(Set.class), any(Date.class), any(Date.class), any(KeyPair.class), any(BigInteger.class), any(String.class));
}
Also used : KeyPair(java.security.KeyPair) PEMKeyPair(org.bouncycastle.openssl.PEMKeyPair) Set(java.util.Set) HashSet(java.util.HashSet) Environment(org.candlepin.model.Environment) BigInteger(java.math.BigInteger) EnvironmentContent(org.candlepin.model.EnvironmentContent) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Environment (org.candlepin.model.Environment)29 EnvironmentContent (org.candlepin.model.EnvironmentContent)10 Test (org.junit.Test)9 HashSet (java.util.HashSet)8 Owner (org.candlepin.model.Owner)7 ApiOperation (io.swagger.annotations.ApiOperation)6 HashMap (java.util.HashMap)6 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 Content (org.candlepin.model.Content)6 ApiResponses (io.swagger.annotations.ApiResponses)5 Date (java.util.Date)5 Consumer (org.candlepin.model.Consumer)5 NotFoundException (org.candlepin.common.exceptions.NotFoundException)4 ConsumerCapability (org.candlepin.model.ConsumerCapability)4 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)4 ConsumerType (org.candlepin.model.ConsumerType)4 Release (org.candlepin.model.Release)4 Matchers.anyString (org.mockito.Matchers.anyString)4 KeyPair (java.security.KeyPair)3