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()));
}
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);
}
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);
}
}
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;
}
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);
}
Aggregations