Search in sources :

Example 16 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class ConsumerResource method unbindBySerial.

@ApiOperation(notes = "Removes an Entitlement from a Consumer By the Certificate Serial", value = "unbindBySerial")
@ApiResponses({ @ApiResponse(code = 403, message = ""), @ApiResponse(code = 404, message = "") })
@DELETE
@Produces(MediaType.WILDCARD)
@Path("/{consumer_uuid}/certificates/{serial}")
public void unbindBySerial(@PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @PathParam("serial") Long serial) {
    consumerCurator.verifyAndLookupConsumer(consumerUuid);
    Entitlement toDelete = entitlementCurator.findByCertificateSerial(serial);
    if (toDelete != null) {
        poolManager.revokeEntitlement(toDelete);
        return;
    }
    throw new NotFoundException(i18n.tr("Entitlement Certificate with serial number \"{0}\" could not be found.", // prevent serial number formatting.
    serial.toString()));
}
Also used : NotFoundException(org.candlepin.common.exceptions.NotFoundException) Entitlement(org.candlepin.model.Entitlement) 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 17 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException in project candlepin by candlepin.

the class ConsumerResource method list.

@ApiOperation(notes = "Retrieves a list of the Consumers", value = "list", response = Consumer.class, responseContainer = "list")
@ApiResponses({ @ApiResponse(code = 400, message = ""), @ApiResponse(code = 404, message = "") })
@GET
@Produces(MediaType.APPLICATION_JSON)
@Wrapped(element = "consumers")
@SuppressWarnings("checkstyle:indentation")
public CandlepinQuery<ConsumerDTO> list(@QueryParam("username") String userName, @QueryParam("type") Set<String> typeLabels, @QueryParam("owner") String ownerKey, @QueryParam("uuid") List<String> uuids, @QueryParam("hypervisor_id") List<String> hypervisorIds, @QueryParam("fact") @CandlepinParam(type = KeyValueParameter.class) List<KeyValueParameter> attrFilters, @Context PageRequest pageRequest) {
    if (userName == null && (typeLabels == null || typeLabels.isEmpty()) && ownerKey == null && (uuids == null || uuids.isEmpty()) && (hypervisorIds == null || hypervisorIds.isEmpty()) && (attrFilters == null || attrFilters.isEmpty())) {
        throw new BadRequestException(i18n.tr("Must specify at least one search criteria."));
    }
    Owner owner = null;
    if (ownerKey != null) {
        owner = ownerCurator.lookupByKey(ownerKey);
        if (owner == null) {
            throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", ownerKey));
        }
    }
    List<ConsumerType> types = consumerTypeValidator.findAndValidateTypeLabels(typeLabels);
    CandlepinQuery<Consumer> query = this.consumerCurator.searchOwnerConsumers(owner, userName, types, uuids, hypervisorIds, attrFilters, Collections.<String>emptyList(), Collections.<String>emptyList(), Collections.<String>emptyList());
    return this.translator.translateQuery(query, ConsumerDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) DeletedConsumer(org.candlepin.model.DeletedConsumer) Consumer(org.candlepin.model.Consumer) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) ConsumerType(org.candlepin.model.ConsumerType) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Wrapped(org.jboss.resteasy.annotations.providers.jaxb.Wrapped) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 18 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException 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 19 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException 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 20 with NotFoundException

use of org.candlepin.common.exceptions.NotFoundException 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)

Aggregations

NotFoundException (org.candlepin.common.exceptions.NotFoundException)44 ApiOperation (io.swagger.annotations.ApiOperation)25 Produces (javax.ws.rs.Produces)25 ApiResponses (io.swagger.annotations.ApiResponses)24 Path (javax.ws.rs.Path)22 BadRequestException (org.candlepin.common.exceptions.BadRequestException)16 Consumer (org.candlepin.model.Consumer)12 Owner (org.candlepin.model.Owner)12 DELETE (javax.ws.rs.DELETE)11 GET (javax.ws.rs.GET)10 Entitlement (org.candlepin.model.Entitlement)10 Pool (org.candlepin.model.Pool)9 ArrayList (java.util.ArrayList)7 Transactional (com.google.inject.persist.Transactional)6 Consumes (javax.ws.rs.Consumes)6 ConsumerType (org.candlepin.model.ConsumerType)6 PUT (javax.ws.rs.PUT)5 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)5 Environment (org.candlepin.model.Environment)5 Test (org.junit.Test)5