Search in sources :

Example 6 with NamespaceDTO

use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.

the class NamespaceService method deleteNamespace.

@Transactional
public void deleteNamespace(String appId, Env env, String clusterName, String namespaceName) {
    AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
    // 1. check parent namespace has not instances
    if (namespaceHasInstances(appId, env, clusterName, namespaceName)) {
        throw new BadRequestException("Can not delete namespace because namespace has active instances");
    }
    // 2. check child namespace has not instances
    NamespaceDTO childNamespace = branchService.findBranchBaseInfo(appId, env, clusterName, namespaceName);
    if (childNamespace != null && namespaceHasInstances(appId, env, childNamespace.getClusterName(), namespaceName)) {
        throw new BadRequestException("Can not delete namespace because namespace's branch has active instances");
    }
    // 3. check public namespace has not associated namespace
    if (appNamespace != null && appNamespace.isPublic() && publicAppNamespaceHasAssociatedNamespace(namespaceName, env)) {
        throw new BadRequestException("Can not delete public namespace which has associated namespaces");
    }
    String operator = userInfoHolder.getUser().getUserId();
    namespaceAPI.deleteNamespace(env, appId, clusterName, namespaceName, operator);
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with NamespaceDTO

use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.

the class NamespaceService method fillAppNamespaceProperties.

private void fillAppNamespaceProperties(NamespaceBO namespace) {
    final NamespaceDTO namespaceDTO = namespace.getBaseInfo();
    final String appId = namespaceDTO.getAppId();
    final String clusterName = namespaceDTO.getClusterName();
    final String namespaceName = namespaceDTO.getNamespaceName();
    // 先从当前appId下面找,包含私有的和公共的
    AppNamespace appNamespace = appNamespaceService.findByAppIdAndName(appId, namespaceName);
    // 再从公共的app namespace里面找
    if (appNamespace == null) {
        appNamespace = appNamespaceService.findPublicAppNamespace(namespaceName);
    }
    final String format;
    final boolean isPublic;
    if (appNamespace == null) {
        // dirty data
        LOGGER.warn("Dirty data, cannot find appNamespace by namespaceName [{}], appId = {}, cluster = {}, set it format to {}, make public", namespaceName, appId, clusterName, ConfigFileFormat.Properties.getValue());
        format = ConfigFileFormat.Properties.getValue();
        // set to true, because public namespace allowed to delete by user
        isPublic = true;
    } else {
        format = appNamespace.getFormat();
        isPublic = appNamespace.isPublic();
        namespace.setParentAppId(appNamespace.getAppId());
        namespace.setComment(appNamespace.getComment());
    }
    namespace.setFormat(format);
    namespace.setPublic(isPublic);
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) AppNamespace(com.ctrip.framework.apollo.common.entity.AppNamespace)

Example 8 with NamespaceDTO

use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.

the class ItemService method revokeItem.

public void revokeItem(String appId, Env env, String clusterName, String namespaceName) {
    NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
    if (namespace == null) {
        throw new BadRequestException("namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
    }
    long namespaceId = namespace.getId();
    Map<String, String> releaseItemDTOs = new HashMap<>();
    ReleaseDTO latestRelease = releaseAPI.loadLatestRelease(appId, env, clusterName, namespaceName);
    if (latestRelease != null) {
        releaseItemDTOs = GSON.fromJson(latestRelease.getConfigurations(), GsonType.CONFIG);
    }
    List<ItemDTO> baseItems = itemAPI.findItems(appId, env, clusterName, namespaceName);
    Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems);
    Map<String, ItemDTO> deletedItemDTOs = new HashMap<>();
    // deleted items for comment
    findDeletedItems(appId, env, clusterName, namespaceName).forEach(item -> {
        deletedItemDTOs.put(item.getKey(), item);
    });
    ItemChangeSets changeSets = new ItemChangeSets();
    AtomicInteger lineNum = new AtomicInteger(1);
    releaseItemDTOs.forEach((key, value) -> {
        ItemDTO oldItem = oldKeyMapItem.get(key);
        if (oldItem == null) {
            ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO());
            changeSets.addCreateItem(buildNormalItem(0L, namespaceId, key, value, deletedItemDto.getComment(), lineNum.get()));
        } else if (!oldItem.getValue().equals(value) || lineNum.get() != oldItem.getLineNum()) {
            changeSets.addUpdateItem(buildNormalItem(oldItem.getId(), namespaceId, key, value, oldItem.getComment(), lineNum.get()));
        }
        oldKeyMapItem.remove(key);
        lineNum.set(lineNum.get() + 1);
    });
    oldKeyMapItem.forEach((key, value) -> changeSets.addDeleteItem(oldKeyMapItem.get(key)));
    changeSets.setDataChangeLastModifiedBy(userInfoHolder.getUser().getUserId());
    updateItems(appId, env, clusterName, namespaceName, changeSets);
    String formatStr = String.format("%s+%s+%s+%s", appId, env, clusterName, namespaceName);
    Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE_BY_TEXT, formatStr);
    Tracer.logEvent(TracerEventType.MODIFY_NAMESPACE, formatStr);
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) HashMap(java.util.HashMap) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) ReleaseDTO(com.ctrip.framework.apollo.common.dto.ReleaseDTO)

Example 9 with NamespaceDTO

use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.

the class ItemService method createCommentItem.

public ItemDTO createCommentItem(String appId, Env env, String clusterName, String namespaceName, ItemDTO item) {
    NamespaceDTO namespace = namespaceAPI.loadNamespace(appId, env, clusterName, namespaceName);
    if (namespace == null) {
        throw new BadRequestException("namespace:" + namespaceName + " not exist in env:" + env + ", cluster:" + clusterName);
    }
    item.setNamespaceId(namespace.getId());
    return itemAPI.createCommentItem(appId, env, clusterName, namespaceName, item);
}
Also used : NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException)

Example 10 with NamespaceDTO

use of com.ctrip.framework.apollo.common.dto.NamespaceDTO in project apollo by ctripcorp.

the class SearchController method searchByItem.

private PageDTO<App> searchByItem(String itemKey, Pageable pageable) {
    List<App> result = Lists.newLinkedList();
    if (StringUtils.isEmpty(itemKey)) {
        return new PageDTO<>(result, pageable, 0);
    }
    // use the env witch has the most namespace as page index.
    final AtomicLong maxTotal = new AtomicLong(0);
    List<Env> activeEnvs = portalSettings.getActiveEnvs();
    activeEnvs.forEach(env -> {
        PageDTO<NamespaceDTO> namespacePage = namespaceService.findNamespacesByItem(env, itemKey, pageable);
        if (!namespacePage.hasContent()) {
            return;
        }
        long currentEnvNSTotal = namespacePage.getTotal();
        if (currentEnvNSTotal > maxTotal.get()) {
            maxTotal.set(namespacePage.getTotal());
        }
        List<NamespaceDTO> namespaceDTOS = namespacePage.getContent();
        namespaceDTOS.forEach(namespaceDTO -> {
            String cluster = namespaceDTO.getClusterName();
            String namespaceName = namespaceDTO.getNamespaceName();
            App app = new App();
            app.setAppId(namespaceDTO.getAppId());
            app.setName(env.getName() + " / " + cluster + " / " + namespaceName);
            app.setOrgId(env.getName() + "+" + cluster + "+" + namespaceName);
            app.setOrgName("SearchByItem" + "+" + itemKey);
            result.add(app);
        });
    });
    return new PageDTO<>(result, pageable, maxTotal.get());
}
Also used : App(com.ctrip.framework.apollo.common.entity.App) AtomicLong(java.util.concurrent.atomic.AtomicLong) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) PageDTO(com.ctrip.framework.apollo.common.dto.PageDTO) Env(com.ctrip.framework.apollo.portal.environment.Env)

Aggregations

NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)36 Test (org.junit.Test)13 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)12 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)10 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)8 AppNamespace (com.ctrip.framework.apollo.common.entity.AppNamespace)7 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)7 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)4 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)4 Sql (org.springframework.test.context.jdbc.Sql)4 Item (com.ctrip.framework.apollo.biz.entity.Item)3 PageDTO (com.ctrip.framework.apollo.common.dto.PageDTO)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)3 OpenNamespaceDTO (com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO)3 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)3 Env (com.ctrip.framework.apollo.portal.environment.Env)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)3 RestTemplate (org.springframework.web.client.RestTemplate)3