Search in sources :

Example 1 with PreAcquireNamespaceLock

use of com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock 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)

Example 2 with PreAcquireNamespaceLock

use of com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock in project apollo by ctripcorp.

the class ItemController method delete.

@PreAcquireNamespaceLock
@DeleteMapping("/items/{itemId}")
public void delete(@PathVariable("itemId") long itemId, @RequestParam String operator) {
    Item entity = itemService.findOne(itemId);
    if (entity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    itemService.delete(entity.getId(), operator);
    Namespace namespace = namespaceService.findOne(entity.getNamespaceId());
    Commit commit = new Commit();
    commit.setAppId(namespace.getAppId());
    commit.setClusterName(namespace.getClusterName());
    commit.setNamespaceName(namespace.getNamespaceName());
    commit.setChangeSets(new ConfigChangeContentBuilder().deleteItem(entity).build());
    commit.setDataChangeCreatedBy(operator);
    commit.setDataChangeLastModifiedBy(operator);
    commitService.save(commit);
}
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) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)

Example 3 with PreAcquireNamespaceLock

use of com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock in project apollo by ctripcorp.

the class ItemController method update.

@PreAcquireNamespaceLock
@PutMapping("/apps/{appId}/clusters/{clusterName}/namespaces/{namespaceName}/items/{itemId}")
public ItemDTO update(@PathVariable("appId") String appId, @PathVariable("clusterName") String clusterName, @PathVariable("namespaceName") String namespaceName, @PathVariable("itemId") long itemId, @RequestBody ItemDTO itemDTO) {
    Item managedEntity = itemService.findOne(itemId);
    if (managedEntity == null) {
        throw new NotFoundException("item not found for itemId " + itemId);
    }
    Namespace namespace = namespaceService.findOne(appId, clusterName, namespaceName);
    // In case someone constructs an attack scenario
    if (namespace == null || namespace.getId() != managedEntity.getNamespaceId()) {
        throw new BadRequestException("Invalid request, item and namespace do not match!");
    }
    Item entity = BeanUtils.transform(Item.class, itemDTO);
    ConfigChangeContentBuilder builder = new ConfigChangeContentBuilder();
    Item beforeUpdateItem = BeanUtils.transform(Item.class, managedEntity);
    // protect. only value,comment,lastModifiedBy can be modified
    managedEntity.setValue(entity.getValue());
    managedEntity.setComment(entity.getComment());
    managedEntity.setDataChangeLastModifiedBy(entity.getDataChangeLastModifiedBy());
    entity = itemService.update(managedEntity);
    builder.updateItem(beforeUpdateItem, entity);
    itemDTO = BeanUtils.transform(ItemDTO.class, entity);
    if (builder.hasContent()) {
        Commit commit = new Commit();
        commit.setAppId(appId);
        commit.setClusterName(clusterName);
        commit.setNamespaceName(namespaceName);
        commit.setChangeSets(builder.build());
        commit.setDataChangeCreatedBy(itemDTO.getDataChangeLastModifiedBy());
        commit.setDataChangeLastModifiedBy(itemDTO.getDataChangeLastModifiedBy());
        commitService.save(commit);
    }
    return itemDTO;
}
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) NotFoundException(com.ctrip.framework.apollo.common.exception.NotFoundException) BadRequestException(com.ctrip.framework.apollo.common.exception.BadRequestException) Namespace(com.ctrip.framework.apollo.biz.entity.Namespace) PutMapping(org.springframework.web.bind.annotation.PutMapping) PreAcquireNamespaceLock(com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)

Aggregations

PreAcquireNamespaceLock (com.ctrip.framework.apollo.adminservice.aop.PreAcquireNamespaceLock)3 Commit (com.ctrip.framework.apollo.biz.entity.Commit)3 Item (com.ctrip.framework.apollo.biz.entity.Item)3 ConfigChangeContentBuilder (com.ctrip.framework.apollo.biz.utils.ConfigChangeContentBuilder)3 Namespace (com.ctrip.framework.apollo.biz.entity.Namespace)2 ItemDTO (com.ctrip.framework.apollo.common.dto.ItemDTO)2 BadRequestException (com.ctrip.framework.apollo.common.exception.BadRequestException)2 NotFoundException (com.ctrip.framework.apollo.common.exception.NotFoundException)2 DeleteMapping (org.springframework.web.bind.annotation.DeleteMapping)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 PutMapping (org.springframework.web.bind.annotation.PutMapping)1