diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java index 5fda72412f2770097b85f3eec87b5bb790908262..fb3d3b06eb95fb55e45345d25aeeef14158994e4 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/openapi/v1/controller/ItemController.java @@ -35,8 +35,7 @@ public class ItemController { @PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)") @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.POST) public OpenItemDTO createItem(@PathVariable String appId, @PathVariable String env, - @PathVariable String clusterName, @PathVariable String - namespaceName, + @PathVariable String clusterName, @PathVariable String namespaceName, @RequestBody OpenItemDTO item, HttpServletRequest request) { RequestPrecondition.checkArguments( @@ -60,48 +59,53 @@ public class ItemController { } @PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)") - @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.PUT) + @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}", method = RequestMethod.PUT) public void updateItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, - @PathVariable long itemId, @RequestBody OpenItemDTO item, - HttpServletRequest request) { + @PathVariable String key, @RequestBody OpenItemDTO item, HttpServletRequest request) { + + RequestPrecondition.checkArguments(item != null, "item payload can not be empty"); - RequestPrecondition.checkArguments(item != null && item.getId() > 0 && itemId == item.getId(), - "item data error"); RequestPrecondition.checkArguments( - !StringUtils.isContainEmpty(item.getKey(), item.getValue(), item - .getDataChangeLastModifiedBy()), + !StringUtils.isContainEmpty(item.getKey(), item.getValue(), item.getDataChangeLastModifiedBy()), "key,value,dataChangeLastModifiedBy 字段不能为空"); + RequestPrecondition.checkArguments(item.getKey().equals(key), "item payload can not be empty"); + if (userService.findByUserId(item.getDataChangeLastModifiedBy()) == null) { - throw new BadRequestException("用户不存在."); + throw new BadRequestException("用户不存在"); } - ItemDTO toUpdateItem = itemService.loadItem(Env.fromString(env), itemId); + ItemDTO toUpdateItem = itemService.loadItem(Env.fromString(env), appId, clusterName, namespaceName, item.getKey()); if (toUpdateItem == null) { - throw new BadRequestException("item not exist"); + throw new BadRequestException("item不存在"); } //protect. only value,comment,lastModifiedBy can be modified toUpdateItem.setComment(item.getComment()); toUpdateItem.setValue(item.getValue()); toUpdateItem.setDataChangeLastModifiedBy(item.getDataChangeLastModifiedBy()); - itemService.updateItem(appId, Env.fromString(env), clusterName, namespaceName, - toUpdateItem); + itemService.updateItem(appId, Env.fromString(env), clusterName, namespaceName, toUpdateItem); } @PreAuthorize(value = "@consumerPermissionValidator.hasModifyNamespacePermission(#request, #appId, #namespaceName)") - @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}", method = RequestMethod.DELETE) + @RequestMapping(value = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key:.+}", method = RequestMethod.DELETE) public void deleteItem(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, - @PathVariable long itemId, @RequestParam String operator, + @PathVariable String key, @RequestParam String operator, HttpServletRequest request) { if (userService.findByUserId(operator) == null) { - throw new BadRequestException("用户不存在."); + throw new BadRequestException("用户不存在"); + } + + ItemDTO toDeletedItem = itemService.loadItem(Env.valueOf(env), appId, clusterName, namespaceName, key); + if (toDeletedItem == null){ + throw new BadRequestException("item不存在"); } - itemService.deleteItem(Env.fromString(env), itemId, operator); + + itemService.deleteItem(Env.fromString(env), toDeletedItem.getId(), operator); } } diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java index 9b05f8d127c50370db0c6a0c5202902bb5abfe7d..43db3227ae98899b4e38711356a4c80ea5bcba4b 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/api/AdminServiceAPI.java @@ -90,8 +90,9 @@ public class AdminServiceAPI { return Arrays.asList(itemDTOs); } - public ItemDTO loadItem(Env env, long itemId) { - return restTemplate.get(env, "/items/{itemId}", ItemDTO.class, itemId); + public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) { + return restTemplate.get(env, "apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{key}", + ItemDTO.class, appId, clusterName, namespaceName, key); } public void updateItemsByChangeSet(String appId, Env env, String clusterName, String namespace, diff --git a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java index d97c8b0f3ec5a02406580e92161b44046f165599..b627f4b90789df3fbb4a56b1075ec9f58b184295 100644 --- a/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java +++ b/apollo-portal/src/main/java/com/ctrip/framework/apollo/portal/service/ItemService.java @@ -114,8 +114,8 @@ public class ItemService { return itemAPI.findItems(appId, env, clusterName, namespaceName); } - public ItemDTO loadItem(Env env, long itemId) { - return itemAPI.loadItem(env, itemId); + public ItemDTO loadItem(Env env, String appId, String clusterName, String namespaceName, String key) { + return itemAPI.loadItem(env, appId, clusterName, namespaceName, key); } public void syncItems(List comparedNamespaces, List sourceItems) {