Search in sources :

Example 1 with ReleaseDTO

use of com.ctrip.framework.apollo.common.dto.ReleaseDTO in project apollo by ctripcorp.

the class InstanceConfigController method getByReleasesNotIn.

@RequestMapping(value = "/by-namespace-and-releases-not-in", method = RequestMethod.GET)
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(String.format("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.transfrom(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) Autowired(org.springframework.beans.factory.annotation.Autowired) 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) 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) RequestMethod(org.springframework.web.bind.annotation.RequestMethod) 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with ReleaseDTO

use of com.ctrip.framework.apollo.common.dto.ReleaseDTO in project apollo by ctripcorp.

the class ReleaseControllerTest method testReleaseBuild.

@Test
@Sql(scripts = "/controller/test-release.sql", executionPhase = ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = "/controller/cleanup.sql", executionPhase = ExecutionPhase.AFTER_TEST_METHOD)
public void testReleaseBuild() {
    String appId = "someAppId";
    AppDTO app = restTemplate.getForObject("http://localhost:" + port + "/apps/" + appId, AppDTO.class);
    ClusterDTO cluster = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/default", ClusterDTO.class);
    NamespaceDTO namespace = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/application", NamespaceDTO.class);
    Assert.assertEquals("someAppId", app.getAppId());
    Assert.assertEquals("default", cluster.getName());
    Assert.assertEquals("application", namespace.getNamespaceName());
    ItemDTO[] items = restTemplate.getForObject("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/items", ItemDTO[].class);
    Assert.assertEquals(3, items.length);
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String, String> parameters = new LinkedMultiValueMap<String, String>();
    parameters.add("name", "someReleaseName");
    parameters.add("comment", "someComment");
    parameters.add("operator", "test");
    HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<MultiValueMap<String, String>>(parameters, headers);
    ResponseEntity<ReleaseDTO> response = restTemplate.postForEntity("http://localhost:" + port + "/apps/" + app.getAppId() + "/clusters/" + cluster.getName() + "/namespaces/" + namespace.getNamespaceName() + "/releases", entity, ReleaseDTO.class);
    Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
    ReleaseDTO release = response.getBody();
    Assert.assertEquals("someReleaseName", release.getName());
    Assert.assertEquals("someComment", release.getComment());
    Assert.assertEquals("someAppId", release.getAppId());
    Assert.assertEquals("default", release.getClusterName());
    Assert.assertEquals("application", release.getNamespaceName());
    Map<String, String> configurations = new HashMap<String, String>();
    configurations.put("k1", "v1");
    configurations.put("k2", "v2");
    configurations.put("k3", "v3");
    Gson gson = new Gson();
    Assert.assertEquals(gson.toJson(configurations), release.getConfigurations());
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) HashMap(java.util.HashMap) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) Gson(com.google.gson.Gson) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) AppDTO(com.ctrip.framework.apollo.common.dto.AppDTO) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) MultiValueMap(org.springframework.util.MultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) Test(org.junit.Test) Sql(org.springframework.test.context.jdbc.Sql)

Example 3 with ReleaseDTO

use of com.ctrip.framework.apollo.common.dto.ReleaseDTO in project apollo by ctripcorp.

the class ReleaseController method createRelease.

@PreAuthorize(value = "@permissionValidator.hasReleaseNamespacePermission(#appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/releases", method = RequestMethod.POST)
public ReleaseDTO createRelease(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @RequestBody NamespaceReleaseModel model) {
    checkModel(Objects.nonNull(model));
    model.setAppId(appId);
    model.setEnv(env);
    model.setClusterName(clusterName);
    model.setNamespaceName(namespaceName);
    if (model.isEmergencyPublish() && !portalConfig.isEmergencyPublishAllowed(Env.valueOf(env))) {
        throw new BadRequestException(String.format("Env: %s is not supported emergency publish now", env));
    }
    ReleaseDTO createdRelease = releaseService.publish(model);
    ConfigPublishEvent event = ConfigPublishEvent.instance();
    event.withAppId(appId).withCluster(clusterName).withNamespace(namespaceName).withReleaseId(createdRelease.getId()).setNormalPublishEvent(true).setEnv(Env.valueOf(env));
    publisher.publishEvent(event);
    return createdRelease;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ConfigPublishEvent(com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with ReleaseDTO

use of com.ctrip.framework.apollo.common.dto.ReleaseDTO in project apollo by ctripcorp.

the class NamespaceBranchController method merge.

@PreAuthorize(value = "@permissionValidator.hasReleaseNamespacePermission(#appId, #namespaceName)")
@RequestMapping(value = "/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/branches/{branchName}/merge", method = RequestMethod.POST)
public ReleaseDTO merge(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, @PathVariable String branchName, @RequestParam(value = "deleteBranch", defaultValue = "true") boolean deleteBranch, @RequestBody NamespaceReleaseModel model) {
    if (model.isEmergencyPublish() && !portalConfig.isEmergencyPublishAllowed(Env.fromString(env))) {
        throw new BadRequestException(String.format("Env: %s is not supported emergency publish now", env));
    }
    ReleaseDTO createdRelease = namespaceBranchService.merge(appId, Env.valueOf(env), clusterName, namespaceName, branchName, model.getReleaseTitle(), model.getReleaseComment(), model.isEmergencyPublish(), deleteBranch);
    ConfigPublishEvent event = ConfigPublishEvent.instance();
    event.withAppId(appId).withCluster(clusterName).withNamespace(namespaceName).withReleaseId(createdRelease.getId()).setMergeEvent(true).setEnv(Env.valueOf(env));
    publisher.publishEvent(event);
    return createdRelease;
}
Also used : BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ConfigPublishEvent(com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with ReleaseDTO

use of com.ctrip.framework.apollo.common.dto.ReleaseDTO in project apollo by ctripcorp.

the class NamespaceService method transformNamespace2BO.

private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) {
    NamespaceBO namespaceBO = new NamespaceBO();
    namespaceBO.setBaseInfo(namespace);
    String appId = namespace.getAppId();
    String clusterName = namespace.getClusterName();
    String namespaceName = namespace.getNamespaceName();
    fillAppNamespaceProperties(namespaceBO);
    List<ItemBO> itemBOs = new LinkedList<>();
    namespaceBO.setItems(itemBOs);
    // latest Release
    ReleaseDTO latestRelease;
    Map<String, String> releaseItems = new HashMap<>();
    latestRelease = releaseService.loadLatestRelease(appId, env, clusterName, namespaceName);
    if (latestRelease != null) {
        releaseItems = gson.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
    }
    // not Release config items
    List<ItemDTO> items = itemService.findItems(appId, env, clusterName, namespaceName);
    int modifiedItemCnt = 0;
    for (ItemDTO itemDTO : items) {
        ItemBO itemBO = transformItem2BO(itemDTO, releaseItems);
        if (itemBO.isModified()) {
            modifiedItemCnt++;
        }
        itemBOs.add(itemBO);
    }
    // deleted items
    List<ItemBO> deletedItems = parseDeletedItems(items, releaseItems);
    itemBOs.addAll(deletedItems);
    modifiedItemCnt += deletedItems.size();
    namespaceBO.setItemModifiedCnt(modifiedItemCnt);
    return namespaceBO;
}
Also used : ItemBO(com.ctrip.framework.apollo.portal.entity.bo.ItemBO) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO)

Aggregations

ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 ReleaseHistoryDTO (com.ctrip.framework.apollo.common.dto.ReleaseHistoryDTO)4 ConfigPublishEvent (com.ctrip.framework.apollo.portal.listener.ConfigPublishEvent)4 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)3 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)2 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Instance (com.ctrip.framework.apollo.biz.entity.Instance)1 InstanceConfig (com.ctrip.framework.apollo.biz.entity.InstanceConfig)1 Release (com.ctrip.framework.apollo.biz.entity.Release)1 InstanceService (com.ctrip.framework.apollo.biz.service.InstanceService)1 ReleaseService (com.ctrip.framework.apollo.biz.service.ReleaseService)1 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)1 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)1 InstanceConfigDTO (com.ctrip.framework.apollo.common.dto.InstanceConfigDTO)1 InstanceDTO (com.ctrip.framework.apollo.common.dto.InstanceDTO)1