Search in sources :

Example 6 with BadRequestException

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

the class ClusterController method create.

@RequestMapping(path = "/apps/{appId}/clusters", method = RequestMethod.POST)
public ClusterDTO create(@PathVariable("appId") String appId, @RequestParam(value = "autoCreatePrivateNamespace", defaultValue = "true") boolean autoCreatePrivateNamespace, @RequestBody ClusterDTO dto) {
    if (!InputValidator.isValidClusterNamespace(dto.getName())) {
        throw new BadRequestException(String.format("Cluster格式错误: %s", InputValidator.INVALID_CLUSTER_NAMESPACE_MESSAGE));
    }
    Cluster entity = BeanUtils.transfrom(Cluster.class, dto);
    Cluster managedEntity = clusterService.findOne(appId, entity.getName());
    if (managedEntity != null) {
        throw new BadRequestException("cluster already exist.");
    }
    if (autoCreatePrivateNamespace) {
        entity = clusterService.saveWithInstanceOfAppNamespaces(entity);
    } else {
        entity = clusterService.saveWithoutInstanceOfAppNamespaces(entity);
    }
    dto = BeanUtils.transfrom(ClusterDTO.class, entity);
    return dto;
}
Also used : ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with BadRequestException

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

the class NamespaceBranchService method createBranch.

@Transactional
public Namespace createBranch(String appId, String parentClusterName, String namespaceName, String operator) {
    Namespace childNamespace = findBranch(appId, parentClusterName, namespaceName);
    if (childNamespace != null) {
        throw new BadRequestException("namespace already has branch");
    }
    Cluster parentCluster = clusterService.findOne(appId, parentClusterName);
    if (parentCluster == null || parentCluster.getParentClusterId() != 0) {
        throw new BadRequestException("cluster not exist or illegal cluster");
    }
    // create child cluster
    Cluster childCluster = createChildCluster(appId, parentCluster, namespaceName, operator);
    Cluster createdChildCluster = clusterService.saveWithoutInstanceOfAppNamespaces(childCluster);
    // create child namespace
    childNamespace = createNamespaceBranch(appId, createdChildCluster.getName(), namespaceName, operator);
    return namespaceService.save(childNamespace);
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Cluster(com.ctrip.framework.apollo.biz.entity.Cluster) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with BadRequestException

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

the class NamespaceService method findPublicNamespaceForAssociatedNamespace.

public Namespace findPublicNamespaceForAssociatedNamespace(String clusterName, String namespaceName) {
    AppNamespace appNamespace = appNamespaceService.findPublicNamespaceByName(namespaceName);
    if (appNamespace == null) {
        throw new BadRequestException("namespace not exist");
    }
    String appId = appNamespace.getAppId();
    Namespace namespace = findOne(appId, clusterName, namespaceName);
    // default cluster's namespace
    if (Objects.equals(clusterName, ConfigConsts.CLUSTER_NAME_DEFAULT)) {
        return namespace;
    }
    // return default cluster's namespace
    if (namespace == null) {
        return findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
    }
    // custom cluster's namespace exist and has published.
    // return custom cluster's namespace
    Release latestActiveRelease = releaseService.findLatestActiveRelease(namespace);
    if (latestActiveRelease != null) {
        return namespace;
    }
    Namespace defaultNamespace = findOne(appId, ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName);
    // return custom cluster's namespace
    if (defaultNamespace == null) {
        return namespace;
    }
    // custom cluster's namespace exist but never published.
    // and default cluster's namespace exist and has published.
    // return default cluster's namespace
    Release defaultNamespaceLatestActiveRelease = releaseService.findLatestActiveRelease(defaultNamespace);
    if (defaultNamespaceLatestActiveRelease != null) {
        return defaultNamespace;
    }
    // return custom cluster's namespace
    return namespace;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Release(com.ctrip.framework.apollo.biz.entity.Release)

Example 9 with BadRequestException

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

the class NamespaceService method findPublicAppNamespaceAllNamespaces.

public List<Namespace> findPublicAppNamespaceAllNamespaces(String namespaceName, Pageable page) {
    AppNamespace publicAppNamespace = appNamespaceService.findPublicNamespaceByName(namespaceName);
    if (publicAppNamespace == null) {
        throw new BadRequestException(String.format("Public appNamespace not exists. NamespaceName = %s", namespaceName));
    }
    List<Namespace> namespaces = namespaceRepository.findByNamespaceName(namespaceName, page);
    return filterChildNamespace(namespaces);
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Example 10 with BadRequestException

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

the class ReleaseService method rollback.

@Transactional
public Release rollback(long releaseId, String operator) {
    Release release = findOne(releaseId);
    if (release == null) {
        throw new NotFoundException("release not found");
    }
    if (release.isAbandoned()) {
        throw new BadRequestException("release is not active");
    }
    String appId = release.getAppId();
    String clusterName = release.getClusterName();
    String namespaceName = release.getNamespaceName();
    PageRequest page = new PageRequest(0, 2);
    List<Release> twoLatestActiveReleases = findActiveReleases(appId, clusterName, namespaceName, page);
    if (twoLatestActiveReleases == null || twoLatestActiveReleases.size() < 2) {
        throw new BadRequestException(String.format("Can't rollback namespace(appId=%s, clusterName=%s, namespaceName=%s) because there is only one active release", appId, clusterName, namespaceName));
    }
    release.setAbandoned(true);
    release.setDataChangeLastModifiedBy(operator);
    releaseRepository.save(release);
    releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, clusterName, twoLatestActiveReleases.get(1).getId(), release.getId(), ReleaseOperation.ROLLBACK, null, operator);
    // publish child namespace if namespace has child
    rollbackChildNamespace(appId, clusterName, namespaceName, twoLatestActiveReleases, operator);
    return release;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Release(com.ctrip.framework.apollo.biz.entity.Release) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)46 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 Transactional (org.springframework.transaction.annotation.Transactional)12 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)9 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)7 App (com.ctrip.framework.apollo.common.entity.App)6 Cluster (com.ctrip.framework.apollo.biz.entity.Cluster)5 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)5 Item (com.ctrip.framework.apollo.biz.entity.Item)4 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)4 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)3 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)3 ConfigPublishEvent (com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent)3 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)2 Commit (com.ctrip.framework.apollo.biz.entity.Commit)2 NamespaceLock (com.ctrip.framework.apollo.biz.entity.NamespaceLock)2 Release (com.ctrip.framework.apollo.biz.entity.Release)2