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;
}
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());
}
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;
}
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;
}
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;
}
Aggregations