Commit 3481843e authored by nobodyiam's avatar nobodyiam

refactor gray release open api a little bit

parent 9bb54dd6
......@@ -108,26 +108,14 @@ public class OpenApiBeanUtils {
}
public static OpenGrayReleaseRuleDTO transformFromGrayReleaseRuleDTO(GrayReleaseRuleDTO grayReleaseRuleDTO){
OpenGrayReleaseRuleDTO openGrayReleaseRuleDTO = new OpenGrayReleaseRuleDTO();
openGrayReleaseRuleDTO.setAppId(grayReleaseRuleDTO.getAppId());
openGrayReleaseRuleDTO.setBranchName(grayReleaseRuleDTO.getBranchName());
openGrayReleaseRuleDTO.setClusterName(grayReleaseRuleDTO.getClusterName());
openGrayReleaseRuleDTO.setNamespaceName(grayReleaseRuleDTO.getNamespaceName());
Set<GrayReleaseRuleItemDTO> grayReleaseRuleItemDTOSet = grayReleaseRuleDTO.getRuleItems();
Set<OpenGrayReleaseRuleItemDTO> ruleItems = new HashSet<>();
grayReleaseRuleItemDTOSet.forEach(grayReleaseRuleItemDTO -> {
OpenGrayReleaseRuleItemDTO item = new OpenGrayReleaseRuleItemDTO();
item.setClientAppId(grayReleaseRuleItemDTO.getClientAppId());
item.setClientIpList(grayReleaseRuleItemDTO.getClientIpList());
ruleItems.add(item);
});
openGrayReleaseRuleDTO.setRuleItems(ruleItems);
Preconditions.checkArgument(grayReleaseRuleDTO != null);
return openGrayReleaseRuleDTO;
return BeanUtils.transfrom(OpenGrayReleaseRuleDTO.class, grayReleaseRuleDTO);
}
public static GrayReleaseRuleDTO transformToGrayReleaseRuleDTO(OpenGrayReleaseRuleDTO openGrayReleaseRuleDTO){
Preconditions.checkArgument(openGrayReleaseRuleDTO != null);
String appId = openGrayReleaseRuleDTO.getAppId();
String branchName = openGrayReleaseRuleDTO.getBranchName();
String clusterName = openGrayReleaseRuleDTO.getClusterName();
......
......@@ -6,8 +6,11 @@ package com.ctrip.framework.apollo.openapi.v1.controller;
import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleDTO;
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
import com.ctrip.framework.apollo.core.enums.Env;
import com.ctrip.framework.apollo.core.utils.StringUtils;
import com.ctrip.framework.apollo.openapi.auth.ConsumerPermissionValidator;
import com.ctrip.framework.apollo.openapi.dto.OpenGrayReleaseRuleDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
......@@ -16,6 +19,7 @@ import com.ctrip.framework.apollo.portal.component.PermissionValidator;
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
import com.ctrip.framework.apollo.portal.service.NamespaceBranchService;
import com.ctrip.framework.apollo.portal.service.ReleaseService;
import com.ctrip.framework.apollo.portal.spi.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.prepost.PreAuthorize;
......@@ -33,6 +37,8 @@ public class NamespaceBranchController {
private ReleaseService releaseService;
@Autowired
private NamespaceBranchService namespaceBranchService;
@Autowired
private UserService userService;
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/branches", method = RequestMethod.GET)
public OpenNamespaceDTO findBranch(@PathVariable String appId,
......@@ -54,6 +60,12 @@ public class NamespaceBranchController {
@PathVariable String namespaceName,
@RequestParam("operator") String operator,
HttpServletRequest request) {
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),"operator can not be empty");
if (userService.findByUserId(operator) == null) {
throw new BadRequestException("operator " + operator + " not exists");
}
NamespaceDTO namespaceDTO = namespaceBranchService.createBranch(appId, Env.valueOf(env.toUpperCase()), clusterName, namespaceName, operator);
if (namespaceDTO == null) {
return null;
......@@ -70,6 +82,12 @@ public class NamespaceBranchController {
@PathVariable String branchName,
@RequestParam("operator") String operator,
HttpServletRequest request) {
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),"operator can not be empty");
if (userService.findByUserId(operator) == null) {
throw new BadRequestException("operator " + operator + " not exists");
}
boolean canDelete = consumerPermissionValidator.hasReleaseNamespacePermission(request, appId, namespaceName, env) ||
(consumerPermissionValidator.hasModifyNamespacePermission(request, appId, namespaceName, env) &&
releaseService.loadLatestRelease(appId, Env.valueOf(env), branchName, namespaceName) == null);
......@@ -103,6 +121,17 @@ public class NamespaceBranchController {
@PathVariable String branchName, @RequestBody OpenGrayReleaseRuleDTO rules,
@RequestParam("operator") String operator,
HttpServletRequest request) {
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),"operator can not be empty");
if (userService.findByUserId(operator) == null) {
throw new BadRequestException("operator " + operator + " not exists");
}
rules.setAppId(appId);
rules.setClusterName(clusterName);
rules.setNamespaceName(namespaceName);
rules.setBranchName(branchName);
GrayReleaseRuleDTO grayReleaseRuleDTO = OpenApiBeanUtils.transformToGrayReleaseRuleDTO(rules);
namespaceBranchService
.updateBranchGrayRules(appId, Env.valueOf(env.toUpperCase()), clusterName, namespaceName, branchName, grayReleaseRuleDTO, operator);
......
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