Search in sources :

Example 1 with NamespaceIdentifier

use of com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier in project apollo by ctripcorp.

the class ItemController method update.

@PutMapping(value = "/apps/{appId}/namespaces/{namespaceName}/items", consumes = { "application/json" })
public ResponseEntity<Void> update(@PathVariable String appId, @PathVariable String namespaceName, @RequestBody NamespaceSyncModel model) {
    checkModel(!model.isInvalid());
    boolean hasPermission = permissionValidator.hasModifyNamespacePermission(appId, namespaceName);
    Env envNoPermission = null;
    // if uses has ModifyNamespace permission then he has permission
    if (!hasPermission) {
        // else check if user has every env's ModifyNamespace permission
        hasPermission = true;
        for (NamespaceIdentifier namespaceIdentifier : model.getSyncToNamespaces()) {
            // once user has not one of the env's ModifyNamespace permission, then break the loop
            hasPermission &= permissionValidator.hasModifyNamespacePermission(namespaceIdentifier.getAppId(), namespaceIdentifier.getNamespaceName(), namespaceIdentifier.getEnv().toString());
            if (!hasPermission) {
                envNoPermission = namespaceIdentifier.getEnv();
                break;
            }
        }
    }
    if (hasPermission) {
        configService.syncItems(model.getSyncToNamespaces(), model.getSyncItems());
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    throw new AccessDeniedException(String.format("You don't have the permission to modify environment: %s", envNoPermission));
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Env(com.ctrip.framework.apollo.portal.environment.Env) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 2 with NamespaceIdentifier

use of com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier in project apollo by ctripcorp.

the class ConfigServiceTest method testCompareTargetNamespaceHasNoItems.

@Test
public void testCompareTargetNamespaceHasNoItems() {
    ItemDTO sourceItem1 = new ItemDTO("a", "b", "comment", 1);
    List<ItemDTO> sourceItems = Collections.singletonList(sourceItem1);
    String appId = "6666", env = "LOCAL", clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName = ConfigConsts.NAMESPACE_APPLICATION;
    List<NamespaceIdentifier> namespaceIdentifiers = generateNamespaceIdentifier(appId, env, clusterName, namespaceName);
    NamespaceDTO namespaceDTO = generateNamespaceDTO(appId, clusterName, namespaceName);
    when(namespaceAPI.loadNamespace(appId, Env.valueOf(env), clusterName, namespaceName)).thenReturn(namespaceDTO);
    when(itemAPI.findItems(appId, Env.valueOf(env), clusterName, namespaceName)).thenReturn(null);
    UserInfo userInfo = new UserInfo();
    userInfo.setUserId("test");
    when(userInfoHolder.getUser()).thenReturn(userInfo);
    List<ItemDiffs> itemDiffses = configService.compare(namespaceIdentifiers, sourceItems);
    assertEquals(1, itemDiffses.size());
    ItemDiffs itemDiffs = itemDiffses.get(0);
    ItemChangeSets changeSets = itemDiffs.getDiffs();
    assertEquals(0, changeSets.getUpdateItems().size());
    assertEquals(0, changeSets.getDeleteItems().size());
    List<ItemDTO> createItems = changeSets.getCreateItems();
    ItemDTO createItem = createItems.get(0);
    assertEquals(1, createItem.getLineNum());
    assertEquals("a", createItem.getKey());
    assertEquals("b", createItem.getValue());
    assertEquals("comment", createItem.getComment());
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Example 3 with NamespaceIdentifier

use of com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier in project apollo by ctripcorp.

the class ItemController method diff.

@PostMapping(value = "/namespaces/{namespaceName}/diff", consumes = { "application/json" })
public List<ItemDiffs> diff(@RequestBody NamespaceSyncModel model) {
    checkModel(!model.isInvalid());
    List<ItemDiffs> itemDiffs = configService.compare(model.getSyncToNamespaces(), model.getSyncItems());
    for (ItemDiffs diff : itemDiffs) {
        NamespaceIdentifier namespace = diff.getNamespace();
        if (namespace == null) {
            continue;
        }
        if (permissionValidator.shouldHideConfigToCurrentUser(namespace.getAppId(), namespace.getEnv().name(), namespace.getNamespaceName())) {
            diff.setDiffs(new ItemChangeSets());
            diff.setExtInfo("You are not this project's administrator, nor you have edit or release permission for the namespace in environment: " + namespace.getEnv());
        }
    }
    return itemDiffs;
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) PostMapping(org.springframework.web.bind.annotation.PostMapping)

Example 4 with NamespaceIdentifier

use of com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier in project apollo by ctripcorp.

the class ConfigServiceTest method generateNamespaceIdentifier.

private List<NamespaceIdentifier> generateNamespaceIdentifier(String appId, String env, String clusterName, String namespaceName) {
    NamespaceIdentifier targetNamespace = new NamespaceIdentifier();
    targetNamespace.setAppId(appId);
    targetNamespace.setEnv(env);
    targetNamespace.setClusterName(clusterName);
    targetNamespace.setNamespaceName(namespaceName);
    return Collections.singletonList(targetNamespace);
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier)

Example 5 with NamespaceIdentifier

use of com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier in project apollo by ctripcorp.

the class ConfigServiceTest method testCompare.

@Test
public void testCompare() {
    // not modified
    ItemDTO sourceItem1 = new ItemDTO("a", "b", "comment", 1);
    // new item
    ItemDTO sourceItem2 = new ItemDTO("newKey", "c", "comment", 2);
    // update value
    ItemDTO sourceItem3 = new ItemDTO("c", "newValue", "comment", 3);
    // update comment
    ItemDTO sourceItem4 = new ItemDTO("d", "b", "newComment", 4);
    List<ItemDTO> sourceItems = Arrays.asList(sourceItem1, sourceItem2, sourceItem3, sourceItem4);
    ItemDTO targetItem1 = new ItemDTO("a", "b", "comment", 1);
    ItemDTO targetItem2 = new ItemDTO("c", "oldValue", "comment", 2);
    ItemDTO targetItem3 = new ItemDTO("d", "b", "oldComment", 3);
    List<ItemDTO> targetItems = Arrays.asList(targetItem1, targetItem2, targetItem3);
    String appId = "6666", env = "LOCAL", clusterName = ConfigConsts.CLUSTER_NAME_DEFAULT, namespaceName = ConfigConsts.NAMESPACE_APPLICATION;
    List<NamespaceIdentifier> namespaceIdentifiers = generateNamespaceIdentifier(appId, env, clusterName, namespaceName);
    NamespaceDTO namespaceDTO = generateNamespaceDTO(appId, clusterName, namespaceName);
    when(namespaceAPI.loadNamespace(appId, Env.valueOf(env), clusterName, namespaceName)).thenReturn(namespaceDTO);
    when(itemAPI.findItems(appId, Env.valueOf(env), clusterName, namespaceName)).thenReturn(targetItems);
    UserInfo userInfo = new UserInfo();
    userInfo.setUserId("test");
    when(userInfoHolder.getUser()).thenReturn(userInfo);
    List<ItemDiffs> itemDiffses = configService.compare(namespaceIdentifiers, sourceItems);
    assertEquals(1, itemDiffses.size());
    ItemDiffs itemDiffs = itemDiffses.get(0);
    ItemChangeSets changeSets = itemDiffs.getDiffs();
    assertEquals(0, changeSets.getDeleteItems().size());
    assertEquals(2, changeSets.getUpdateItems().size());
    assertEquals(1, changeSets.getCreateItems().size());
    NamespaceIdentifier namespaceIdentifier = itemDiffs.getNamespace();
    assertEquals(appId, namespaceIdentifier.getAppId());
    assertEquals(Env.valueOf("LOCAL"), namespaceIdentifier.getEnv());
    assertEquals(clusterName, namespaceIdentifier.getClusterName());
    assertEquals(namespaceName, namespaceIdentifier.getNamespaceName());
    ItemDTO createdItem = changeSets.getCreateItems().get(0);
    assertEquals("newKey", createdItem.getKey());
    assertEquals("c", createdItem.getValue());
    assertEquals("comment", createdItem.getComment());
    assertEquals(4, createdItem.getLineNum());
    List<ItemDTO> updateItems = changeSets.getUpdateItems();
    ItemDTO updateItem1 = updateItems.get(0);
    ItemDTO updateItem2 = updateItems.get(1);
    assertEquals("c", updateItem1.getKey());
    assertEquals("newValue", updateItem1.getValue());
    assertEquals("comment", updateItem1.getComment());
    assertEquals(2, updateItem1.getLineNum());
    assertEquals("d", updateItem2.getKey());
    assertEquals("b", updateItem2.getValue());
    assertEquals("newComment", updateItem2.getComment());
    assertEquals(3, updateItem2.getLineNum());
}
Also used : NamespaceIdentifier(com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier) NamespaceDTO(com.ctrip.framework.apollo.common.dto.NamespaceDTO) ItemDTO(com.ctrip.framework.apollo.common.dto.ItemDTO) ItemChangeSets(com.ctrip.framework.apollo.common.dto.ItemChangeSets) UserInfo(com.ctrip.framework.apollo.portal.entity.bo.UserInfo) ItemDiffs(com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs) Test(org.junit.Test) AbstractUnitTest(com.ctrip.framework.apollo.portal.AbstractUnitTest)

Aggregations

NamespaceIdentifier (com.ctrip.framework.apollo.portal.entity.vo.NamespaceIdentifier)7 ItemChangeSets (com.ctrip.framework.apollo.common.dto.ItemChangeSets)5 ItemDiffs (com.ctrip.framework.apollo.portal.entity.vo.ItemDiffs)5 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)2 NamespaceDTO (com.ctrip.framework.apollo.common.dto.NamespaceDTO)2 AbstractUnitTest (com.ctrip.framework.apollo.portal.AbstractUnitTest)2 UserInfo (com.ctrip.framework.apollo.portal.entity.bo.UserInfo)2 Env (com.ctrip.framework.apollo.portal.environment.Env)2 Test (org.junit.Test)2 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)1 LinkedList (java.util.LinkedList)1 AccessDeniedException (org.springframework.security.access.AccessDeniedException)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1