diff --git a/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java b/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java index f21bb32246ddc69ced9bbe06d6ea641a139362d2..6767986cec991cc0712b09c058965177fb8fc796 100644 --- a/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java +++ b/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ItemController.java @@ -3,6 +3,8 @@ package com.ctrip.apollo.adminservice.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -15,6 +17,7 @@ import com.ctrip.apollo.biz.service.ViewService; import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.core.dto.ItemChangeSets; import com.ctrip.apollo.core.dto.ItemDTO; +import com.ctrip.apollo.core.exception.NotFoundException; @RestController public class ItemController { @@ -25,18 +28,40 @@ public class ItemController { @Autowired private ItemService itemService; + @RequestMapping(path = "/items/", method = RequestMethod.POST) + public ResponseEntity create(@RequestBody ItemDTO dto) { + Item entity = BeanUtils.transfrom(Item.class, dto); + entity = itemService.save(entity); + dto = BeanUtils.transfrom(ItemDTO.class, entity); + return ResponseEntity.status(HttpStatus.CREATED).body(dto); + } + + @RequestMapping(path = "/items/{itemId}", method = RequestMethod.DELETE) + public void delete(@PathVariable("itemId") long itemId) { + Item entity = itemService.findOne(itemId); + if (entity == null) throw new NotFoundException("item not found for itemId " + itemId); + itemService.delete(entity.getId()); + } + @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items") public List findItems(@PathVariable("appId") String appId, - @PathVariable("clusterName") String clusterName, - @PathVariable("namespaceName") String namespaceName) { + @PathVariable("clusterName") String clusterName, + @PathVariable("namespaceName") String namespaceName) { List items = viewService.findItems(appId, clusterName, namespaceName); return BeanUtils.batchTransform(ItemDTO.class, items); } @RequestMapping("/items/{itemId}") - public ItemDTO findOne(@PathVariable("itemId") long itemId) { + public ItemDTO get(@PathVariable("itemId") long itemId) { Item item = itemService.findOne(itemId); return BeanUtils.transfrom(ItemDTO.class, item); } + @RequestMapping(path = "/item/{itemId}", method = RequestMethod.PUT) + public ItemDTO update(@PathVariable("itemId") long itemId, @RequestBody ItemDTO dto) { + Item entity = itemService.findOne(itemId); + if (entity == null) throw new NotFoundException("item not found for itemId " + itemId); + entity = itemService.update(BeanUtils.transfrom(Item.class, dto)); + return BeanUtils.transfrom(ItemDTO.class, entity); + } } diff --git a/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java b/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java index dcee7b50a9cd397cb0e02693e2a389cfc90032b8..d285a8a54597eafd73f4a1944d5448de7629694f 100644 --- a/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java +++ b/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/NamespaceController.java @@ -30,6 +30,14 @@ public class NamespaceController { @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces", method = RequestMethod.POST) public ResponseEntity create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @RequestBody NamespaceDTO dto) { + if (!appId.equals(dto.getAppId())) { + throw new IllegalArgumentException(String + .format("Path variable %s is not equals to object field %s", appId, dto.getAppId())); + } + if (!clusterName.equals(dto.getClusterName())) { + throw new IllegalArgumentException(String.format( + "Path variable %s is not equals to object field %s", clusterName, dto.getClusterName())); + } Namespace entity = BeanUtils.transfrom(Namespace.class, dto); entity = namespaceService.save(entity); dto = BeanUtils.transfrom(NamespaceDTO.class, entity); @@ -41,8 +49,8 @@ public class NamespaceController { @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName) { Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName); - if (entity == null) - throw new NotFoundException("namespace not found for namespaceName " + namespaceName); + if (entity == null) throw new NotFoundException( + String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName)); namespaceService.delete(entity.getId()); } @@ -56,6 +64,8 @@ public class NamespaceController { @RequestMapping("/namespaces/{namespaceId}") public NamespaceDTO get(@PathVariable("namespaceId") Long namespaceId) { Namespace namespace = namespaceService.findOne(namespaceId); + if (namespace == null) + throw new NotFoundException(String.format("namespace not found for %s", namespaceId)); return BeanUtils.transfrom(NamespaceDTO.class, namespace); } @@ -64,6 +74,8 @@ public class NamespaceController { @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName) { Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName); + if (namespace == null) throw new NotFoundException( + String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName)); return BeanUtils.transfrom(NamespaceDTO.class, namespace); } @@ -71,14 +83,22 @@ public class NamespaceController { public NamespaceDTO update(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @RequestBody NamespaceDTO dto) { + if (!appId.equals(dto.getAppId())) { + throw new IllegalArgumentException(String + .format("Path variable %s is not equals to object field %s", appId, dto.getAppId())); + } + if (!clusterName.equals(dto.getClusterName())) { + throw new IllegalArgumentException(String.format( + "Path variable %s is not equals to object field %s", clusterName, dto.getClusterName())); + } if (!namespaceName.equals(dto.getNamespaceName())) { throw new IllegalArgumentException( String.format("Path variable %s is not equals to object field %s", namespaceName, dto.getNamespaceName())); } Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName); - if (entity == null) - throw new NotFoundException("namespace not found for name " + namespaceName); + if (entity == null) throw new NotFoundException( + String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName)); entity = namespaceService.update(BeanUtils.transfrom(Namespace.class, dto)); return BeanUtils.transfrom(NamespaceDTO.class, entity); } diff --git a/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java b/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java index 9f6e8d0a019a4bd05eebed7adc4b149d75189d6f..1820d53a527fee8453e17c78e693bef8a0c4f5fb 100644 --- a/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java +++ b/apollo-adminservice/src/main/java/com/ctrip/apollo/adminservice/controller/ReleaseController.java @@ -5,6 +5,8 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import com.ctrip.apollo.biz.entity.Release; @@ -13,7 +15,7 @@ import com.ctrip.apollo.biz.service.ReleaseService; import com.ctrip.apollo.biz.service.ViewService; import com.ctrip.apollo.biz.utils.BeanUtils; import com.ctrip.apollo.core.dto.ReleaseDTO; -import com.ctrip.apollo.core.utils.StringUtils; +import com.ctrip.apollo.core.exception.NotFoundException; @RestController public class ReleaseController { @@ -28,13 +30,15 @@ public class ReleaseController { private ConfigService configService; @RequestMapping("/release/{releaseId}") - public ReleaseDTO findOne(@PathVariable("releaseId") long releaseId) { + public ReleaseDTO get(@PathVariable("releaseId") long releaseId) { Release release = releaseService.findOne(releaseId); + if (release == null) + throw new NotFoundException(String.format("release not found for %s", releaseId)); return BeanUtils.transfrom(ReleaseDTO.class, release); } @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases") - public List findReleases(@PathVariable("appId") String appId, + public List find(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName) { List releases = viewSerivce.findReleases(appId, clusterName, namespaceName); @@ -42,14 +46,21 @@ public class ReleaseController { } @RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases/latest") - public ReleaseDTO findLatestRelease(@PathVariable("appId") String appId, - @PathVariable("clusterName") String clusterName, - @PathVariable("namespaceName") String namespaceName){ - - if (StringUtils.isContainEmpty(appId, clusterName, namespaceName)){ - return null; - } + public ReleaseDTO getLatest(@PathVariable("appId") String appId, + @PathVariable("clusterName") String clusterName, + @PathVariable("namespaceName") String namespaceName) { Release release = configService.findRelease(appId, clusterName, namespaceName); + if (release == null) throw new NotFoundException( + String.format("latest release not found for %s %s %s", appId, clusterName, namespaceName)); + return BeanUtils.transfrom(ReleaseDTO.class, release); + } + + @RequestMapping(path = "/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST) + public ReleaseDTO buildRelease(@PathVariable("appId") String appId, + @PathVariable("clusterName") String clusterName, + @PathVariable("namespaceName") String namespaceName, @RequestParam("name") String name, + @RequestParam(name = "comment", required = false) String comment) { + Release release = releaseService.buildRelease(name, comment, appId, clusterName, namespaceName); return BeanUtils.transfrom(ReleaseDTO.class, release); } } diff --git a/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java b/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java index ffacf3330c8d8baf05df9b29d21e36e019efc8ba..30ab5b124d56bd0094b668fb54b753a72f9e0ecb 100644 --- a/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java +++ b/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ClusterService.java @@ -3,7 +3,6 @@ package com.ctrip.apollo.biz.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import com.ctrip.apollo.biz.entity.App; import com.ctrip.apollo.biz.entity.Cluster; import com.ctrip.apollo.biz.repository.ClusterRepository; import com.ctrip.apollo.biz.utils.BeanUtils; diff --git a/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java b/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java index 79463423a880508f0e78411444a203d7b00cc62b..8f3870e6a8628d517784c43d663b2068a948652e 100644 --- a/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java +++ b/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ItemService.java @@ -5,6 +5,7 @@ import org.springframework.stereotype.Service; import com.ctrip.apollo.biz.entity.Item; import com.ctrip.apollo.biz.repository.ItemRepository; +import com.ctrip.apollo.biz.utils.BeanUtils; @Service public class ItemService { @@ -12,9 +13,23 @@ public class ItemService { @Autowired private ItemRepository itemRepository; + public void delete(long id) { + itemRepository.delete(id); + } + public Item findOne(long itemId) { Item item = itemRepository.findOne(itemId); return item; } + public Item save(Item item) { + return itemRepository.save(item); + } + + public Item update(Item item) { + Item managedItem = itemRepository.findOne(item.getId()); + BeanUtils.copyEntityProperties(item, managedItem); + return itemRepository.save(managedItem); + } + } diff --git a/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java b/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java index 4b54a2dc7f3bd2b64d25b4d227747d6a1e32be90..4cfae4a0a831bcb27d78bdb45da6cee3562c57dd 100644 --- a/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java +++ b/apollo-biz/src/main/java/com/ctrip/apollo/biz/service/ReleaseService.java @@ -1,23 +1,65 @@ package com.ctrip.apollo.biz.service; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import com.ctrip.apollo.biz.entity.Item; +import com.ctrip.apollo.biz.entity.Namespace; import com.ctrip.apollo.biz.entity.Release; +import com.ctrip.apollo.biz.repository.ItemRepository; +import com.ctrip.apollo.biz.repository.NamespaceRepository; import com.ctrip.apollo.biz.repository.ReleaseRepository; +import com.ctrip.apollo.core.exception.NotFoundException; +import com.google.gson.Gson; /** * @author Jason Song(song_s@ctrip.com) */ @Service public class ReleaseService { - + @Autowired private ReleaseRepository releaseRepository; - + + @Autowired + private NamespaceRepository namespaceRepository; + + @Autowired + private ItemRepository itemRepository; + + private Gson gson = new Gson(); + public Release findOne(long releaseId) { Release release = releaseRepository.findOne(releaseId); return release; } + public Release buildRelease(String name, String comment, String appId, String clusterName, + String namespaceName) { + Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId, + clusterName, namespaceName); + if (namespace == null) { + throw new NotFoundException(String.format("Could not find namespace for %s %s %s", appId, + clusterName, namespaceName)); + } + List items = itemRepository.findByNamespaceId(namespace.getId()); + Map configurations = new HashMap(); + for (Item item : items) { + configurations.put(item.getKey(), item.getValue()); + } + + Release release = new Release(); + release.setName(name); + release.setComment(comment); + release.setAppId(appId); + release.setClusterName(clusterName); + release.setNamespaceName(namespaceName); + release.setConfigurations(gson.toJson(configurations)); + return releaseRepository.save(release); + } + } diff --git a/apollo-core/src/main/java/com/ctrip/apollo/core/dto/NamespaceDTO.java b/apollo-core/src/main/java/com/ctrip/apollo/core/dto/NamespaceDTO.java index 54c6dadc2bf8cb1c66349c3fa130a6e0ea931386..f160f6ae4e8826d5268af9c77c139ff02b8bd622 100644 --- a/apollo-core/src/main/java/com/ctrip/apollo/core/dto/NamespaceDTO.java +++ b/apollo-core/src/main/java/com/ctrip/apollo/core/dto/NamespaceDTO.java @@ -14,7 +14,7 @@ public class NamespaceDTO { return appId; } - public String getClusterId() { + public String getClusterName() { return clusterName; }