use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class GuestIdResource method updateGuest.
@ApiOperation(notes = "Updates a single Guest on a Consumer. Allows virt-who to avoid uploading" + " an entire list of guests", value = "updateGuest")
@PUT
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{guest_id}")
public void updateGuest(@ApiParam("consumer who owns or hosts the guest in question") @PathParam("consumer_uuid") @Verify(Consumer.class) String consumerUuid, @ApiParam("guest virtual uuid") @PathParam("guest_id") String guestId, @ApiParam(name = "updated", required = true, value = "updated guest data to use") GuestIdDTO updatedDTO) {
// I'm not sure this can happen
if (guestId == null || guestId.isEmpty()) {
throw new BadRequestException(i18n.tr("Please supply a valid guest id"));
}
if (updatedDTO == null) {
// If they're not sending attributes, we can get the guestId from the url
updatedDTO = new GuestIdDTO().setGuestId(guestId);
}
// Allow the id to be left out in this case, we can use the path param
if (updatedDTO.getGuestId() == null) {
updatedDTO.setGuestId(guestId);
}
// If the guest uuids do not match, something is wrong
if (!guestId.equalsIgnoreCase(updatedDTO.getGuestId())) {
throw new BadRequestException(i18n.tr("Guest ID in json \"{0}\" does not match path guest ID \"{1}\"", updatedDTO.getGuestId(), guestId));
}
Consumer consumer = consumerCurator.verifyAndLookupConsumer(consumerUuid);
GuestId guestIdEntity = new GuestId();
populateEntity(guestIdEntity, updatedDTO);
guestIdEntity.setConsumer(consumer);
GuestId toUpdate = guestIdCurator.findByGuestIdAndOrg(guestId, consumer.getOwnerId());
if (toUpdate != null) {
guestIdEntity.setId(toUpdate.getId());
}
guestIdCurator.merge(guestIdEntity);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class ConsumerTypeResource method create.
@ApiOperation(notes = "Creates a Consumer Type", value = "create")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ConsumerTypeDTO create(@ApiParam(name = "consumerType", required = true) ConsumerTypeDTO dto) throws BadRequestException {
try {
ConsumerType type = new ConsumerType();
this.populateEntity(type, dto);
type = consumerTypeCurator.create(type);
return this.translator.translate(type, ConsumerTypeDTO.class);
} catch (Exception e) {
log.error("Problem creating unit type: ", e);
throw new BadRequestException(i18n.tr("Problem creating unit type: {0}", dto));
}
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class EntitlementResource method updateEntitlement.
@ApiOperation(notes = "Updates an Entitlement. This only works for the quantity.", value = "updateEntitlement")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("{entitlement_id}")
public void updateEntitlement(@PathParam("entitlement_id") @Verify(Entitlement.class) String id, @ApiParam(name = "update", required = true) EntitlementDTO update) {
// Check that quantity param was set and is not 0:
if (update.getQuantity() <= 0) {
throw new BadRequestException(i18n.tr("Quantity value must be greater than 0."));
}
// Verify entitlement exists:
Entitlement entitlement = entitlementCurator.find(id);
if (entitlement != null) {
// make sure that this will be a change
if (!entitlement.getQuantity().equals(update.getQuantity())) {
Consumer consumer = entitlement.getConsumer();
entitler.adjustEntitlementQuantity(consumer, entitlement, update.getQuantity());
}
} else {
throw new NotFoundException(i18n.tr("Entitlement with ID \"{0}\" could not be found.", id));
}
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class HypervisorResource method hypervisorUpdateAsync.
@ApiOperation(notes = "Creates or Updates the list of Hypervisor hosts Allows agents such" + " as virt-who to update hosts' information . This is typically used when a host is" + " unable to register to candlepin via subscription manager. In situations where " + "consumers already exist it is probably best not to allow creation of new hypervisor" + " consumers. Most consumers do not have a hypervisorId attribute, so that should be" + " added manually when necessary by the management environment. Default is true. " + "If false is specified, hypervisorIds that are not found will result in a failed " + "state of the job.", value = "hypervisorUpdateAsync")
@ApiResponses({ @ApiResponse(code = 202, message = "") })
@POST
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@Path("/{owner}")
@UpdateConsumerCheckIn
@SuppressWarnings("checkstyle:indentation")
public JobDetail hypervisorUpdateAsync(String hypervisorJson, @Context Principal principal, @PathParam("owner") @Verify(value = Owner.class, require = Access.READ_ONLY, subResource = SubResource.HYPERVISOR) String ownerKey, @ApiParam("specify whether or not to create missing hypervisors." + "Default is true. If false is specified, hypervisorIds that are not found" + "will result in failed entries in the resulting HypervisorCheckInResult") @QueryParam("create_missing") @DefaultValue("true") boolean createMissing, @QueryParam("reporter_id") String reporterId) {
if (hypervisorJson == null || hypervisorJson.isEmpty()) {
log.debug("Host/Guest mapping provided during hypervisor update was null.");
throw new BadRequestException(i18n.tr("Host to guest mapping was not provided for hypervisor update."));
}
log.info("Hypervisor update by principal: " + principal);
Owner owner = this.getOwner(ownerKey);
return HypervisorUpdateJob.forOwner(owner, hypervisorJson, createMissing, principal, reporterId);
}
use of org.candlepin.common.exceptions.BadRequestException in project candlepin by candlepin.
the class ActivationKeyResource method addProductIdToKey.
@ApiOperation(notes = "Adds an Product ID to an Activation Key", value = "Add Product ID to key")
@ApiResponses({ @ApiResponse(code = 400, message = "") })
@POST
@Path("{activation_key_id}/product/{product_id}")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.WILDCARD)
public ActivationKeyDTO addProductIdToKey(@PathParam("activation_key_id") @Verify(ActivationKey.class) String activationKeyId, @PathParam("product_id") String productId) {
ActivationKey key = activationKeyCurator.verifyAndLookupKey(activationKeyId);
Product product = confirmProduct(key.getOwner(), productId);
// Make sure we don't try to register the product ID twice.
if (key.hasProduct(product)) {
throw new BadRequestException(i18n.tr("Product ID \"{0}\" has already been registered with this activation key", productId));
}
key.addProduct(product);
activationKeyCurator.update(key);
return this.translator.translate(key, ActivationKeyDTO.class);
}
Aggregations