Commit 9fb54c68 by lepdou

update

parent f2c7de85
...@@ -50,7 +50,7 @@ public class AdminServiceTransactionTest { ...@@ -50,7 +50,7 @@ public class AdminServiceTransactionTest {
System.out.println(app.getAppId()); System.out.println(app.getAppId());
} }
Assert.assertEquals(0, appRepository.count()); Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count()); Assert.assertEquals(7, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count()); Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count()); Assert.assertEquals(0, clusterRepository.count());
} }
...@@ -58,7 +58,7 @@ public class AdminServiceTransactionTest { ...@@ -58,7 +58,7 @@ public class AdminServiceTransactionTest {
@Before @Before
public void setUpTestDataWithinTransaction() { public void setUpTestDataWithinTransaction() {
Assert.assertEquals(0, appRepository.count()); Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count()); Assert.assertEquals(7, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count()); Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count()); Assert.assertEquals(0, clusterRepository.count());
} }
...@@ -82,7 +82,7 @@ public class AdminServiceTransactionTest { ...@@ -82,7 +82,7 @@ public class AdminServiceTransactionTest {
@After @After
public void tearDownWithinTransaction() { public void tearDownWithinTransaction() {
Assert.assertEquals(1, appRepository.count()); Assert.assertEquals(1, appRepository.count());
Assert.assertEquals(1, appNamespaceRepository.count()); Assert.assertEquals(8, appNamespaceRepository.count());
Assert.assertEquals(1, namespaceRepository.count()); Assert.assertEquals(1, namespaceRepository.count());
Assert.assertEquals(1, clusterRepository.count()); Assert.assertEquals(1, clusterRepository.count());
} }
...@@ -90,7 +90,7 @@ public class AdminServiceTransactionTest { ...@@ -90,7 +90,7 @@ public class AdminServiceTransactionTest {
@AfterTransaction @AfterTransaction
public void verifyFinalDatabaseState() { public void verifyFinalDatabaseState() {
Assert.assertEquals(0, appRepository.count()); Assert.assertEquals(0, appRepository.count());
Assert.assertEquals(0, appNamespaceRepository.count()); Assert.assertEquals(7, appNamespaceRepository.count());
Assert.assertEquals(0, namespaceRepository.count()); Assert.assertEquals(0, namespaceRepository.count());
Assert.assertEquals(0, clusterRepository.count()); Assert.assertEquals(0, clusterRepository.count());
} }
......
...@@ -31,16 +31,6 @@ public class ConfigController { ...@@ -31,16 +31,6 @@ public class ConfigController {
@Autowired @Autowired
private ConfigService configService; 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 = { @RequestMapping(value = "/apps/{appId}/env/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items", method = RequestMethod.PUT, consumes = {
"application/json"}) "application/json"})
public void modifyItems(@PathVariable String appId, @PathVariable String env, public void modifyItems(@PathVariable String appId, @PathVariable String env,
......
...@@ -5,6 +5,7 @@ import com.ctrip.apollo.core.dto.NamespaceDTO; ...@@ -5,6 +5,7 @@ import com.ctrip.apollo.core.dto.NamespaceDTO;
import com.ctrip.apollo.core.enums.Env; import com.ctrip.apollo.core.enums.Env;
import com.ctrip.apollo.core.exception.BadRequestException; import com.ctrip.apollo.core.exception.BadRequestException;
import com.ctrip.apollo.core.utils.StringUtils; import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.entity.NamespaceVO;
import com.ctrip.apollo.portal.service.NamespaceService; import com.ctrip.apollo.portal.service.NamespaceService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -18,6 +19,7 @@ import java.util.List; ...@@ -18,6 +19,7 @@ import java.util.List;
@RestController @RestController
public class NamespaceController { public class NamespaceController {
@Autowired @Autowired
private NamespaceService namespaceService; private NamespaceService namespaceService;
...@@ -35,4 +37,14 @@ public class NamespaceController { ...@@ -35,4 +37,14 @@ public class NamespaceController {
} }
@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);
}
} }
...@@ -50,90 +50,6 @@ public class ConfigService { ...@@ -50,90 +50,6 @@ public class ConfigService {
@Autowired @Autowired
private ConfigTextResolver resolver; 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 * parse config text and update config items
...@@ -160,10 +76,8 @@ public class ConfigService { ...@@ -160,10 +76,8 @@ public class ConfigService {
namespaceName); namespaceName);
throw new ServiceException(e.getMessage()); throw new ServiceException(e.getMessage());
} }
} }
/** /**
* createRelease config items * createRelease config items
*/ */
...@@ -190,7 +104,6 @@ public class ConfigService { ...@@ -190,7 +104,6 @@ public class ConfigService {
throw new ServiceException(String.format("sync item error. env:%s, clusterName:%s", namespaceIdentifer.getEnv(), throw new ServiceException(String.format("sync item error. env:%s, clusterName:%s", namespaceIdentifer.getEnv(),
namespaceIdentifer.getClusterName()), e); namespaceIdentifer.getClusterName()), e);
} }
} }
} }
......
package com.ctrip.apollo.portal.service; 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.AppNamespaceDTO;
import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.NamespaceDTO; 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.enums.Env;
import com.ctrip.apollo.core.utils.StringUtils;
import com.ctrip.apollo.portal.PortalSettings; import com.ctrip.apollo.portal.PortalSettings;
import com.ctrip.apollo.portal.api.AdminServiceAPI; 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.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service; 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.List;
import java.util.Map;
@Service @Service
public class NamespaceService { public class NamespaceService {
private Logger logger = LoggerFactory.getLogger(NamespaceService.class);
@Autowired
private AdminServiceAPI.ItemAPI itemAPI;
@Autowired
private AdminServiceAPI.ReleaseAPI releaseAPI;
@Autowired @Autowired
private AdminServiceAPI.NamespaceAPI namespaceAPI; private AdminServiceAPI.NamespaceAPI namespaceAPI;
@Autowired @Autowired
private PortalSettings portalSettings; private PortalSettings portalSettings;
private Gson gson = new Gson();
public List<AppNamespaceDTO> findPublicAppNamespaces(){ public List<AppNamespaceDTO> findPublicAppNamespaces(){
return namespaceAPI.findPublicAppNamespaces(portalSettings.getFirstEnv()); return namespaceAPI.findPublicAppNamespaces(portalSettings.getFirstEnv());
} }
...@@ -27,4 +54,87 @@ public class NamespaceService { ...@@ -27,4 +54,87 @@ public class NamespaceService {
return namespaceAPI.save(env, 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;
}
} }
...@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses; ...@@ -8,7 +8,7 @@ import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class) @RunWith(Suite.class)
@SuiteClasses({ @SuiteClasses({
ConfigServiceTest.class, PropertyResolverTest.class, ConfigServiceTest.class, PropertyResolverTest.class,
AppServiceTest.class AppServiceTest.class, NamespaceServiceTest.class
}) })
public class AllTests { public class AllTests {
......
...@@ -4,12 +4,10 @@ import com.ctrip.apollo.core.ConfigConsts; ...@@ -4,12 +4,10 @@ import com.ctrip.apollo.core.ConfigConsts;
import com.ctrip.apollo.core.dto.ItemChangeSets; import com.ctrip.apollo.core.dto.ItemChangeSets;
import com.ctrip.apollo.core.dto.ItemDTO; import com.ctrip.apollo.core.dto.ItemDTO;
import com.ctrip.apollo.core.dto.NamespaceDTO; 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.enums.Env;
import com.ctrip.apollo.portal.api.AdminServiceAPI; import com.ctrip.apollo.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.entity.ItemDiffs; import com.ctrip.apollo.portal.entity.ItemDiffs;
import com.ctrip.apollo.portal.entity.NamespaceIdentifer; import com.ctrip.apollo.portal.entity.NamespaceIdentifer;
import com.ctrip.apollo.portal.entity.NamespaceVO;
import com.ctrip.apollo.portal.entity.form.NamespaceTextModel; import com.ctrip.apollo.portal.entity.form.NamespaceTextModel;
import com.ctrip.apollo.portal.service.ConfigService; import com.ctrip.apollo.portal.service.ConfigService;
import com.ctrip.apollo.portal.service.txtresolver.PropertyResolver; import com.ctrip.apollo.portal.service.txtresolver.PropertyResolver;
...@@ -48,51 +46,6 @@ public class ConfigServiceTest { ...@@ -48,51 +46,6 @@ public class ConfigServiceTest {
} }
@Test @Test
public void testFindNamespace() {
String appId = "6666";
String clusterName = "default";
String namespaceName = "application";
NamespaceDTO application = new NamespaceDTO();
application.setId(1);
application.setClusterName(clusterName);
application.setAppId(appId);
application.setNamespaceName(namespaceName);
NamespaceDTO hermas = new NamespaceDTO();
hermas.setId(2);
hermas.setClusterName("default");
hermas.setAppId(appId);
hermas.setNamespaceName("hermas");
List<NamespaceDTO> namespaces = Arrays.asList(application, hermas);
ReleaseDTO someRelease = new ReleaseDTO();
someRelease.setConfigurations("{\"a\":\"123\",\"b\":\"123\"}");
ItemDTO i1 = new ItemDTO("a", "123", "", 1);
ItemDTO i2 = new ItemDTO("b", "1", "", 2);
ItemDTO i3 = new ItemDTO("", "", "#dddd", 3);
ItemDTO i4 = new ItemDTO("c", "1", "", 4);
List<ItemDTO> someItems = Arrays.asList(i1, i2, i3, i4);
when(namespaceAPI.findNamespaceByCluster(appId, Env.DEV, clusterName)).thenReturn(namespaces);
when(releaseAPI.loadLatestRelease(appId, Env.DEV, clusterName, namespaceName)).thenReturn(someRelease);
when(releaseAPI.loadLatestRelease(appId, Env.DEV, clusterName, "hermas")).thenReturn(someRelease);
when(itemAPI.findItems(appId, Env.DEV, clusterName, namespaceName)).thenReturn(someItems);
List<NamespaceVO> namespaceVOs = configService.findNampspaces(appId, Env.DEV, clusterName);
assertEquals(2, namespaceVOs.size());
NamespaceVO namespaceVO = namespaceVOs.get(0);
assertEquals(4, namespaceVO.getItems().size());
assertEquals("a", namespaceVO.getItems().get(0).getItem().getKey());
assertEquals(2, namespaceVO.getItemModifiedCnt());
assertEquals(appId, namespaceVO.getNamespace().getAppId());
assertEquals(clusterName, namespaceVO.getNamespace().getClusterName());
assertEquals(namespaceName, namespaceVO.getNamespace().getNamespaceName());
}
@Test
public void testUpdateConfigByText() { public void testUpdateConfigByText() {
String appId = "6666"; String appId = "6666";
String clusterName = "default"; String clusterName = "default";
......
package com.ctrip.apollo.portal;
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.portal.api.AdminServiceAPI;
import com.ctrip.apollo.portal.entity.NamespaceVO;
import com.ctrip.apollo.portal.service.NamespaceService;
import com.ctrip.apollo.portal.service.txtresolver.PropertyResolver;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class NamespaceServiceTest {
@Mock
private AdminServiceAPI.NamespaceAPI namespaceAPI;
@Mock
private AdminServiceAPI.ReleaseAPI releaseAPI;
@Mock
private AdminServiceAPI.ItemAPI itemAPI;
@Mock
private PropertyResolver resolver;
@InjectMocks
private NamespaceService namespaceService;
@Before
public void setup() {
}
@Test
public void testFindNamespace() {
String appId = "6666";
String clusterName = "default";
String namespaceName = "application";
NamespaceDTO application = new NamespaceDTO();
application.setId(1);
application.setClusterName(clusterName);
application.setAppId(appId);
application.setNamespaceName(namespaceName);
NamespaceDTO hermas = new NamespaceDTO();
hermas.setId(2);
hermas.setClusterName("default");
hermas.setAppId(appId);
hermas.setNamespaceName("hermas");
List<NamespaceDTO> namespaces = Arrays.asList(application, hermas);
ReleaseDTO someRelease = new ReleaseDTO();
someRelease.setConfigurations("{\"a\":\"123\",\"b\":\"123\"}");
ItemDTO i1 = new ItemDTO("a", "123", "", 1);
ItemDTO i2 = new ItemDTO("b", "1", "", 2);
ItemDTO i3 = new ItemDTO("", "", "#dddd", 3);
ItemDTO i4 = new ItemDTO("c", "1", "", 4);
List<ItemDTO> someItems = Arrays.asList(i1, i2, i3, i4);
when(namespaceAPI.findNamespaceByCluster(appId, Env.DEV, clusterName)).thenReturn(namespaces);
when(releaseAPI.loadLatestRelease(appId, Env.DEV, clusterName, namespaceName)).thenReturn(someRelease);
when(releaseAPI.loadLatestRelease(appId, Env.DEV, clusterName, "hermas")).thenReturn(someRelease);
when(itemAPI.findItems(appId, Env.DEV, clusterName, namespaceName)).thenReturn(someItems);
List<NamespaceVO> namespaceVOs = namespaceService.findNampspaces(appId, Env.DEV, clusterName);
assertEquals(2, namespaceVOs.size());
NamespaceVO namespaceVO = namespaceVOs.get(0);
assertEquals(4, namespaceVO.getItems().size());
assertEquals("a", namespaceVO.getItems().get(0).getItem().getKey());
assertEquals(2, namespaceVO.getItemModifiedCnt());
assertEquals(appId, namespaceVO.getNamespace().getAppId());
assertEquals(clusterName, namespaceVO.getNamespace().getClusterName());
assertEquals(namespaceName, namespaceVO.getNamespace().getNamespaceName());
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment