Commit 9b6a6985 by lepdou

bugfix public appnamespace unique & repeat create namespace permission

parent 38f02745
...@@ -44,12 +44,17 @@ public class GlobalDefaultExceptionHandler { ...@@ -44,12 +44,17 @@ public class GlobalDefaultExceptionHandler {
return handleError(request, INTERNAL_SERVER_ERROR, ex); return handleError(request, INTERNAL_SERVER_ERROR, ex);
} }
@ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class, BadRequestException.class}) @ExceptionHandler({HttpRequestMethodNotSupportedException.class, HttpMediaTypeException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request,
ServletException ex) { ServletException ex) {
return handleError(request, BAD_REQUEST, ex); return handleError(request, BAD_REQUEST, ex);
} }
@ExceptionHandler({BadRequestException.class})
public ResponseEntity<Map<String, Object>> badRequest(HttpServletRequest request, BadRequestException ex) {
return handleError(request, BAD_REQUEST, ex);
}
@ExceptionHandler(NotFoundException.class) @ExceptionHandler(NotFoundException.class)
public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request, public ResponseEntity<Map<String, Object>> notFound(HttpServletRequest request,
NotFoundException ex) { NotFoundException ex) {
......
...@@ -5,13 +5,18 @@ import com.ctrip.framework.apollo.core.dto.NamespaceDTO; ...@@ -5,13 +5,18 @@ import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.auth.UserInfoHolder; import com.ctrip.framework.apollo.portal.auth.UserInfoHolder;
import com.ctrip.framework.apollo.portal.entity.form.NamespaceCreationModel;
import com.ctrip.framework.apollo.portal.entity.vo.NamespaceVO; import com.ctrip.framework.apollo.portal.entity.vo.NamespaceVO;
import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent; import com.ctrip.framework.apollo.portal.listener.AppNamespaceCreationEvent;
import com.ctrip.framework.apollo.portal.service.NamespaceService; import com.ctrip.framework.apollo.portal.service.NamespaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -21,10 +26,13 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -21,10 +26,13 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.List; import java.util.List;
import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkArgument; import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkArgument;
import static com.ctrip.framework.apollo.portal.util.RequestPrecondition.checkModel;
@RestController @RestController
public class NamespaceController { public class NamespaceController {
Logger logger = LoggerFactory.getLogger(NamespaceController.class);
@Autowired @Autowired
private ApplicationEventPublisher publisher; private ApplicationEventPublisher publisher;
@Autowired @Autowired
...@@ -37,13 +45,24 @@ public class NamespaceController { ...@@ -37,13 +45,24 @@ public class NamespaceController {
return namespaceService.findPublicAppNamespaces(); return namespaceService.findPublicAppNamespaces();
} }
@PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#namespace.appId)") @PreAuthorize(value = "@permissionValidator.hasCreateNamespacePermission(#appId)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces", method = RequestMethod.POST) @RequestMapping(value = "/apps/{appId}/namespaces", method = RequestMethod.POST)
public NamespaceDTO createNamespace(@PathVariable String env, @RequestBody NamespaceDTO namespace) { public ResponseEntity<Void> createNamespace(@PathVariable String appId, @RequestBody List<NamespaceCreationModel> models) {
checkModel(!CollectionUtils.isEmpty(models));
for (NamespaceCreationModel model : models) {
NamespaceDTO namespace = model.getNamespace();
checkArgument(namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName()); checkArgument(model.getEnv(), namespace.getAppId(), namespace.getClusterName(), namespace.getNamespaceName());
return namespaceService.createNamespace(Env.valueOf(env), namespace); try {
namespaceService.createNamespace(Env.valueOf(model.getEnv()), namespace);
} catch (Exception e) {
logger.error("create namespace error.", e);
}
}
return ResponseEntity.ok().build();
} }
@RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST) @RequestMapping(value = "/apps/{appId}/appnamespaces", method = RequestMethod.POST)
......
package com.ctrip.framework.apollo.portal.cumsomize; package com.ctrip.framework.apollo.portal.customize;
import com.ctrip.framework.apollo.common.customize.LoggingCustomizer; import com.ctrip.framework.apollo.common.customize.LoggingCustomizer;
import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository; import com.ctrip.framework.apollo.portal.repository.ServerConfigRepository;
......
/** /**
* 携程内部的日志系统,第三方公司可删除 * 携程内部的日志系统,第三方公司可删除
*/ */
package com.ctrip.framework.apollo.portal.cumsomize; package com.ctrip.framework.apollo.portal.customize;
package com.ctrip.framework.apollo.portal.entity.form;
import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
public class NamespaceCreationModel {
private String env;
private NamespaceDTO namespace;
public String getEnv() {
return env;
}
public void setEnv(String env) {
this.env = env;
}
public NamespaceDTO getNamespace() {
return namespace;
}
public void setNamespace(NamespaceDTO namespace) {
this.namespace = namespace;
}
}
...@@ -11,6 +11,7 @@ import com.ctrip.framework.apollo.core.dto.ItemDTO; ...@@ -11,6 +11,7 @@ import com.ctrip.framework.apollo.core.dto.ItemDTO;
import com.ctrip.framework.apollo.core.dto.NamespaceDTO; import com.ctrip.framework.apollo.core.dto.NamespaceDTO;
import com.ctrip.framework.apollo.core.dto.ReleaseDTO; import com.ctrip.framework.apollo.core.dto.ReleaseDTO;
import com.ctrip.framework.apollo.core.enums.Env; import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.exception.BadRequestException;
import com.ctrip.framework.apollo.core.exception.ServiceException; import com.ctrip.framework.apollo.core.exception.ServiceException;
import com.ctrip.framework.apollo.core.utils.StringUtils; import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.portal.PortalSettings; import com.ctrip.framework.apollo.portal.PortalSettings;
...@@ -97,6 +98,10 @@ public class NamespaceService { ...@@ -97,6 +98,10 @@ public class NamespaceService {
@Transactional @Transactional
public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) { public AppNamespace createAppNamespaceInLocal(AppNamespace appNamespace) {
//not unique
if (appNamespaceRepository.findByName(appNamespace.getName()) != null){
throw new BadRequestException(appNamespace.getName() + "已存在");
}
AppNamespace managedAppNamespace = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName()); AppNamespace managedAppNamespace = appNamespaceRepository.findByAppIdAndName(appNamespace.getAppId(), appNamespace.getName());
//update //update
if (managedAppNamespace != null){ if (managedAppNamespace != null){
......
...@@ -47,22 +47,23 @@ namespace_module.controller("LinkNamespaceController", ...@@ -47,22 +47,23 @@ namespace_module.controller("LinkNamespaceController",
} }
var hasCreatedClusterCnt = 0; var hasCreatedClusterCnt = 0;
var namespaceCreationModels = [];
selectedClusters.forEach(function (cluster) { selectedClusters.forEach(function (cluster) {
NamespaceService.createNamespace($scope.appId, cluster.env, cluster.clusterName, namespaceCreationModels.push({
$scope.namespaceName).then(function (result) { env: cluster.env,
toastr.success( namespace: {
cluster.env + "_" + result.clusterName + "_" + result.namespaceName appId: $scope.appId,
+ "创建成功"); clusterName: cluster.clusterName,
hasCreatedClusterCnt ++; namespaceName: $scope.namespaceName
if (hasCreatedClusterCnt == selectedClustersSize){
$scope.step = 2;
} }
}, function (result) {
toastr.error(AppUtil.errorMsg(result),
cluster.env + "_" + cluster.clusterName + "_"
+ $scope.namespaceName + "创建失败");
}); });
}); });
NamespaceService.createNamespace($scope.appId, namespaceCreationModels)
.then(function (result) {
toastr.success("创建成功");
}, function (result) {
toastr.error(AppUtil.errorMsg(result));
});
}else { }else {
NamespaceService.createAppNamespace($scope.appId, $scope.appNamespace).then(function (result) { NamespaceService.createAppNamespace($scope.appId, $scope.appNamespace).then(function (result) {
$scope.step = 2; $scope.step = 2;
......
...@@ -7,7 +7,7 @@ appService.service("NamespaceService", ['$resource', '$q', function ($resource, ...@@ -7,7 +7,7 @@ appService.service("NamespaceService", ['$resource', '$q', function ($resource,
}, },
createNamespace: { createNamespace: {
method: 'POST', method: 'POST',
url: '/apps/:appId/envs/:env/clusters/:clusterName/namespaces', url: '/apps/:appId/namespaces',
isArray: false isArray: false
}, },
createAppNamespace: { createAppNamespace: {
...@@ -27,17 +27,11 @@ appService.service("NamespaceService", ['$resource', '$q', function ($resource, ...@@ -27,17 +27,11 @@ appService.service("NamespaceService", ['$resource', '$q', function ($resource,
}); });
return d.promise; return d.promise;
}, },
createNamespace: function (appId, env, clusterName, namespaceName) { createNamespace: function (appId, namespaceCreationModel) {
var d = $q.defer(); var d = $q.defer();
namespace_source.createNamespace({ namespace_source.createNamespace({
appId: appId, appId: appId
env: env, }, namespaceCreationModel, function (result) {
clusterName: clusterName
}, {
appId: appId,
clusterName: clusterName,
namespaceName: namespaceName
}, function (result) {
d.resolve(result); d.resolve(result);
}, function (result) { }, function (result) {
d.reject(result); d.reject(result);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment