Search in sources :

Example 16 with ItemDTO

use of com.ctrip.framework.apollo.common.dto.ItemDTO 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 17 with ItemDTO

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

the class NamespaceService method parseDeletedItems.

private List<ItemBO> parseDeletedItems(List<ItemDTO> newItems, Map<String, String> releaseItems, Map<String, ItemDTO> deletedItemDTOs) {
    Map<String, ItemDTO> newItemMap = BeanUtils.mapByKey("key", newItems);
    List<ItemBO> deletedItems = new LinkedList<>();
    for (Map.Entry<String, String> entry : releaseItems.entrySet()) {
        String key = entry.getKey();
        if (newItemMap.get(key) == null) {
            ItemBO deletedItem = new ItemBO();
            deletedItem.setDeleted(true);
            ItemDTO deletedItemDto = deletedItemDTOs.computeIfAbsent(key, k -> new ItemDTO());
            deletedItemDto.setKey(key);
            String oldValue = entry.getValue();
            deletedItem.setItem(deletedItemDto);
            deletedItemDto.setValue(oldValue);
            deletedItem.setModified(true);
            deletedItem.setOldValue(oldValue);
            deletedItem.setNewValue("");
            deletedItems.add(deletedItem);
        }
    }
    return deletedItems;
}
Also used : ItemBO(com.ctrip.framework.apollo.portal.entity.bo.ItemBO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 18 with ItemDTO

use of com.ctrip.framework.apollo.common.dto.ItemDTO 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 19 with ItemDTO

use of com.ctrip.framework.apollo.common.dto.ItemDTO 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 20 with ItemDTO

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

the class ItemController method create.

@PreAcquireNamespaceLock
@PostMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items")
public ItemDTO create(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @RequestBody ItemDTO dto) {
    Item entity = BeanUtils.transform(Item.class, dto);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item managedEntity = itemService.findOne(appId, clusterName, namespaceName, entity.getKey());
    if (managedEntity != null) {
        throw new BadRequestException("item already exists");
    }
    entity = itemService.save(entity);
    builder.createItem(entity);
    dto = BeanUtils.transform(ItemDTO.class, entity);
    Commit commit = new Commit();
    commit.setAppId(appId);
    commit.setClusterName(clusterName);
    commit.setNamespaceName(namespaceName);
    commit.setChangeSets(builder.build());
    commit.setDataChangeCreatedBy(dto.getDataChangeLastModifiedBy());
    commit.setDataChangeLastModifiedBy(dto.getDataChangeLastModifiedBy());
    commitService.save(commit);
    return dto;
}
Also used : ConfigChangeContentBuilder(com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder) Item(com.ctrip.framework.apollo.biz.entity.Item) Commit(com.ctrip.framework.apollo.biz.entity.Commit) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) PostMapping(org.springframework.web.bind.annotation.PostMapping) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)

Aggregations

ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)44 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)14 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)12 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)11 Test (org.junit.Test)11 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)7 Item (com.ctrip.framework.apollo.biz.entity.Item)6 OpenItemDTO (com.ctrip.framework.apollo.openapi.dto.OpenItemDTO)6 HashMap (java.util.HashMap)6 ItemBO (com.ctrip.framework.apollo.portal.entity.bo.ItemBO)5 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)4 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)4 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)4 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)4 Sql (org.springframework.test.context.jdbc.Sql)4 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 RestTemplate (org.springframework.web.client.RestTemplate)3