Skip to content

Commit

Permalink
add delete cluster and namespace rest api
Browse files Browse the repository at this point in the history
  • Loading branch information
lepdou committed Aug 21, 2016
1 parent 92a90e9 commit 6c7e3c4
Show file tree
Hide file tree
Showing 28 changed files with 607 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,11 @@ public ItemDTO update(@PathVariable("appId") String appId,

Item beforeUpdateItem = BeanUtils.transfrom(Item.class, managedEntity);

BeanUtils.copyEntityProperties(entity, 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.transfrom(ItemDTO.class, entity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public void delete(@PathVariable("appId") String appId,
Namespace entity = namespaceService.findOne(appId, clusterName, namespaceName);
if (entity == null) throw new NotFoundException(
String.format("namespace not found for %s %s %s", appId, clusterName, namespaceName));
namespaceService.delete(entity.getId(), operator);

namespaceService.deleteNamespace(entity, operator);
}

@RequestMapping("/apps/{appId}/clusters/{clusterName}/namespaces")
Expand Down
2 changes: 2 additions & 0 deletions apollo-assembly/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
<property name="LOG_FILE"
value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}apollo-assembly.log}" />
<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="CONSOLE" />
</root>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import javax.persistence.Table;

@Entity
@Table(name = "commit")
@SQLDelete(sql = "Update commit set isDeleted = 1 where id = ?")
@Table(name = "Commit")
@SQLDelete(sql = "Update Commit set isDeleted = 1 where id = ?")
@Where(clause = "isDeleted = 0")
public class Commit extends BaseEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import com.ctrip.framework.apollo.biz.entity.Commit;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

public interface CommitRepository extends PagingAndSortingRepository<Commit, Long> {

List<Commit> findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(String appId, String clusterName,
String namespaceName, Pageable pageable);
String namespaceName, Pageable pageable);

@Modifying
@Query("update Commit set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.ctrip.framework.apollo.biz.entity.Item;
Expand All @@ -14,4 +16,8 @@ public interface ItemRepository extends PagingAndSortingRepository<Item, Long> {

Item findFirst1ByNamespaceIdOrderByLineNumDesc(Long namespaceId);

@Modifying
@Query("update Item set isdeleted=1,DataChange_LastModifiedBy = ?2 where namespaceId = ?1")
int deleteByNamespaceId(long namespaceId, String operator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import com.ctrip.framework.apollo.biz.entity.Namespace;
Expand All @@ -11,4 +13,9 @@ public interface NamespaceRepository extends PagingAndSortingRepository<Namespac
List<Namespace> findByAppIdAndClusterNameOrderByIdAsc(String appId, String clusterName);

Namespace findByAppIdAndClusterNameAndNamespaceName(String appId, String clusterName, String namespaceName);

@Modifying
@Query("update Namespace set isdeleted=1,DataChange_LastModifiedBy = ?3 where appId=?1 and clusterName=?2")
int batchDelete(String appId, String clusterName, String operator);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;

import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

Expand All @@ -19,4 +21,8 @@ Release findFirstByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderBy
List<Release> findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(String appId, String clusterName, String namespaceName, Pageable page);

List<Release> findByAppIdAndClusterNameAndNamespaceNameAndIsAbandonedFalseOrderByIdDesc(String appId, String clusterName, String namespaceName, Pageable page);

@Modifying
@Query("update Release set isdeleted=1,DataChange_LastModifiedBy = ?4 where appId=?1 and clusterName=?2 and namespaceName = ?3")
int batchDelete(String appId, String clusterName, String namespaceName, String operator);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.ctrip.framework.apollo.biz.entity.Audit;
import com.ctrip.framework.apollo.biz.entity.Cluster;
import com.ctrip.framework.apollo.biz.repository.ClusterRepository;
import com.ctrip.framework.apollo.common.exception.BadRequestException;
import com.ctrip.framework.apollo.common.utils.BeanUtils;
import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.common.exception.ServiceException;
Expand All @@ -26,8 +27,6 @@ public class ClusterService {
private AuditService auditService;
@Autowired
private NamespaceService namespaceService;
@Autowired
private AppNamespaceService appNamespaceService;


public boolean isClusterNameUnique(String appId, String clusterName) {
Expand Down Expand Up @@ -75,9 +74,12 @@ public Cluster save(Cluster entity) {
public void delete(long id, String operator) {
Cluster cluster = clusterRepository.findOne(id);
if (cluster == null) {
return;
throw new BadRequestException("cluster not exist");
}

//delete linked namespaces
namespaceService.deleteByAppIdAndClusterName(cluster.getAppId(), cluster.getName(), operator);

cluster.setDeleted(true);
cluster.setDataChangeLastModifiedBy(operator);
clusterRepository.save(cluster);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,9 @@ public List<Commit> find(String appId, String clusterName, String namespaceName,
return commitRepository.findByAppIdAndClusterNameAndNamespaceNameOrderByIdDesc(appId, clusterName, namespaceName, page);
}

@Transactional
public int batchDelete(String appId, String clusterName, String namespaceName, String operator){
return commitRepository.batchDelete(appId, clusterName, namespaceName, operator);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public Item delete(long id, String operator) {
return deletedItem;
}

@Transactional
public int batchDelete(long namespaceId, String operator) {
return itemRepository.deleteByNamespaceId(namespaceId, operator);

}

public Item findOne(String appId, String clusterName, String namespaceName, String key) {
Namespace namespace = namespaceRepository.findByAppIdAndClusterNameAndNamespaceName(appId,
clusterName, namespaceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.ctrip.framework.apollo.common.dto.ItemChangeSets;
import com.ctrip.framework.apollo.common.dto.ItemDTO;
import com.ctrip.framework.apollo.common.exception.NotFoundException;
import com.ctrip.framework.apollo.core.utils.StringUtils;


@Service
Expand Down Expand Up @@ -50,14 +49,19 @@ public ItemChangeSets updateSet(String appId, String clusterName,
for (ItemDTO item : changeSet.getUpdateItems()) {
Item entity = BeanUtils.transfrom(Item.class, item);

Item beforeUpdateItem = itemService.findOne(entity.getId());
if (beforeUpdateItem == null) {
Item managedItem = itemService.findOne(entity.getId());
if (managedItem == null) {
throw new NotFoundException(String.format("item not found.(key=%s)", entity.getKey()));
}
beforeUpdateItem = BeanUtils.transfrom(Item.class, beforeUpdateItem);
Item beforeUpdateItem = BeanUtils.transfrom(Item.class, managedItem);

//protect. only value,comment,lastModifiedBy,lineNum can be modified
managedItem.setValue(entity.getValue());
managedItem.setComment(entity.getComment());
managedItem.setLineNum(entity.getLineNum());
entity.setDataChangeLastModifiedBy(operator);
Item updatedItem = itemService.update(entity);

Item updatedItem = itemService.update(managedItem);
configChangeContentBuilder.updateItem(beforeUpdateItem, updatedItem);

}
Expand All @@ -72,8 +76,7 @@ public ItemChangeSets updateSet(String appId, String clusterName,
auditService.audit("ItemSet", null, Audit.OP.DELETE, operator);
}

String configChangeContent = configChangeContentBuilder.build();
if (!StringUtils.isEmpty(configChangeContent)) {
if (configChangeContentBuilder.hasContent()){
createCommit(appId, clusterName, namespaceName, configChangeContentBuilder.build(),
changeSet.getDataChangeLastModifiedBy());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class NamespaceService {
private AuditService auditService;
@Autowired
private AppNamespaceService appNamespaceService;
@Autowired
private ItemService itemService;
@Autowired
private CommitService commitService;
@Autowired
private ReleaseService releaseService;


public boolean isNamespaceUnique(String appId, String cluster, String namespace) {
Objects.requireNonNull(appId, "AppId must not be null");
Expand All @@ -34,17 +41,32 @@ public boolean isNamespaceUnique(String appId, String cluster, String namespace)
}

@Transactional
public void delete(long id, String operator) {
Namespace namespace = namespaceRepository.findOne(id);
if (namespace == null) {
return;
public void deleteByAppIdAndClusterName(String appId, String clusterName, String operator){

List<Namespace> toDeleteNamespaces = findNamespaces(appId, clusterName);

for (Namespace namespace: toDeleteNamespaces){

deleteNamespace(namespace, operator);

}
}

@Transactional
public Namespace deleteNamespace(Namespace namespace, String operator){
String appId = namespace.getAppId();
String clusterName = namespace.getClusterName();

itemService.batchDelete(namespace.getId(), operator);
commitService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);
releaseService.batchDelete(appId, clusterName, namespace.getNamespaceName(), operator);

namespace.setDeleted(true);
namespace.setDataChangeLastModifiedBy(operator);
namespaceRepository.save(namespace);

auditService.audit(Namespace.class.getSimpleName(), id, Audit.OP.DELETE, operator);
auditService.audit(Namespace.class.getSimpleName(), namespace.getId(), Audit.OP.DELETE, operator);

return namespaceRepository.save(namespace);
}

public Namespace findOne(Long namespaceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,9 @@ public Release rollback(long releaseId, String operator) {

return releaseRepository.save(release);
}

@Transactional
public int batchDelete(String appId, String clusterName, String namespaceName, String operator){
return releaseRepository.batchDelete(appId, clusterName, namespaceName, operator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import com.google.gson.GsonBuilder;

import com.ctrip.framework.apollo.biz.entity.Item;
import com.ctrip.framework.apollo.core.utils.StringUtils;

import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
Expand All @@ -20,7 +22,9 @@ public class ConfigChangeContentBuilder {


public ConfigChangeContentBuilder createItem(Item item) {
createItems.add(item);
if (!StringUtils.isEmpty(item.getKey())){
createItems.add(item);
}
return this;
}

Expand All @@ -33,10 +37,16 @@ public ConfigChangeContentBuilder updateItem(Item oldItem, Item newItem) {
}

public ConfigChangeContentBuilder deleteItem(Item item) {
deleteItems.add(item);
if (!StringUtils.isEmpty(item.getKey())) {
deleteItems.add(item);
}
return this;
}

public boolean hasContent(){
return !createItems.isEmpty() || !updateItems.isEmpty() || !deleteItems.isEmpty();
}

public String build() {
//因为事务第一段提交并没有更新时间,所以build时统一更新
for (Item item : createItems) {
Expand Down
Loading

0 comments on commit 6c7e3c4

Please sign in to comment.