Search in sources :

Example 11 with NotFoundException

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

the class PoolResource method deletePool.

@ApiOperation(notes = "Remove a Pool", value = "deletePool")
@ApiResponses({ @ApiResponse(code = 404, message = "if the pool with the specified id is not found") })
@DELETE
@Path("/{pool_id}")
@Produces(MediaType.APPLICATION_JSON)
public void deletePool(@PathParam("pool_id") String id) {
    Pool pool = poolManager.find(id);
    if (pool == null) {
        throw new NotFoundException(i18n.tr("Entitlement Pool with ID \"{0}\" could not be found.", id));
    }
    if (pool.getType() != PoolType.NORMAL) {
        throw new BadRequestException(i18n.tr("Cannot delete bonus pools, as they are auto generated"));
    }
    if (pool.isCreatedByShare()) {
        throw new BadRequestException(i18n.tr("Cannot delete shared pools, This should be triggered by" + " deleting the share entitlement instead"));
    }
    poolManager.deletePools(Collections.singleton(pool));
}
Also used : NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Pool(org.candlepin.model.Pool) 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 12 with NotFoundException

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

the class RoleResource method createRole.

@ApiOperation(notes = "Creates a Role", value = "createRole")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Role createRole(@ApiParam(name = "role", required = true) Role role) {
    // Attach actual owner objects to each incoming permission:
    for (PermissionBlueprint p : role.getPermissions()) {
        Owner temp = p.getOwner();
        Owner actual = ownerCurator.lookupByKey(temp.getKey());
        if (actual == null) {
            throw new NotFoundException(i18n.tr("No such owner: {0}", temp.getKey()));
        }
        p.setOwner(actual);
    }
    Role r = this.userService.createRole(role);
    return r;
}
Also used : Role(org.candlepin.model.Role) Owner(org.candlepin.model.Owner) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) NotFoundException(org.candlepin.common.exceptions.NotFoundException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with NotFoundException

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

the class RoleResource method removeRolePermission.

@ApiOperation(notes = "Removes a Permission from a Role. Returns the updated Role.", value = "removeRolePermission")
@ApiResponses({ @ApiResponse(code = 404, message = "") })
@DELETE
@Path("{role_id}/permissions/{perm_id}")
@Produces(MediaType.APPLICATION_JSON)
public Role removeRolePermission(@PathParam("role_id") String roleId, @PathParam("perm_id") String permissionId) {
    Role existingRole = lookupRole(roleId);
    Set<PermissionBlueprint> picks = new HashSet<>();
    boolean found = false;
    PermissionBlueprint toRemove = null;
    for (PermissionBlueprint op : existingRole.getPermissions()) {
        if (!op.getId().equals(permissionId)) {
            picks.add(op);
        } else {
            found = true;
            toRemove = op;
        }
    }
    if (!found) {
        throw new NotFoundException(i18n.tr("No such permission: {0} in role: {1}", permissionId, roleId));
    }
    existingRole.setPermissions(picks);
    Role r = this.userService.updateRole(existingRole);
    toRemove.setOwner(null);
    permissionCurator.delete(toRemove);
    return r;
}
Also used : Role(org.candlepin.model.Role) PermissionBlueprint(org.candlepin.model.PermissionBlueprint) NotFoundException(org.candlepin.common.exceptions.NotFoundException) 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 14 with NotFoundException

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

the class ConsumerResource method setupOwner.

private Owner setupOwner(Principal principal, String ownerKey) {
    // has admin rights for. If more than one, we have to error out.
    if (ownerKey == null && (principal instanceof UserPrincipal)) {
        // check for this cast?
        List<String> ownerKeys = ((UserPrincipal) principal).getOwnerKeys();
        if (ownerKeys.size() != 1) {
            throw new BadRequestException(i18n.tr("You must specify an organization for new units."));
        }
        ownerKey = ownerKeys.get(0);
    }
    createOwnerIfNeeded(principal);
    Owner owner = ownerCurator.lookupByKey(ownerKey);
    if (owner == null) {
        throw new BadRequestException(i18n.tr("Organization {0} does not exist.", ownerKey));
    }
    // Check permissions for current principal on the owner:
    if ((principal instanceof UserPrincipal) && !principal.canAccess(owner, SubResource.CONSUMERS, Access.CREATE)) {
        log.warn("User {} does not have access to create consumers in org {}", principal.getPrincipalName(), owner.getKey());
        throw new NotFoundException(i18n.tr("owner with key: {0} was not found.", owner.getKey()));
    }
    return owner;
}
Also used : Owner(org.candlepin.model.Owner) BadRequestException(org.candlepin.common.exceptions.BadRequestException) NotFoundException(org.candlepin.common.exceptions.NotFoundException) UserPrincipal(org.candlepin.auth.UserPrincipal)

Example 15 with NotFoundException

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

the class ConsumerResource method performConsumerUpdates.

@Transactional
public boolean performConsumerUpdates(ConsumerDTO updated, Consumer toUpdate, GuestMigration guestMigration, boolean isIdCert) {
    log.debug("Updating consumer: {}", toUpdate.getUuid());
    // We need a representation of the consumer before making any modifications.
    // If nothing changes we won't send.  The new entity needs to be correct though,
    // so we should get a Jsonstring now, and finish it off if we're going to send
    EventBuilder eventBuilder = eventFactory.getEventBuilder(Target.CONSUMER, Type.MODIFIED).setEventData(toUpdate);
    // version changed on non-checked in consumer, or list of capabilities
    // changed on checked in consumer
    boolean changesMade = updateCapabilities(toUpdate, updated);
    changesMade = checkForFactsUpdate(toUpdate, updated) || changesMade;
    changesMade = checkForInstalledProductsUpdate(toUpdate, updated) || changesMade;
    changesMade = checkForHypervisorIdUpdate(toUpdate, updated) || changesMade;
    changesMade = guestMigration.isMigrationPending() || changesMade;
    if (updated.getContentTags() != null && !updated.getContentTags().equals(toUpdate.getContentTags())) {
        log.info("   Updating content tags.");
        toUpdate.setContentTags(updated.getContentTags());
        changesMade = true;
    }
    // Allow optional setting of the autoheal attribute:
    if (updated.getAutoheal() != null && !updated.getAutoheal().equals(toUpdate.isAutoheal())) {
        log.info("   Updating consumer autoheal setting.");
        toUpdate.setAutoheal(updated.getAutoheal());
        changesMade = true;
    }
    if (updated.getReleaseVersion() != null && !updated.getReleaseVersion().equals(toUpdate.getReleaseVer() == null ? null : toUpdate.getReleaseVer().getReleaseVer())) {
        log.info("   Updating consumer releaseVer setting.");
        toUpdate.setReleaseVer(new Release(updated.getReleaseVersion()));
        changesMade = true;
    }
    // Allow optional setting of the service level attribute:
    String level = updated.getServiceLevel();
    if (level != null && !level.equals(toUpdate.getServiceLevel())) {
        log.info("   Updating consumer service level setting.");
        consumerBindUtil.validateServiceLevel(toUpdate.getOwnerId(), level);
        toUpdate.setServiceLevel(level);
        changesMade = true;
    }
    String environmentId = updated.getEnvironment() == null ? null : updated.getEnvironment().getId();
    if (environmentId != null && (toUpdate.getEnvironmentId() == null || !toUpdate.getEnvironmentId().equals(environmentId))) {
        Environment e = environmentCurator.find(environmentId);
        if (e == null) {
            throw new NotFoundException(i18n.tr("Environment with ID \"{0}\" could not be found.", environmentId));
        }
        log.info("Updating environment to: {}", environmentId);
        toUpdate.setEnvironment(e);
        // lazily regenerate certs, so the client can still work
        poolManager.regenerateCertificatesOf(toUpdate, true);
        changesMade = true;
    }
    // it should remain the same
    if (updated.getName() != null && !toUpdate.getName().equals(updated.getName())) {
        checkConsumerName(updated);
        log.info("Updating consumer name: {} -> {}", toUpdate.getName(), updated.getName());
        toUpdate.setName(updated.getName());
        changesMade = true;
        // get the new name into the id cert if we are using the cert
        if (isIdCert) {
            IdentityCertificate ic = generateIdCert(toUpdate, true);
            toUpdate.setIdCert(ic);
        }
    }
    ConsumerType ctype = this.consumerTypeCurator.getConsumerType(toUpdate);
    if (updated.getContentAccessMode() != null && !updated.getContentAccessMode().equals(toUpdate.getContentAccessMode()) && ctype.isManifest()) {
        Owner toUpdateOwner = ownerCurator.findOwnerById(toUpdate.getOwnerId());
        if (!toUpdateOwner.isAllowedContentAccessMode(updated.getContentAccessMode())) {
            throw new BadRequestException(i18n.tr("The consumer cannot use the supplied content access mode."));
        }
        toUpdate.setContentAccessMode(updated.getContentAccessMode());
        changesMade = true;
    }
    if (!StringUtils.isEmpty(updated.getContentAccessMode()) && !ctype.isManifest()) {
        throw new BadRequestException(i18n.tr("The consumer cannot be assigned a content access mode."));
    }
    if (updated.getLastCheckin() != null) {
        log.info("Updating to specific last checkin time: {}", updated.getLastCheckin());
        toUpdate.setLastCheckin(updated.getLastCheckin());
        changesMade = true;
    }
    if (changesMade) {
        log.debug("Consumer {} updated.", toUpdate.getUuid());
        // Set the updated date here b/c @PreUpdate will not get fired
        // since only the facts table will receive the update.
        toUpdate.setUpdated(new Date());
        // this should update compliance on toUpdate, but not call the curator
        complianceRules.getStatus(toUpdate, null, false, false);
        Event event = eventBuilder.setEventData(toUpdate).buildEvent();
        sink.queueEvent(event);
    }
    return changesMade;
}
Also used : Owner(org.candlepin.model.Owner) EventBuilder(org.candlepin.audit.EventBuilder) Environment(org.candlepin.model.Environment) NotFoundException(org.candlepin.common.exceptions.NotFoundException) BadRequestException(org.candlepin.common.exceptions.BadRequestException) Event(org.candlepin.audit.Event) ConsumerType(org.candlepin.model.ConsumerType) Release(org.candlepin.model.Release) Date(java.util.Date) IdentityCertificate(org.candlepin.model.IdentityCertificate) Transactional(com.google.inject.persist.Transactional)

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