Search in sources :

Example 1 with NotFoundException

use of com.ctrip.framework.apollo.common.exception.NotFoundException in project apollo by ctripcorp.

the class ItemService method findOne.

public Item findOne(String appId, String clusterName, String namespaceName, String key) {
    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));
    }
    Item item = itemRepository.findByNamespaceIdAndKey(namespace.getId(), key);
    return item;
}
Also used : Item(com.ctrip.framework.apollo.biz.entity.Item) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace)

Example 2 with NotFoundException

use of com.ctrip.framework.apollo.common.exception.NotFoundException in project apollo by ctripcorp.

the class ReleaseService method rollback.

@Transactional
public Release rollback(long releaseId, String operator) {
    Release release = findOne(releaseId);
    if (release == null) {
        throw new NotFoundException("release not found");
    }
    if (release.isAbandoned()) {
        throw new BadRequestException("release is not active");
    }
    String appId = release.getAppId();
    String clusterName = release.getClusterName();
    String namespaceName = release.getNamespaceName();
    PageRequest page = PageRequest.of(0, 2);
    List<Release> twoLatestActiveReleases = findActiveReleases(appId, clusterName, namespaceName, page);
    if (twoLatestActiveReleases == null || twoLatestActiveReleases.size() < 2) {
        throw new BadRequestException(String.format("Can't rollback namespace(appId=%s, clusterName=%s, namespaceName=%s) because there is only one active release", appId, clusterName, namespaceName));
    }
    release.setAbandoned(true);
    release.setDataChangeLastModifiedBy(operator);
    releaseRepository.save(release);
    releaseHistoryService.createReleaseHistory(appId, clusterName, namespaceName, clusterName, twoLatestActiveReleases.get(1).getId(), release.getId(), ReleaseOperation.ROLLBACK, null, operator);
    // publish child namespace if namespace has child
    rollbackChildNamespace(appId, clusterName, namespaceName, twoLatestActiveReleases, operator);
    return release;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Release(com.ctrip.framework.apollo.biz.entity.Release) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with NotFoundException

use of com.ctrip.framework.apollo.common.exception.NotFoundException in project apollo by ctripcorp.

the class InstanceConfigController method getByReleasesNotIn.

@GetMapping("/by-namespace-and-releases-not-in")
public List<InstanceDTO> getByReleasesNotIn(@RequestParam("appId") String appId, @RequestParam("clusterName") String clusterName, @RequestParam("namespaceName") String namespaceName, @RequestParam("releaseIds") String releaseIds) {
    Set<Long> releaseIdSet = RELEASES_SPLITTER.splitToList(releaseIds).stream().map(Long::parseLong).collect(Collectors.toSet());
    List<Release> releases = releaseService.findByReleaseIds(releaseIdSet);
    if (CollectionUtils.isEmpty(releases)) {
        throw new NotFoundException("releases not found for %s", releaseIds);
    }
    Set<String> releaseKeys = releases.stream().map(Release::getReleaseKey).collect(Collectors.toSet());
    List<InstanceConfig> instanceConfigs = instanceService.findInstanceConfigsByNamespaceWithReleaseKeysNotIn(appId, clusterName, namespaceName, releaseKeys);
    Multimap<Long, InstanceConfig> instanceConfigMap = HashMultimap.create();
    Set<String> otherReleaseKeys = Sets.newHashSet();
    for (InstanceConfig instanceConfig : instanceConfigs) {
        instanceConfigMap.put(instanceConfig.getInstanceId(), instanceConfig);
        otherReleaseKeys.add(instanceConfig.getReleaseKey());
    }
    List<Instance> instances = instanceService.findInstancesByIds(instanceConfigMap.keySet());
    if (CollectionUtils.isEmpty(instances)) {
        return Collections.emptyList();
    }
    List<InstanceDTO> instanceDTOs = BeanUtils.batchTransform(InstanceDTO.class, instances);
    List<Release> otherReleases = releaseService.findByReleaseKeys(otherReleaseKeys);
    Map<String, ReleaseDTO> releaseMap = Maps.newHashMap();
    for (Release release : otherReleases) {
        // unset configurations to save space
        release.setConfigurations(null);
        ReleaseDTO releaseDTO = BeanUtils.transform(ReleaseDTO.class, release);
        releaseMap.put(release.getReleaseKey(), releaseDTO);
    }
    for (InstanceDTO instanceDTO : instanceDTOs) {
        Collection<InstanceConfig> configs = instanceConfigMap.get(instanceDTO.getId());
        List<InstanceConfigDTO> configDTOs = configs.stream().map(instanceConfig -> {
            InstanceConfigDTO instanceConfigDTO = new InstanceConfigDTO();
            instanceConfigDTO.setRelease(releaseMap.get(instanceConfig.getReleaseKey()));
            instanceConfigDTO.setReleaseDeliveryTime(instanceConfig.getReleaseDeliveryTime());
            instanceConfigDTO.setDataChangeLastModifiedTime(instanceConfig.getDataChangeLastModifiedTime());
            return instanceConfigDTO;
        }).collect(Collectors.toList());
        instanceDTO.setConfigs(configDTOs);
    }
    return instanceDTOs;
}
Also used : RequestParam(org.springframework.web.bind.annotation.RequestParam) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) Multimap(com.google.common.collect.Multimap) BeanUtils(com.ctrip.framework.apollo.common.utils.BeanUtils) Strings(com.google.common.base.Strings) HashMultimap(com.google.common.collect.HashMultimap) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) Instance(com.ctrip.framework.apollo.biz.entity.Instance) Map(java.util.Map) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) GetMapping(org.springframework.web.bind.annotation.GetMapping) InstanceService(com.ctrip.framework.apollo.biz.service.InstanceService) Pageable(org.springframework.data.domain.Pageable) Splitter(com.google.common.base.Splitter) Collection(java.util.Collection) ReleaseService(com.ctrip.framework.apollo.biz.service.ReleaseService) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) PageRequest(org.springframework.data.domain.PageRequest) Set(java.util.Set) Release(com.ctrip.framework.apollo.biz.entity.Release) Page(org.springframework.data.domain.Page) Maps(com.google.common.collect.Maps) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) CollectionUtils(org.springframework.util.CollectionUtils) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Collections(java.util.Collections) Instance(com.ctrip.framework.apollo.biz.entity.Instance) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) InstanceConfig(com.ctrip.framework.apollo.biz.entity.InstanceConfig) InstanceDTO(com.ctrip.framework.apollo.common.dto.InstanceDTO) InstanceConfigDTO(com.ctrip.framework.apollo.common.dto.InstanceConfigDTO) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) Release(com.ctrip.framework.apollo.biz.entity.Release) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 4 with NotFoundException

use of com.ctrip.framework.apollo.common.exception.NotFoundException in project apollo by ctripcorp.

the class AppController method delete.

@DeleteMapping("/apps/{appId:.+}")
public void delete(@PathVariable("appId") String appId, @RequestParam String operator) {
    App entity = appService.findOne(appId);
    if (entity == null) {
        throw new NotFoundException("app not found for appId " + appId);
    }
    adminService.deleteApp(entity, operator);
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping)

Example 5 with NotFoundException

use of com.ctrip.framework.apollo.common.exception.NotFoundException in project apollo by ctripcorp.

the class ItemController method delete.

@PreAcquireNamespaceLock
@DeleteMapping("/items/{itemId}")
public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
    Item entity = itemService.findOne(itemId);
    if (entity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    itemService.delete(entity.getId(), operator);
    Namespace namespace = namespaceService.findOne(entity.getNamespaceId());
    Commit commit = new Commit();
    commit.setAppId(namespace.getAppId());
    commit.setClusterName(namespace.getClusterName());
    commit.setNamespaceName(namespace.getNamespaceName());
    commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
    commit.setDataChangeCreatedBy(operator);
    commit.setDataChangeLastModifiedBy(operator);
    commitService.save(commit);
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)

Aggregations

NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)16 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)8 Release (com.ctrip.framework.apollo.biz.entity.Release)7 Transactional (org.springframework.transaction.annotation.Transactional)7 Item (com.ctrip.framework.apollo.biz.entity.Item)4 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)3 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)3 PageRequest (org.springframework.data.domain.PageRequest)3 PostMapping (org.springframework.web.bind.annotation.PostMapping)3 PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)2 Commit (com.ctrip.framework.apollo.biz.entity.Commit)2 Instance (com.ctrip.framework.apollo.biz.entity.Instance)2 InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)2 InstanceService (com.ctrip.framework.apollo.biz.service.InstanceService)2 ReleaseService (com.ctrip.framework.apollo.biz.service.ReleaseService)2 InstanceConfigDTO (com.ctrip.framework.apollo.common.dto.InstanceConfigDTO)2 InstanceDTO (com.ctrip.framework.apollo.common.dto.InstanceDTO)2 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)2