Skip to content

Commit 39ef9ca

Browse files
authored
Merge pull request apolloconfig#1533 from nobodyiam/open-api-gray-release-refactor
refactor gray release open api a little bit
2 parents 9bb54dd + 3481843 commit 39ef9ca

File tree

2 files changed

+33
-16
lines changed

2 files changed

+33
-16
lines changed

apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/util/OpenApiBeanUtils.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,14 @@ public static OpenNamespaceLockDTO transformFromNamespaceLockDTO(String namespac
108108
}
109109

110110
public static OpenGrayReleaseRuleDTO transformFromGrayReleaseRuleDTO(GrayReleaseRuleDTO grayReleaseRuleDTO){
111-
OpenGrayReleaseRuleDTO openGrayReleaseRuleDTO = new OpenGrayReleaseRuleDTO();
112-
openGrayReleaseRuleDTO.setAppId(grayReleaseRuleDTO.getAppId());
113-
openGrayReleaseRuleDTO.setBranchName(grayReleaseRuleDTO.getBranchName());
114-
openGrayReleaseRuleDTO.setClusterName(grayReleaseRuleDTO.getClusterName());
115-
openGrayReleaseRuleDTO.setNamespaceName(grayReleaseRuleDTO.getNamespaceName());
116-
117-
Set<GrayReleaseRuleItemDTO> grayReleaseRuleItemDTOSet = grayReleaseRuleDTO.getRuleItems();
118-
Set<OpenGrayReleaseRuleItemDTO> ruleItems = new HashSet<>();
119-
grayReleaseRuleItemDTOSet.forEach(grayReleaseRuleItemDTO -> {
120-
OpenGrayReleaseRuleItemDTO item = new OpenGrayReleaseRuleItemDTO();
121-
item.setClientAppId(grayReleaseRuleItemDTO.getClientAppId());
122-
item.setClientIpList(grayReleaseRuleItemDTO.getClientIpList());
123-
ruleItems.add(item);
124-
});
125-
openGrayReleaseRuleDTO.setRuleItems(ruleItems);
111+
Preconditions.checkArgument(grayReleaseRuleDTO != null);
126112

127-
return openGrayReleaseRuleDTO;
113+
return BeanUtils.transfrom(OpenGrayReleaseRuleDTO.class, grayReleaseRuleDTO);
128114
}
129115

130116
public static GrayReleaseRuleDTO transformToGrayReleaseRuleDTO(OpenGrayReleaseRuleDTO openGrayReleaseRuleDTO){
117+
Preconditions.checkArgument(openGrayReleaseRuleDTO != null);
118+
131119
String appId = openGrayReleaseRuleDTO.getAppId();
132120
String branchName = openGrayReleaseRuleDTO.getBranchName();
133121
String clusterName = openGrayReleaseRuleDTO.getClusterName();

apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/NamespaceBranchController.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
import com.ctrip.framework.apollo.common.dto.GrayReleaseRuleDTO;
88
import com.ctrip.framework.apollo.common.dto.NamespaceDTO;
9+
import com.ctrip.framework.apollo.common.exception.BadRequestException;
910
import com.ctrip.framework.apollo.common.utils.BeanUtils;
11+
import com.ctrip.framework.apollo.common.utils.RequestPrecondition;
1012
import com.ctrip.framework.apollo.core.enums.Env;
13+
import com.ctrip.framework.apollo.core.utils.StringUtils;
1114
import com.ctrip.framework.apollo.openapi.auth.ConsumerPermissionValidator;
1215
import com.ctrip.framework.apollo.openapi.dto.OpenGrayReleaseRuleDTO;
1316
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;
@@ -16,6 +19,7 @@
1619
import com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO;
1720
import com.ctrip.framework.apollo.portal.service.NamespaceBranchService;
1821
import com.ctrip.framework.apollo.portal.service.ReleaseService;
22+
import com.ctrip.framework.apollo.portal.spi.UserService;
1923
import org.springframework.beans.factory.annotation.Autowired;
2024
import org.springframework.security.access.AccessDeniedException;
2125
import org.springframework.security.access.prepost.PreAuthorize;
@@ -33,6 +37,8 @@ public class NamespaceBranchController {
3337
private ReleaseService releaseService;
3438
@Autowired
3539
private NamespaceBranchService namespaceBranchService;
40+
@Autowired
41+
private UserService userService;
3642

3743
@RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/branches", method = RequestMethod.GET)
3844
public OpenNamespaceDTO findBranch(@PathVariable String appId,
@@ -54,6 +60,12 @@ public OpenNamespaceDTO createBranch(@PathVariable String appId,
5460
@PathVariable String namespaceName,
5561
@RequestParam("operator") String operator,
5662
HttpServletRequest request) {
63+
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),"operator can not be empty");
64+
65+
if (userService.findByUserId(operator) == null) {
66+
throw new BadRequestException("operator " + operator + " not exists");
67+
}
68+
5769
NamespaceDTO namespaceDTO = namespaceBranchService.createBranch(appId, Env.valueOf(env.toUpperCase()), clusterName, namespaceName, operator);
5870
if (namespaceDTO == null) {
5971
return null;
@@ -70,6 +82,12 @@ public void deleteBranch(@PathVariable String appId,
7082
@PathVariable String branchName,
7183
@RequestParam("operator") String operator,
7284
HttpServletRequest request) {
85+
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),"operator can not be empty");
86+
87+
if (userService.findByUserId(operator) == null) {
88+
throw new BadRequestException("operator " + operator + " not exists");
89+
}
90+
7391
boolean canDelete = consumerPermissionValidator.hasReleaseNamespacePermission(request, appId, namespaceName, env) ||
7492
(consumerPermissionValidator.hasModifyNamespacePermission(request, appId, namespaceName, env) &&
7593
releaseService.loadLatestRelease(appId, Env.valueOf(env), branchName, namespaceName) == null);
@@ -103,6 +121,17 @@ public void updateBranchRules(@PathVariable String appId, @PathVariable String e
103121
@PathVariable String branchName, @RequestBody OpenGrayReleaseRuleDTO rules,
104122
@RequestParam("operator") String operator,
105123
HttpServletRequest request) {
124+
RequestPrecondition.checkArguments(!StringUtils.isContainEmpty(operator),"operator can not be empty");
125+
126+
if (userService.findByUserId(operator) == null) {
127+
throw new BadRequestException("operator " + operator + " not exists");
128+
}
129+
130+
rules.setAppId(appId);
131+
rules.setClusterName(clusterName);
132+
rules.setNamespaceName(namespaceName);
133+
rules.setBranchName(branchName);
134+
106135
GrayReleaseRuleDTO grayReleaseRuleDTO = OpenApiBeanUtils.transformToGrayReleaseRuleDTO(rules);
107136
namespaceBranchService
108137
.updateBranchGrayRules(appId, Env.valueOf(env.toUpperCase()), clusterName, namespaceName, branchName, grayReleaseRuleDTO, operator);

0 commit comments

Comments
 (0)