Search in sources :

Example 6 with ItemChangeSets

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

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

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

the class PropertyResolverTest method testDeleteItem.

@Test
public void testDeleteItem() {
    ItemChangeSets changeSets = resolver.resolve(1, "a=b", mockBaseItemHas3Key());
    Assert.assertEquals(2, changeSets.getDeleteItems().size());
}
Also used : ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 9 with ItemChangeSets

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

the class PropertyResolverTest method testDeleteBlankItem.

@Test
public void testDeleteBlankItem() {
    ItemChangeSets changeSets = resolver.resolve(1, "#qqqq\na=b\nb=c", mockBaseItemWith2Key1Comment1Blank());
    Assert.assertEquals(1, changeSets.getDeleteItems().size());
    Assert.assertEquals(1, changeSets.getUpdateItems().size());
    Assert.assertEquals(0, changeSets.getCreateItems().size());
}
Also used : ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 10 with ItemChangeSets

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

the class PropertyResolverTest method testAddCommentAndBlankItem.

@Test
public void testAddCommentAndBlankItem() {
    ItemChangeSets changeSets = resolver.resolve(1, "#ddd\na=b\n\nb=c\nc=d", mockBaseItemHas3Key());
    Assert.assertEquals(2, changeSets.getCreateItems().size());
    Assert.assertEquals(3, changeSets.getUpdateItems().size());
}
Also used : ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Aggregations

ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)28 Test (org.junit.Test)18 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)15 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)14 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)8 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)5 ItemDiffs (com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs)5 NamespaceIdentifier (com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier)5 Item (com.ctrip.framework.apollo.biz.entity.Item)3 AppDTO (com.ctrip.framework.apollo.common.dto.AppDTO)3 ClusterDTO (com.ctrip.framework.apollo.common.dto.ClusterDTO)3 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)3 TestRestTemplate (org.springframework.boot.test.web.client.TestRestTemplate)3 Sql (org.springframework.test.context.jdbc.Sql)3 RestTemplate (org.springframework.web.client.RestTemplate)3 ReleaseDTO (com.ctrip.framework.apollo.common.dto.ReleaseDTO)2 Env (com.ctrip.framework.apollo.portal.environment.Env)2 HashMap (java.util.HashMap)2 ConfigTextResolver (com.ctrip.framework.apollo.portal.component.txtresolver.ConfigTextResolver)1 NamespaceBO (com.ctrip.framework.apollo.portal.entity.bo.NamespaceBO)1