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