Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed May 6, 2016
1 parent f2c7de8 commit 9fb54c6
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 149 deletions.
Expand Up @@ -50,15 +50,15 @@ public void verifyInitialDatabaseState() {
System.out.println(app.getAppId());
}
Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count());
Assert.assertEquals(7, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count());
}

@Before
public void setUpTestDataWithinTransaction() {
Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count());
Assert.assertEquals(7, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count());
}
Expand All @@ -82,15 +82,15 @@ public void modifyDatabaseWithinTransaction() {
@After
public void tearDownWithinTransaction() {
Assert.assertEquals(1, appRepository.count());
Assert.assertEquals(1, appNamespaceRepository.count());
Assert.assertEquals(8, appNamespaceRepository.count());
Assert.assertEquals(1, namespaceRepository.count());
Assert.assertEquals(1, clusterRepository.count());
}

@AfterTransaction
public void verifyFinalDatabaseState() {
Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count());
Assert.assertEquals(7, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count());
}
Expand Down
Expand Up @@ -31,16 +31,6 @@ public class ConfigController {
@Autowired
private ConfigService configService;

@RequestMapping("/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces")
public List<NamespaceVO> findNamespaces(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName) {
if (StringUtils.isContainEmpty(appId, env, clusterName)) {
throw new BadRequestException("app id and cluster name can not be empty");
}

return configService.findNampspaces(appId, Env.valueOf(env), clusterName);
}

@RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = {
"application/json"})
public void modifyItems(@PathVariable String appId, @PathVariable String env,
Expand Down
Expand Up @@ -5,6 +5,7 @@
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.BadRequestException;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.entity.NamespaceVO;
import com.ctrip.apollo.portal.service.NamespaceService;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -18,6 +19,7 @@

@RestController
public class NamespaceController {

@Autowired
private NamespaceService namespaceService;

Expand All @@ -35,4 +37,14 @@ public NamespaceDTO save(@PathVariable String env, @RequestBody NamespaceDTO nam

}

@RequestMapping("/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces")
public List<NamespaceVO> findNamespaces(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName) {
if (StringUtils.isContainEmpty(appId, env, clusterName)) {
throw new BadRequestException("app id and cluster name can not be empty");
}

return namespaceService.findNampspaces(appId, Env.valueOf(env), clusterName);
}

}
Expand Up @@ -50,90 +50,6 @@ public class ConfigService {
@Autowired
private ConfigTextResolver resolver;

private Gson gson = new Gson();

/**
* load cluster all namespace info with items
*/
public List<NamespaceVO> findNampspaces(String appId, Env env, String clusterName) {

List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
if (namespaces == null || namespaces.size() == 0) {
return Collections.emptyList();
}

List<NamespaceVO> namespaceVOs = new LinkedList<>();
for (NamespaceDTO namespace : namespaces) {

NamespaceVO namespaceVO = null;
try {
namespaceVO = parseNamespace(appId, env, clusterName, namespace);
namespaceVOs.add(namespaceVO);
} catch (Exception e) {
logger.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
appId, env, clusterName, namespace.getNamespaceName(), e);
throw e;
}
}

return namespaceVOs;
}

@SuppressWarnings("unchecked")
private NamespaceVO parseNamespace(String appId, Env env, String clusterName, NamespaceDTO namespace) {
NamespaceVO namespaceVO = new NamespaceVO();
namespaceVO.setNamespace(namespace);

List<NamespaceVO.ItemVO> itemVos = new LinkedList<>();
namespaceVO.setItems(itemVos);

String namespaceName = namespace.getNamespaceName();

//latest Release
ReleaseDTO release = null;
Map<String, String> releaseItems = new HashMap<>();
try {
release = releaseAPI.loadLatestRelease(appId, env, clusterName, namespaceName);
releaseItems = gson.fromJson(release.getConfigurations(), Map.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
logger.warn(ExceptionUtils.toString(e));
} else {
throw e;
}
}

//not Release config items
List<ItemDTO> items = itemAPI.findItems(appId, env, clusterName, namespaceName);
int modifiedItemCnt = 0;
for (ItemDTO itemDTO : items) {

NamespaceVO.ItemVO itemVO = parseItemVO(itemDTO, releaseItems);

if (itemVO.isModified()) {
modifiedItemCnt++;
}

itemVos.add(itemVO);
}
namespaceVO.setItemModifiedCnt(modifiedItemCnt);

return namespaceVO;
}

private NamespaceVO.ItemVO parseItemVO(ItemDTO itemDTO, Map<String, String> releaseItems) {
String key = itemDTO.getKey();
NamespaceVO.ItemVO itemVO = new NamespaceVO.ItemVO();
itemVO.setItem(itemDTO);
String newValue = itemDTO.getValue();
String oldValue = releaseItems.get(key);
if (!StringUtils.isEmpty(key) && (oldValue == null || !newValue.equals(oldValue))) {
itemVO.setModified(true);
itemVO.setOldValue(oldValue == null ? "" : oldValue);
itemVO.setNewValue(newValue);
}
return itemVO;
}

/**
* parse config text and update config items
Expand All @@ -160,10 +76,8 @@ public void updateConfigItemByText(NamespaceTextModel model) {
namespaceName);
throw new ServiceException(e.getMessage());
}

}


/**
* createRelease config items
*/
Expand All @@ -190,7 +104,6 @@ public void syncItems(List<NamespaceIdentifer> comparedNamespaces, List<ItemDTO>
throw new ServiceException(String.format("sync item error. env:%s, clusterName:%s", namespaceIdentifer.getEnv(),
namespaceIdentifer.getClusterName()), e);
}

}
}

Expand Down
@@ -1,24 +1,51 @@
package com.ctrip.apollo.portal.service;

import com.google.gson.Gson;

import com.ctrip.apollo.common.utils.ExceptionUtils;
import com.ctrip.apollo.core.dto.AppNamespaceDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.NamespaceDTO;
import com.ctrip.apollo.core.dto.ReleaseDTO;
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.PortalSettings;
import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.entity.NamespaceVO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class NamespaceService {

private Logger logger = LoggerFactory.getLogger(NamespaceService.class);

@Autowired
private AdminServiceAPI.ItemAPI itemAPI;

@Autowired
private AdminServiceAPI.ReleaseAPI releaseAPI;

@Autowired
private AdminServiceAPI.NamespaceAPI namespaceAPI;

@Autowired
private PortalSettings portalSettings;

private Gson gson = new Gson();


public List<AppNamespaceDTO> findPublicAppNamespaces(){
return namespaceAPI.findPublicAppNamespaces(portalSettings.getFirstEnv());
}
Expand All @@ -27,4 +54,87 @@ public NamespaceDTO save(Env env, NamespaceDTO namespace){
return namespaceAPI.save(env, namespace);
}

/**
* load cluster all namespace info with items
*/
public List<NamespaceVO> findNampspaces(String appId, Env env, String clusterName) {

List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
if (namespaces == null || namespaces.size() == 0) {
return Collections.emptyList();
}

List<NamespaceVO> namespaceVOs = new LinkedList<>();
for (NamespaceDTO namespace : namespaces) {

NamespaceVO namespaceVO = null;
try {
namespaceVO = parseNamespace(appId, env, clusterName, namespace);
namespaceVOs.add(namespaceVO);
} catch (Exception e) {
logger.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
appId, env, clusterName, namespace.getNamespaceName(), e);
throw e;
}
}

return namespaceVOs;
}

@SuppressWarnings("unchecked")
private NamespaceVO parseNamespace(String appId, Env env, String clusterName, NamespaceDTO namespace) {
NamespaceVO namespaceVO = new NamespaceVO();
namespaceVO.setNamespace(namespace);

List<NamespaceVO.ItemVO> itemVos = new LinkedList<>();
namespaceVO.setItems(itemVos);

String namespaceName = namespace.getNamespaceName();

//latest Release
ReleaseDTO release = null;
Map<String, String> releaseItems = new HashMap<>();
try {
release = releaseAPI.loadLatestRelease(appId, env, clusterName, namespaceName);
releaseItems = gson.fromJson(release.getConfigurations(), Map.class);
} catch (HttpClientErrorException e) {
if (e.getStatusCode() == HttpStatus.NOT_FOUND) {
logger.warn(ExceptionUtils.toString(e));
} else {
throw e;
}
}

//not Release config items
List<ItemDTO> items = itemAPI.findItems(appId, env, clusterName, namespaceName);
int modifiedItemCnt = 0;
for (ItemDTO itemDTO : items) {

NamespaceVO.ItemVO itemVO = parseItemVO(itemDTO, releaseItems);

if (itemVO.isModified()) {
modifiedItemCnt++;
}

itemVos.add(itemVO);
}
namespaceVO.setItemModifiedCnt(modifiedItemCnt);

return namespaceVO;
}

private NamespaceVO.ItemVO parseItemVO(ItemDTO itemDTO, Map<String, String> releaseItems) {
String key = itemDTO.getKey();
NamespaceVO.ItemVO itemVO = new NamespaceVO.ItemVO();
itemVO.setItem(itemDTO);
String newValue = itemDTO.getValue();
String oldValue = releaseItems.get(key);
if (!StringUtils.isEmpty(key) && (oldValue == null || !newValue.equals(oldValue))) {
itemVO.setModified(true);
itemVO.setOldValue(oldValue == null ? "" : oldValue);
itemVO.setNewValue(newValue);
}
return itemVO;
}

}
Expand Up @@ -8,7 +8,7 @@
@RunWith(Suite.class)
@SuiteClasses({
ConfigServiceTest.class, PropertyResolverTest.class,
AppServiceTest.class
AppServiceTest.class, NamespaceServiceTest.class
})
public class AllTests {

Expand Down

0 comments on commit 9fb54c6

Please sign in to comment.