Search in sources :

Example 1 with NamespaceBO

use of com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO in project apollo by ctripcorp.

the class NamespaceBranchService method calculateBranchChangeSet.

private ItemChangeSets calculateBranchChangeSet(String appId, Env env, String clusterName, String namespaceName, String branchName, String operator) {
    NamespaceBO parentNamespace = namespaceService.loadNamespaceBO(appId, env, clusterName, namespaceName);
    if (parentNamespace == null) {
        throw new BadRequestException("base namespace not existed");
    }
    if (parentNamespace.getItemModifiedCnt() > 0) {
        throw new BadRequestException("Merge operation failed. Because master has modified items");
    }
    List<ItemDTO> masterItems = itemService.findItems(appId, env, clusterName, namespaceName);
    List<ItemDTO> branchItems = itemService.findItems(appId, env, branchName, namespaceName);
    ItemChangeSets changeSets = itemsComparator.compareIgnoreBlankAndCommentItem(parentNamespace.getBaseInfo().getId(), masterItems, branchItems);
    changeSets.setDeleteItems(Collections.emptyList());
    changeSets.setDataChangeLastModifiedBy(operator);
    return changeSets;
}
Also used : NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 2 with NamespaceBO

use of com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO 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<>();
    Map<String, ItemDTO> deletedItemDTOs = 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);
    additionalUserInfoEnrichService.enrichAdditionalUserInfo(items, BaseDtoUserInfoEnrichedAdapter::new);
    int modifiedItemCnt = 0;
    for (ItemDTO itemDTO : items) {
        ItemBO itemBO = transformItem2BO(itemDTO, releaseItems);
        if (itemBO.isModified()) {
            modifiedItemCnt++;
        }
        itemBOs.add(itemBO);
    }
    // deleted items
    itemService.findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> {
        deletedItemDTOs.put(item.getKey(), item);
    });
    List<ItemBO> deletedItems = parseDeletedItems(items, releaseItems, deletedItemDTOs);
    itemBOs.addAll(deletedItems);
    modifiedItemCnt += deletedItems.size();
    namespaceBO.setItemModifiedCnt(modifiedItemCnt);
    return namespaceBO;
}
Also used : BaseDtoUserInfoEnrichedAdapter(com.ctrip.framework.apollo.portal.enricher.adapter.BaseDtoUserInfoEnrichedAdapter) ItemBO(com.ctrip.framework.apollo.portal.entity.bo.ItemBO) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO)

Example 3 with NamespaceBO

use of com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO in project apollo by ctripcorp.

the class ConfigsExportServiceTest method testNamespaceExportImport.

@Test
public void testNamespaceExportImport() throws FileNotFoundException {
    String filePath = "/tmp/apollo.zip";
    File file = new File(filePath);
    if (file.exists()) {
        file.delete();
    }
    // export config
    UserInfo userInfo = genUser();
    when(userInfoHolder.getUser()).thenReturn(userInfo);
    Env env = Env.DEV;
    String appId1 = "app1";
    String appId2 = "app2";
    App app1 = genApp(appId1, appId1, "org1", "org2");
    App app2 = genApp(appId2, appId2, "org1", "org2");
    List<App> exportApps = Lists.newArrayList(app1, app2);
    String appNamespaceName1 = "ns1";
    String appNamespaceName2 = "ns2";
    AppNamespace app1Namespace1 = genAppNamespace(appId1, appNamespaceName1, false);
    AppNamespace app1Namespace2 = genAppNamespace(appId1, appNamespaceName2, true);
    AppNamespace app2Namespace1 = genAppNamespace(appId2, appNamespaceName1, false);
    List<AppNamespace> appNamespaces = Lists.newArrayList(app1Namespace1, app1Namespace2, app2Namespace1);
    String clusterName1 = "c1";
    String clusterName2 = "c2";
    ClusterDTO app1Cluster1 = genCluster(clusterName1, appId1);
    ClusterDTO app1Cluster2 = genCluster(clusterName2, appId1);
    ClusterDTO app2Cluster1 = genCluster(clusterName1, appId2);
    ClusterDTO app2Cluster2 = genCluster(clusterName2, appId2);
    List<ClusterDTO> app1Clusters = Lists.newArrayList(app1Cluster1, app1Cluster2);
    List<ClusterDTO> app2Clusters = Lists.newArrayList(app2Cluster1, app2Cluster2);
    ItemBO item1 = genItem("k1", "v1");
    ItemBO item2 = genItem("k2", "v2");
    List<ItemBO> items = Lists.newArrayList(item1, item2);
    String namespaceName1 = "namespace1";
    String namespaceName2 = "namespace2";
    NamespaceBO app1Cluster1Namespace1 = genNamespace(app1, app1Cluster1, items, namespaceName1);
    NamespaceBO app1Cluster1Namespace2 = genNamespace(app1, app1Cluster1, items, namespaceName2);
    List<NamespaceBO> app1Cluster1Namespace = Lists.newArrayList(app1Cluster1Namespace1, app1Cluster1Namespace2);
    NamespaceBO app1Cluster2Namespace1 = genNamespace(app1, app1Cluster2, items, namespaceName1);
    List<NamespaceBO> app1Cluster2Namespace = Lists.newArrayList(app1Cluster2Namespace1);
    NamespaceBO app2Cluster1Namespace1 = genNamespace(app2, app1Cluster1, items, namespaceName1);
    List<NamespaceBO> app2Cluster1Namespace = Lists.newArrayList(app2Cluster1Namespace1);
    NamespaceBO app2Cluster2Namespace1 = genNamespace(app2, app1Cluster2, items, namespaceName1);
    NamespaceBO app2Cluster2Namespace2 = genNamespace(app2, app1Cluster2, items, namespaceName2);
    List<NamespaceBO> app2Cluster2Namespace = Lists.newArrayList(app2Cluster2Namespace1, app2Cluster2Namespace2);
    when(appService.findAll()).thenReturn(exportApps);
    when(appNamespaceService.findAll()).thenReturn(appNamespaces);
    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)).thenReturn(app1Cluster1Namespace);
    when(namespaceService.findNamespaceBOs(appId1, Env.DEV, clusterName2)).thenReturn(app1Cluster2Namespace);
    when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName1)).thenReturn(app2Cluster1Namespace);
    when(namespaceService.findNamespaceBOs(appId2, Env.DEV, clusterName2)).thenReturn(app2Cluster2Namespace);
    FileOutputStream fileOutputStream = new FileOutputStream("/tmp/apollo.zip");
    configsExportService.exportData(fileOutputStream, Lists.newArrayList(Env.DEV));
    // import config
    when(appNamespaceService.findByAppIdAndName(any(), any())).thenReturn(null);
    when(appNamespaceService.importAppNamespaceInLocal(any())).thenReturn(app1Namespace1);
    when(appService.load(any())).thenReturn(null);
    when(appService.load(any(), any())).thenThrow(new RuntimeException());
    when(clusterService.loadCluster(any(), any(), any())).thenThrow(new RuntimeException());
    when(namespaceService.loadNamespaceBaseInfo(any(), any(), any(), any())).thenThrow(new RuntimeException());
    when(namespaceService.createNamespace(any(), any())).thenReturn(genNamespaceDTO(1));
    when(itemService.findItems(any(), any(), any(), any())).thenReturn(Lists.newArrayList());
    HttpStatusCodeException itemNotFoundException = new HttpClientErrorException(HttpStatus.NOT_FOUND);
    when(itemService.loadItem(any(), any(), any(), any(), anyString())).thenThrow(itemNotFoundException);
    FileInputStream fileInputStream = new FileInputStream("/tmp/apollo.zip");
    ZipInputStream zipInputStream = new ZipInputStream(fileInputStream);
    try {
        configsImportService.importDataFromZipFile(Lists.newArrayList(Env.DEV), zipInputStream, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
    verify(appNamespaceService, times(3)).importAppNamespaceInLocal(any());
    verify(applicationEventPublisher, times(3)).publishEvent(any());
    verify(appService, times(2)).createAppInRemote(any(), any());
    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());
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) ItemBO(com.ctrip.framework.apollo.portal.entity.bo.ItemBO) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) ClusterDTO(com.ctrip.framework.apollo.common.dto.ClusterDTO) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Env(com.ctrip.framework.apollo.portal.environment.Env) FileInputStream(java.io.FileInputStream) HttpStatusCodeException(org.springframework.web.client.HttpStatusCodeException) FileNotFoundException(java.io.FileNotFoundException) HttpClientErrorException(org.springframework.web.client.HttpClientErrorException) ZipInputStream(java.util.zip.ZipInputStream) FileOutputStream(java.io.FileOutputStream) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) File(java.io.File) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 4 with NamespaceBO

use of com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO in project apollo by ctripcorp.

the class ConfigsExportController method exportItems.

/**
 * export one config as file.
 * keep compatibility.
 * file name examples:
 * <pre>
 *   application.properties
 *   application.yml
 *   application.json
 * </pre>
 */
@PreAuthorize(value = "!@permissionValidator.shouldHideConfigToCurrentUser(#appId, #env, #namespaceName)")
@GetMapping("/apps/{appId}/envs/{env}/clusters/{clusterName}/namespaces/{namespaceName}/items/export")
public void exportItems(@PathVariable String appId, @PathVariable String env, @PathVariable String clusterName, @PathVariable String namespaceName, HttpServletResponse res) {
    List<String> fileNameSplit = Splitter.on(".").splitToList(namespaceName);
    String fileName = namespaceName;
    // properties file or public namespace has not suffix (.properties)
    if (fileNameSplit.size() <= 1 || !ConfigFileFormat.isValidFormat(fileNameSplit.get(fileNameSplit.size() - 1))) {
        fileName = Joiner.on(".").join(namespaceName, ConfigFileFormat.Properties.getValue());
    }
    NamespaceBO namespaceBO = namespaceService.loadNamespaceBO(appId, Env.valueOf(env), clusterName, namespaceName);
    // generate a file.
    res.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + fileName);
    // file content
    final String configFileContent = NamespaceBOUtils.convert2configFileContent(namespaceBO);
    try {
        // write content to net
        res.getOutputStream().write(configFileContent.getBytes());
    } catch (Exception e) {
        throw new ServiceException("export items failed:{}", e);
    }
}
Also used : ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) IOException(java.io.IOException) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) GetMapping(org.springframework.web.bind.annotation.GetMapping) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 5 with NamespaceBO

use of com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO in project apollo by ctripcorp.

the class NamespaceService method findNamespaceBOs.

/**
 * load cluster all namespace info with items
 */
public List<NamespaceBO> findNamespaceBOs(String appId, Env env, String clusterName) {
    List<NamespaceDTO> namespaces = namespaceAPI.findNamespaceByCluster(appId, env, clusterName);
    if (namespaces == null || namespaces.size() == 0) {
        throw new BadRequestException("namespaces not exist");
    }
    List<NamespaceBO> namespaceBOs = new LinkedList<>();
    for (NamespaceDTO namespace : namespaces) {
        NamespaceBO namespaceBO;
        try {
            namespaceBO = transformNamespace2BO(env, namespace);
            namespaceBOs.add(namespaceBO);
        } catch (Exception e) {
            LOGGER.error("parse namespace error. app id:{}, env:{}, clusterName:{}, namespace:{}", appId, env, clusterName, namespace.getNamespaceName(), e);
            throw e;
        }
    }
    return namespaceBOs;
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) NamespaceBO(com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) LinkedList(java.util.LinkedList) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Aggregations

NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)8 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)3 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)2 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)2 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)2 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)2 ItemBO (com.ctrip.framework.apollo.portal.entity.bo.ItemBO)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)1 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)1 App (com.ctrip.framework.apollo.common.entity.App)1 ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)1 BaseDtoUserInfoEnrichedAdapter (com.ctrip.framework.apollo.portal.enricher.adapter.BaseDtoUserInfoEnrichedAdapter)1 ConfigBO (com.ctrip.framework.apollo.portal.entity.bo.ConfigBO)1 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)1 Env (com.ctrip.framework.apollo.portal.environment.Env)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1