Search in sources :

Example 56 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class ConsumerService method assignAppRoleToConsumer.

@Transactional
public ConsumerRole assignAppRoleToConsumer(String token, String appId) {
    Long consumerId = getConsumerIdByToken(token);
    if (consumerId == null) {
        throw new BadRequestException("Token is Illegal");
    }
    Role masterRole = rolePermissionService.findRoleByRoleName(RoleUtils.buildAppMasterRoleName(appId));
    if (masterRole == null) {
        throw new BadRequestException("App's role does not exist. Please check whether app has created.");
    }
    long roleId = masterRole.getId();
    ConsumerRole managedModifyRole = consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, roleId);
    if (managedModifyRole != null) {
        return managedModifyRole;
    }
    String operator = userInfoHolder.getUser().getUserId();
    ConsumerRole consumerRole = createConsumerRole(consumerId, roleId, operator);
    return consumerRoleRepository.save(consumerRole);
}
Also used : Role(com.ctrip.framework.apollo.portal.entity.po.Role) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) ConsumerRole(com.ctrip.framework.apollo.openapi.entity.ConsumerRole) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 57 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class ReleaseService method rollbackTo.

@Transactional
public Release rollbackTo(long releaseId, long toReleaseId, String operator) {
    if (releaseId == toReleaseId) {
        throw new BadRequestException("current release equal to target release");
    }
    Release release = findOne(releaseId);
    Release toRelease = findOne(toReleaseId);
    if (release == null || toRelease == null) {
        throw new NotFoundException("release not found");
    }
    if (release.isAbandoned() || toRelease.isAbandoned()) {
        throw new BadRequestException("release is not active");
    }
    String appId = release.getAppId();
    String clusterName = release.getClusterName();
    String namespaceName = release.getNamespaceName();
    List<Release> releases = findActiveReleasesBetween(appId, clusterName, namespaceName, toReleaseId, releaseId);
    for (int i = 0; i < releases.size() - 1; i++) {
        releases.get(i).setAbandoned(true);
        releases.get(i).setDataChangeLastModifiedBy(operator);
    }
    releaseRepository.saveAll(releases);
    releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, clusterName, toReleaseId, release.getId(), ReleaseOperation.ROLLBACK, null, operator);
    // publish child namespace if namespace has child
    rollbackChildNamespace(appId, clusterName, namespaceName, Lists.newArrayList(release, toRelease), operator);
    return release;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Release(com.ctrip.framework.apollo.biz.entity.Release) Transactional(org.springframework.transaction.annotation.Transactional)

Example 58 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AccessKeyService method create.

@Transactional
public AccessKey create(String appId, AccessKey entity) {
    long count = accessKeyRepository.countByAppId(appId);
    if (count >= ACCESSKEY_COUNT_LIMIT) {
        throw new BadRequestException("AccessKeys count limit exceeded");
    }
    entity.setId(0L);
    entity.setAppId(appId);
    entity.setDataChangeLastModifiedBy(entity.getDataChangeCreatedBy());
    AccessKey accessKey = accessKeyRepository.save(entity);
    auditService.audit(AccessKey.class.getSimpleName(), accessKey.getId(), Audit.OP.INSERT, accessKey.getDataChangeCreatedBy());
    return accessKey;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey) Transactional(org.springframework.transaction.annotation.Transactional)

Example 59 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class AccessKeyService method delete.

@Transactional
public void delete(String appId, long id, String operator) {
    AccessKey accessKey = accessKeyRepository.findOneByAppIdAndId(appId, id);
    if (accessKey == null) {
        throw new BadRequestException("AccessKey not exist");
    }
    if (accessKey.isEnabled()) {
        throw new BadRequestException("AccessKey should disable first");
    }
    accessKey.setDeleted(Boolean.TRUE);
    accessKey.setDataChangeLastModifiedBy(operator);
    accessKeyRepository.save(accessKey);
    auditService.audit(AccessKey.class.getSimpleName(), id, Audit.OP.DELETE, operator);
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AccessKey(com.ctrip.framework.apollo.biz.entity.AccessKey) Transactional(org.springframework.transaction.annotation.Transactional)

Example 60 with BadRequestException

use of com.ctrip.framework.apollo.common.exception.BadRequestException in project apollo by ctripcorp.

the class ClusterService method saveWithoutInstanceOfAppNamespaces.

@Transactional
public Cluster saveWithoutInstanceOfAppNamespaces(Cluster entity) {
    if (!isClusterNameUnique(entity.getAppId(), entity.getName())) {
        throw new BadRequestException("cluster not unique");
    }
    // protection
    entity.setId(0);
    Cluster cluster = clusterRepository.save(entity);
    auditService.audit(Cluster.class.getSimpleName(), cluster.getId(), Audit.OP.INSERT, cluster.getDataChangeCreatedBy());
    return cluster;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)74 Transactional (org.springframework.transaction.annotation.Transactional)20 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)16 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)16 PostMapping (org.springframework.web.bind.annotation.PostMapping)14 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)11 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)9 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)8 App (com.ctrip.framework.apollo.common.entity.App)8 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)6 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)6 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)6 Item (com.ctrip.framework.apollo.biz.entity.Item)5 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)4 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 AccessKey (com.ctrip.framework.apollo.biz.entity.AccessKey)3 Release (com.ctrip.framework.apollo.biz.entity.Release)3 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)3