Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: openapi query namespace support not fill item #5249

Merged
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Apollo 2.4.0
* [Feature add limit for items count per namespace](https://github.com/apolloconfig/apollo/pull/5227)
* [Feature: Add ConfigService cache record stats function](https://github.com/apolloconfig/apollo/pull/5247)
* [RefreshAdminServerAddressTask supports dynamic configuration of time interval](https://github.com/apolloconfig/apollo/pull/5248)
* [Feature: openapi query namespace support not fill item](https://github.com/apolloconfig/apollo/pull/5249)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo/milestone/15?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ public ServerNamespaceOpenApiService(

@Override
public OpenNamespaceDTO getNamespace(String appId, String env, String clusterName,
String namespaceName) {
String namespaceName, boolean fillItemDetail) {
NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf
(env), clusterName, namespaceName, false);
(env), clusterName, namespaceName, fillItemDetail, false);
if (namespaceBO == null) {
return null;
}
return OpenApiBeanUtils.transformFromNamespaceBO(namespaceBO);
}

@Override
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName) {
public List<OpenNamespaceDTO> getNamespaces(String appId, String env, String clusterName, boolean fillItemDetail) {
return OpenApiBeanUtils
.batchTransformFromNamespaceBOs(namespaceService.findNamespaceBOs(appId, Env
.valueOf(env), clusterName, false));
.valueOf(env), clusterName, fillItemDetail, false));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -81,15 +82,16 @@ public OpenAppNamespaceDTO createNamespace(@PathVariable String appId,

@GetMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces")
public List<OpenNamespaceDTO> findNamespaces(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName) {
return this.namespaceOpenApiService.getNamespaces(appId, env, clusterName);
@PathVariable String clusterName,
@RequestParam(defaultValue="true") boolean fillItemDetail) {
youngzil marked this conversation as resolved.
Show resolved Hide resolved
return this.namespaceOpenApiService.getNamespaces(appId, env, clusterName, fillItemDetail);
}

@GetMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName:.+}")
public OpenNamespaceDTO loadNamespace(@PathVariable String appId, @PathVariable String env,
@PathVariable String clusterName, @PathVariable String
namespaceName) {
return this.namespaceOpenApiService.getNamespace(appId, env, clusterName, namespaceName);
@PathVariable String clusterName, @PathVariable String namespaceName,
@RequestParam(defaultValue="true") boolean fillItemDetail) {
return this.namespaceOpenApiService.getNamespace(appId, env, clusterName, namespaceName, fillItemDetail);
}

@GetMapping(value = "/openapi/v1/envs/{env}/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/lock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void exportItems(@PathVariable String appId, @PathVariable String env,
}

NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf
(env), clusterName, namespaceName, false);
(env), clusterName, namespaceName, true, false);

//generate a file.
res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void exportNamespaces(final Env env, final App exportApp, final ClusterD
ZipOutputStream zipOutputStream) {
String clusterName = exportCluster.getName();

List<NamespaceBO> namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName, false);
List<NamespaceBO> namespaceBOS = namespaceService.findNamespaceBOs(exportApp.getAppId(), env, clusterName, true, false);

if (CollectionUtils.isEmpty(namespaceBOS)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public NamespaceDTO loadNamespaceBaseInfo(String appId, Env env, String clusterN
/**
* load cluster all namespace info with items
*/
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName, boolean includeDeletedItems) {
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName, boolean fillItemDetail, boolean includeDeletedItems) {

List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
if (namespaces == null || namespaces.size() == 0) {
Expand All @@ -200,7 +200,7 @@ public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterN
executorService.submit(() -> {
NamespaceBO namespaceBO;
try {
namespaceBO = transformNamespace2BO(env, namespace, includeDeletedItems);
namespaceBO = transformNamespace2BO(env, namespace, fillItemDetail, includeDeletedItems);
namespaceBOs.add(namespaceBO);
} catch (Exception e) {
LOGGER.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}",
Expand Down Expand Up @@ -229,7 +229,7 @@ public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterN
}

public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName) {
return findNamespaceBOs(appId, env, clusterName, true);
return findNamespaceBOs(appId, env, clusterName, true, true);
}

public List<NamespaceDTO> findNamespaces(String appId, Env env, String clusterName) {
Expand All @@ -250,17 +250,17 @@ public List<NamespaceDTO> getPublicAppNamespaceAllNamespaces(Env env, String pub
}

public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
String namespaceName, boolean includeDeletedItems) {
String namespaceName, boolean fillItemDetail, boolean includeDeletedItems) {
NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
if (namespace == null) {
throw BadRequestException.namespaceNotExists(appId, clusterName, namespaceName);
}
return transformNamespace2BO(env, namespace, includeDeletedItems);
return transformNamespace2BO(env, namespace, fillItemDetail, includeDeletedItems);
}

public NamespaceBO loadNamespaceBO(String appId, Env env, String clusterName,
String namespaceName) {
return loadNamespaceBO(appId, env, clusterName, namespaceName, true);
return loadNamespaceBO(appId, env, clusterName, namespaceName, true, true);
}

public boolean publicAppNamespaceHasAssociatedNamespace(String publicNamespaceName, Env env) {
Expand Down Expand Up @@ -293,7 +293,7 @@ public Map<String, Map<String, Boolean>> getNamespacesPublishInfo(String appId)
return result;
}

private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boolean includeDeletedItems) {
private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boolean fillItemDetail, boolean includeDeletedItems) {
NamespaceBO namespaceBO = new NamespaceBO();
namespaceBO.setBaseInfo(namespace);

Expand All @@ -306,6 +306,10 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole
List<ItemBO> itemBOs = new LinkedList<>();
namespaceBO.setItems(itemBOs);

if (!fillItemDetail) {
return namespaceBO;
}

//latest Release
ReleaseDTO latestRelease;
Map<String, String> releaseItems = new HashMap<>();
Expand Down Expand Up @@ -347,7 +351,7 @@ private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace, boole
}

private NamespaceBO transformNamespace2BO(Env env, NamespaceDTO namespace) {
return transformNamespace2BO(env, namespace, true);
return transformNamespace2BO(env, namespace, true, true);
}

private void fillAppNamespaceProperties(NamespaceBO namespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ public class ConfigsExportServiceTest extends AbstractUnitTest {

@Test
public void testNamespaceExportImport() throws FileNotFoundException {
// Test with fillItemDetail = true
testExportImportScenario(true);
}

@Test
public void testNamespaceExportImportWithFillItemDetail() throws FileNotFoundException {
// Test with fillItemDetail = false
testExportImportScenario(false);
}

private void testExportImportScenario(boolean fillItemDetail) throws FileNotFoundException {

File temporaryFolder = Files.newTemporaryFolder();
temporaryFolder.deleteOnExit();
String filePath = temporaryFolder + File.separator + "export.zip";
Expand Down Expand Up @@ -146,10 +158,10 @@ public void testNamespaceExportImport() throws FileNotFoundException {
when(permissionValidator.isAppAdmin(any())).thenReturn(true);
when(clusterService.findClusters(env, appId1)).thenReturn(app1Clusters);
when(clusterService.findClusters(env, appId2)).thenReturn(app2Clusters);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1, false)).thenReturn(app1Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2, false)).thenReturn(app1Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1, false)).thenReturn(app2Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2, false)).thenReturn(app2Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName1, fillItemDetail, false)).thenReturn(app1Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2, fillItemDetail, false)).thenReturn(app1Cluster2Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1, fillItemDetail, false)).thenReturn(app2Cluster1Namespace);
when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2, fillItemDetail, false)).thenReturn(app2Cluster2Namespace);

FileOutputStream fileOutputStream = new FileOutputStream(filePath);

Expand Down Expand Up @@ -186,10 +198,12 @@ public void testNamespaceExportImport() throws FileNotFoundException {

verify(clusterService, times(4)).createCluster(any(), any());

verify(namespaceService, times(6)).createNamespace(any(), any());
verify(roleInitializationService,times(6)).initNamespaceRoles(any(), any(), anyString());
verify(roleInitializationService,times(6)).initNamespaceEnvRoles(any(), any(), anyString());
verify(itemService, times(12)).createItem(any(), any(), any(), any(), any());
if(fillItemDetail){
verify(namespaceService, times(6)).createNamespace(any(), any());
verify(roleInitializationService,times(6)).initNamespaceRoles(any(), any(), anyString());
verify(roleInitializationService,times(6)).initNamespaceEnvRoles(any(), any(), anyString());
verify(itemService, times(12)).createItem(any(), any(), any(), any(), any());
}
}

private App genApp(String name, String appId, String orgId, String orgName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,24 @@ public void testDeleteEmptyNamespace() {

@Test
public void testLoadNamespaceBO() {
boolean fillItemDetail = true;
NamespaceBO namespaceBO = loadNamespaceBO(fillItemDetail);

List<String> namespaceKey2 = namespaceBO.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO.getItemModifiedCnt()).isEqualTo(2);
assertThat(namespaceBO.getItems().size()).isEqualTo(2);
assertThat(namespaceKey2).isEqualTo(Arrays.asList("k1", "k2"));
}

@Test
public void testLoadNamespaceBOWithoutItemDetail() {
boolean fillItemDetail = false;
NamespaceBO namespaceBO = loadNamespaceBO(fillItemDetail);

assertThat(namespaceBO.getItems().size()).isEqualTo(0);
}

private NamespaceBO loadNamespaceBO(boolean fillItemDetail) {
String branchName = "branch";
NamespaceDTO namespaceDTO = createNamespace(testAppId, branchName, testNamespaceName);
when(namespaceAPI.loadNamespace(any(), any(), any(), any())).thenReturn(namespaceDTO);
Expand Down Expand Up @@ -265,11 +283,49 @@ public void testLoadNamespaceBO() {
assertThat(namespaceBO1.getItems().size()).isEqualTo(3);
assertThat(namespaceKey1).isEqualTo(Arrays.asList("k1", "k2", "k3"));

NamespaceBO namespaceBO2 = namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName, false);
List<String> namespaceKey2 = namespaceBO2.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO2.getItemModifiedCnt()).isEqualTo(2);
assertThat(namespaceBO2.getItems().size()).isEqualTo(2);
assertThat(namespaceKey2).isEqualTo(Arrays.asList("k1", "k2"));
return namespaceService.loadNamespaceBO(testAppId, testEnv, testClusterName, testNamespaceName, fillItemDetail, false);
}

@Test
public void testFindPublicNamespaceForAssociatedNamespace() {
String branchName = "branch";
NamespaceDTO namespaceDTO = createNamespace(testAppId, branchName, testNamespaceName);
when(namespaceAPI.findPublicNamespaceForAssociatedNamespace(any(), any(), any(), any())).thenReturn(namespaceDTO);

ReleaseDTO releaseDTO = new ReleaseDTO();
releaseDTO.setConfigurations("{\"k1\":\"k1\",\"k2\":\"k2\", \"k3\":\"\"}");
when(releaseService.loadLatestRelease(any(), any(), any(), any())).thenReturn(releaseDTO);

List<ItemDTO> itemDTOList = Lists.newArrayList();
ItemDTO itemDTO1 = new ItemDTO();
itemDTO1.setId(1);
itemDTO1.setNamespaceId(1);
itemDTO1.setKey("k1");
itemDTO1.setValue(String.valueOf(1));
itemDTOList.add(itemDTO1);

ItemDTO itemDTO2 = new ItemDTO();
itemDTO2.setId(2);
itemDTO2.setNamespaceId(2);
itemDTO2.setKey("k2");
itemDTO2.setValue(String.valueOf(2));
itemDTOList.add(itemDTO2);
when(itemService.findItems(any(), any(), any(), any())).thenReturn(itemDTOList);

List<ItemDTO> deletedItemDTOList = Lists.newArrayList();
ItemDTO deletedItemDTO = new ItemDTO();
deletedItemDTO.setId(3);
deletedItemDTO.setNamespaceId(3);
deletedItemDTO.setKey("k3");
deletedItemDTOList.add(deletedItemDTO);
when(itemService.findDeletedItems(any(), any(), any(), any())).thenReturn(deletedItemDTOList);

NamespaceBO namespaceBO = namespaceService.findPublicNamespaceForAssociatedNamespace(testEnv, testAppId, testClusterName, testNamespaceName);

List<String> namespaceKey2 = namespaceBO.getItems().stream().map(s -> s.getItem().getKey()).collect(Collectors.toList());
assertThat(namespaceBO.getItemModifiedCnt()).isEqualTo(3);
assertThat(namespaceBO.getItems().size()).isEqualTo(3);
assertThat(namespaceKey2).isEqualTo(Arrays.asList("k1", "k2", "k3"));
youngzil marked this conversation as resolved.
Show resolved Hide resolved
}

private AppNamespace createAppNamespace(String appId, String name, boolean isPublic) {
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<revision>2.4.0-SNAPSHOT</revision>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<apollo-java.version>2.2.0</apollo-java.version>
<apollo-java.version>2.4.0-SNAPSHOT</apollo-java.version>
<spring-boot.version>2.7.11</spring-boot.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
<!-- sort by alphabet -->
Expand Down Expand Up @@ -649,4 +649,4 @@
</snapshots>
</repository>
</repositories>
</project>
</project>
Loading